Skip to content

Instantly share code, notes, and snippets.

@alanfoandrade
Created March 15, 2021 17:32
Show Gist options
  • Save alanfoandrade/fa712d98db86e96a5175d269eae7146b to your computer and use it in GitHub Desktop.
Save alanfoandrade/fa712d98db86e96a5175d269eae7146b to your computer and use it in GitHub Desktop.
Category Videos N:M migrations
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');
}
}
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');
}
}
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