Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/fix-brevo-table-constraints-migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@comet/brevo-api": patch
---

Add migration to fix Brevo table constraints and column types

- Update `BrevoEmailCampaign_targetGroups` primary key and foreign key constraints to use correct naming
- Add nullable to `BrevoEmailImportLog.importId` Property and Field
- Remove Property decorator from `BrevoEmailImportLog.contactSource` since it already has Enum decorator
- Change `BrevoTargetGroup.isMainList` column to not null
14 changes: 14 additions & 0 deletions demo/api/src/db/migrations/Migration20260310094014.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Migration } from '@mikro-orm/migrations';

export class Migration20260310094014 extends Migration {

override async up(): Promise<void> {
// Migrations for demo code changes
this.addSql(`alter table "News" alter column "category" drop default;`);
this.addSql(`alter table "News" alter column "category" type text using ("category"::text);`);

this.addSql(`alter table "PageTreeNode" drop constraint if exists "PageTreeNode_userGroup_check";`);
this.addSql(`alter table "PageTreeNode" add constraint "PageTreeNode_userGroup_check" check("userGroup" in ('all', 'admin', 'editor'));`);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ export function createBrevoEmailImportLogEntity({ Scope }: { Scope: Type<EmailCa
@Field(() => Scope)
scope: typeof Scope;

@Property({ columnType: "uuid" })
@Property({ columnType: "uuid", nullable: true })
@IsUndefinable()
@Field(() => ID)
@Field(() => ID, { nullable: true })
importId?: string = uuid();

@Property({ columnType: "text" })
@Field(() => ContactSource)
@Enum({ items: () => ContactSource })
contactSource: ContactSource;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Migration } from "@mikro-orm/migrations";

export class Migration20260310111347 extends Migration {
async up(): Promise<void> {
// Required migrations for constraints due to adding Brevo prefix to tables
this.addSql(`alter table "BrevoEmailCampaign_targetGroups" drop constraint "EmailCampaign_targetGroups_pkey";`);
this.addSql(
`alter table "BrevoEmailCampaign_targetGroups" add constraint "BrevoEmailCampaign_targetGroups_pkey" primary key ("brevoEmailCampaign", "brevoTargetGroup");`,
);

this.addSql(`alter table "BrevoEmailCampaign_targetGroups" drop constraint "EmailCampaign_targetGroups_emailCampaign_foreign";`);
this.addSql(
`alter table "BrevoEmailCampaign_targetGroups" add constraint "BrevoEmailCampaign_targetGroups_brevoEmailCampaign_foreign" foreign key ("brevoEmailCampaign") references "BrevoEmailCampaign" ("id") on update cascade on delete cascade;`,
);

this.addSql(`alter table "BrevoEmailCampaign_targetGroups" drop constraint "EmailCampaign_targetGroups_targetGroup_foreign";`);
this.addSql(
`alter table "BrevoEmailCampaign_targetGroups" add constraint "BrevoEmailCampaign_targetGroups_brevoTargetGroup_foreign" foreign key ("brevoTargetGroup") references "BrevoTargetGroup" ("id") on update cascade on delete cascade;`,
);

// Migrations for Brevo code column changes
this.addSql(`alter table "BrevoTargetGroup" alter column "isMainList" type boolean using ("isMainList"::boolean);`);
this.addSql(`alter table "BrevoTargetGroup" alter column "isMainList" set not null;`);
}
}
2 changes: 2 additions & 0 deletions packages/api/brevo-api/src/mikro-orm/migrations/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Migration20250221073825 } from "./Migration20250221073825";
import { Migration20250317131301 } from "./Migration20250317131301";
import { Migration20250321132034 } from "./Migration20250321132034";
import { Migration20250703155205 } from "./Migration20250703155205";
import { Migration20260310111347 } from "./Migration20260310111347";

export const migrationsList: MigrationObject[] = [
{ name: "Migration20240115095733", class: Migration20240115095733 },
Expand All @@ -38,4 +39,5 @@ export const migrationsList: MigrationObject[] = [
{ name: "Migration20250321132034", class: Migration20250321132034 },
{ name: "Migration20250317131301", class: Migration20250317131301 },
{ name: "Migration20250703155205", class: Migration20250703155205 },
{ name: "Migration20260310111347", class: Migration20260310111347 },
];