@nmakarov/cli-toolkit 0.43.0 → 0.44.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/tasks.cjs CHANGED
@@ -448,22 +448,46 @@ async function ensureTaskTables(context, options = {}) {
448
448
  }
449
449
  }
450
450
  const { actions } = await ensureSchema(db, spec, { dryRun, logger: context.logger });
451
+ const legacyDrops = await dropLegacyTaskNameColumn(db, [tasksTable, historyTable], {
452
+ dryRun,
453
+ log,
454
+ label
455
+ });
456
+ const allActions = [...actions, ...legacyDrops];
451
457
  if (dryRun) {
452
- if (actions.length === 0) {
458
+ if (allActions.length === 0) {
453
459
  log.info?.(`[tasks-schema] dryRun \u2014 ${label}: queue "${queueName}" already up to date; no DDL`);
454
460
  } else {
455
461
  log.info?.(
456
- `[tasks-schema] dryRun \u2014 ${label}: would run ${actions.length} statement(s) for queue "${queueName}":`
462
+ `[tasks-schema] dryRun \u2014 ${label}: would run ${allActions.length} statement(s) for queue "${queueName}":`
457
463
  );
458
- for (const s of actions) log.info?.(` - ${s}`);
464
+ for (const s of allActions) log.info?.(` - ${s}`);
459
465
  }
460
- } else if (actions.length > 0) {
466
+ } else if (allActions.length > 0) {
461
467
  log.info?.(
462
- `[tasks-schema] ${label}: applied ${actions.length} DDL statement(s) for queue "${queueName}"`
468
+ `[tasks-schema] ${label}: applied ${allActions.length} DDL statement(s) for queue "${queueName}"`
463
469
  );
464
470
  }
465
471
  }
466
472
  }
473
+ async function dropLegacyTaskNameColumn(db, tableNames, { dryRun, log, label }) {
474
+ const actions = [];
475
+ for (const table of tableNames) {
476
+ if (!await db.tableExists(table).catch(() => false)) continue;
477
+ const hasTask = await db.schema.hasColumn(table, "task");
478
+ const hasName = await db.schema.hasColumn(table, "name");
479
+ if (!hasTask || !hasName) continue;
480
+ const sql = `ALTER TABLE "${table}" DROP COLUMN IF EXISTS "task"`;
481
+ actions.push(sql);
482
+ if (dryRun) {
483
+ log?.info?.(`[tasks-schema] dryRun \u2014 ${label}: ${sql}`);
484
+ } else {
485
+ await db.raw(sql);
486
+ log?.info?.(`[tasks-schema] ${label}: dropped legacy column ${table}.task`);
487
+ }
488
+ }
489
+ return actions;
490
+ }
467
491
  async function enqueueTask(context, options) {
468
492
  const db = getDb(context);
469
493
  const queueName = options.queueName ?? "tasks";
@@ -480,7 +504,7 @@ async function enqueueTask(context, options) {
480
504
  } else if (schedule) {
481
505
  nextRunAt = nextTimeMatch(schedule, /* @__PURE__ */ new Date());
482
506
  }
483
- await db(tasksTable).insert({
507
+ const row = {
484
508
  id,
485
509
  name,
486
510
  params: toJsonColumn(options.params ?? null),
@@ -494,9 +518,21 @@ async function enqueueTask(context, options) {
494
518
  server_name: options.serverName ?? null,
495
519
  status: "idle",
496
520
  status_changed_at: db.fn.now()
497
- });
521
+ };
522
+ if (await tableHasLegacyTaskColumn(db, tasksTable)) {
523
+ row.task = name;
524
+ }
525
+ await db(tasksTable).insert(row);
498
526
  return id;
499
527
  }
528
+ var legacyTaskColumnCache = /* @__PURE__ */ new Map();
529
+ async function tableHasLegacyTaskColumn(db, tableName) {
530
+ const key = `${db?.config?.name ?? "db"}:${tableName}`;
531
+ if (!legacyTaskColumnCache.has(key)) {
532
+ legacyTaskColumnCache.set(key, await db.schema.hasColumn(tableName, "task"));
533
+ }
534
+ return legacyTaskColumnCache.get(key);
535
+ }
500
536
  async function updateTaskProgress(context, tasksTable, taskId, progress) {
501
537
  const db = getDb(context);
502
538
  await db(tasksTable).where({ id: taskId }).update({