LocChat/server/database/migrations/1638307700052-AddRoles.ts

74 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-11-30 17:40:07 -05:00
import { MigrationInterface, QueryRunner, Table, TableForeignKey } from 'typeorm';
export class AddRoles1638307700052 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: 'role',
columns: [
{
name: 'id',
type: 'int',
isPrimary: true,
isGenerated: true,
},
{
name: 'key',
type: 'text',
isNullable: false,
},
],
}),
);
await queryRunner.createTable(
new Table({
name: 'user_role',
columns: [
{
name: 'id',
type: 'int',
isPrimary: true,
isGenerated: true,
},
{
name: 'userId',
type: 'int',
isNullable: false,
},
{
name: 'roleId',
type: 'int',
isNullable: false,
},
],
}),
);
await queryRunner.createForeignKey(
'user_role',
new TableForeignKey({
columnNames: ['userId'],
referencedColumnNames: ['id'],
referencedTableName: 'user',
onDelete: 'CASCADE',
}),
);
await queryRunner.createForeignKey(
'user_role',
new TableForeignKey({
columnNames: ['roleId'],
referencedColumnNames: ['id'],
referencedTableName: 'role',
onDelete: 'CASCADE',
}),
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable('user_role');
await queryRunner.dropTable('role');
}
}