@qazuor/qzpay-drizzle 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1134,6 +1134,7 @@ function mapDrizzleSubscriptionToCore(drizzle3) {
1134
1134
  cancelAtPeriodEnd: drizzle3.cancelAtPeriodEnd ?? false,
1135
1135
  providerSubscriptionIds,
1136
1136
  metadata: drizzle3.metadata ?? {},
1137
+ scheduledPlanChange: drizzle3.scheduledPlanChange ?? null,
1137
1138
  livemode: drizzle3.livemode,
1138
1139
  createdAt: drizzle3.createdAt,
1139
1140
  updatedAt: drizzle3.updatedAt,
@@ -1204,6 +1205,9 @@ function mapCoreSubscriptionUpdateToDrizzle(input) {
1204
1205
  update.mpSubscriptionId = mpId;
1205
1206
  }
1206
1207
  }
1208
+ if (input.scheduledPlanChange !== void 0) {
1209
+ update.scheduledPlanChange = input.scheduledPlanChange;
1210
+ }
1207
1211
  return update;
1208
1212
  }
1209
1213
 
@@ -1410,6 +1414,15 @@ var billingSubscriptions = pgCore.pgTable(
1410
1414
  nextRetryAt: pgCore.timestamp("next_retry_at", { withTimezone: true }),
1411
1415
  stripeSubscriptionId: pgCore.varchar("stripe_subscription_id", { length: 255 }),
1412
1416
  mpSubscriptionId: pgCore.varchar("mp_subscription_id", { length: 255 }),
1417
+ /**
1418
+ * Plan change scheduled to apply at a future point in time
1419
+ * (typically `current_period_end`). Stored as JSONB so the
1420
+ * shape can evolve without a migration; conforms to
1421
+ * `QZPayScheduledPlanChange` in qzpay-core. Null when no
1422
+ * change is pending. The application-level scheduler (cron)
1423
+ * owns the lifecycle — qzpay-drizzle provides storage only.
1424
+ */
1425
+ scheduledPlanChange: pgCore.jsonb("scheduled_plan_change"),
1413
1426
  livemode: pgCore.boolean("livemode").notNull().default(true),
1414
1427
  metadata: pgCore.jsonb("metadata").default({}),
1415
1428
  version: pgCore.uuid("version").notNull().defaultRandom(),
@@ -1440,7 +1453,12 @@ var billingSubscriptions = pgCore.pgTable(
1440
1453
  // Supports findTrialsEndingSoon() query
1441
1454
  lifecycleTrialIdx: pgCore.index("idx_subscriptions_lifecycle_trial").on(table.status, table.trialEnd),
1442
1455
  // Supports findScheduledForCancellation() query
1443
- lifecycleCancelIdx: pgCore.index("idx_subscriptions_lifecycle_cancel").on(table.cancelAtPeriodEnd, table.status, table.currentPeriodEnd)
1456
+ lifecycleCancelIdx: pgCore.index("idx_subscriptions_lifecycle_cancel").on(table.cancelAtPeriodEnd, table.status, table.currentPeriodEnd),
1457
+ // Partial index for the scheduled-plan-change cron query —
1458
+ // only rows with a pending scheduled change need to be
1459
+ // scanned, so a partial index keeps the per-tick cost O(k)
1460
+ // where k = #pending changes (NOT O(n) full table scan).
1461
+ lifecyclePendingPlanChangeIdx: pgCore.index("idx_subscriptions_pending_plan_change").on(table.scheduledPlanChange).where(drizzleOrm.sql`scheduled_plan_change IS NOT NULL AND (scheduled_plan_change->>'status') = 'pending'`)
1444
1462
  })
1445
1463
  );
1446
1464
  var billingSubscriptionInsertSchema = drizzleZod.createInsertSchema(billingSubscriptions);
@@ -6478,9 +6496,9 @@ async function runMigrations(config) {
6478
6496
  if (verbose) {
6479
6497
  console.log("[QZPay] Starting database migrations...");
6480
6498
  }
6481
- const sql19 = postgres2__default.default(connectionUrl, { max: 1 });
6499
+ const sql20 = postgres2__default.default(connectionUrl, { max: 1 });
6482
6500
  try {
6483
- const db = postgresJs.drizzle(sql19);
6501
+ const db = postgresJs.drizzle(sql20);
6484
6502
  await migrator.migrate(db, {
6485
6503
  migrationsFolder
6486
6504
  });
@@ -6491,14 +6509,14 @@ async function runMigrations(config) {
6491
6509
  console.error("[QZPay] Migration failed:", error);
6492
6510
  throw error;
6493
6511
  } finally {
6494
- await sql19.end();
6512
+ await sql20.end();
6495
6513
  }
6496
6514
  }
6497
6515
  async function hasPendingMigrations(connectionUrl) {
6498
- const sql19 = postgres2__default.default(connectionUrl, { max: 1 });
6516
+ const sql20 = postgres2__default.default(connectionUrl, { max: 1 });
6499
6517
  try {
6500
- postgresJs.drizzle(sql19);
6501
- const result = await sql19`
6518
+ postgresJs.drizzle(sql20);
6519
+ const result = await sql20`
6502
6520
  SELECT EXISTS (
6503
6521
  SELECT FROM information_schema.tables
6504
6522
  WHERE table_name = 'drizzle_migrations'
@@ -6509,24 +6527,24 @@ async function hasPendingMigrations(connectionUrl) {
6509
6527
  }
6510
6528
  return false;
6511
6529
  } finally {
6512
- await sql19.end();
6530
+ await sql20.end();
6513
6531
  }
6514
6532
  }
6515
6533
  async function ensureDatabase(config) {
6516
6534
  const { connectionUrl, databaseName } = config;
6517
- const sql19 = postgres2__default.default(connectionUrl, { max: 1 });
6535
+ const sql20 = postgres2__default.default(connectionUrl, { max: 1 });
6518
6536
  try {
6519
- const result = await sql19`
6537
+ const result = await sql20`
6520
6538
  SELECT 1 FROM pg_database WHERE datname = ${databaseName}
6521
6539
  `;
6522
6540
  if (result.length === 0) {
6523
- await sql19.unsafe(`CREATE DATABASE "${databaseName}"`);
6541
+ await sql20.unsafe(`CREATE DATABASE "${databaseName}"`);
6524
6542
  console.log(`[QZPay] Created database: ${databaseName}`);
6525
6543
  return true;
6526
6544
  }
6527
6545
  return false;
6528
6546
  } finally {
6529
- await sql19.end();
6547
+ await sql20.end();
6530
6548
  }
6531
6549
  }
6532
6550