feat(database): 🗃️ Define primeira versão do banco de dados referente a funcionalidade de criar rotas

This commit is contained in:
Hugo Falcao
2022-08-10 00:11:45 -03:00
parent 87e8a49a63
commit 26cac96cc6
5 changed files with 228 additions and 99 deletions

View File

@@ -1,4 +1,4 @@
import { MigrationInterface, QueryRunner, Table } from 'typeorm'; import { MigrationInterface, QueryRunner, Table, TableForeignKey, TableIndex } from 'typeorm';
export class CreateRoutes1659404395471 implements MigrationInterface { export class CreateRoutes1659404395471 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> { public async up(queryRunner: QueryRunner): Promise<void> {
@@ -21,6 +21,16 @@ export class CreateRoutes1659404395471 implements MigrationInterface {
name: 'price', name: 'price',
type: 'float', type: 'float',
}, },
{
name: 'days_of_week',
type: 'bit',
isNullable: true,
},
{
name: 'specific_day',
type: 'date',
isNullable: true,
},
{ {
name: 'estimated_departure_time', name: 'estimated_departure_time',
type: 'time', type: 'time',
@@ -46,9 +56,32 @@ export class CreateRoutes1659404395471 implements MigrationInterface {
], ],
}), }),
); );
await queryRunner.createForeignKey(
'routes',
new TableForeignKey({
name: 'routes_van_plate_fk', // nome da FK, serve para referenciar numa exclusão pelo QueryRunner se necessário
columnNames: ['van_plate'], // coluna que vai virar FK
referencedColumnNames: ['plate'], // coluna PK da primeira tabela
referencedTableName: 'vans', // nome da tabela que possui a PK
onDelete: 'SET NULL',
onUpdate: 'CASCADE',
}),
);
await queryRunner.createIndex(
'routes',
new TableIndex({
name: 'routes_idx',
columnNames: ['van_plate', 'days_of_week', 'specific_day', 'estimated_departure_time'],
isUnique: true,
})
);
} }
public async down(queryRunner: QueryRunner): Promise<void> { public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable('routes'); await queryRunner.dropTable('routes');
await queryRunner.dropForeignKey('routes', 'routes_van_plate_fk');
await queryRunner.dropIndex('routes', 'routes_idx');
} }
} }

View File

@@ -1,4 +1,4 @@
import { MigrationInterface, QueryRunner, Table } from 'typeorm'; import { MigrationInterface, QueryRunner, Table, TableForeignKey } from 'typeorm';
export class CreateNeighborhoodsServed1660009211327 export class CreateNeighborhoodsServed1660009211327
implements MigrationInterface implements MigrationInterface
@@ -34,9 +34,22 @@ export class CreateNeighborhoodsServed1660009211327
], ],
}), }),
); );
await queryRunner.createForeignKey(
'neighborhoods_served',
new TableForeignKey({
name: 'neighborhoods_served_route_id_fk', // nome da FK, serve para referenciar numa exclusão pelo QueryRunner se necessário
columnNames: ['route_id'], // coluna que vai virar FK
referencedColumnNames: ['id_route'], // coluna PK da tabela referenciada
referencedTableName: 'routes', // nome da tabela que possui a PK
onDelete: 'SET NULL',
onUpdate: 'CASCADE',
}),
);
} }
public async down(queryRunner: QueryRunner): Promise<void> { public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable('neighborhoods_served'); await queryRunner.dropTable('neighborhoods_served');
await queryRunner.dropForeignKey('neighborhoods_served', 'neighborhoods_served_route_id_fk');
} }
} }

View File

@@ -1,42 +1,53 @@
import {MigrationInterface, QueryRunner, Table} from "typeorm"; import { MigrationInterface, QueryRunner, Table, TableForeignKey } from 'typeorm';
export class CreateDestinations1660009323138 implements MigrationInterface { export class CreateDestinations1660009323138 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: 'destinations',
columns: [
{
name: 'id_destination',
type: 'integer',
isPrimary: true,
isGenerated: true,
generationStrategy: 'increment',
},
{
name: 'route_id',
type: 'integer',
},
{
name: 'name',
type: 'varchar',
},
{
name: 'latitude',
type: 'numeric',
},
{
name: 'longitude',
type: 'numeric',
},
],
}),
);
public async up(queryRunner: QueryRunner): Promise<void> { await queryRunner.createForeignKey(
await queryRunner.createTable( 'destinations',
new Table({ new TableForeignKey({
name: 'destinations', name: 'destinations_route_id_fk', // nome da FK, serve para referenciar numa exclusão pelo QueryRunner se necessário
columns: [ columnNames: ['route_id'], // coluna que vai virar FK
{ referencedColumnNames: ['id_route'], // coluna PK da tabela referenciada
name: 'id_destination', referencedTableName: 'routes', // nome da tabela que possui a PK
type: 'integer', onDelete: 'SET NULL',
isPrimary: true, onUpdate: 'CASCADE',
isGenerated: true, }),
generationStrategy: 'increment', );
}, }
{
name: 'route_id',
type: 'integer',
},
{
name: 'name',
type: 'varchar',
},
{
name: 'latitude',
type: 'numeric',
},
{
name: 'longitude',
type: 'numeric',
}
],
}),
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable('destinations');
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable('destinations');
await queryRunner.dropForeignKey('destinations', 'destinations_route_id_fk');
}
} }

