Created
March 15, 2021 17:32
-
-
Save alanfoandrade/fa712d98db86e96a5175d269eae7146b to your computer and use it in GitHub Desktop.
Category Videos N:M migrations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { MigrationInterface, QueryRunner, Table } from 'typeorm'; | |
export default class CreateCategories1615810402060 | |
implements MigrationInterface { | |
public async up(queryRunner: QueryRunner): Promise<void> { | |
await queryRunner.createTable( | |
new Table({ | |
name: 'categories', | |
columns: [ | |
{ | |
name: 'id', | |
type: 'uuid', | |
isPrimary: true, | |
generationStrategy: 'uuid', | |
default: 'uuid_generate_v4()', | |
}, | |
{ | |
name: 'description', | |
type: 'varchar', | |
}, | |
{ | |
name: 'created_at', | |
type: 'timestamp with time zone', | |
default: 'now()', | |
}, | |
{ | |
name: 'updated_at', | |
type: 'timestamp with time zone', | |
default: 'now()', | |
}, | |
], | |
}), | |
); | |
} | |
public async down(queryRunner: QueryRunner): Promise<void> { | |
await queryRunner.dropTable('categories'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { MigrationInterface, QueryRunner, Table } from 'typeorm'; | |
export default class CreateCategoriesHasVideos1615817955007 | |
implements MigrationInterface { | |
public async up(queryRunner: QueryRunner): Promise<void> { | |
await queryRunner.createTable( | |
new Table({ | |
name: 'categories_has_videos', | |
columns: [ | |
{ | |
name: 'category_id', | |
type: 'uuid', | |
isPrimary: true, | |
}, | |
{ | |
name: 'video_id', | |
type: 'uuid', | |
isPrimary: true, | |
}, | |
], | |
foreignKeys: [ | |
{ | |
name: 'FK_CATEGORY_ID', | |
columnNames: ['category_id'], | |
referencedTableName: 'categories', | |
referencedColumnNames: ['id'], | |
onDelete: 'CASCADE', | |
onUpdate: 'CASCADE', | |
}, | |
{ | |
name: 'FK_VIDEO_ID', | |
columnNames: ['video_id'], | |
referencedTableName: 'videos', | |
referencedColumnNames: ['id'], | |
onDelete: 'CASCADE', | |
onUpdate: 'CASCADE', | |
}, | |
], | |
indices: [ | |
{ | |
name: 'IDX_CATEGORY', | |
columnNames: ['category_id'], | |
}, | |
{ | |
name: 'IDX_VIDEO', | |
columnNames: ['video_id'], | |
}, | |
], | |
}), | |
); | |
} | |
public async down(queryRunner: QueryRunner): Promise<void> { | |
await queryRunner.dropTable('categories_has_videos'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { MigrationInterface, QueryRunner, Table } from 'typeorm'; | |
export default class CreateVideos1615810411230 implements MigrationInterface { | |
public async up(queryRunner: QueryRunner): Promise<void> { | |
await queryRunner.createTable( | |
new Table({ | |
name: 'videos', | |
columns: [ | |
{ | |
name: 'id', | |
type: 'uuid', | |
isPrimary: true, | |
generationStrategy: 'uuid', | |
default: 'uuid_generate_v4()', | |
}, | |
{ | |
name: 'description', | |
type: 'varchar', | |
}, | |
{ | |
name: 'url', | |
type: 'varchar', | |
}, | |
{ | |
name: 'created_at', | |
type: 'timestamp with time zone', | |
default: 'now()', | |
}, | |
{ | |
name: 'updated_at', | |
type: 'timestamp with time zone', | |
default: 'now()', | |
}, | |
], | |
}), | |
); | |
} | |
public async down(queryRunner: QueryRunner): Promise<void> { | |
await queryRunner.dropTable('videos'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment