1 import { Migration } from "@medusajs/framework/mikro-orm/migrations"
2
3 export class Migration20250828075407 extends Migration {
4 override async up(): Promise<void> {
5 const nullApplyToQuantityResult = await this.execute(`
6 SELECT COUNT(*) as count
7 FROM promotion_application_method pam
8 JOIN promotion p ON pam.promotion_id = p.id
9 WHERE p.type = 'buyget'
10 AND pam.apply_to_quantity IS NULL
11 `)
12
13 const resultCount = parseInt(nullApplyToQuantityResult[0]?.count)
14
15 if (resultCount > 0) {
16 console.log(
17 `Warning: Found ${resultCount} buy-get promotions with null apply_to_quantity. These should be fixed as apply_to_quantity is required for proper buy-get promotion functionality.`
18 )
19 }
20
21 this.addSql(`
22 UPDATE promotion_application_method
23 SET max_quantity = apply_to_quantity
24 WHERE promotion_id IN (
25 SELECT id FROM promotion WHERE type = 'buyget'
26 )
27 AND apply_to_quantity IS NOT NULL
28 `)
29 }
30
31 override async down(): Promise<void> {
32 // Note: This migration cannot be safely rolled back as we don't store
33 // the original max_quantity values. If rollback is needed,
34 // the original values would need to be restored manually.
35 }
36 }