@nmakarov/cli-toolkit 0.40.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({
@@ -828,6 +864,9 @@ var FileDatabase = class _FileDatabase {
828
864
  currentRecord = 0;
829
865
  hasReadFirstPage = false;
830
866
  lastFileData = null;
867
+ /** Cache of the last JSON file parsed during paginated reads (avoid re-parse per page). */
868
+ readFileCache = null;
869
+ // { filePath: string, data: any[] } | null
831
870
  metadata;
832
871
  // Synopsis calculation functions
833
872
  fileSynopsisFunction = null;
@@ -1594,7 +1633,6 @@ var FileDatabase = class _FileDatabase {
1594
1633
  let effectivePageSize;
1595
1634
  if (nextPage && this.hasReadFirstPage) {
1596
1635
  effectivePageSize = pageSize || this.pageSize;
1597
- this.currentRecord += effectivePageSize;
1598
1636
  } else if (!nextPage) {
1599
1637
  effectivePageSize = pageSize !== void 0 ? pageSize : this.metadata.totalRecords;
1600
1638
  this.currentRecord = 0;
@@ -1623,8 +1661,14 @@ var FileDatabase = class _FileDatabase {
1623
1661
  const file = this.metadata.files[i];
1624
1662
  const filePath = import_path3.default.join(this.getDestinationPath(this.currentVersion || void 0), file.fileName);
1625
1663
  try {
1626
- const rawData = await import_fs3.default.promises.readFile(filePath, "utf8");
1627
- const fileData = deserializeData(rawData, this.metadata.dataType);
1664
+ let fileData;
1665
+ if (this.readFileCache?.filePath === filePath && Array.isArray(this.readFileCache.data)) {
1666
+ fileData = this.readFileCache.data;
1667
+ } else {
1668
+ const rawData = await import_fs3.default.promises.readFile(filePath, "utf8");
1669
+ fileData = deserializeData(rawData, this.metadata.dataType);
1670
+ this.readFileCache = { filePath, data: fileData };
1671
+ }
1628
1672
  let startIndex = 0;
1629
1673
  if (i === currentFileIndex) {
1630
1674
  startIndex = this.currentRecord - cumulativeRecords;
@@ -1638,6 +1682,7 @@ var FileDatabase = class _FileDatabase {
1638
1682
  throw new FileDatabaseError(`Failed to read file ${file.fileName}: ${error.message}`);
1639
1683
  }
1640
1684
  }
1685
+ this.currentRecord += recordsRead;
1641
1686
  if (result.length > 0) {
1642
1687
  if (nextPage || pageSize !== void 0 && pageSize < this.metadata.totalRecords) {
1643
1688
  this.hasReadFirstPage = true;
@@ -1651,6 +1696,7 @@ var FileDatabase = class _FileDatabase {
1651
1696
  setStartRecord(startRecord) {
1652
1697
  this.currentRecord = startRecord - 1;
1653
1698
  this.hasReadFirstPage = false;
1699
+ this.readFileCache = null;
1654
1700
  }
1655
1701
  /**
1656
1702
  * Reset read pagination state
@@ -1658,6 +1704,7 @@ var FileDatabase = class _FileDatabase {
1658
1704
  resetPagination() {
1659
1705
  this.currentRecord = 0;
1660
1706
  this.hasReadFirstPage = false;
1707
+ this.readFileCache = null;
1661
1708
  }
1662
1709
  /**
1663
1710
  * List filenames in the table directory.