From 0d2b7a6884a7b08d4a6e305260e564d6c6ace284 Mon Sep 17 00:00:00 2001 From: Matheus Albino Brunhara Date: Mon, 30 May 2022 19:51:29 -0500 Subject: [PATCH] Migrations da tabela socialInformation --- ...3956028190-CreateSocialInformationTable.ts | 61 +++++++++++++++++++ ...180-AddFKUserIdToSocialInformationTable.ts | 23 +++++++ 2 files changed, 84 insertions(+) create mode 100644 src/database/migrations/1653956028190-CreateSocialInformationTable.ts create mode 100644 src/database/migrations/1653956193180-AddFKUserIdToSocialInformationTable.ts diff --git a/src/database/migrations/1653956028190-CreateSocialInformationTable.ts b/src/database/migrations/1653956028190-CreateSocialInformationTable.ts new file mode 100644 index 0000000..50a56b4 --- /dev/null +++ b/src/database/migrations/1653956028190-CreateSocialInformationTable.ts @@ -0,0 +1,61 @@ +import { MigrationInterface, QueryRunner, Table } from 'typeorm'; + +export class CreateSocialInformationTable1653956028190 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.createTable( + new Table({ + name: 'socialInformation', + columns: [ + { + name: 'id_social', + type: 'integer', + isPrimary: true, + isGenerated: true, + generationStrategy: 'increment', + }, + { + name: 'user_id', + type: 'uuid', + isNullable: true, + }, + { + name: 'phone', + type: 'varchar', + isNullable: true, + }, + { + name: 'whatsapp', + type: 'varchar', + isNullable: true, + }, + { + name: 'facebook', + type: 'varchar', + isNullable: true, + }, + { + name: 'telegram', + type: 'varchar', + isNullable: true, + }, + { + name: 'created_at', + type: 'timestamp', + default: 'now()', + }, + { + name: 'updated_at', + type: 'timestamp', + default: 'now()', + }, + ], + }), + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.dropTable('socials'); + } +} diff --git a/src/database/migrations/1653956193180-AddFKUserIdToSocialInformationTable.ts b/src/database/migrations/1653956193180-AddFKUserIdToSocialInformationTable.ts new file mode 100644 index 0000000..d04d6c5 --- /dev/null +++ b/src/database/migrations/1653956193180-AddFKUserIdToSocialInformationTable.ts @@ -0,0 +1,23 @@ +import { MigrationInterface, QueryRunner, TableForeignKey } from 'typeorm'; + +export class AddFKUserIdToSocialInformationTable1653956193180 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.createForeignKey( + 'socialInformation', + new TableForeignKey({ + name: 'SocialInformationUser', // 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 primeira tabela + referencedTableName: 'users', // nome da tabela que possui a PK + onDelete: 'SET NULL', + onUpdate: 'CASCADE', + }), + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.dropForeignKey('socialInformation', 'SocialInformationUser'); + } +}