@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.js CHANGED
@@ -372,22 +372,46 @@ async function ensureTaskTables(context, options = {}) {
372
372
  }
373
373
  }
374
374
  const { actions } = await ensureSchema(db, spec, { dryRun, logger: context.logger });
375
+ const legacyDrops = await dropLegacyTaskNameColumn(db, [tasksTable, historyTable], {
376
+ dryRun,
377
+ log,
378
+ label
379
+ });
380
+ const allActions = [...actions, ...legacyDrops];
375
381
  if (dryRun) {
376
- if (actions.length === 0) {
382
+ if (allActions.length === 0) {
377
383
  log.info?.(`[tasks-schema] dryRun \u2014 ${label}: queue "${queueName}" already up to date; no DDL`);
378
384
  } else {
379
385
  log.info?.(
380
- `[tasks-schema] dryRun \u2014 ${label}: would run ${actions.length} statement(s) for queue "${queueName}":`
386
+ `[tasks-schema] dryRun \u2014 ${label}: would run ${allActions.length} statement(s) for queue "${queueName}":`
381
387
  );
382
- for (const s of actions) log.info?.(` - ${s}`);
388
+ for (const s of allActions) log.info?.(` - ${s}`);
383
389
  }
384
- } else if (actions.length > 0) {
390
+ } else if (allActions.length > 0) {
385
391
  log.info?.(
386
- `[tasks-schema] ${label}: applied ${actions.length} DDL statement(s) for queue "${queueName}"`
392
+ `[tasks-schema] ${label}: applied ${allActions.length} DDL statement(s) for queue "${queueName}"`
387
393
  );
388
394
  }
389
395
  }
390
396
  }
397
+ async function dropLegacyTaskNameColumn(db, tableNames, { dryRun, log, label }) {
398
+ const actions = [];
399
+ for (const table of tableNames) {
400
+ if (!await db.tableExists(table).catch(() => false)) continue;
401
+ const hasTask = await db.schema.hasColumn(table, "task");
402
+ const hasName = await db.schema.hasColumn(table, "name");
403
+ if (!hasTask || !hasName) continue;
404
+ const sql = `ALTER TABLE "${table}" DROP COLUMN IF EXISTS "task"`;
405
+ actions.push(sql);
406
+ if (dryRun) {
407
+ log?.info?.(`[tasks-schema] dryRun \u2014 ${label}: ${sql}`);
408
+ } else {
409
+ await db.raw(sql);
410
+ log?.info?.(`[tasks-schema] ${label}: dropped legacy column ${table}.task`);
411
+ }
412
+ }
413
+ return actions;
414
+ }
391
415
  async function enqueueTask(context, options) {
392
416
  const db = getDb(context);
393
417
  const queueName = options.queueName ?? "tasks";
@@ -404,7 +428,7 @@ async function enqueueTask(context, options) {
404
428
  } else if (schedule) {
405
429
  nextRunAt = nextTimeMatch(schedule, /* @__PURE__ */ new Date());
406
430
  }
407
- await db(tasksTable).insert({
431
+ const row = {
408
432
  id,
409
433
  name,
410
434
  params: toJsonColumn(options.params ?? null),
@@ -418,9 +442,21 @@ async function enqueueTask(context, options) {
418
442
  server_name: options.serverName ?? null,
419
443
  status: "idle",
420
444
  status_changed_at: db.fn.now()
421
- });
445
+ };
446
+ if (await tableHasLegacyTaskColumn(db, tasksTable)) {
447
+ row.task = name;
448
+ }
449
+ await db(tasksTable).insert(row);
422
450
  return id;
423
451
  }
452
+ var legacyTaskColumnCache = /* @__PURE__ */ new Map();
453
+ async function tableHasLegacyTaskColumn(db, tableName) {
454
+ const key = `${db?.config?.name ?? "db"}:${tableName}`;
455
+ if (!legacyTaskColumnCache.has(key)) {
456
+ legacyTaskColumnCache.set(key, await db.schema.hasColumn(tableName, "task"));
457
+ }
458
+ return legacyTaskColumnCache.get(key);
459
+ }
424
460
  async function updateTaskProgress(context, tasksTable, taskId, progress) {
425
461
  const db = getDb(context);
426
462
  await db(tasksTable).where({ id: taskId }).update({