Skip to content

Instantly share code, notes, and snippets.

@evenfrost
Created November 22, 2021 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evenfrost/e358b60ea001feae7eed3860eccb6ab8 to your computer and use it in GitHub Desktop.
Save evenfrost/e358b60ea001feae7eed3860eccb6ab8 to your computer and use it in GitHub Desktop.
import { Migration } from '@mikro-orm/migrations';
export class Migration20211022164646 extends Migration {
async up(): Promise<void> {
this.addSql('alter table "Brands" drop constraint "Brands_clientId_fkey"');
this.addSql('alter table "Brands" add constraint "Brands_clientId_fkey" foreign key ("clientId") references "clients" ("id") on update cascade on delete cascade;');
this.addSql('delete from "Brands" where "clientId" is null');
this.addSql('alter table "DynamicContentItems" drop constraint "DynamicContentItems_clientId_fkey"');
this.addSql('alter table "DynamicContentItems" add constraint "DynamicContentItems_clientId_fkey" foreign key ("clientId") references "clients" ("id") on update cascade on delete cascade;');
this.addSql('delete from "DynamicContentItems" where "clientId" is null');
this.addSql('alter table "Groups" drop constraint "Groups_clientId_fkey"');
this.addSql('alter table "Groups" add constraint "Groups_clientId_fkey" foreign key ("clientId") references "clients" ("id") on update cascade on delete cascade;');
this.addSql('delete from "Groups" where "clientId" is null');
this.addSql('alter table "Users" drop constraint "Users_clientId_fkey"');
this.addSql('alter table "Users" add constraint "Users_clientId_fkey" foreign key ("clientId") references "clients" ("id") on update cascade on delete cascade;');
this.addSql('delete from "Users" where "clientId" is null');
}
async down(): Promise<void> {
this.addSql('alter table "Brands" drop constraint "Brands_clientId_fkey"');
this.addSql('alter table "Brands" add constraint "Brands_clientId_fkey" foreign key ("clientId") references "clients" ("id") on update cascade on delete set null;');
this.addSql('alter table "DynamicContentItems" drop constraint "DynamicContentItems_clientId_fkey"');
this.addSql('alter table "DynamicContentItems" add constraint "DynamicContentItems_clientId_fkey" foreign key ("clientId") references "clients" ("id") on update cascade on delete set null;');
this.addSql('alter table "Groups" drop constraint "Groups_clientId_fkey"');
this.addSql('alter table "Groups" add constraint "Groups_clientId_fkey" foreign key ("clientId") references "clients" ("id") on update cascade on delete set null;');
this.addSql('alter table "Users" drop constraint "Users_clientId_fkey"');
this.addSql('alter table "Users" add constraint "Users_clientId_fkey" foreign key ("clientId") references "clients" ("id") on update cascade on delete set null;');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment