74 lines
1.7 KiB
TypeScript
74 lines
1.7 KiB
TypeScript
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');
|
|
}
|
|
}
|