arkormx 2.12.9 → 2.12.11

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/cli.mjs CHANGED
@@ -242,6 +242,9 @@ const readAppliedMigrationsState = (stateFilePath) => {
242
242
  version: 1,
243
243
  migrations: parsed.migrations.filter((migration) => {
244
244
  return typeof migration?.id === "string" && typeof migration?.file === "string" && typeof migration?.className === "string" && typeof migration?.appliedAt === "string" && (migration?.checksum === void 0 || typeof migration?.checksum === "string");
245
+ }).map((migration) => migration.operations === void 0 || Array.isArray(migration.operations) ? migration : {
246
+ ...migration,
247
+ operations: void 0
245
248
  }),
246
249
  runs: Array.isArray(parsed.runs) ? parsed.runs.filter((run) => {
247
250
  return typeof run?.id === "string" && typeof run?.appliedAt === "string" && Array.isArray(run?.migrationIds) && run.migrationIds.every((item) => typeof item === "string");
@@ -267,6 +270,20 @@ const writeAppliedMigrationsStateToStore = async (adapter, stateFilePath, state)
267
270
  }
268
271
  writeAppliedMigrationsState(stateFilePath, state);
269
272
  };
273
+ /**
274
+ * Whether `identity` counts as applied in `state`.
275
+ *
276
+ * Passing a `checksum` makes an edited migration file count as *not* applied so
277
+ * it is re-run. That only suits the Prisma/file-backed flow, where re-applying a
278
+ * migration just rewrites the generated schema. Adapter-backed runtimes must
279
+ * omit the checksum and let the state store alone decide, since replaying an
280
+ * already-applied `up()` would run its SQL a second time.
281
+ *
282
+ * @param state
283
+ * @param identity
284
+ * @param checksum
285
+ * @returns
286
+ */
270
287
  const isMigrationApplied = (state, identity, checksum) => {
271
288
  const matched = state.migrations.find((migration) => migration.id === identity);
272
289
  if (!matched) return false;
@@ -622,6 +639,30 @@ const applyOperationsToPersistedColumnMappingsState = (state, operations, featur
622
639
  tables: nextTables
623
640
  };
624
641
  };
642
+ /**
643
+ * Replays an applied migration's `up()` from the current file to recover its
644
+ * schema plan.
645
+ *
646
+ * Only used for entries recorded before the plan was stored alongside them; the
647
+ * result reflects the file as it stands now, which is why a recorded plan always
648
+ * wins over this.
649
+ *
650
+ * @param migration
651
+ * @param availableByIdentity
652
+ * @returns
653
+ */
654
+ const replayAppliedMigrationPlan = async (migration, availableByIdentity) => {
655
+ const migrationClass = availableByIdentity.get(migration.id);
656
+ if (!migrationClass) throw new ArkormException(`Unable to rebuild persisted column mappings because migration [${migration.id}] could not be resolved from the current migration files.`, {
657
+ operation: "migration.columnMappings",
658
+ meta: {
659
+ migrationId: migration.id,
660
+ file: migration.file,
661
+ className: migration.className
662
+ }
663
+ });
664
+ return await getMigrationPlan(migrationClass, "up", { inert: true });
665
+ };
625
666
  const rebuildPersistedColumnMappingsState = async (state, availableMigrations, features = resolvePersistedMetadataFeatures()) => {
626
667
  const availableByIdentity = new Map(availableMigrations.map(([migrationClass, file]) => [buildMigrationIdentity(file, migrationClass.name), migrationClass]));
627
668
  let nextState = createEmptyPersistedColumnMappingsState();
@@ -634,16 +675,7 @@ const rebuildPersistedColumnMappingsState = async (state, availableMigrations, f
634
675
  return left.index - right.index;
635
676
  });
636
677
  for (const { migration } of orderedMigrations) {
637
- const migrationClass = availableByIdentity.get(migration.id);
638
- if (!migrationClass) throw new ArkormException(`Unable to rebuild persisted column mappings because migration [${migration.id}] could not be resolved from the current migration files.`, {
639
- operation: "migration.columnMappings",
640
- meta: {
641
- migrationId: migration.id,
642
- file: migration.file,
643
- className: migration.className
644
- }
645
- });
646
- const operations = await getMigrationPlan(migrationClass, "up", { inert: true });
678
+ const operations = migration.operations ?? await replayAppliedMigrationPlan(migration, availableByIdentity);
647
679
  nextState = applyOperationsToPersistedColumnMappingsState(nextState, operations, features);
648
680
  }
649
681
  return nextState;
@@ -5117,13 +5149,18 @@ var MigrateCommand = class extends Command {
5117
5149
  if (!appliedMigrationState) return true;
5118
5150
  const identity = buildMigrationIdentity(file, migrationClass.name);
5119
5151
  const checksum = computeMigrationChecksum(file);
5120
- const alreadyApplied = isMigrationApplied(appliedMigrationState, identity, checksum);
5121
- if (alreadyApplied) skipped.push([migrationClass, file]);
5122
- else if (findAppliedMigration(appliedMigrationState, identity)) changed.push([migrationClass, file]);
5152
+ const recorded = findAppliedMigration(appliedMigrationState, identity);
5153
+ const alreadyApplied = useDatabaseMigrations ? Boolean(recorded) : isMigrationApplied(appliedMigrationState, identity, checksum);
5154
+ if (alreadyApplied) skipped.push([
5155
+ migrationClass,
5156
+ file,
5157
+ Boolean(recorded?.checksum) && recorded?.checksum !== checksum
5158
+ ]);
5159
+ else if (recorded) changed.push([migrationClass, file]);
5123
5160
  return !alreadyApplied;
5124
5161
  });
