From 384a2c520427fc01c9f099e3399bfe376045506c Mon Sep 17 00:00:00 2001
From: olivermrbl <oliver@mrbltech.com>
Date: Mon, 25 Nov 2024 09:23:33 +0100
Subject: [PATCH] fix(product): Update images before adding not null constraint

---
 .../modules/product/src/migrations/Migration20241122120331.ts  | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/packages/modules/product/src/migrations/Migration20241122120331.ts b/packages/modules/product/src/migrations/Migration20241122120331.ts
index efcb5443f0ac2..d8585ef7295e8 100644
--- a/packages/modules/product/src/migrations/Migration20241122120331.ts
+++ b/packages/modules/product/src/migrations/Migration20241122120331.ts
@@ -3,7 +3,7 @@ import { Migration } from '@mikro-orm/migrations';
 export class Migration20241122120331 extends Migration {
 
   async up(): Promise<void> {
-    this.addSql('alter table if exists "image" add column if not exists "rank" integer not null default 0, add column if not exists "product_id" text not null;');
+    this.addSql('alter table if exists "image" add column if not exists "rank" integer not null default 0, add column if not exists "product_id" text null;');
     
     // Migrate existing relationships
     this.addSql(`
@@ -19,6 +19,7 @@ export class Migration20241122120331 extends Migration {
       where pi.image_id = i.id;
     `);
 
+    this.addSql('alter table if exists "image" alter column "product_id" set not null;');
     this.addSql('alter table if exists "image" add constraint "image_product_id_foreign" foreign key ("product_id") references "product" ("id") on update cascade on delete cascade;');
     this.addSql('drop table if exists "product_images" cascade;');
   }