Skip to content

Commit

Permalink
Merge pull request #66 from vivid-planet/migrations-in-module
Browse files Browse the repository at this point in the history
COM-383: Move migrations into module
  • Loading branch information
RainbowBunchie authored Aug 8, 2024
2 parents 1fc8d9d + b1ca84d commit 3fac174
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 7 deletions.
23 changes: 23 additions & 0 deletions .changeset/eighty-flies-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
"@comet/brevo-api": major
---

Basic migrations for EmailCampaign and TargetGroup are now available in the module directly.

They must be imported into the project and added to the `migrationsList` in the `ormConfig`. Migrations for adding the `scope` and `filters` must still be done in the project's migrations.

```diff
export const ormConfig = createOrmConfig({
// ...
migrations: {
// ...
- migrationsList: createMigrationsList(path.resolve(__dirname, "migrations")),
+ migrationsList: [...brevoMigrationsList, ...createMigrationsList(path.resolve(__dirname, "migrations"))],
},
});

```

**Breaking Changes**:

- Requires adaption of the project's migrations
11 changes: 11 additions & 0 deletions demo/api/src/db/migrations/Migration20240802111659.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Migration } from "@mikro-orm/migrations";

export class Migration20240802111659 extends Migration {
// this migration needs to be generated manually
async up(): Promise<void> {
this.addSql('alter table "EmailCampaign" add column "scope_domain" text not null, add column "scope_language" text not null;');
this.addSql(
'alter table "TargetGroup" add column "scope_domain" text not null, add column "scope_language" text not null, add column "filters_SALUTATION" text[] null;',
);
}
}
3 changes: 2 additions & 1 deletion demo/api/src/db/ormconfig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { migrationsList as brevoMigrationsList } from "@comet/brevo-api";
import { createMigrationsList, createOrmConfig } from "@comet/cms-api";
import { EntityCaseNamingStrategy } from "@mikro-orm/core";
import path from "path";
Expand All @@ -20,7 +21,7 @@ export const ormConfig = createOrmConfig({
tableName: "Migrations",
// `path` is only used to tell MikroORM where to place newly generated migrations. Available migrations are defined using `migrationsList`.
path: "./src/db/migrations",
migrationsList: createMigrationsList(path.resolve(__dirname, "migrations")),
migrationsList: [...brevoMigrationsList, ...createMigrationsList(path.resolve(__dirname, "migrations"))],
disableForeignKeys: false,
dropTables: false,
snapshot: false,
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { BrevoContactsService } from "./brevo-contact/brevo-contacts.service";
export { SubscribeResponse } from "./brevo-contact/dto/subscribe-response.enum";
export { IsValidRedirectURL } from "./brevo-contact/validator/redirect-url.validator";
export { BrevoModule } from "./brevo-module";
export { migrationsList } from "./mikro-orm/migrations/migrations";
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Migration } from "@mikro-orm/migrations";

export class Migration20240115095733 extends Migration {
// TODO: move to package
async up(): Promise<void> {
this.addSql(
'create table "EmailCampaign" ("id" uuid not null, "createdAt" timestamp with time zone not null, "updatedAt" timestamp with time zone not null, "title" text not null, "subject" text not null, "scheduledAt" timestamp with time zone null, "scope_domain" text not null, "scope_language" text not null, "brevoId" int null, "contactList" uuid null, "content" json not null, constraint "Campaign_pkey" primary key ("id"));',
'create table "EmailCampaign" ("id" uuid not null, "createdAt" timestamp with time zone not null, "updatedAt" timestamp with time zone not null, "title" text not null, "subject" text not null, "scheduledAt" timestamp with time zone null, "brevoId" int null, "contactList" uuid null, "content" json not null, constraint "Campaign_pkey" primary key ("id"));',
);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Migration } from "@mikro-orm/migrations";

export class Migration20240118144808 extends Migration {
// TODO: move to package and make dynamic
async up(): Promise<void> {
this.addSql(
'create table "TargetGroup" ("id" uuid not null, "createdAt" timestamp with time zone not null, "updatedAt" timestamp with time zone not null, "title" text not null, "scope_domain" text not null, "scope_language" text not null, "brevoId" int not null, "filters_SALUTATION" text[] null, "isMainList" boolean);',
'create table "TargetGroup" ("id" uuid not null, "createdAt" timestamp with time zone not null, "updatedAt" timestamp with time zone not null, "title" text not null, "brevoId" int not null, "isMainList" boolean);',
);
this.addSql('alter table "TargetGroup" add constraint "TargetGroup_pkey" primary key ("id");');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Migration } from "@mikro-orm/migrations";

export class Migration20240123145606 extends Migration {
// TODO: move to package
async up(): Promise<void> {
this.addSql('alter table "EmailCampaign" add column "targetGroup" uuid null');
this.addSql('alter table "EmailCampaign" drop column "contactList";');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Migration } from "@mikro-orm/migrations";
export class Migration20240527112204 extends Migration {
async up(): Promise<void> {
this.addSql(
'create table "BrevoConfig" ("id" uuid not null, "createdAt" timestamp with time zone not null, "updatedAt" timestamp with time zone not null, "scope_domain" text not null, "scope_language" text not null, "senderMail" text not null, "senderName" text not null, constraint "BrevoConfig_pkey" primary key ("id"));',
'create table "BrevoConfig" ("id" uuid not null, "createdAt" timestamp with time zone not null, "updatedAt" timestamp with time zone not null, "senderMail" text not null, "senderName" text not null, constraint "BrevoConfig_pkey" primary key ("id"));',
);
}
}
19 changes: 19 additions & 0 deletions packages/api/src/mikro-orm/migrations/migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MigrationObject } from "@mikro-orm/core";

import { Migration20240115095733 } from "./Migration20240115095733";
import { Migration20240118144808 } from "./Migration20240118144808";
import { Migration20240123145606 } from "./Migration20240123145606";
import { Migration20240527112204 } from "./Migration20240527112204";
import { Migration20240619092554 } from "./Migration20240619092554";
import { Migration20240619145217 } from "./Migration20240619145217";
import { Migration20240621102349 } from "./Migration20240621102349";

export const migrationsList: MigrationObject[] = [
{ name: "Migration20240115095733", class: Migration20240115095733 },
{ name: "Migration20240118144808", class: Migration20240118144808 },
{ name: "Migration20240123145606", class: Migration20240123145606 },
{ name: "Migration20240527112204", class: Migration20240527112204 },
{ name: "Migration20240619092554", class: Migration20240619092554 },
{ name: "Migration20240619145217", class: Migration20240619145217 },
{ name: "Migration20240621102349", class: Migration20240621102349 },
];

0 comments on commit 3fac174

Please sign in to comment.