5125
- skipped.forEach(([migrationClass, file]) => {
5126
- this.success(this.app.splitLogger("Skipped", `${file} (${migrationClass.name})`));
5162
+ skipped.forEach(([migrationClass, file, drifted]) => {
5163
+ this.success(this.app.splitLogger("Skipped", `${file} (${migrationClass.name})${drifted ? " [changed since applied]" : ""}`));
5127
5164
  });
5128
5165
  changed.forEach(([migrationClass, file]) => {
5129
5166
  this.success(this.app.splitLogger("Changed", `${file} (${migrationClass.name})`));
@@ -5140,7 +5177,9 @@ var MigrateCommand = class extends Command {
5140
5177
  return;
5141
5178
  }
5142
5179
  let columnMappingsState = createEmptyPersistedColumnMappingsState();
5143
- for (const [MigrationClassItem] of pending) {
5180
+ const executedPlans = /* @__PURE__ */ new Map();
5181
+ for (const [MigrationClassItem, file] of pending) {
5182
+ const identity = buildMigrationIdentity(file, MigrationClassItem.name);
5144
5183
  if (useDatabaseMigrations) {
5145
5184
  const planned = await this.runWithDatabaseCreationRetry(adapter, () => getMigrationPlan(MigrationClassItem, "up", { inert: true }));
5146
5185
  if (!planned.ok) return;
@@ -5150,13 +5189,16 @@ var MigrateCommand = class extends Command {
5150
5189
  this.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
5151
5190
  return;
5152
5191
  }
5153
- if (!(await this.runWithDatabaseCreationRetry(adapter, () => applyMigrationToDatabase(adapter, MigrationClassItem))).ok) return;
5192
+ const applied = await this.runWithDatabaseCreationRetry(adapter, () => applyMigrationToDatabase(adapter, MigrationClassItem));
5193
+ if (!applied.ok) return;
5194
+ executedPlans.set(identity, applied.value.operations);
5154
5195
  continue;
5155
5196
  }
5156
- await applyMigrationToPrismaSchema(MigrationClassItem, {
5197
+ const applied = await applyMigrationToPrismaSchema(MigrationClassItem, {
5157
5198
  schemaPath,
5158
5199
  write: true
5159
5200
  });
5201
+ executedPlans.set(identity, applied.operations);
5160
5202
  }
5161
5203
  if (appliedMigrationState) {
5162
5204
  const runAppliedIds = [];
@@ -5167,7 +5209,8 @@ var MigrateCommand = class extends Command {
5167
5209
  file,
5168
5210
  className: migrationClass.name,
5169
5211
  appliedAt: (/* @__PURE__ */ new Date()).toISOString(),
5170
- checksum: computeMigrationChecksum(file)
5212
+ checksum: computeMigrationChecksum(file),
5213
+ operations: executedPlans.get(identity)
5171
5214
  });
5172
5215
  runAppliedIds.push(identity);
5173
5216
  }
@@ -5356,23 +5399,32 @@ var MigrateFreshCommand = class extends Command {
5356
5399
  }
5357
5400
  let appliedState = createEmptyAppliedMigrationsState();
5358
5401
  if (!(await this.runWithDatabaseCreationRetry(adapter, () => writeAppliedMigrationsStateToStore(adapter, stateFilePath, appliedState))).ok) return;
5359
- for (const [MigrationClassItem] of migrations) {
5402
+ const executedPlans = /* @__PURE__ */ new Map();
5403
+ for (const [MigrationClassItem, file] of migrations) {
5404
+ const identity = buildMigrationIdentity(file, MigrationClassItem.name);
5360
5405
  if (useDatabaseMigrations) {
5361
- if (!(await this.runWithDatabaseCreationRetry(adapter, () => applyMigrationToDatabase(adapter, MigrationClassItem))).ok) return;
5406
+ const applied = await this.runWithDatabaseCreationRetry(adapter, () => applyMigrationToDatabase(adapter, MigrationClassItem));
5407
+ if (!applied.ok) return;
5408
+ executedPlans.set(identity, applied.value.operations);
5362
5409
  continue;
5363
5410
  }
5364
- await applyMigrationToPrismaSchema(MigrationClassItem, {
5411
+ const applied = await applyMigrationToPrismaSchema(MigrationClassItem, {
5365
5412
  schemaPath,
5366
5413
  write: true
5367
5414
  });
5415
+ executedPlans.set(identity, applied.operations);
5416
+ }
5417
+ for (const [migrationClass, file] of migrations) {
5418
+ const identity = buildMigrationIdentity(file, migrationClass.name);
5419
+ appliedState = markMigrationApplied(appliedState, {
5420
+ id: identity,
5421
+ file,
5422
+ className: migrationClass.name,
5423
+ appliedAt: (/* @__PURE__ */ new Date()).toISOString(),
5424
+ checksum: computeMigrationChecksum(file),
5425
+ operations: executedPlans.get(identity)
5426
+ });
5368
5427
  }
5369
- for (const [migrationClass, file] of migrations) appliedState = markMigrationApplied(appliedState, {
5370
- id: buildMigrationIdentity(file, migrationClass.name),
5371
- file,
5372
- className: migrationClass.name,
5373
- appliedAt: (/* @__PURE__ */ new Date()).toISOString(),
5374
- checksum: computeMigrationChecksum(file)
5375
- });
5376
5428
  appliedState = markMigrationRun(appliedState, {
5377
5429
  id: buildMigrationRunId(),
5378
5430
  appliedAt: (/* @__PURE__ */ new Date()).toISOString(),
@@ -5582,7 +5634,7 @@ var MigrationHistoryCommand = class extends Command {
5582
5634
  {--state-file= : Path to applied migration state file}
5583
5635
  {--reset : Clear tracked migration history file}
5584
5636
  {--delete : Delete tracked migration history file}
5585
- {--json : Print raw JSON output}
5637
+ {--json : Print JSON output (without each migration's recorded schema plan)}
5586
5638
  `;
5587
5639
  this.description = "Inspect or reset tracked migration history";
5588
5640
  }
@@ -5618,7 +5670,11 @@ var MigrationHistoryCommand = class extends Command {
5618
5670
  if (this.option("json")) {
5619
5671
  this.success(JSON.stringify({
5620
5672
  path: usesDatabaseState ? "database" : stateFilePath,
5621
- ...state
5673
+ ...state,
5674
+ migrations: state.migrations.map(({ operations: _operations, ...migration }) => ({
5675
+ ...migration,
5676
+ hasRecordedPlan: Array.isArray(_operations)
5677
+ }))
5622
5678
  }, null, 2));
5623
5679
  return;
5624
5680
  }
@@ -1,7 +1,7 @@
1
- import { Kysely, Transaction } from "kysely";
2
- import { PrismaClient } from "@prisma/client";
3
1
  import { Collection } from "@h3ravel/collect.js";
2
+ import { Kysely, Transaction } from "kysely";
4
3
  import { Application, Command, Musket } from "@h3ravel/musket";
4
+ import { PrismaClient } from "@prisma/client";
5
5
  //#region src/types/migrations.d.ts
6
6
  type SchemaColumnType = 'id' | 'uuid' | 'enum' | 'string' | 'text' | 'integer' | 'bigInteger' | 'float' | 'decimal' | 'boolean' | 'json' | 'date' | 'dateTime' | 'timestamp';
7
7
  interface PrimaryKeyGeneration {
@@ -122,6 +122,16 @@ interface AppliedMigrationEntry {
122
122
  className: string;
123
123
  appliedAt: string;
124
124
  checksum?: string;
125
+ /**
126
+ * The schema plan this migration actually executed when it was applied.
127
+ *
128
+ * Recorded so derived metadata (persisted column mappings, enums) can be
129
+ * rebuilt from what ran rather than from the migration file's current
130
+ * contents, which may have been edited since. Absent on entries written
131
+ * before this was recorded, and on migrations applied through a backend that
132
+ * does not produce a plan; consumers fall back to replaying the file.
133
+ */
134
+ operations?: SchemaOperation[];
125
135
  }
126
136
  interface AppliedMigrationRun {
127
137
  id: string;
@@ -6534,10 +6544,30 @@ declare class KyselyDatabaseAdapter implements DatabaseAdapter {
6534
6544
  private buildWhereClause;
6535
6545
  private buildPaginationClause;
6536
6546
  private buildColumnReference;
6547
+ /**
6548
+ * Renders the relation's source table, aliasing it when a self-referential
6549
+ * relation would otherwise put the same name on both sides of a correlated
6550
+ * subquery.
6551
+ *
6552
+ * @param sourceTable
6553
+ * @param table
6554
+ * @returns
6555
+ */
6556
+ private buildTableSource;
6537
6557
  private buildRelatedTargetFromRelation;
6538
6558
  private resolveMappedTable;
6539
6559
  private buildBelongsToManyJoinSource;
6540
6560
  private buildThroughJoinSource;
6561
+ /**
6562
+ * The value a polymorphic child stores in its type column for this parent: the
6563
+ * parent model's class name, matching what the relationship writes on create
6564
+ * and what the set-based eager loader groups parents by.
6565
+ *
6566
+ * @param target
6567
+ * @param relation
6568
+ * @returns
6569
+ */
6570
+ private resolveMorphType;
6541
6571
  private buildRelatedJoinCondition;
6542
6572
  private combineConditions;
6543
6573
  private buildRelationFilterExpression;
@@ -8494,6 +8524,20 @@ declare const readAppliedMigrationsStateFromStore: (adapter: DatabaseAdapter | u
8494
8524
  declare const writeAppliedMigrationsState: (stateFilePath: string, state: AppliedMigrationsState) => void;
8495
8525
  declare const writeAppliedMigrationsStateToStore: (adapter: DatabaseAdapter | undefined, stateFilePath: string, state: AppliedMigrationsState) => Promise<void>;
8496
8526
  declare const deleteAppliedMigrationsStateFromStore: (adapter: DatabaseAdapter | undefined, stateFilePath: string) => Promise<"database" | "file" | "missing-file">;
8527
+ /**
8528
+ * Whether `identity` counts as applied in `state`.
8529
+ *
8530
+ * Passing a `checksum` makes an edited migration file count as *not* applied so
8531
+ * it is re-run. That only suits the Prisma/file-backed flow, where re-applying a
8532
+ * migration just rewrites the generated schema. Adapter-backed runtimes must
8533
+ * omit the checksum and let the state store alone decide, since replaying an
8534
+ * already-applied `up()` would run its SQL a second time.
8535
+ *
8536
+ * @param state
8537
+ * @param identity
8538
+ * @param checksum
8539
+ * @returns
8540
+ */
8497
8541
  declare const isMigrationApplied: (state: AppliedMigrationsState, identity: string, checksum?: string) => boolean;
8498
8542
  declare const findAppliedMigration: (state: AppliedMigrationsState, identity: string) => AppliedMigrationEntry | undefined;
8499
8543
  declare const markMigrationApplied: (state: AppliedMigrationsState, entry: AppliedMigrationEntry) => AppliedMigrationsState;
@@ -1,7 +1,7 @@
1
- import { Collection } from "@h3ravel/collect.js";
2
1
  import { Kysely, Transaction } from "kysely";
3
- import { Application, Command, Musket } from "@h3ravel/musket";
4
2
  import { PrismaClient } from "@prisma/client";
3
+ import { Collection } from "@h3ravel/collect.js";
4
+ import { Application, Command, Musket } from "@h3ravel/musket";
5
5
  //#region src/types/migrations.d.ts
6
6
  type SchemaColumnType = 'id' | 'uuid' | 'enum' | 'string' | 'text' | 'integer' | 'bigInteger' | 'float' | 'decimal' | 'boolean' | 'json' | 'date' | 'dateTime' | 'timestamp';
7
7
  interface PrimaryKeyGeneration {
@@ -122,6 +122,16 @@ interface AppliedMigrationEntry {
122
122
  className: string;
123
123
  appliedAt: string;
124
124
  checksum?: string;
125
+ /**
126
+ * The schema plan this migration actually executed when it was applied.
127
+ *
128
+ * Recorded so derived metadata (persisted column mappings, enums) can be
129
+ * rebuilt from what ran rather than from the migration file's current
130
+ * contents, which may have been edited since. Absent on entries written
131
+ * before this was recorded, and on migrations applied through a backend that
132
+ * does not produce a plan; consumers fall back to replaying the file.
133
+ */
134
+ operations?: SchemaOperation[];
125
135
  }
126
136
  interface AppliedMigrationRun {
127
137
  id: string;
@@ -6534,10 +6544,30 @@ declare class KyselyDatabaseAdapter implements DatabaseAdapter {
6534
6544
  private buildWhereClause;
6535
6545
  private buildPaginationClause;
6536
6546
  private buildColumnReference;
6547
+ /**
6548
+ * Renders the relation's source table, aliasing it when a self-referential
6549
+ * relation would otherwise put the same name on both sides of a correlated
6550
+ * subquery.
6551
+ *
6552
+ * @param sourceTable
6553
+ * @param table
6554
+ * @returns
6555
+ */
6556
+ private buildTableSource;
6537
6557
  private buildRelatedTargetFromRelation;
6538
6558
  private resolveMappedTable;
6539
6559
  private buildBelongsToManyJoinSource;
6540
6560
  private buildThroughJoinSource;
6561
+ /**
6562
+ * The value a polymorphic child stores in its type column for this parent: the
6563
+ * parent model's class name, matching what the relationship writes on create
6564
+ * and what the set-based eager loader groups parents by.
6565
+ *
6566
+ * @param target
6567
+ * @param relation
6568
+ * @returns
6569
+ */
6570
+ private resolveMorphType;
6541
6571
  private buildRelatedJoinCondition;
6542
6572
  private combineConditions;
6543
6573
  private buildRelationFilterExpression;
@@ -8494,6 +8524,20 @@ declare const readAppliedMigrationsStateFromStore: (adapter: DatabaseAdapter | u
8494
8524
  declare const writeAppliedMigrationsState: (stateFilePath: string, state: AppliedMigrationsState) => void;
8495
8525
  declare const writeAppliedMigrationsStateToStore: (adapter: DatabaseAdapter | undefined, stateFilePath: string, state: AppliedMigrationsState) => Promise<void>;
8496
8526
  declare const deleteAppliedMigrationsStateFromStore: (adapter: DatabaseAdapter | undefined, stateFilePath: string) => Promise<"database" | "file" | "missing-file">;
8527
+ /**
8528
+ * Whether `identity` counts as applied in `state`.
8529
+ *
8530
+ * Passing a `checksum` makes an edited migration file count as *not* applied so
8531
+ * it is re-run. That only suits the Prisma/file-backed flow, where re-applying a
8532
+ * migration just rewrites the generated schema. Adapter-backed runtimes must
8533
+ * omit the checksum and let the state store alone decide, since replaying an
8534
+ * already-applied `up()` would run its SQL a second time.
8535
+ *
8536
+ * @param state
8537
+ * @param identity
8538
+ * @param checksum
8539
+ * @returns
8540
+ */
8497
8541
  declare const isMigrationApplied: (state: AppliedMigrationsState, identity: string, checksum?: string) => boolean;
8498
8542
  declare const findAppliedMigration: (state: AppliedMigrationsState, identity: string) => AppliedMigrationEntry | undefined;
8499
8543
  declare const markMigrationApplied: (state: AppliedMigrationsState, entry: AppliedMigrationEntry) => AppliedMigrationsState;