View File

@@ -1,34 +1,68 @@
import {MigrationInterface, QueryRunner, Table} from "typeorm"; import { MigrationInterface, QueryRunner, Table, TableForeignKey, TableIndex } from 'typeorm';
export class CreatePassengers1660010452826 implements MigrationInterface { export class CreatePassengers1660010452826 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: 'passengers',
columns: [
{
name: 'id_passenger',
type: 'integer',
isPrimary: true,
isGenerated: true,
generationStrategy: 'increment',
},
{
name: 'route_id',
type: 'integer',
},
{
name: 'user_id',
type: 'uuid',
},
],
}),
);
public async up(queryRunner: QueryRunner): Promise<void> { await queryRunner.createForeignKey(
await queryRunner.createTable( 'passengers',
new Table({ new TableForeignKey({
name: 'passengers', name: 'passengers_route_id_fk', // nome da FK, serve para referenciar numa exclusão pelo QueryRunner se necessário
columns: [ columnNames: ['route_id'], // coluna que vai virar FK
{ referencedColumnNames: ['id_route'], // coluna PK da tabela referenciada
name: 'id_passenger', referencedTableName: 'routes', // nome da tabela que possui a PK
type: 'integer', onDelete: 'SET NULL',
isPrimary: true, onUpdate: 'CASCADE',
isGenerated: true, }),
generationStrategy: 'increment', );
},
{
name: 'route_id',
type: 'integer',
},
{
name: 'user_id',
type: 'uuid',
}
],
}),
);
}
public async down(queryRunner: QueryRunner): Promise<void> { await queryRunner.createForeignKey(
await queryRunner.dropTable('passengers'); 'passengers',
} new TableForeignKey({
name: 'passengers_user_id_fk', // nome da FK, serve para referenciar numa exclusão pelo QueryRunner se necessário
columnNames: ['user_id'], // coluna que vai virar FK
referencedColumnNames: ['id_user'], // coluna PK da tabela referenciada
referencedTableName: 'users', // nome da tabela que possui a PK
onDelete: 'SET NULL',
onUpdate: 'CASCADE',
}),
);
await queryRunner.createIndex(
'passengers',
new TableIndex({
name: 'passengers_route_user_idx',
columnNames: ['route_id', 'user_id'],
isUnique: true,
}),
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable('passengers');
await queryRunner.dropForeignKey('passengers', 'passengers_route_id_fk');
await queryRunner.dropForeignKey('passengers', 'passengers_user_id_fk');
await queryRunner.dropIndex('passengers', 'passengers_route_user_idx');
}
} }

View File

@@ -1,38 +1,76 @@
import {MigrationInterface, QueryRunner, Table} from "typeorm"; import { MigrationInterface, QueryRunner, Table, TableForeignKey, TableIndex } from 'typeorm';
export class CreateRouteHistoric1660010491828 implements MigrationInterface { export class CreateRouteHistoric1660010491828 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: 'route_historic',
columns: [
{
name: 'id_historic',
type: 'integer',
isPrimary: true,
isGenerated: true,
generationStrategy: 'increment',
},
{
name: 'route_id',
type: 'integer',
},
{
name: 'user_id',
type: 'uuid',
},
{
name: 'is_return',
type: 'boolean',
},
{
name: 'date',
type: 'date',
},
],
}),
);
public async up(queryRunner: QueryRunner): Promise<void> { await queryRunner.createForeignKey(
await queryRunner.createTable( 'route_historic',
new Table({ new TableForeignKey({
name: 'route_historic', name: 'route_historic_route_id_fk', // nome da FK, serve para referenciar numa exclusão pelo QueryRunner se necessário
columns: [ columnNames: ['route_id'], // coluna que vai virar FK
{ referencedColumnNames: ['id_route'], // coluna PK da tabela referenciada
name: 'id_historic', referencedTableName: 'routes', // nome da tabela que possui a PK
type: 'integer', onDelete: 'SET NULL',
isPrimary: true, onUpdate: 'CASCADE',
isGenerated: true, }),
generationStrategy: 'increment', );
},
{
name: 'route_id',
type: 'integer',
},
{
name: 'user_id',
type: 'uuid',
},
{
name: 'date',
type: 'date',
},
],
}),
);
}
public async down(queryRunner: QueryRunner): Promise<void> { await queryRunner.createForeignKey(
await queryRunner.dropTable('route_historic'); 'route_historic',
} new TableForeignKey({
name: 'route_historic_user_id_fk', // nome da FK, serve para referenciar numa exclusão pelo QueryRunner se necessário
columnNames: ['user_id'], // coluna que vai virar FK
referencedColumnNames: ['id_user'], // coluna PK da tabela referenciada
referencedTableName: 'users', // nome da tabela que possui a PK
onDelete: 'SET NULL',
onUpdate: 'CASCADE',
}),
);
await queryRunner.createIndex(
'route_historic',
new TableIndex({
name: 'route_historic_idx',
columnNames: ['route_id', 'user_id', 'is_return', 'date'],
isUnique: true,
}),
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable('route_historic');
await queryRunner.dropForeignKey('route_historic', 'route_historic_route_id_fk');
await queryRunner.dropForeignKey('route_historic', 'route_historic_user_id_fk');
await queryRunner.dropIndex('route_historic', 'route_historic_idx');
}
} }