@warlock.js/cascade 5.23.2 → 5.25.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.
Files changed (72) hide show
  1. package/CHANGELOG.md +352 -329
  2. package/README.md +101 -101
  3. package/bin/cascade.js +0 -0
  4. package/cjs/index.cjs +208 -69
  5. package/cjs/index.cjs.map +1 -1
  6. package/esm/contracts/database-driver.contract.d.mts +22 -1
  7. package/esm/contracts/database-driver.contract.d.mts.map +1 -1
  8. package/esm/contracts/index.d.mts +1 -1
  9. package/esm/data-source/data-source-registry.d.mts +8 -6
  10. package/esm/data-source/data-source-registry.d.mts.map +1 -1
  11. package/esm/data-source/data-source-registry.mjs +7 -1
  12. package/esm/data-source/data-source-registry.mjs.map +1 -1
  13. package/esm/drivers/mongodb/mongodb-driver.d.mts +13 -0
  14. package/esm/drivers/mongodb/mongodb-driver.d.mts.map +1 -1
  15. package/esm/drivers/mongodb/mongodb-driver.mjs +71 -2
  16. package/esm/drivers/mongodb/mongodb-driver.mjs.map +1 -1
  17. package/esm/drivers/mongodb/mongodb-query-builder.d.mts.map +1 -1
  18. package/esm/drivers/mongodb/mongodb-query-builder.mjs +2 -1
  19. package/esm/drivers/mongodb/mongodb-query-builder.mjs.map +1 -1
  20. package/esm/drivers/postgres/postgres-driver.d.mts +9 -0
  21. package/esm/drivers/postgres/postgres-driver.d.mts.map +1 -1
  22. package/esm/drivers/postgres/postgres-driver.mjs +61 -5
  23. package/esm/drivers/postgres/postgres-driver.mjs.map +1 -1
  24. package/esm/drivers/postgres/postgres-query-parser.d.mts +1 -4
  25. package/esm/drivers/postgres/postgres-query-parser.d.mts.map +1 -1
  26. package/esm/drivers/postgres/postgres-query-parser.mjs +4 -5
  27. package/esm/drivers/postgres/postgres-query-parser.mjs.map +1 -1
  28. package/esm/drivers/postgres/types.d.mts +5 -0
  29. package/esm/drivers/postgres/types.d.mts.map +1 -1
  30. package/esm/errors/unsafe-filter.error.mjs.map +1 -1
  31. package/esm/errors/unsafe-raw-expression.error.mjs.map +1 -1
  32. package/esm/errors/unsupported-lock-for-update.error.d.mts +11 -0
  33. package/esm/errors/unsupported-lock-for-update.error.d.mts.map +1 -0
  34. package/esm/errors/unsupported-lock-for-update.error.mjs +16 -0
  35. package/esm/errors/unsupported-lock-for-update.error.mjs.map +1 -0
  36. package/esm/events/model-events.mjs.map +1 -1
  37. package/esm/index.d.mts +3 -2
  38. package/esm/index.mjs +2 -1
  39. package/esm/migration/migration-runner.mjs.map +1 -1
  40. package/esm/migration/sql-grammar.mjs.map +1 -1
  41. package/esm/model/generate-model-id.mjs +23 -0
  42. package/esm/model/generate-model-id.mjs.map +1 -0
  43. package/esm/model/methods/delete-methods.mjs.map +1 -1
  44. package/esm/model/methods/meta-methods.mjs.map +1 -1
  45. package/esm/model/methods/query-methods.mjs +25 -2
  46. package/esm/model/methods/query-methods.mjs.map +1 -1
  47. package/esm/model/methods/write-methods.mjs.map +1 -1
  48. package/esm/query-builder/query-builder.mjs +1 -1
  49. package/esm/query-builder/query-builder.mjs.map +1 -1
  50. package/esm/utils/sanitize-filter.mjs.map +1 -1
  51. package/esm/validation/plugins/database-rules-plugin.mjs.map +1 -1
  52. package/esm/writer/database-writer.d.mts +0 -33
  53. package/esm/writer/database-writer.d.mts.map +1 -1
  54. package/esm/writer/database-writer.mjs +4 -52
  55. package/esm/writer/database-writer.mjs.map +1 -1
  56. package/llms-full.txt +2835 -2783
  57. package/package.json +5 -5
  58. package/skills/aggregate-data/SKILL.md +165 -165
  59. package/skills/alter-migration/SKILL.md +531 -531
  60. package/skills/configure-delete-strategy/SKILL.md +130 -130
  61. package/skills/define-model/SKILL.md +205 -205
  62. package/skills/define-relations/SKILL.md +171 -171
  63. package/skills/manage-data-sources/SKILL.md +177 -140
  64. package/skills/manage-transactions/SKILL.md +233 -208
  65. package/skills/paginate-results/SKILL.md +122 -122
  66. package/skills/perform-atomic-ops/SKILL.md +156 -156
  67. package/skills/query-data/SKILL.md +213 -213
  68. package/skills/run-cascade-cli/SKILL.md +125 -125
  69. package/skills/search-by-vector/SKILL.md +127 -127
  70. package/skills/subscribe-to-model-events/SKILL.md +148 -148
  71. package/skills/track-changes/SKILL.md +127 -127
  72. package/skills/write-migration/SKILL.md +206 -206
@@ -1 +1 @@
1
- {"version":3,"file":"migration-runner.mjs","names":[],"sources":["../../../../../../../cascade/src/migration/migration-runner.ts"],"sourcesContent":["import { colors } from \"@mongez/copper\";\r\nimport { log } from \"@warlock.js/logger\";\r\nimport fs from \"fs\";\r\nimport path from \"path\";\r\nimport type { MigrationDriverContract } from \"../contracts/migration-driver.contract\";\r\nimport type { DataSource } from \"../data-source/data-source\";\r\nimport { dataSourceRegistry } from \"../data-source/data-source-registry\";\r\nimport { type Migration, type MigrationContract, type MigrationStep } from \"./migration\";\r\nimport { sortMigrations, sortMigrationsForRollback } from \"./migration-order\";\r\nimport { parseCreatedAt } from \"./parse-created-at\";\r\nimport { SQLGrammar } from \"./sql-grammar\";\r\nimport type { MigrationRecord, MigrationResult, TaggedSQL } from \"./types\";\r\n\r\n/**\r\n * Migration class type with static name property.\r\n */\r\ntype MigrationClass = (new () => Migration) & {\r\n migrationName: string;\r\n createdAt?: string;\r\n};\r\n\r\n/**\r\n * What a caller may hand to {@link MigrationRunner.register}.\r\n *\r\n * Looser than {@link MigrationClass} in exactly one way: `migrationName` may be\r\n * absent. That is the documented workflow — the CLI imports a migration file and\r\n * sets the name from the filename afterwards — and `register()` enforces it at\r\n * runtime, throwing a message that says so. Once past that check the name IS\r\n * present, which is why everything stored and read downstream uses the stricter\r\n * type. Requiring it on the INPUT made the runner reject the very classes the\r\n * public `MigrationConstructor` describes.\r\n *\r\n * It constructs a MigrationContract rather than the abstract Migration class for the\r\n * same reason: `typeof Migration` is abstract and cannot satisfy `new () => Migration`,\r\n * and the public MigrationConstructor is declared against the contract. The runner only\r\n * ever uses contract members on what it registers.\r\n */\r\ntype RegisterableMigration = (new () => MigrationContract) & {\r\n migrationName?: string;\r\n createdAt?: string;\r\n};\r\n\r\n/**\r\n * Resolved instance data for a single pending migration.\r\n * @internal\r\n */\r\ntype MigrationData = {\r\n MigrationClass: MigrationClass;\r\n migration: Migration;\r\n name: string;\r\n /** Data source this migration runs against (its own, else the runner's). */\r\n dataSource: DataSource;\r\n /** Serialized `up()` statements (empty for direct-execution drivers). */\r\n statements: string[];\r\n /** Same as `statements`, with queued data steps kept in authored order. */\r\n steps: MigrationStep[];\r\n /** Whether the migration runs inside a transaction. */\r\n transactional: boolean;\r\n /** Driver has no SQL dialect (MongoDB): executed through `runMigration`. */\r\n direct: boolean;\r\n};\r\n\r\n/**\r\n * Options for migration execution.\r\n */\r\ntype ExecuteOptions = {\r\n /** Run in dry-run mode (no actual changes) */\r\n readonly dryRun?: boolean;\r\n /** Record to migrations table (default: true for batch, false for single) */\r\n readonly record?: boolean;\r\n /**\r\n * Rollback only: delete the `_migrations` rows whose migration file is no\r\n * longer registered, instead of failing on them.\r\n */\r\n readonly pruneMissing?: boolean;\r\n};\r\n\r\n/** Name of the lock document Mongo migrations claim inside the migrations collection. */\r\nconst MIGRATION_LOCK_NAME = \"__warlock_migration_lock__\";\r\nconst MIGRATION_LOCK_KEY = \"warlock:migrations\";\r\nconst MIGRATION_LOCK_TTL_MS = 10 * 60 * 1000;\r\nconst MIGRATION_LOCK_WAIT_MS = 60 * 1000;\r\nconst MIGRATION_LOCK_RETRY_MS = 1000;\r\n\r\n/** Held lock for one data source. `refresh` extends the TTL; `release` frees it. */\r\ntype MigrationLock = {\r\n refresh: () => Promise<void>;\r\n release: () => Promise<void>;\r\n};\r\n\r\ntype PgLockClient = {\r\n query: (sql: string, params?: unknown[]) => Promise<unknown>;\r\n release: () => void;\r\n};\r\n\r\n/** Minimal Mongo surface the lock needs (kept structural: no driver import). */\r\ntype MongoLockDatabase = {\r\n collection: (name: string) => {\r\n findOneAndUpdate: (filter: object, update: object, options: object) => Promise<unknown>;\r\n findOne: (filter: object) => Promise<{ owner?: string; expiresAt?: unknown } | null>;\r\n deleteOne: (filter: object) => Promise<unknown>;\r\n };\r\n};\r\n\r\nconst sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));\r\n\r\n/**\r\n * Derive a migration name from a file path: drop everything after the first dot\r\n * and strip a trailing `-migration` / `_migration` suffix. Shared by every CLI\r\n * so the same file is recorded under the same name.\r\n */\r\nexport function inferMigrationName(file: string): string {\r\n const basename = path.basename(file).split(\".\")[0] ?? \"\";\r\n\r\n return basename.replace(/-migration$/, \"\").replace(/_migration$/, \"\");\r\n}\r\n\r\n/**\r\n * Migration runner that executes migrations.\r\n *\r\n * This is a pure executor - it doesn't discover migrations.\r\n * Discovery is handled by the framework (e.g., @warlock.js/core CLI).\r\n *\r\n * The migration name is read from the static `name` property on the class,\r\n * which should be set by the CLI after importing:\r\n *\r\n * @example\r\n * ```typescript\r\n * // In CLI after importing:\r\n * const { default: MigrationClass } = await import(\"./create-users.migration.ts\");\r\n * MigrationClass.migrationName ??= \"create-users\";\r\n *\r\n * // Then register or execute:\r\n * runner.register(MigrationClass);\r\n * // or\r\n * await runner.execute(MigrationClass);\r\n * ```\r\n *\r\n * @example\r\n * ```typescript\r\n * // Simple direct execution\r\n * await runner.execute(CreateUsersTable);\r\n * await runner.rollback(CreateUsersTable);\r\n *\r\n * // Registry pattern for batch operations\r\n * runner.register(CreateUsersTable);\r\n * runner.register(AddEmailIndex);\r\n * await runner.runAll();\r\n * await runner.rollbackAll();\r\n * ```\r\n */\r\nexport class MigrationRunner {\r\n /** Registered migrations */\r\n public readonly migrations: MigrationClass[] = [];\r\n\r\n /** Data source to use */\r\n private dataSource?: DataSource;\r\n\r\n /** Cached migration drivers, one per data source */\r\n private readonly migrationDrivers = new Map<DataSource, MigrationDriverContract>();\r\n\r\n /** Table name for tracking migrations */\r\n private readonly migrationsTable: string;\r\n\r\n /** Whether to log operations */\r\n private readonly verbose: boolean;\r\n\r\n /**\r\n * Create a new migration runner.\r\n *\r\n * @param options - Runner options\r\n */\r\n public constructor(\r\n options: {\r\n dataSource?: DataSource;\r\n migrationsTable?: string;\r\n verbose?: boolean;\r\n } = {},\r\n ) {\r\n this.dataSource = options.dataSource;\r\n this.migrationsTable = options.migrationsTable ?? \"_migrations\";\r\n this.verbose = options.verbose ?? true;\r\n }\r\n\r\n // ============================================================================\r\n // DATA SOURCE\r\n // ============================================================================\r\n\r\n /**\r\n * Set the data source.\r\n */\r\n public setDataSource(dataSource: DataSource): this {\r\n this.dataSource = dataSource;\r\n this.migrationDrivers.clear();\r\n return this;\r\n }\r\n\r\n /**\r\n * Get the data source.\r\n */\r\n private getDataSource(): DataSource {\r\n if (!this.dataSource) {\r\n this.dataSource = dataSourceRegistry.get();\r\n }\r\n return this.dataSource;\r\n }\r\n\r\n /**\r\n * Get the migration driver.\r\n */\r\n private getMigrationDriver(\r\n dataSource: DataSource = this.getDataSource(),\r\n ): MigrationDriverContract {\r\n let driver = this.migrationDrivers.get(dataSource);\r\n if (!driver) {\r\n driver = dataSource.driver.migrationDriver();\r\n this.migrationDrivers.set(dataSource, driver);\r\n }\r\n return driver;\r\n }\r\n\r\n /**\r\n * Resolve a migration's declared data source (name or instance).\r\n * Falls back to the runner's own data source when it declares none.\r\n */\r\n private resolveDataSource(declared?: string | DataSource): DataSource {\r\n if (!declared) return this.getDataSource();\r\n if (typeof declared === \"string\") return dataSourceRegistry.get(declared);\r\n return declared;\r\n }\r\n\r\n /** Data source a registered migration class runs against. */\r\n private sourceOf(MigrationClass: MigrationClass): DataSource {\r\n return this.resolveDataSource(new MigrationClass().dataSource);\r\n }\r\n\r\n /** Distinct data sources across the registered migrations (runner's own included). */\r\n private getSources(): DataSource[] {\r\n const sources = new Set<DataSource>([this.getDataSource()]);\r\n for (const MigrationClass of this.migrations) {\r\n sources.add(this.sourceOf(MigrationClass));\r\n }\r\n return [...sources];\r\n }\r\n\r\n /**\r\n * Resolve whether a migration runs in a transaction:\r\n * 1. migration-level flag (instance, else the static from `Migration.create/alter`)\r\n * 2. `CONCURRENTLY` statements cannot run in a transaction block\r\n * 3. data-source config\r\n * 4. driver default\r\n */\r\n private resolveTransactional(\r\n MigrationClass: MigrationClass,\r\n migration: Migration,\r\n dataSource: DataSource,\r\n statements: string[] = [],\r\n ): boolean {\r\n const explicit =\r\n migration.transactional ?? (MigrationClass as { transactional?: boolean }).transactional;\r\n\r\n if (explicit !== undefined) return explicit;\r\n\r\n if (statements.some((sql) => /\\bCONCURRENTLY\\b/i.test(sql))) return false;\r\n\r\n return (\r\n dataSource.migrations?.transactional ??\r\n this.getMigrationDriver(dataSource).getDefaultTransactional()\r\n );\r\n }\r\n\r\n // ============================================================================\r\n // LOCKING\r\n // ============================================================================\r\n\r\n /** Locks held by the run in progress (Mongo TTL heartbeat). */\r\n private activeLocks: MigrationLock[] = [];\r\n\r\n /** Extend every held lock's TTL; called between migrations. */\r\n private async refreshLocks(): Promise<void> {\r\n for (const lock of this.activeLocks) {\r\n await lock.refresh();\r\n }\r\n }\r\n\r\n /**\r\n * Run `callback` while holding a migration lock on every given data source, so\r\n * concurrent `migrate` invocations (replicas, init containers) serialize.\r\n */\r\n private async withLock<T>(sources: DataSource[], callback: () => Promise<T>): Promise<T> {\r\n const held: MigrationLock[] = [];\r\n\r\n try {\r\n for (const dataSource of sources) {\r\n const lock = await this.acquireLock(dataSource);\r\n if (lock) held.push(lock);\r\n }\r\n\r\n this.activeLocks = held;\r\n\r\n return await callback();\r\n } finally {\r\n this.activeLocks = [];\r\n\r\n for (const lock of held.reverse()) {\r\n try {\r\n await lock.release();\r\n } catch (error) {\r\n log.warn(\r\n \"database\",\r\n \"migration\",\r\n `Failed to release migration lock: ${error instanceof Error ? error.message : String(error)}`,\r\n );\r\n }\r\n }\r\n }\r\n }\r\n\r\n /** Acquire the lock for one data source; `undefined` when its driver has no lock support. */\r\n private async acquireLock(dataSource: DataSource): Promise<MigrationLock | undefined> {\r\n const driver = dataSource.driver as unknown as {\r\n name: string;\r\n getClient?: () => { connect?: () => Promise<PgLockClient> };\r\n getDatabase?: () => MongoLockDatabase;\r\n };\r\n\r\n if (driver.name === \"postgres\" && driver.getClient) {\r\n // A dedicated client: advisory locks are per-session, not per-pool.\r\n const client = await driver.getClient().connect?.();\r\n if (!client) return undefined;\r\n\r\n try {\r\n await client.query(\"SELECT pg_advisory_lock(hashtext($1))\", [MIGRATION_LOCK_KEY]);\r\n } catch (error) {\r\n client.release();\r\n throw error;\r\n }\r\n\r\n return {\r\n refresh: async () => {},\r\n release: async () => {\r\n try {\r\n await client.query(\"SELECT pg_advisory_unlock(hashtext($1))\", [MIGRATION_LOCK_KEY]);\r\n } finally {\r\n client.release();\r\n }\r\n },\r\n };\r\n }\r\n\r\n if (driver.getDatabase) {\r\n // The unique index on `name` (created here) makes the claim below atomic.\r\n await this.getMigrationDriver(dataSource).ensureMigrationsTable(this.migrationsTable);\r\n\r\n const collection = driver.getDatabase().collection(this.migrationsTable);\r\n const owner = `${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`;\r\n const deadline = Date.now() + MIGRATION_LOCK_WAIT_MS;\r\n\r\n const claim = async (): Promise<boolean> => {\r\n const now = new Date();\r\n\r\n try {\r\n await collection.findOneAndUpdate(\r\n {\r\n name: MIGRATION_LOCK_NAME,\r\n $or: [{ expiresAt: { $lt: now } }, { owner }],\r\n },\r\n {\r\n $set: { owner, expiresAt: new Date(now.getTime() + MIGRATION_LOCK_TTL_MS) },\r\n $setOnInsert: { batch: 0 },\r\n },\r\n { upsert: true },\r\n );\r\n\r\n return true;\r\n } catch (error) {\r\n // Held by someone else: the upsert collides with the unique `name` index.\r\n if ((error as { code?: number }).code === 11000) return false;\r\n throw error;\r\n }\r\n };\r\n\r\n while (!(await claim())) {\r\n if (Date.now() >= deadline) {\r\n const holder = await collection.findOne({ name: MIGRATION_LOCK_NAME });\r\n throw new Error(\r\n `Could not acquire the migration lock within ${MIGRATION_LOCK_WAIT_MS / 1000}s; ` +\r\n `held by \"${holder?.owner ?? \"unknown\"}\" until ${holder?.expiresAt ?? \"unknown\"}.`,\r\n );\r\n }\r\n\r\n await sleep(MIGRATION_LOCK_RETRY_MS);\r\n }\r\n\r\n return {\r\n refresh: async () => {\r\n await claim();\r\n },\r\n release: async () => {\r\n await collection.deleteOne({ name: MIGRATION_LOCK_NAME, owner });\r\n },\r\n };\r\n }\r\n\r\n return undefined;\r\n }\r\n\r\n // ============================================================================\r\n // REGISTRATION\r\n // ============================================================================\r\n\r\n /**\r\n * Register a migration.\r\n *\r\n * The migration name is read from `MigrationClass.migrationName`.\r\n *\r\n * @param MigrationClass - Migration class (must have static `name` set)\r\n * @param createdAt - Optional timestamp for ordering\r\n * @returns This runner for chaining\r\n *\r\n * @example\r\n * ```typescript\r\n * CreateUsersTable.migrationName = \"2024-01-15_create-users\";\r\n * runner.register(CreateUsersTable);\r\n * ```\r\n */\r\n public register(MigrationClass: RegisterableMigration): this {\r\n const name = MigrationClass.migrationName;\r\n if (!name) {\r\n throw new Error(\r\n `Migration class must have a static 'migrationName' property set. ` +\r\n `Set it in CLI after importing: MigrationClass.migrationName = \"filename\";`,\r\n );\r\n }\r\n const existing = this.migrations.find((m) => m.migrationName === name);\r\n\r\n if (existing) {\r\n // Re-registering the same class is idempotent; a DIFFERENT class under the\r\n // same name would silently never run, so fail loudly.\r\n if ((existing as unknown) !== MigrationClass) {\r\n throw new Error(\r\n `Duplicate migration name \"${name}\": two different migration classes resolve to the same name. ` +\r\n `Rename one of the files (or set a unique static 'migrationName').`,\r\n );\r\n }\r\n\r\n return this;\r\n }\r\n\r\n // `name` is proven present by the throw above, so this satisfies the stricter\r\n // stored type that every reader downstream relies on.\r\n this.migrations.push(MigrationClass as MigrationClass);\r\n\r\n return this;\r\n }\r\n\r\n /**\r\n * Register multiple migrations.\r\n *\r\n * @param migrations - Array of migration classes\r\n * @returns This runner for chaining\r\n */\r\n public registerMany(migrations: RegisterableMigration[]): this {\r\n for (const MigrationClass of migrations) {\r\n this.register(MigrationClass);\r\n }\r\n return this;\r\n }\r\n\r\n /**\r\n * Clear all registered migrations.\r\n */\r\n public clear(): this {\r\n this.migrations.length = 0;\r\n return this;\r\n }\r\n\r\n /**\r\n * Get all registered migration names.\r\n */\r\n public getRegisteredNames(): string[] {\r\n return this.migrations.map((m) => m.migrationName);\r\n }\r\n\r\n // ============================================================================\r\n // SINGLE EXECUTION\r\n // ============================================================================\r\n\r\n /**\r\n * Execute a single migration's up() method.\r\n *\r\n * @param MigrationClass - Migration class to execute\r\n * @param options - Execution options\r\n * @returns Migration result\r\n *\r\n * @example\r\n * ```typescript\r\n * await runner.execute(CreateUsersTable);\r\n * await runner.execute(AddEmailIndex, { dryRun: true });\r\n * ```\r\n */\r\n public async run(\r\n MigrationClass: MigrationClass,\r\n options: ExecuteOptions = {},\r\n ): Promise<MigrationResult> {\r\n return this.runMigration(MigrationClass, \"up\", {\r\n dryRun: options.dryRun,\r\n record: options.record ?? false,\r\n });\r\n }\r\n\r\n /**\r\n * Execute a single migration's down() method.\r\n *\r\n * @param MigrationClass - Migration class to rollback\r\n * @param options - Execution options\r\n * @returns Migration result\r\n *\r\n * @example\r\n * ```typescript\r\n * await runner.rollback(CreateUsersTable);\r\n * ```\r\n */\r\n public async rollback(\r\n MigrationClass: MigrationClass,\r\n options: ExecuteOptions = {},\r\n ): Promise<MigrationResult> {\r\n return this.runMigration(MigrationClass, \"down\", {\r\n dryRun: options.dryRun,\r\n record: options.record ?? false,\r\n });\r\n }\r\n\r\n // ============================================================================\r\n // BATCH EXECUTION (REGISTERED MIGRATIONS)\r\n // ============================================================================\r\n\r\n /**\r\n * Run all pending registered migrations.\r\n *\r\n * Only runs migrations not already in the migrations table.\r\n *\r\n * @param options - Execution options\r\n * @returns Results for each migration\r\n *\r\n * @example\r\n * ```typescript\r\n * runner.register(CreateUsersTable);\r\n * runner.register(AddEmailIndex);\r\n * const results = await runner.runAll();\r\n * ```\r\n */\r\n public async runAll(options: ExecuteOptions = {}): Promise<MigrationResult[]> {\r\n // A dry run writes nothing, so it needs no lock.\r\n if (options.dryRun) return this.runAllUnlocked(options);\r\n\r\n // The pending set is computed INSIDE the lock, so a replica that waited sees\r\n // what the winner already applied.\r\n return this.withLock(this.getSources(), () => this.runAllUnlocked(options));\r\n }\r\n\r\n private async runAllUnlocked(options: ExecuteOptions): Promise<MigrationResult[]> {\r\n const { dryRun = false, record = true } = options;\r\n\r\n const results: MigrationResult[] = [];\r\n\r\n // Get pending migrations (a dry run must not create the migrations table)\r\n const pending = await this.getPendingMigrations({ readOnly: dryRun });\r\n\r\n if (pending.length === 0) {\r\n log.warn(\"database\", \"migration\", \"Nothing to migrate.\");\r\n return results;\r\n }\r\n\r\n // Next batch number per data source: each source has its own migrations table.\r\n const sources = new Set<DataSource>();\r\n const migrationsData: MigrationData[] = [];\r\n\r\n log.info(\"database\", \"migration\", `Found ${pending.length} pending migration(s).`);\r\n\r\n // 1. Resolve each migration's data source and transactional mode, and collect\r\n // its SQL. Drivers without SQL serialization (MongoDB) are executed\r\n // directly through the migration driver instead.\r\n // Extension checks fire concurrently as CREATE EXTENSION statements are met.\r\n const extensionChecks: Promise<void>[] = [];\r\n\r\n for (const MigrationClass of pending) {\r\n const migration = this.createMigrationInstance(MigrationClass);\r\n const name = MigrationClass.migrationName;\r\n const dataSource = this.resolveDataSource(migration.dataSource);\r\n sources.add(dataSource);\r\n\r\n if (dataSource.driver.supportsSqlSerialization === false) {\r\n migrationsData.push({\r\n MigrationClass,\r\n migration,\r\n name,\r\n dataSource,\r\n statements: [],\r\n steps: [],\r\n transactional: false,\r\n direct: true,\r\n });\r\n continue;\r\n }\r\n\r\n migration.setDriver(this.getMigrationDriver(dataSource));\r\n migration.setMigrationDefaults(dataSource.migrationDefaults);\r\n // Collection must not run `withConnection` side effects during a dry run.\r\n migration.setDryRun?.(dryRun);\r\n\r\n await migration.up();\r\n const steps: MigrationStep[] = migration.toSteps?.() ?? migration.toSQL();\r\n const statements = steps.map((step) =>\r\n typeof step === \"string\" ? step : \"-- data step (withConnection) skipped\",\r\n );\r\n migration.setDryRun?.(false);\r\n\r\n migrationsData.push({\r\n MigrationClass,\r\n migration,\r\n name,\r\n dataSource,\r\n statements,\r\n steps,\r\n transactional:\r\n !!dataSource.driver.transaction &&\r\n this.resolveTransactional(MigrationClass, migration, dataSource, statements),\r\n direct: false,\r\n });\r\n\r\n for (const sql of statements) {\r\n if (SQLGrammar.classify(sql) === \"CREATE_EXTENSION\") {\r\n const ext = SQLGrammar.extractExtensionName(sql);\r\n if (ext) extensionChecks.push(this.informIfExtensionMissing(ext, dataSource));\r\n }\r\n }\r\n }\r\n\r\n // 2. Resolve all extension checks before any SQL is executed.\r\n await Promise.all(extensionChecks);\r\n\r\n // 3. Dry run: print statements in migration order (authored order kept; no\r\n // global phase sort, which would reorder add-column → backfill → NOT NULL).\r\n if (dryRun) {\r\n log.info(\"database\", \"migration\", \"Dry run enabled. Would execute the following statements:\");\r\n for (const data of migrationsData) {\r\n for (const sql of data.statements) {\r\n console.log(\r\n `-- [${SQLGrammar.classify(sql)}] Phase ${SQLGrammar.phase(sql)} [${data.name}]`,\r\n );\r\n console.log(sql + \";\\n\");\r\n }\r\n }\r\n return [];\r\n }\r\n\r\n const batches = new Map<DataSource, number>();\r\n for (const dataSource of sources) {\r\n batches.set(dataSource, await this.getNextBatchNumber(dataSource));\r\n }\r\n\r\n // 4. Split into runs: consecutive transactional migrations on the same data\r\n // source share one transaction; every other migration (non-transactional\r\n // or direct) runs alone.\r\n const runs: MigrationData[][] = [];\r\n for (const data of migrationsData) {\r\n const last = runs[runs.length - 1];\r\n const lastFirst = last?.[0];\r\n if (\r\n data.transactional &&\r\n last &&\r\n lastFirst?.transactional &&\r\n lastFirst.dataSource === data.dataSource\r\n ) {\r\n last.push(data);\r\n } else {\r\n runs.push([data]);\r\n }\r\n }\r\n\r\n for (const run of runs) {\r\n const first = run[0];\r\n if (!first) continue;\r\n await this.refreshLocks();\r\n const dataSource = first.dataSource;\r\n const nextBatch = batches.get(dataSource)!;\r\n const startTime = Date.now();\r\n\r\n if (first.direct) {\r\n const result = await this.runMigration(first.MigrationClass, \"up\", {\r\n dryRun,\r\n record,\r\n batch: nextBatch,\r\n });\r\n results.push(result);\r\n if (!result.success) break;\r\n continue;\r\n }\r\n\r\n const driver = dataSource.driver;\r\n let failed = false;\r\n let errorMessage = \"\";\r\n /** The migration that owns the SQL statement that threw. */\r\n let failingMigrationName: string | undefined;\r\n\r\n const executeRun = async (): Promise<void> => {\r\n for (const data of run) {\r\n for (const step of data.steps) {\r\n try {\r\n if (typeof step === \"string\") await driver.query(step);\r\n else await step.data();\r\n } catch (err) {\r\n failingMigrationName = data.name;\r\n throw err;\r\n }\r\n }\r\n\r\n // A non-transactional migration is recorded right after it applied.\r\n // Transactional ones are recorded inside the shared transaction.\r\n if (record) {\r\n await this.recordMigration(\r\n data.name,\r\n nextBatch,\r\n data.MigrationClass.createdAt\r\n ? parseCreatedAt(data.MigrationClass.createdAt)\r\n : new Date(),\r\n dataSource,\r\n );\r\n }\r\n }\r\n };\r\n\r\n try {\r\n if (first.transactional && driver.transaction) {\r\n await driver.transaction(executeRun);\r\n } else {\r\n await executeRun();\r\n }\r\n } catch (err) {\r\n failed = true;\r\n errorMessage = err instanceof Error ? err.message : String(err);\r\n }\r\n\r\n const durationMs = Date.now() - startTime;\r\n\r\n // Only the migration that owns the failing statement is marked failed;\r\n // the rest of its run is reported as rolled back.\r\n for (const data of run) {\r\n const isCulprit = failed && data.name === failingMigrationName;\r\n const wasSkipped = failed && !isCulprit;\r\n\r\n results.push({\r\n name: data.name,\r\n table: data.migration.table,\r\n direction: \"up\",\r\n success: !failed,\r\n error: isCulprit ? errorMessage : undefined,\r\n durationMs: Math.round(durationMs / run.length),\r\n executedAt: new Date(),\r\n });\r\n\r\n if (isCulprit) {\r\n log.error(\r\n \"database\",\r\n \"migration\",\r\n `${colors.magenta(data.name)}: ✗ Failed: ${errorMessage}`,\r\n );\r\n } else if (wasSkipped) {\r\n log.warn(\r\n \"database\",\r\n \"migration\",\r\n `${colors.magenta(data.name)}: rolled back (batch transaction failed)`,\r\n );\r\n } else {\r\n log.success(\r\n \"database\",\r\n \"migration\",\r\n `Migrated: ${colors.magenta(data.name)} successfully`,\r\n );\r\n }\r\n }\r\n\r\n if (failed) {\r\n log.error(\r\n \"database\",\r\n \"migration\",\r\n `Batch execution failed. Rollback performed if transactional.`,\r\n );\r\n throw new Error(\"Migration batch failed: \" + errorMessage);\r\n }\r\n }\r\n\r\n const successCount = results.filter((r) => r.success).length;\r\n log.success(\r\n \"database\",\r\n \"migration\",\r\n `Migration execution complete: ${successCount}/${pending.length} migrations processed successfully.`,\r\n );\r\n\r\n return results;\r\n }\r\n\r\n /**\r\n * Export migrations as phase-ordered SQL files in database/sql/ directory.\r\n * By default, it exports all registered migrations. Use `pendingOnly: true` to export only pending ones.\r\n */\r\n public async exportSQL(options: { pendingOnly?: boolean; compact?: boolean } = {}): Promise<void> {\r\n if (this.getDataSource().driver.supportsSqlSerialization === false) {\r\n throw new Error(\r\n \"SQL export is not supported on this data source — its driver has no SQL dialect. \" +\r\n \"Migrations on this driver execute native commands through the migration driver instead.\",\r\n );\r\n }\r\n\r\n const migrationsToExport = options.pendingOnly\r\n ? await this.getPendingMigrations({ readOnly: true })\r\n : this.migrations;\r\n\r\n if (migrationsToExport.length === 0) {\r\n log.warn(\"database\", \"migration\", \"No migrations to export.\");\r\n return;\r\n }\r\n\r\n log.info(\r\n \"database\",\r\n \"migration\",\r\n `Exporting ${migrationsToExport.length} ${options.pendingOnly ? \"pending \" : \"\"}migration(s) to SQL files...`,\r\n );\r\n\r\n const upStatements: TaggedSQL[] = [];\r\n // Down SQL is grouped per migration so only the migration order is reversed,\r\n // never the statements inside one migration's down().\r\n const downGroups: { createdAt?: string; index: number; statements: TaggedSQL[] }[] = [];\r\n\r\n for (const [index, MigrationClass] of migrationsToExport.entries()) {\r\n const migration = this.createMigrationInstance(MigrationClass);\r\n const name = MigrationClass.migrationName;\r\n\r\n // Export performs no writes: `withConnection` callbacks are skipped.\r\n migration.setDryRun?.(true);\r\n\r\n // Collect up SQL\r\n await migration.up();\r\n for (const sql of migration.toSQL()) {\r\n upStatements.push({\r\n sql,\r\n phase: SQLGrammar.phase(sql),\r\n statementType: SQLGrammar.classify(sql),\r\n createdAt: MigrationClass.createdAt,\r\n migrationName: name,\r\n });\r\n }\r\n\r\n // Collect down SQL (reuse same instance — toSQL() cleared pendingOps)\r\n await migration.down();\r\n const statements: TaggedSQL[] = [];\r\n for (const sql of migration.toSQL()) {\r\n statements.push({\r\n sql,\r\n phase: SQLGrammar.phase(sql),\r\n statementType: SQLGrammar.classify(sql),\r\n createdAt: MigrationClass.createdAt,\r\n migrationName: name,\r\n });\r\n }\r\n downGroups.push({ createdAt: MigrationClass.createdAt, index, statements });\r\n }\r\n\r\n const sortedUp = SQLGrammar.sort(upStatements);\r\n // Down SQL: newest migration first (chronological, registration order breaks\r\n // ties), each migration's own down() statements keep their order.\r\n const sortedDown = downGroups\r\n .sort((a, b) => {\r\n const byDate = (a.createdAt ?? \"\").localeCompare(b.createdAt ?? \"\");\r\n return byDate !== 0 ? byDate : a.index - b.index;\r\n })\r\n .reverse()\r\n .flatMap((group) => group.statements);\r\n\r\n const upSQLString = this.formatSQLForExport(sortedUp, options.compact);\r\n const downSQLString = this.formatSQLForExport(sortedDown, options.compact);\r\n\r\n const rootPath = process.cwd();\r\n const sqlDir = path.join(rootPath, \"database\", \"sql\");\r\n\r\n if (!fs.existsSync(sqlDir)) {\r\n fs.mkdirSync(sqlDir, { recursive: true });\r\n }\r\n\r\n const timestamp = new Date().toISOString().replace(/T/, \"_\").replace(/:/g, \"-\").split(\".\")[0];\r\n const upPath = path.join(sqlDir, `migration_${timestamp}.up.sql`);\r\n const downPath = path.join(sqlDir, `migration_${timestamp}.down.sql`);\r\n\r\n fs.writeFileSync(upPath, upSQLString);\r\n fs.writeFileSync(downPath, downSQLString);\r\n\r\n log.success(\"database\", \"migration\", `Exported to:\\n- ${upPath}\\n- ${downPath}`);\r\n }\r\n\r\n /**\r\n * Rollback the last batch of migrations.\r\n *\r\n * @param options - Execution options\r\n * @returns Results for each migration\r\n */\r\n public async rollbackLast(options: ExecuteOptions = {}): Promise<MigrationResult[]> {\r\n return this.rollbackBatches(1, options);\r\n }\r\n\r\n /**\r\n * Rollback N batches of migrations.\r\n *\r\n * @param batches - Number of batches to rollback\r\n * @param options - Execution options\r\n * @returns Results for each migration\r\n */\r\n public async rollbackBatches(\r\n batches: number,\r\n options: ExecuteOptions = {},\r\n ): Promise<MigrationResult[]> {\r\n if (options.dryRun) return this.rollbackBatchesUnlocked(batches, options);\r\n\r\n return this.withLock(this.getSources(), () => this.rollbackBatchesUnlocked(batches, options));\r\n }\r\n\r\n private async rollbackBatchesUnlocked(\r\n batches: number,\r\n options: ExecuteOptions,\r\n ): Promise<MigrationResult[]> {\r\n const dryRun = options.dryRun ?? false;\r\n const record = options.record ?? true;\r\n const results: MigrationResult[] = [];\r\n\r\n const toRollback = await this.getMigrationsToRollback(batches, {\r\n pruneMissing: options.pruneMissing,\r\n readOnly: dryRun,\r\n });\r\n\r\n if (toRollback.length === 0) {\r\n log.warn(\"database\", \"migration\", \"Nothing to rollback.\");\r\n return results;\r\n }\r\n\r\n log.info(\"database\", \"migration\", `Rolling back ${toRollback.length} migration(s).`);\r\n\r\n for (const MigrationClass of toRollback) {\r\n await this.refreshLocks();\r\n\r\n const result = await this.runMigration(MigrationClass, \"down\", {\r\n dryRun,\r\n record,\r\n });\r\n results.push(result);\r\n\r\n if (!result.success) {\r\n break;\r\n }\r\n }\r\n\r\n const successCount = results.filter((r) => r.success).length;\r\n log.success(\r\n \"database\",\r\n \"migration\",\r\n `Rollback complete: ${successCount}/${toRollback.length} successful.`,\r\n );\r\n\r\n return results;\r\n }\r\n\r\n /**\r\n * Rollback all executed migrations.\r\n *\r\n * @param options - Execution options\r\n * @returns Results for each migration\r\n */\r\n public async rollbackAll(options: ExecuteOptions = {}): Promise<MigrationResult[]> {\r\n // Batch numbers are per data source; rolling back \"all\" is every batch of each.\r\n return this.rollbackBatches(Number.POSITIVE_INFINITY, options);\r\n }\r\n\r\n /**\r\n * Reset and re-run: rollback all then run all.\r\n *\r\n * @param options - Execution options\r\n * @returns Combined results\r\n */\r\n public async fresh(options: ExecuteOptions = {}): Promise<MigrationResult[]> {\r\n const rollbackResults = await this.rollbackAll(options);\r\n const runResults = await this.runAll(options);\r\n return [...rollbackResults, ...runResults];\r\n }\r\n\r\n // ============================================================================\r\n // STATUS\r\n // ============================================================================\r\n\r\n /**\r\n * Get status of all registered migrations.\r\n */\r\n public async status(): Promise<\r\n Array<{\r\n name: string;\r\n table: string;\r\n executed: boolean;\r\n batch: number | null;\r\n }>\r\n > {\r\n const executedBySource = new Map<DataSource, Map<string, MigrationRecord>>();\r\n\r\n for (const dataSource of this.getSources()) {\r\n const executed = await this.getExecutedMigrations(dataSource, { readOnly: true });\r\n executedBySource.set(dataSource, new Map(executed.map((r) => [r.name, r])));\r\n }\r\n\r\n return this.migrations.map((MigrationClass) => {\r\n const instance = new MigrationClass();\r\n const name = MigrationClass.migrationName;\r\n const record = executedBySource.get(this.sourceOf(MigrationClass))?.get(name);\r\n return {\r\n name,\r\n table: instance.table,\r\n executed: !!record,\r\n batch: record?.batch ?? null,\r\n };\r\n });\r\n }\r\n\r\n // ============================================================================\r\n // EXTENSION PRE-FLIGHT\r\n // ============================================================================\r\n\r\n /**\r\n * Check whether a database extension is available and inform the developer\r\n * if it is not installed.\r\n *\r\n * Does NOT throw — execution proceeds normally. If the extension is truly\r\n * missing, the database will surface its own error with full context already\r\n * displayed to the developer.\r\n *\r\n * @example\r\n * await this.informIfExtensionMissing(\"vector\");\r\n */\r\n private async informIfExtensionMissing(\r\n extension: string,\r\n dataSource: DataSource = this.getDataSource(),\r\n ): Promise<void> {\r\n try {\r\n const migrationDriver = this.getMigrationDriver(dataSource);\r\n const isAvailable = await migrationDriver.isExtensionAvailable(extension);\r\n\r\n if (!isAvailable) {\r\n const hr = \"─\".repeat(60);\r\n console.log(`\\n${colors.yellow(hr)}`);\r\n console.log(colors.yellow(` ⚠ Missing Database Extension: ${colors.bold(extension)}`));\r\n console.log(colors.yellow(hr));\r\n console.log();\r\n console.log(` A pending migration requires the ${colors.cyan(extension)} extension,`);\r\n console.log(` which is not installed on your database server.`);\r\n console.log();\r\n console.log(\r\n ` ${colors.bold(\"This means the physical database server is missing the extension package.\")}`,\r\n );\r\n console.log(` You cannot simply run CREATE EXTENSION until the package is installed`);\r\n console.log(` on the host machine or Docker container.`);\r\n console.log();\r\n\r\n const docsUrl = migrationDriver.getExtensionDocsUrl(extension);\r\n if (docsUrl) {\r\n console.log(` ${colors.bold(\"Or follow the installation guide:\")}`);\r\n console.log(` ${colors.cyan(docsUrl)}`);\r\n }\r\n console.log(`\\n${colors.yellow(hr)}\\n`);\r\n }\r\n } catch {\r\n // If the check itself fails, silently skip — don't break the migration.\r\n }\r\n }\r\n\r\n // ============================================================================\r\n // PRIVATE HELPERS\r\n // ============================================================================\r\n\r\n /**\r\n * Run a single migration.\r\n */\r\n private async runMigration(\r\n MigrationClass: MigrationClass,\r\n direction: \"up\" | \"down\",\r\n options: {\r\n dryRun?: boolean;\r\n record?: boolean;\r\n batch?: number;\r\n } = {},\r\n ): Promise<MigrationResult> {\r\n const { dryRun = false, record = true } = options;\r\n const startTime = Date.now();\r\n let success = true;\r\n let error: string | undefined;\r\n\r\n const migration = new MigrationClass();\r\n const name = MigrationClass.migrationName;\r\n\r\n log.info(\r\n \"database\",\r\n \"migration\",\r\n `${direction === \"up\" ? \"Migrating\" : \"Rolling back\"}: ${colors.magenta(name)}...`,\r\n );\r\n\r\n try {\r\n if (!dryRun) {\r\n // The migration runs against its declared data source (default: the runner's)\r\n // and is recorded in THAT source's migrations table.\r\n const dataSource = this.resolveDataSource(migration.dataSource);\r\n const driver = this.getMigrationDriver(dataSource);\r\n migration.setDriver(driver);\r\n migration.setMigrationDefaults(dataSource.migrationDefaults);\r\n\r\n // ============================================================================\r\n // TRANSACTION RESOLUTION (3-tier hierarchy)\r\n // ============================================================================\r\n // 1. Migration-level explicit override (instance or static)\r\n // 2. CONCURRENTLY statements (cannot run in a transaction block)\r\n // 3. Config-level global override\r\n // 4. Driver default (PostgreSQL: true, MongoDB: false)\r\n // Resolved below, once the statements are known.\r\n\r\n // ============================================================================\r\n // EXECUTE WITH OR WITHOUT TRANSACTION\r\n // ============================================================================\r\n\r\n // Collect the requested direction's operations\r\n if (direction === \"up\") {\r\n await migration.up();\r\n } else {\r\n await migration.down();\r\n }\r\n\r\n const databaseDriver = dataSource.driver;\r\n\r\n // SQL-capable drivers serialize the queued operations to SQL strings;\r\n // drivers without SQL serialization (MongoDB) keep them queued and\r\n // execute them directly through the migration driver.\r\n const directExecution = databaseDriver.supportsSqlSerialization === false;\r\n const steps: MigrationStep[] = directExecution\r\n ? []\r\n : (migration.toSteps?.() ?? migration.toSQL());\r\n const sqlStatements = steps.filter((step): step is string => typeof step === \"string\");\r\n const shouldUseTransaction = this.resolveTransactional(\r\n MigrationClass,\r\n migration,\r\n dataSource,\r\n sqlStatements,\r\n );\r\n\r\n const applyMigration = async (): Promise<void> => {\r\n if (directExecution) {\r\n await migration.execute();\r\n return;\r\n }\r\n\r\n // Execute generated SQL statements sequentially (no phase-sorting here since it's single execution)\r\n for (const step of steps) {\r\n if (typeof step === \"string\") await databaseDriver.query(step);\r\n else await step.data();\r\n }\r\n };\r\n\r\n if (shouldUseTransaction && databaseDriver.transaction) {\r\n // Transactional execution\r\n await databaseDriver.transaction(async () => {\r\n await applyMigration();\r\n\r\n // Record migration tracking\r\n if (record) {\r\n if (direction === \"up\") {\r\n const batch = options.batch ?? (await this.getNextBatchNumber(dataSource));\r\n await this.recordMigration(\r\n name,\r\n batch,\r\n MigrationClass.createdAt ? parseCreatedAt(MigrationClass.createdAt) : new Date(),\r\n dataSource,\r\n );\r\n } else {\r\n await this.removeMigrationRecord(name, dataSource);\r\n }\r\n }\r\n });\r\n } else {\r\n // Non-transactional execution\r\n await applyMigration();\r\n\r\n if (record) {\r\n if (direction === \"up\") {\r\n const batch = options.batch ?? (await this.getNextBatchNumber(dataSource));\r\n await this.recordMigration(\r\n name,\r\n batch,\r\n MigrationClass.createdAt ? parseCreatedAt(MigrationClass.createdAt) : new Date(),\r\n dataSource,\r\n );\r\n } else {\r\n await this.removeMigrationRecord(name, dataSource);\r\n }\r\n }\r\n }\r\n }\r\n } catch (err) {\r\n success = false;\r\n error = err instanceof Error ? err.message : String(err);\r\n log.error(\"database\", \"migration\", `${colors.magenta(name)}: ✗ Failed: ${error}`);\r\n throw err;\r\n }\r\n\r\n const durationMs = Date.now() - startTime;\r\n\r\n if (success) {\r\n log.success(\r\n \"database\",\r\n \"migration\",\r\n `${direction == \"up\" ? \"Migrated\" : \"Rolled back\"}: ${colors.magenta(name)} successfully (${durationMs}ms)`,\r\n );\r\n }\r\n\r\n return {\r\n name,\r\n table: migration.table,\r\n direction,\r\n success,\r\n error,\r\n durationMs,\r\n executedAt: new Date(),\r\n };\r\n }\r\n\r\n /**\r\n * Create, configure, and return a ready-to-use migration instance.\r\n *\r\n * Centralises the repeated \"new + setDriver + setMigrationDefaults\" boilerplate\r\n * that all batch/single execution paths need.\r\n *\r\n * @internal\r\n */\r\n private createMigrationInstance(MigrationClass: MigrationClass): Migration {\r\n const migration = new MigrationClass();\r\n const dataSource = this.resolveDataSource(migration.dataSource);\r\n migration.setDriver(this.getMigrationDriver(dataSource));\r\n migration.setMigrationDefaults(dataSource.migrationDefaults);\r\n return migration;\r\n }\r\n\r\n /**\r\n * Format an ordered array of TaggedSQL into a human-readable SQL file string.\r\n *\r\n * Consecutive statements that belong to the same (phase, migration) group share\r\n * a single block comment at the top, avoiding the noisy per-statement repetition.\r\n *\r\n * Example output:\r\n * ```sql\r\n * /* Phase 3 [create-users] *\\/\r\n * ALTER TABLE \"users\" ADD COLUMN \"name\" TEXT NOT NULL;\r\n * ALTER TABLE \"users\" ADD COLUMN \"email\" TEXT NOT NULL;\r\n *\r\n * /* Phase 4 [create-users] *\\/\r\n * CREATE UNIQUE INDEX ...;\r\n * ```\r\n *\r\n * @internal\r\n */\r\n private formatSQLForExport(statements: TaggedSQL[], compact: boolean = false): string {\r\n const lines: string[] = [];\r\n\r\n if (compact) {\r\n // Just output raw statements, no grouping, no blank lines\r\n for (const stmt of statements) {\r\n lines.push(`${stmt.sql};`);\r\n }\r\n return lines.join(\"\\n\");\r\n }\r\n\r\n // Group statements by their phase and migration name\r\n const grouped = new Map<string, string[]>();\r\n\r\n for (const stmt of statements) {\r\n const groupKey = `Phase ${stmt.phase} [${stmt.migrationName}]`;\r\n if (!grouped.has(groupKey)) {\r\n grouped.set(groupKey, []);\r\n }\r\n grouped.get(groupKey)!.push(stmt.sql);\r\n }\r\n\r\n // Format each group\r\n for (const [groupKey, sqls] of grouped.entries()) {\r\n if (lines.length > 0) lines.push(\"\"); // blank line between groups\r\n lines.push(`/* ${groupKey} */`);\r\n for (const sql of sqls) {\r\n lines.push(`${sql};`);\r\n }\r\n }\r\n\r\n return lines.join(\"\\n\");\r\n }\r\n\r\n /**\r\n * Get pending (not executed) registered migrations, in the order they would\r\n * execute.\r\n *\r\n * ⚠️ \"Registered\" is load-bearing. This filters `this.migrations`, which is\r\n * populated only by `register()` / `registerMany()`. A caller that has not\r\n * registered anything gets `[]` — indistinguishable from a database with\r\n * nothing pending. Anything reporting this set to a human MUST register\r\n * migrations first, or it will print a confident all-clear over an unknown.\r\n *\r\n * @see listPendingMigrations\r\n */\r\n public async getPendingMigrations(\r\n options: { readOnly?: boolean } = {},\r\n ): Promise<MigrationClass[]> {\r\n // \"Executed\" is judged in each migration's own data source's migrations table.\r\n const executedNames = new Map<DataSource, Set<string>>();\r\n for (const dataSource of this.getSources()) {\r\n const executed = await this.getExecutedMigrations(dataSource, options);\r\n executedNames.set(dataSource, new Set(executed.map((r) => r.name)));\r\n }\r\n\r\n const migrations = this.migrations.filter(\r\n (m) => !executedNames.get(this.sourceOf(m))?.has(m.migrationName),\r\n );\r\n\r\n return migrations.sort(sortMigrations);\r\n }\r\n\r\n /**\r\n * Get migrations to rollback, newest-first.\r\n *\r\n * A rollback must undo migrations in the exact inverse of the order `up`\r\n * applied them, or a `down()` will hit schema its predecessor already\r\n * removed — dropping a table before dropping the column added to it, say.\r\n *\r\n * The sort has to be explicitly **descending**. Reversing the executed list\r\n * is not enough: `getExecutedMigrations` orders by `batch, name`, so the\r\n * input is alphabetical rather than chronological, and reversing it only\r\n * yields reverse-alphabetical order. (Sorting *ascending* here — which is\r\n * what this method used to do after a `.reverse()` — silently restored the\r\n * forward `up` order and made the reverse dead code.)\r\n */\r\n private async getMigrationsToRollback(\r\n batches: number,\r\n options: { pruneMissing?: boolean; readOnly?: boolean } = {},\r\n ): Promise<MigrationClass[]> {\r\n const migrations: MigrationClass[] = [];\r\n const batchOf = new Map<MigrationClass, number>();\r\n const orphans: Array<{ name: string; dataSource: DataSource }> = [];\r\n\r\n // Batches are numbered per data source (each has its own migrations table).\r\n for (const dataSource of this.getSources()) {\r\n const executed = await this.getExecutedMigrations(dataSource, {\r\n readOnly: options.readOnly,\r\n });\r\n if (executed.length === 0) continue;\r\n\r\n const batchNumbers = [...new Set(executed.map((r) => r.batch))]\r\n .sort((a, b) => b - a)\r\n .slice(0, batches);\r\n\r\n for (const record of executed) {\r\n if (!batchNumbers.includes(record.batch)) continue;\r\n\r\n const MigrationClass = this.migrations.find(\r\n (m) => m.migrationName === record.name && this.sourceOf(m) === dataSource,\r\n );\r\n if (MigrationClass) {\r\n migrations.push(MigrationClass);\r\n batchOf.set(MigrationClass, record.batch);\r\n } else {\r\n orphans.push({ name: record.name, dataSource });\r\n }\r\n }\r\n }\r\n\r\n if (orphans.length > 0) {\r\n if (!options.pruneMissing) {\r\n throw new Error(\r\n `Cannot roll back: ${orphans.length} executed migration(s) have no registered file: ` +\r\n `${orphans.map((o) => o.name).join(\", \")}. ` +\r\n `They were renamed, deleted, or not matched by the migration pattern. ` +\r\n `Restore the file(s), or pass pruneMissing (--prune-missing) to delete those records.`,\r\n );\r\n }\r\n\r\n if (!options.readOnly) {\r\n for (const orphan of orphans) {\r\n await this.removeMigrationRecord(orphan.name, orphan.dataSource);\r\n log.warn(\"database\", \"migration\", `Pruned orphaned migration record: ${orphan.name}`);\r\n }\r\n }\r\n }\r\n\r\n // Newest batch first, then newest migration first within a batch.\r\n return migrations.sort(\r\n (a, b) =>\r\n (batchOf.get(b) ?? 0) - (batchOf.get(a) ?? 0) || sortMigrationsForRollback(a, b),\r\n );\r\n }\r\n\r\n /**\r\n * Get executed migration records.\r\n */\r\n public async getExecutedMigrations(\r\n dataSource: DataSource = this.getDataSource(),\r\n options: { readOnly?: boolean } = {},\r\n ): Promise<MigrationRecord[]> {\r\n const driver = dataSource.driver;\r\n const migrationDriver = this.getMigrationDriver(dataSource);\r\n\r\n if (options.readOnly) {\r\n // Dry runs and listings must not create the migrations table.\r\n if (!(await migrationDriver.tableExists(this.migrationsTable))) return [];\r\n } else {\r\n // A missing table is created here; every other error (connection,\r\n // permission…) propagates instead of looking like \"nothing executed\".\r\n await migrationDriver.ensureMigrationsTable(this.migrationsTable);\r\n }\r\n\r\n const queryBuilder = driver.queryBuilder<MigrationRecord>(this.migrationsTable);\r\n const records = await queryBuilder.orderBy(\"batch\", \"asc\").orderBy(\"name\", \"asc\").get();\r\n\r\n // The Mongo lock document lives in this collection; it is not a migration.\r\n return records.filter((record) => record.name !== MIGRATION_LOCK_NAME);\r\n }\r\n\r\n /**\r\n * Record a migration.\r\n */\r\n private async recordMigration(\r\n name: string,\r\n batch: number,\r\n createdAt?: Date,\r\n dataSource: DataSource = this.getDataSource(),\r\n ): Promise<void> {\r\n const driver = dataSource.driver;\r\n const migrationDriver = this.getMigrationDriver(dataSource);\r\n\r\n // Ensure migrations table exists\r\n await migrationDriver.ensureMigrationsTable(this.migrationsTable);\r\n\r\n await driver.insert(this.migrationsTable, {\r\n name,\r\n batch,\r\n executedAt: new Date(),\r\n createdAt,\r\n });\r\n }\r\n\r\n /**\r\n * Remove a migration record.\r\n */\r\n private async removeMigrationRecord(\r\n name: string,\r\n dataSource: DataSource = this.getDataSource(),\r\n ): Promise<void> {\r\n const driver = dataSource.driver;\r\n await driver.delete(this.migrationsTable, { name });\r\n }\r\n\r\n /**\r\n * Get next batch number.\r\n */\r\n private async getNextBatchNumber(\r\n dataSource: DataSource = this.getDataSource(),\r\n readOnly = false,\r\n ): Promise<number> {\r\n const executed = await this.getExecutedMigrations(dataSource, { readOnly });\r\n if (executed.length === 0) return 1;\r\n return Math.max(...executed.map((r) => r.batch)) + 1;\r\n }\r\n}\r\n\r\nexport const migrationRunner = new MigrationRunner();\r\n"],"mappings":";;;;;;;;;;;AA8EA,MAAM,sBAAsB;AAC5B,MAAM,qBAAqB;AAC3B,MAAM,wBAAwB,MAAU;AACxC,MAAM,yBAAyB,KAAK;AACpC,MAAM,0BAA0B;AAsBhC,MAAM,SAAS,OAAe,IAAI,SAAe,YAAY,WAAW,SAAS,EAAE,CAAC;;;;;;AAOpF,SAAgB,mBAAmB,MAAsB;CAGvD,QAFiB,KAAK,SAAS,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,GAEvC,CAAC,QAAQ,eAAe,EAAE,CAAC,CAAC,QAAQ,eAAe,EAAE;AACtE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,IAAa,kBAAb,MAA6B;;CAE3B,AAAgB,aAA+B,CAAC;;CAGhD,AAAQ;;CAGR,AAAiB,mCAAmB,IAAI,IAAyC;;CAGjF,AAAiB;;CAGjB,AAAiB;;;;;;CAOjB,AAAO,YACL,UAII,CAAC,GACL;EACA,KAAK,aAAa,QAAQ;EAC1B,KAAK,kBAAkB,QAAQ,mBAAmB;EAClD,KAAK,UAAU,QAAQ,WAAW;CACpC;;;;CASA,AAAO,cAAc,YAA8B;EACjD,KAAK,aAAa;EAClB,KAAK,iBAAiB,MAAM;EAC5B,OAAO;CACT;;;;CAKA,AAAQ,gBAA4B;EAClC,IAAI,CAAC,KAAK,YACR,KAAK,aAAa,m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":"migration-runner.mjs","names":[],"sources":["../../../../../../../cascade/src/migration/migration-runner.ts"],"sourcesContent":["import { colors } from \"@mongez/copper\";\nimport { log } from \"@warlock.js/logger\";\nimport fs from \"fs\";\nimport path from \"path\";\nimport type { MigrationDriverContract } from \"../contracts/migration-driver.contract\";\nimport type { DataSource } from \"../data-source/data-source\";\nimport { dataSourceRegistry } from \"../data-source/data-source-registry\";\nimport { type Migration, type MigrationContract, type MigrationStep } from \"./migration\";\nimport { sortMigrations, sortMigrationsForRollback } from \"./migration-order\";\nimport { parseCreatedAt } from \"./parse-created-at\";\nimport { SQLGrammar } from \"./sql-grammar\";\nimport type { MigrationRecord, MigrationResult, TaggedSQL } from \"./types\";\n\n/**\n * Migration class type with static name property.\n */\ntype MigrationClass = (new () => Migration) & {\n migrationName: string;\n createdAt?: string;\n};\n\n/**\n * What a caller may hand to {@link MigrationRunner.register}.\n *\n * Looser than {@link MigrationClass} in exactly one way: `migrationName` may be\n * absent. That is the documented workflow — the CLI imports a migration file and\n * sets the name from the filename afterwards — and `register()` enforces it at\n * runtime, throwing a message that says so. Once past that check the name IS\n * present, which is why everything stored and read downstream uses the stricter\n * type. Requiring it on the INPUT made the runner reject the very classes the\n * public `MigrationConstructor` describes.\n *\n * It constructs a MigrationContract rather than the abstract Migration class for the\n * same reason: `typeof Migration` is abstract and cannot satisfy `new () => Migration`,\n * and the public MigrationConstructor is declared against the contract. The runner only\n * ever uses contract members on what it registers.\n */\ntype RegisterableMigration = (new () => MigrationContract) & {\n migrationName?: string;\n createdAt?: string;\n};\n\n/**\n * Resolved instance data for a single pending migration.\n * @internal\n */\ntype MigrationData = {\n MigrationClass: MigrationClass;\n migration: Migration;\n name: string;\n /** Data source this migration runs against (its own, else the runner's). */\n dataSource: DataSource;\n /** Serialized `up()` statements (empty for direct-execution drivers). */\n statements: string[];\n /** Same as `statements`, with queued data steps kept in authored order. */\n steps: MigrationStep[];\n /** Whether the migration runs inside a transaction. */\n transactional: boolean;\n /** Driver has no SQL dialect (MongoDB): executed through `runMigration`. */\n direct: boolean;\n};\n\n/**\n * Options for migration execution.\n */\ntype ExecuteOptions = {\n /** Run in dry-run mode (no actual changes) */\n readonly dryRun?: boolean;\n /** Record to migrations table (default: true for batch, false for single) */\n readonly record?: boolean;\n /**\n * Rollback only: delete the `_migrations` rows whose migration file is no\n * longer registered, instead of failing on them.\n */\n readonly pruneMissing?: boolean;\n};\n\n/** Name of the lock document Mongo migrations claim inside the migrations collection. */\nconst MIGRATION_LOCK_NAME = \"__warlock_migration_lock__\";\nconst MIGRATION_LOCK_KEY = \"warlock:migrations\";\nconst MIGRATION_LOCK_TTL_MS = 10 * 60 * 1000;\nconst MIGRATION_LOCK_WAIT_MS = 60 * 1000;\nconst MIGRATION_LOCK_RETRY_MS = 1000;\n\n/** Held lock for one data source. `refresh` extends the TTL; `release` frees it. */\ntype MigrationLock = {\n refresh: () => Promise<void>;\n release: () => Promise<void>;\n};\n\ntype PgLockClient = {\n query: (sql: string, params?: unknown[]) => Promise<unknown>;\n release: () => void;\n};\n\n/** Minimal Mongo surface the lock needs (kept structural: no driver import). */\ntype MongoLockDatabase = {\n collection: (name: string) => {\n findOneAndUpdate: (filter: object, update: object, options: object) => Promise<unknown>;\n findOne: (filter: object) => Promise<{ owner?: string; expiresAt?: unknown } | null>;\n deleteOne: (filter: object) => Promise<unknown>;\n };\n};\n\nconst sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));\n\n/**\n * Derive a migration name from a file path: drop everything after the first dot\n * and strip a trailing `-migration` / `_migration` suffix. Shared by every CLI\n * so the same file is recorded under the same name.\n */\nexport function inferMigrationName(file: string): string {\n const basename = path.basename(file).split(\".\")[0] ?? \"\";\n\n return basename.replace(/-migration$/, \"\").replace(/_migration$/, \"\");\n}\n\n/**\n * Migration runner that executes migrations.\n *\n * This is a pure executor - it doesn't discover migrations.\n * Discovery is handled by the framework (e.g., @warlock.js/core CLI).\n *\n * The migration name is read from the static `name` property on the class,\n * which should be set by the CLI after importing:\n *\n * @example\n * ```typescript\n * // In CLI after importing:\n * const { default: MigrationClass } = await import(\"./create-users.migration.ts\");\n * MigrationClass.migrationName ??= \"create-users\";\n *\n * // Then register or execute:\n * runner.register(MigrationClass);\n * // or\n * await runner.execute(MigrationClass);\n * ```\n *\n * @example\n * ```typescript\n * // Simple direct execution\n * await runner.execute(CreateUsersTable);\n * await runner.rollback(CreateUsersTable);\n *\n * // Registry pattern for batch operations\n * runner.register(CreateUsersTable);\n * runner.register(AddEmailIndex);\n * await runner.runAll();\n * await runner.rollbackAll();\n * ```\n */\nexport class MigrationRunner {\n /** Registered migrations */\n public readonly migrations: MigrationClass[] = [];\n\n /** Data source to use */\n private dataSource?: DataSource;\n\n /** Cached migration drivers, one per data source */\n private readonly migrationDrivers = new Map<DataSource, MigrationDriverContract>();\n\n /** Table name for tracking migrations */\n private readonly migrationsTable: string;\n\n /** Whether to log operations */\n private readonly verbose: boolean;\n\n /**\n * Create a new migration runner.\n *\n * @param options - Runner options\n */\n public constructor(\n options: {\n dataSource?: DataSource;\n migrationsTable?: string;\n verbose?: boolean;\n } = {},\n ) {\n this.dataSource = options.dataSource;\n this.migrationsTable = options.migrationsTable ?? \"_migrations\";\n this.verbose = options.verbose ?? true;\n }\n\n // ============================================================================\n // DATA SOURCE\n // ============================================================================\n\n /**\n * Set the data source.\n */\n public setDataSource(dataSource: DataSource): this {\n this.dataSource = dataSource;\n this.migrationDrivers.clear();\n return this;\n }\n\n /**\n * Get the data source.\n */\n private getDataSource(): DataSource {\n if (!this.dataSource) {\n this.dataSource = dataSourceRegistry.get();\n }\n return this.dataSource;\n }\n\n /**\n * Get the migration driver.\n */\n private getMigrationDriver(\n dataSource: DataSource = this.getDataSource(),\n ): MigrationDriverContract {\n let driver = this.migrationDrivers.get(dataSource);\n if (!driver) {\n driver = dataSource.driver.migrationDriver();\n this.migrationDrivers.set(dataSource, driver);\n }\n return driver;\n }\n\n /**\n * Resolve a migration's declared data source (name or instance).\n * Falls back to the runner's own data source when it declares none.\n */\n private resolveDataSource(declared?: string | DataSource): DataSource {\n if (!declared) return this.getDataSource();\n if (typeof declared === \"string\") return dataSourceRegistry.get(declared);\n return declared;\n }\n\n /** Data source a registered migration class runs against. */\n private sourceOf(MigrationClass: MigrationClass): DataSource {\n return this.resolveDataSource(new MigrationClass().dataSource);\n }\n\n /** Distinct data sources across the registered migrations (runner's own included). */\n private getSources(): DataSource[] {\n const sources = new Set<DataSource>([this.getDataSource()]);\n for (const MigrationClass of this.migrations) {\n sources.add(this.sourceOf(MigrationClass));\n }\n return [...sources];\n }\n\n /**\n * Resolve whether a migration runs in a transaction:\n * 1. migration-level flag (instance, else the static from `Migration.create/alter`)\n * 2. `CONCURRENTLY` statements cannot run in a transaction block\n * 3. data-source config\n * 4. driver default\n */\n private resolveTransactional(\n MigrationClass: MigrationClass,\n migration: Migration,\n dataSource: DataSource,\n statements: string[] = [],\n ): boolean {\n const explicit =\n migration.transactional ?? (MigrationClass as { transactional?: boolean }).transactional;\n\n if (explicit !== undefined) return explicit;\n\n if (statements.some((sql) => /\\bCONCURRENTLY\\b/i.test(sql))) return false;\n\n return (\n dataSource.migrations?.transactional ??\n this.getMigrationDriver(dataSource).getDefaultTransactional()\n );\n }\n\n // ============================================================================\n // LOCKING\n // ============================================================================\n\n /** Locks held by the run in progress (Mongo TTL heartbeat). */\n private activeLocks: MigrationLock[] = [];\n\n /** Extend every held lock's TTL; called between migrations. */\n private async refreshLocks(): Promise<void> {\n for (const lock of this.activeLocks) {\n await lock.refresh();\n }\n }\n\n /**\n * Run `callback` while holding a migration lock on every given data source, so\n * concurrent `migrate` invocations (replicas, init containers) serialize.\n */\n private async withLock<T>(sources: DataSource[], callback: () => Promise<T>): Promise<T> {\n const held: MigrationLock[] = [];\n\n try {\n for (const dataSource of sources) {\n const lock = await this.acquireLock(dataSource);\n if (lock) held.push(lock);\n }\n\n this.activeLocks = held;\n\n return await callback();\n } finally {\n this.activeLocks = [];\n\n for (const lock of held.reverse()) {\n try {\n await lock.release();\n } catch (error) {\n log.warn(\n \"database\",\n \"migration\",\n `Failed to release migration lock: ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n }\n }\n }\n\n /** Acquire the lock for one data source; `undefined` when its driver has no lock support. */\n private async acquireLock(dataSource: DataSource): Promise<MigrationLock | undefined> {\n const driver = dataSource.driver as unknown as {\n name: string;\n getClient?: () => { connect?: () => Promise<PgLockClient> };\n getDatabase?: () => MongoLockDatabase;\n };\n\n if (driver.name === \"postgres\" && driver.getClient) {\n // A dedicated client: advisory locks are per-session, not per-pool.\n const client = await driver.getClient().connect?.();\n if (!client) return undefined;\n\n try {\n await client.query(\"SELECT pg_advisory_lock(hashtext($1))\", [MIGRATION_LOCK_KEY]);\n } catch (error) {\n client.release();\n throw error;\n }\n\n return {\n refresh: async () => {},\n release: async () => {\n try {\n await client.query(\"SELECT pg_advisory_unlock(hashtext($1))\", [MIGRATION_LOCK_KEY]);\n } finally {\n client.release();\n }\n },\n };\n }\n\n if (driver.getDatabase) {\n // The unique index on `name` (created here) makes the claim below atomic.\n await this.getMigrationDriver(dataSource).ensureMigrationsTable(this.migrationsTable);\n\n const collection = driver.getDatabase().collection(this.migrationsTable);\n const owner = `${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`;\n const deadline = Date.now() + MIGRATION_LOCK_WAIT_MS;\n\n const claim = async (): Promise<boolean> => {\n const now = new Date();\n\n try {\n await collection.findOneAndUpdate(\n {\n name: MIGRATION_LOCK_NAME,\n $or: [{ expiresAt: { $lt: now } }, { owner }],\n },\n {\n $set: { owner, expiresAt: new Date(now.getTime() + MIGRATION_LOCK_TTL_MS) },\n $setOnInsert: { batch: 0 },\n },\n { upsert: true },\n );\n\n return true;\n } catch (error) {\n // Held by someone else: the upsert collides with the unique `name` index.\n if ((error as { code?: number }).code === 11000) return false;\n throw error;\n }\n };\n\n while (!(await claim())) {\n if (Date.now() >= deadline) {\n const holder = await collection.findOne({ name: MIGRATION_LOCK_NAME });\n throw new Error(\n `Could not acquire the migration lock within ${MIGRATION_LOCK_WAIT_MS / 1000}s; ` +\n `held by \"${holder?.owner ?? \"unknown\"}\" until ${holder?.expiresAt ?? \"unknown\"}.`,\n );\n }\n\n await sleep(MIGRATION_LOCK_RETRY_MS);\n }\n\n return {\n refresh: async () => {\n await claim();\n },\n release: async () => {\n await collection.deleteOne({ name: MIGRATION_LOCK_NAME, owner });\n },\n };\n }\n\n return undefined;\n }\n\n // ============================================================================\n // REGISTRATION\n // ============================================================================\n\n /**\n * Register a migration.\n *\n * The migration name is read from `MigrationClass.migrationName`.\n *\n * @param MigrationClass - Migration class (must have static `name` set)\n * @param createdAt - Optional timestamp for ordering\n * @returns This runner for chaining\n *\n * @example\n * ```typescript\n * CreateUsersTable.migrationName = \"2024-01-15_create-users\";\n * runner.register(CreateUsersTable);\n * ```\n */\n public register(MigrationClass: RegisterableMigration): this {\n const name = MigrationClass.migrationName;\n if (!name) {\n throw new Error(\n `Migration class must have a static 'migrationName' property set. ` +\n `Set it in CLI after importing: MigrationClass.migrationName = \"filename\";`,\n );\n }\n const existing = this.migrations.find((m) => m.migrationName === name);\n\n if (existing) {\n // Re-registering the same class is idempotent; a DIFFERENT class under the\n // same name would silently never run, so fail loudly.\n if ((existing as unknown) !== MigrationClass) {\n throw new Error(\n `Duplicate migration name \"${name}\": two different migration classes resolve to the same name. ` +\n `Rename one of the files (or set a unique static 'migrationName').`,\n );\n }\n\n return this;\n }\n\n // `name` is proven present by the throw above, so this satisfies the stricter\n // stored type that every reader downstream relies on.\n this.migrations.push(MigrationClass as MigrationClass);\n\n return this;\n }\n\n /**\n * Register multiple migrations.\n *\n * @param migrations - Array of migration classes\n * @returns This runner for chaining\n */\n public registerMany(migrations: RegisterableMigration[]): this {\n for (const MigrationClass of migrations) {\n this.register(MigrationClass);\n }\n return this;\n }\n\n /**\n * Clear all registered migrations.\n */\n public clear(): this {\n this.migrations.length = 0;\n return this;\n }\n\n /**\n * Get all registered migration names.\n */\n public getRegisteredNames(): string[] {\n return this.migrations.map((m) => m.migrationName);\n }\n\n // ============================================================================\n // SINGLE EXECUTION\n // ============================================================================\n\n /**\n * Execute a single migration's up() method.\n *\n * @param MigrationClass - Migration class to execute\n * @param options - Execution options\n * @returns Migration result\n *\n * @example\n * ```typescript\n * await runner.execute(CreateUsersTable);\n * await runner.execute(AddEmailIndex, { dryRun: true });\n * ```\n */\n public async run(\n MigrationClass: MigrationClass,\n options: ExecuteOptions = {},\n ): Promise<MigrationResult> {\n return this.runMigration(MigrationClass, \"up\", {\n dryRun: options.dryRun,\n record: options.record ?? false,\n });\n }\n\n /**\n * Execute a single migration's down() method.\n *\n * @param MigrationClass - Migration class to rollback\n * @param options - Execution options\n * @returns Migration result\n *\n * @example\n * ```typescript\n * await runner.rollback(CreateUsersTable);\n * ```\n */\n public async rollback(\n MigrationClass: MigrationClass,\n options: ExecuteOptions = {},\n ): Promise<MigrationResult> {\n return this.runMigration(MigrationClass, \"down\", {\n dryRun: options.dryRun,\n record: options.record ?? false,\n });\n }\n\n // ============================================================================\n // BATCH EXECUTION (REGISTERED MIGRATIONS)\n // ============================================================================\n\n /**\n * Run all pending registered migrations.\n *\n * Only runs migrations not already in the migrations table.\n *\n * @param options - Execution options\n * @returns Results for each migration\n *\n * @example\n * ```typescript\n * runner.register(CreateUsersTable);\n * runner.register(AddEmailIndex);\n * const results = await runner.runAll();\n * ```\n */\n public async runAll(options: ExecuteOptions = {}): Promise<MigrationResult[]> {\n // A dry run writes nothing, so it needs no lock.\n if (options.dryRun) return this.runAllUnlocked(options);\n\n // The pending set is computed INSIDE the lock, so a replica that waited sees\n // what the winner already applied.\n return this.withLock(this.getSources(), () => this.runAllUnlocked(options));\n }\n\n private async runAllUnlocked(options: ExecuteOptions): Promise<MigrationResult[]> {\n const { dryRun = false, record = true } = options;\n\n const results: MigrationResult[] = [];\n\n // Get pending migrations (a dry run must not create the migrations table)\n const pending = await this.getPendingMigrations({ readOnly: dryRun });\n\n if (pending.length === 0) {\n log.warn(\"database\", \"migration\", \"Nothing to migrate.\");\n return results;\n }\n\n // Next batch number per data source: each source has its own migrations table.\n const sources = new Set<DataSource>();\n const migrationsData: MigrationData[] = [];\n\n log.info(\"database\", \"migration\", `Found ${pending.length} pending migration(s).`);\n\n // 1. Resolve each migration's data source and transactional mode, and collect\n // its SQL. Drivers without SQL serialization (MongoDB) are executed\n // directly through the migration driver instead.\n // Extension checks fire concurrently as CREATE EXTENSION statements are met.\n const extensionChecks: Promise<void>[] = [];\n\n for (const MigrationClass of pending) {\n const migration = this.createMigrationInstance(MigrationClass);\n const name = MigrationClass.migrationName;\n const dataSource = this.resolveDataSource(migration.dataSource);\n sources.add(dataSource);\n\n if (dataSource.driver.supportsSqlSerialization === false) {\n migrationsData.push({\n MigrationClass,\n migration,\n name,\n dataSource,\n statements: [],\n steps: [],\n transactional: false,\n direct: true,\n });\n continue;\n }\n\n migration.setDriver(this.getMigrationDriver(dataSource));\n migration.setMigrationDefaults(dataSource.migrationDefaults);\n // Collection must not run `withConnection` side effects during a dry run.\n migration.setDryRun?.(dryRun);\n\n await migration.up();\n const steps: MigrationStep[] = migration.toSteps?.() ?? migration.toSQL();\n const statements = steps.map((step) =>\n typeof step === \"string\" ? step : \"-- data step (withConnection) skipped\",\n );\n migration.setDryRun?.(false);\n\n migrationsData.push({\n MigrationClass,\n migration,\n name,\n dataSource,\n statements,\n steps,\n transactional:\n !!dataSource.driver.transaction &&\n this.resolveTransactional(MigrationClass, migration, dataSource, statements),\n direct: false,\n });\n\n for (const sql of statements) {\n if (SQLGrammar.classify(sql) === \"CREATE_EXTENSION\") {\n const ext = SQLGrammar.extractExtensionName(sql);\n if (ext) extensionChecks.push(this.informIfExtensionMissing(ext, dataSource));\n }\n }\n }\n\n // 2. Resolve all extension checks before any SQL is executed.\n await Promise.all(extensionChecks);\n\n // 3. Dry run: print statements in migration order (authored order kept; no\n // global phase sort, which would reorder add-column → backfill → NOT NULL).\n if (dryRun) {\n log.info(\"database\", \"migration\", \"Dry run enabled. Would execute the following statements:\");\n for (const data of migrationsData) {\n for (const sql of data.statements) {\n console.log(\n `-- [${SQLGrammar.classify(sql)}] Phase ${SQLGrammar.phase(sql)} [${data.name}]`,\n );\n console.log(sql + \";\\n\");\n }\n }\n return [];\n }\n\n const batches = new Map<DataSource, number>();\n for (const dataSource of sources) {\n batches.set(dataSource, await this.getNextBatchNumber(dataSource));\n }\n\n // 4. Split into runs: consecutive transactional migrations on the same data\n // source share one transaction; every other migration (non-transactional\n // or direct) runs alone.\n const runs: MigrationData[][] = [];\n for (const data of migrationsData) {\n const last = runs[runs.length - 1];\n const lastFirst = last?.[0];\n if (\n data.transactional &&\n last &&\n lastFirst?.transactional &&\n lastFirst.dataSource === data.dataSource\n ) {\n last.push(data);\n } else {\n runs.push([data]);\n }\n }\n\n for (const run of runs) {\n const first = run[0];\n if (!first) continue;\n await this.refreshLocks();\n const dataSource = first.dataSource;\n const nextBatch = batches.get(dataSource)!;\n const startTime = Date.now();\n\n if (first.direct) {\n const result = await this.runMigration(first.MigrationClass, \"up\", {\n dryRun,\n record,\n batch: nextBatch,\n });\n results.push(result);\n if (!result.success) break;\n continue;\n }\n\n const driver = dataSource.driver;\n let failed = false;\n let errorMessage = \"\";\n /** The migration that owns the SQL statement that threw. */\n let failingMigrationName: string | undefined;\n\n const executeRun = async (): Promise<void> => {\n for (const data of run) {\n for (const step of data.steps) {\n try {\n if (typeof step === \"string\") await driver.query(step);\n else await step.data();\n } catch (err) {\n failingMigrationName = data.name;\n throw err;\n }\n }\n\n // A non-transactional migration is recorded right after it applied.\n // Transactional ones are recorded inside the shared transaction.\n if (record) {\n await this.recordMigration(\n data.name,\n nextBatch,\n data.MigrationClass.createdAt\n ? parseCreatedAt(data.MigrationClass.createdAt)\n : new Date(),\n dataSource,\n );\n }\n }\n };\n\n try {\n if (first.transactional && driver.transaction) {\n await driver.transaction(executeRun);\n } else {\n await executeRun();\n }\n } catch (err) {\n failed = true;\n errorMessage = err instanceof Error ? err.message : String(err);\n }\n\n const durationMs = Date.now() - startTime;\n\n // Only the migration that owns the failing statement is marked failed;\n // the rest of its run is reported as rolled back.\n for (const data of run) {\n const isCulprit = failed && data.name === failingMigrationName;\n const wasSkipped = failed && !isCulprit;\n\n results.push({\n name: data.name,\n table: data.migration.table,\n direction: \"up\",\n success: !failed,\n error: isCulprit ? errorMessage : undefined,\n durationMs: Math.round(durationMs / run.length),\n executedAt: new Date(),\n });\n\n if (isCulprit) {\n log.error(\n \"database\",\n \"migration\",\n `${colors.magenta(data.name)}: ✗ Failed: ${errorMessage}`,\n );\n } else if (wasSkipped) {\n log.warn(\n \"database\",\n \"migration\",\n `${colors.magenta(data.name)}: rolled back (batch transaction failed)`,\n );\n } else {\n log.success(\n \"database\",\n \"migration\",\n `Migrated: ${colors.magenta(data.name)} successfully`,\n );\n }\n }\n\n if (failed) {\n log.error(\n \"database\",\n \"migration\",\n `Batch execution failed. Rollback performed if transactional.`,\n );\n throw new Error(\"Migration batch failed: \" + errorMessage);\n }\n }\n\n const successCount = results.filter((r) => r.success).length;\n log.success(\n \"database\",\n \"migration\",\n `Migration execution complete: ${successCount}/${pending.length} migrations processed successfully.`,\n );\n\n return results;\n }\n\n /**\n * Export migrations as phase-ordered SQL files in database/sql/ directory.\n * By default, it exports all registered migrations. Use `pendingOnly: true` to export only pending ones.\n */\n public async exportSQL(options: { pendingOnly?: boolean; compact?: boolean } = {}): Promise<void> {\n if (this.getDataSource().driver.supportsSqlSerialization === false) {\n throw new Error(\n \"SQL export is not supported on this data source — its driver has no SQL dialect. \" +\n \"Migrations on this driver execute native commands through the migration driver instead.\",\n );\n }\n\n const migrationsToExport = options.pendingOnly\n ? await this.getPendingMigrations({ readOnly: true })\n : this.migrations;\n\n if (migrationsToExport.length === 0) {\n log.warn(\"database\", \"migration\", \"No migrations to export.\");\n return;\n }\n\n log.info(\n \"database\",\n \"migration\",\n `Exporting ${migrationsToExport.length} ${options.pendingOnly ? \"pending \" : \"\"}migration(s) to SQL files...`,\n );\n\n const upStatements: TaggedSQL[] = [];\n // Down SQL is grouped per migration so only the migration order is reversed,\n // never the statements inside one migration's down().\n const downGroups: { createdAt?: string; index: number; statements: TaggedSQL[] }[] = [];\n\n for (const [index, MigrationClass] of migrationsToExport.entries()) {\n const migration = this.createMigrationInstance(MigrationClass);\n const name = MigrationClass.migrationName;\n\n // Export performs no writes: `withConnection` callbacks are skipped.\n migration.setDryRun?.(true);\n\n // Collect up SQL\n await migration.up();\n for (const sql of migration.toSQL()) {\n upStatements.push({\n sql,\n phase: SQLGrammar.phase(sql),\n statementType: SQLGrammar.classify(sql),\n createdAt: MigrationClass.createdAt,\n migrationName: name,\n });\n }\n\n // Collect down SQL (reuse same instance — toSQL() cleared pendingOps)\n await migration.down();\n const statements: TaggedSQL[] = [];\n for (const sql of migration.toSQL()) {\n statements.push({\n sql,\n phase: SQLGrammar.phase(sql),\n statementType: SQLGrammar.classify(sql),\n createdAt: MigrationClass.createdAt,\n migrationName: name,\n });\n }\n downGroups.push({ createdAt: MigrationClass.createdAt, index, statements });\n }\n\n const sortedUp = SQLGrammar.sort(upStatements);\n // Down SQL: newest migration first (chronological, registration order breaks\n // ties), each migration's own down() statements keep their order.\n const sortedDown = downGroups\n .sort((a, b) => {\n const byDate = (a.createdAt ?? \"\").localeCompare(b.createdAt ?? \"\");\n return byDate !== 0 ? byDate : a.index - b.index;\n })\n .reverse()\n .flatMap((group) => group.statements);\n\n const upSQLString = this.formatSQLForExport(sortedUp, options.compact);\n const downSQLString = this.formatSQLForExport(sortedDown, options.compact);\n\n const rootPath = process.cwd();\n const sqlDir = path.join(rootPath, \"database\", \"sql\");\n\n if (!fs.existsSync(sqlDir)) {\n fs.mkdirSync(sqlDir, { recursive: true });\n }\n\n const timestamp = new Date().toISOString().replace(/T/, \"_\").replace(/:/g, \"-\").split(\".\")[0];\n const upPath = path.join(sqlDir, `migration_${timestamp}.up.sql`);\n const downPath = path.join(sqlDir, `migration_${timestamp}.down.sql`);\n\n fs.writeFileSync(upPath, upSQLString);\n fs.writeFileSync(downPath, downSQLString);\n\n log.success(\"database\", \"migration\", `Exported to:\\n- ${upPath}\\n- ${downPath}`);\n }\n\n /**\n * Rollback the last batch of migrations.\n *\n * @param options - Execution options\n * @returns Results for each migration\n */\n public async rollbackLast(options: ExecuteOptions = {}): Promise<MigrationResult[]> {\n return this.rollbackBatches(1, options);\n }\n\n /**\n * Rollback N batches of migrations.\n *\n * @param batches - Number of batches to rollback\n * @param options - Execution options\n * @returns Results for each migration\n */\n public async rollbackBatches(\n batches: number,\n options: ExecuteOptions = {},\n ): Promise<MigrationResult[]> {\n if (options.dryRun) return this.rollbackBatchesUnlocked(batches, options);\n\n return this.withLock(this.getSources(), () => this.rollbackBatchesUnlocked(batches, options));\n }\n\n private async rollbackBatchesUnlocked(\n batches: number,\n options: ExecuteOptions,\n ): Promise<MigrationResult[]> {\n const dryRun = options.dryRun ?? false;\n const record = options.record ?? true;\n const results: MigrationResult[] = [];\n\n const toRollback = await this.getMigrationsToRollback(batches, {\n pruneMissing: options.pruneMissing,\n readOnly: dryRun,\n });\n\n if (toRollback.length === 0) {\n log.warn(\"database\", \"migration\", \"Nothing to rollback.\");\n return results;\n }\n\n log.info(\"database\", \"migration\", `Rolling back ${toRollback.length} migration(s).`);\n\n for (const MigrationClass of toRollback) {\n await this.refreshLocks();\n\n const result = await this.runMigration(MigrationClass, \"down\", {\n dryRun,\n record,\n });\n results.push(result);\n\n if (!result.success) {\n break;\n }\n }\n\n const successCount = results.filter((r) => r.success).length;\n log.success(\n \"database\",\n \"migration\",\n `Rollback complete: ${successCount}/${toRollback.length} successful.`,\n );\n\n return results;\n }\n\n /**\n * Rollback all executed migrations.\n *\n * @param options - Execution options\n * @returns Results for each migration\n */\n public async rollbackAll(options: ExecuteOptions = {}): Promise<MigrationResult[]> {\n // Batch numbers are per data source; rolling back \"all\" is every batch of each.\n return this.rollbackBatches(Number.POSITIVE_INFINITY, options);\n }\n\n /**\n * Reset and re-run: rollback all then run all.\n *\n * @param options - Execution options\n * @returns Combined results\n */\n public async fresh(options: ExecuteOptions = {}): Promise<MigrationResult[]> {\n const rollbackResults = await this.rollbackAll(options);\n const runResults = await this.runAll(options);\n return [...rollbackResults, ...runResults];\n }\n\n // ============================================================================\n // STATUS\n // ============================================================================\n\n /**\n * Get status of all registered migrations.\n */\n public async status(): Promise<\n Array<{\n name: string;\n table: string;\n executed: boolean;\n batch: number | null;\n }>\n > {\n const executedBySource = new Map<DataSource, Map<string, MigrationRecord>>();\n\n for (const dataSource of this.getSources()) {\n const executed = await this.getExecutedMigrations(dataSource, { readOnly: true });\n executedBySource.set(dataSource, new Map(executed.map((r) => [r.name, r])));\n }\n\n return this.migrations.map((MigrationClass) => {\n const instance = new MigrationClass();\n const name = MigrationClass.migrationName;\n const record = executedBySource.get(this.sourceOf(MigrationClass))?.get(name);\n return {\n name,\n table: instance.table,\n executed: !!record,\n batch: record?.batch ?? null,\n };\n });\n }\n\n // ============================================================================\n // EXTENSION PRE-FLIGHT\n // ============================================================================\n\n /**\n * Check whether a database extension is available and inform the developer\n * if it is not installed.\n *\n * Does NOT throw — execution proceeds normally. If the extension is truly\n * missing, the database will surface its own error with full context already\n * displayed to the developer.\n *\n * @example\n * await this.informIfExtensionMissing(\"vector\");\n */\n private async informIfExtensionMissing(\n extension: string,\n dataSource: DataSource = this.getDataSource(),\n ): Promise<void> {\n try {\n const migrationDriver = this.getMigrationDriver(dataSource);\n const isAvailable = await migrationDriver.isExtensionAvailable(extension);\n\n if (!isAvailable) {\n const hr = \"─\".repeat(60);\n console.log(`\\n${colors.yellow(hr)}`);\n console.log(colors.yellow(` ⚠ Missing Database Extension: ${colors.bold(extension)}`));\n console.log(colors.yellow(hr));\n console.log();\n console.log(` A pending migration requires the ${colors.cyan(extension)} extension,`);\n console.log(` which is not installed on your database server.`);\n console.log();\n console.log(\n ` ${colors.bold(\"This means the physical database server is missing the extension package.\")}`,\n );\n console.log(` You cannot simply run CREATE EXTENSION until the package is installed`);\n console.log(` on the host machine or Docker container.`);\n console.log();\n\n const docsUrl = migrationDriver.getExtensionDocsUrl(extension);\n if (docsUrl) {\n console.log(` ${colors.bold(\"Or follow the installation guide:\")}`);\n console.log(` ${colors.cyan(docsUrl)}`);\n }\n console.log(`\\n${colors.yellow(hr)}\\n`);\n }\n } catch {\n // If the check itself fails, silently skip — don't break the migration.\n }\n }\n\n // ============================================================================\n // PRIVATE HELPERS\n // ============================================================================\n\n /**\n * Run a single migration.\n */\n private async runMigration(\n MigrationClass: MigrationClass,\n direction: \"up\" | \"down\",\n options: {\n dryRun?: boolean;\n record?: boolean;\n batch?: number;\n } = {},\n ): Promise<MigrationResult> {\n const { dryRun = false, record = true } = options;\n const startTime = Date.now();\n let success = true;\n let error: string | undefined;\n\n const migration = new MigrationClass();\n const name = MigrationClass.migrationName;\n\n log.info(\n \"database\",\n \"migration\",\n `${direction === \"up\" ? \"Migrating\" : \"Rolling back\"}: ${colors.magenta(name)}...`,\n );\n\n try {\n if (!dryRun) {\n // The migration runs against its declared data source (default: the runner's)\n // and is recorded in THAT source's migrations table.\n const dataSource = this.resolveDataSource(migration.dataSource);\n const driver = this.getMigrationDriver(dataSource);\n migration.setDriver(driver);\n migration.setMigrationDefaults(dataSource.migrationDefaults);\n\n // ============================================================================\n // TRANSACTION RESOLUTION (3-tier hierarchy)\n // ============================================================================\n // 1. Migration-level explicit override (instance or static)\n // 2. CONCURRENTLY statements (cannot run in a transaction block)\n // 3. Config-level global override\n // 4. Driver default (PostgreSQL: true, MongoDB: false)\n // Resolved below, once the statements are known.\n\n // ============================================================================\n // EXECUTE WITH OR WITHOUT TRANSACTION\n // ============================================================================\n\n // Collect the requested direction's operations\n if (direction === \"up\") {\n await migration.up();\n } else {\n await migration.down();\n }\n\n const databaseDriver = dataSource.driver;\n\n // SQL-capable drivers serialize the queued operations to SQL strings;\n // drivers without SQL serialization (MongoDB) keep them queued and\n // execute them directly through the migration driver.\n const directExecution = databaseDriver.supportsSqlSerialization === false;\n const steps: MigrationStep[] = directExecution\n ? []\n : (migration.toSteps?.() ?? migration.toSQL());\n const sqlStatements = steps.filter((step): step is string => typeof step === \"string\");\n const shouldUseTransaction = this.resolveTransactional(\n MigrationClass,\n migration,\n dataSource,\n sqlStatements,\n );\n\n const applyMigration = async (): Promise<void> => {\n if (directExecution) {\n await migration.execute();\n return;\n }\n\n // Execute generated SQL statements sequentially (no phase-sorting here since it's single execution)\n for (const step of steps) {\n if (typeof step === \"string\") await databaseDriver.query(step);\n else await step.data();\n }\n };\n\n if (shouldUseTransaction && databaseDriver.transaction) {\n // Transactional execution\n await databaseDriver.transaction(async () => {\n await applyMigration();\n\n // Record migration tracking\n if (record) {\n if (direction === \"up\") {\n const batch = options.batch ?? (await this.getNextBatchNumber(dataSource));\n await this.recordMigration(\n name,\n batch,\n MigrationClass.createdAt ? parseCreatedAt(MigrationClass.createdAt) : new Date(),\n dataSource,\n );\n } else {\n await this.removeMigrationRecord(name, dataSource);\n }\n }\n });\n } else {\n // Non-transactional execution\n await applyMigration();\n\n if (record) {\n if (direction === \"up\") {\n const batch = options.batch ?? (await this.getNextBatchNumber(dataSource));\n await this.recordMigration(\n name,\n batch,\n MigrationClass.createdAt ? parseCreatedAt(MigrationClass.createdAt) : new Date(),\n dataSource,\n );\n } else {\n await this.removeMigrationRecord(name, dataSource);\n }\n }\n }\n }\n } catch (err) {\n success = false;\n error = err instanceof Error ? err.message : String(err);\n log.error(\"database\", \"migration\", `${colors.magenta(name)}: ✗ Failed: ${error}`);\n throw err;\n }\n\n const durationMs = Date.now() - startTime;\n\n if (success) {\n log.success(\n \"database\",\n \"migration\",\n `${direction == \"up\" ? \"Migrated\" : \"Rolled back\"}: ${colors.magenta(name)} successfully (${durationMs}ms)`,\n );\n }\n\n return {\n name,\n table: migration.table,\n direction,\n success,\n error,\n durationMs,\n executedAt: new Date(),\n };\n }\n\n /**\n * Create, configure, and return a ready-to-use migration instance.\n *\n * Centralises the repeated \"new + setDriver + setMigrationDefaults\" boilerplate\n * that all batch/single execution paths need.\n *\n * @internal\n */\n private createMigrationInstance(MigrationClass: MigrationClass): Migration {\n const migration = new MigrationClass();\n const dataSource = this.resolveDataSource(migration.dataSource);\n migration.setDriver(this.getMigrationDriver(dataSource));\n migration.setMigrationDefaults(dataSource.migrationDefaults);\n return migration;\n }\n\n /**\n * Format an ordered array of TaggedSQL into a human-readable SQL file string.\n *\n * Consecutive statements that belong to the same (phase, migration) group share\n * a single block comment at the top, avoiding the noisy per-statement repetition.\n *\n * Example output:\n * ```sql\n * /* Phase 3 [create-users] *\\/\n * ALTER TABLE \"users\" ADD COLUMN \"name\" TEXT NOT NULL;\n * ALTER TABLE \"users\" ADD COLUMN \"email\" TEXT NOT NULL;\n *\n * /* Phase 4 [create-users] *\\/\n * CREATE UNIQUE INDEX ...;\n * ```\n *\n * @internal\n */\n private formatSQLForExport(statements: TaggedSQL[], compact: boolean = false): string {\n const lines: string[] = [];\n\n if (compact) {\n // Just output raw statements, no grouping, no blank lines\n for (const stmt of statements) {\n lines.push(`${stmt.sql};`);\n }\n return lines.join(\"\\n\");\n }\n\n // Group statements by their phase and migration name\n const grouped = new Map<string, string[]>();\n\n for (const stmt of statements) {\n const groupKey = `Phase ${stmt.phase} [${stmt.migrationName}]`;\n if (!grouped.has(groupKey)) {\n grouped.set(groupKey, []);\n }\n grouped.get(groupKey)!.push(stmt.sql);\n }\n\n // Format each group\n for (const [groupKey, sqls] of grouped.entries()) {\n if (lines.length > 0) lines.push(\"\"); // blank line between groups\n lines.push(`/* ${groupKey} */`);\n for (const sql of sqls) {\n lines.push(`${sql};`);\n }\n }\n\n return lines.join(\"\\n\");\n }\n\n /**\n * Get pending (not executed) registered migrations, in the order they would\n * execute.\n *\n * ⚠️ \"Registered\" is load-bearing. This filters `this.migrations`, which is\n * populated only by `register()` / `registerMany()`. A caller that has not\n * registered anything gets `[]` — indistinguishable from a database with\n * nothing pending. Anything reporting this set to a human MUST register\n * migrations first, or it will print a confident all-clear over an unknown.\n *\n * @see listPendingMigrations\n */\n public async getPendingMigrations(\n options: { readOnly?: boolean } = {},\n ): Promise<MigrationClass[]> {\n // \"Executed\" is judged in each migration's own data source's migrations table.\n const executedNames = new Map<DataSource, Set<string>>();\n for (const dataSource of this.getSources()) {\n const executed = await this.getExecutedMigrations(dataSource, options);\n executedNames.set(dataSource, new Set(executed.map((r) => r.name)));\n }\n\n const migrations = this.migrations.filter(\n (m) => !executedNames.get(this.sourceOf(m))?.has(m.migrationName),\n );\n\n return migrations.sort(sortMigrations);\n }\n\n /**\n * Get migrations to rollback, newest-first.\n *\n * A rollback must undo migrations in the exact inverse of the order `up`\n * applied them, or a `down()` will hit schema its predecessor already\n * removed — dropping a table before dropping the column added to it, say.\n *\n * The sort has to be explicitly **descending**. Reversing the executed list\n * is not enough: `getExecutedMigrations` orders by `batch, name`, so the\n * input is alphabetical rather than chronological, and reversing it only\n * yields reverse-alphabetical order. (Sorting *ascending* here — which is\n * what this method used to do after a `.reverse()` — silently restored the\n * forward `up` order and made the reverse dead code.)\n */\n private async getMigrationsToRollback(\n batches: number,\n options: { pruneMissing?: boolean; readOnly?: boolean } = {},\n ): Promise<MigrationClass[]> {\n const migrations: MigrationClass[] = [];\n const batchOf = new Map<MigrationClass, number>();\n const orphans: Array<{ name: string; dataSource: DataSource }> = [];\n\n // Batches are numbered per data source (each has its own migrations table).\n for (const dataSource of this.getSources()) {\n const executed = await this.getExecutedMigrations(dataSource, {\n readOnly: options.readOnly,\n });\n if (executed.length === 0) continue;\n\n const batchNumbers = [...new Set(executed.map((r) => r.batch))]\n .sort((a, b) => b - a)\n .slice(0, batches);\n\n for (const record of executed) {\n if (!batchNumbers.includes(record.batch)) continue;\n\n const MigrationClass = this.migrations.find(\n (m) => m.migrationName === record.name && this.sourceOf(m) === dataSource,\n );\n if (MigrationClass) {\n migrations.push(MigrationClass);\n batchOf.set(MigrationClass, record.batch);\n } else {\n orphans.push({ name: record.name, dataSource });\n }\n }\n }\n\n if (orphans.length > 0) {\n if (!options.pruneMissing) {\n throw new Error(\n `Cannot roll back: ${orphans.length} executed migration(s) have no registered file: ` +\n `${orphans.map((o) => o.name).join(\", \")}. ` +\n `They were renamed, deleted, or not matched by the migration pattern. ` +\n `Restore the file(s), or pass pruneMissing (--prune-missing) to delete those records.`,\n );\n }\n\n if (!options.readOnly) {\n for (const orphan of orphans) {\n await this.removeMigrationRecord(orphan.name, orphan.dataSource);\n log.warn(\"database\", \"migration\", `Pruned orphaned migration record: ${orphan.name}`);\n }\n }\n }\n\n // Newest batch first, then newest migration first within a batch.\n return migrations.sort(\n (a, b) =>\n (batchOf.get(b) ?? 0) - (batchOf.get(a) ?? 0) || sortMigrationsForRollback(a, b),\n );\n }\n\n /**\n * Get executed migration records.\n */\n public async getExecutedMigrations(\n dataSource: DataSource = this.getDataSource(),\n options: { readOnly?: boolean } = {},\n ): Promise<MigrationRecord[]> {\n const driver = dataSource.driver;\n const migrationDriver = this.getMigrationDriver(dataSource);\n\n if (options.readOnly) {\n // Dry runs and listings must not create the migrations table.\n if (!(await migrationDriver.tableExists(this.migrationsTable))) return [];\n } else {\n // A missing table is created here; every other error (connection,\n // permission…) propagates instead of looking like \"nothing executed\".\n await migrationDriver.ensureMigrationsTable(this.migrationsTable);\n }\n\n const queryBuilder = driver.queryBuilder<MigrationRecord>(this.migrationsTable);\n const records = await queryBuilder.orderBy(\"batch\", \"asc\").orderBy(\"name\", \"asc\").get();\n\n // The Mongo lock document lives in this collection; it is not a migration.\n return records.filter((record) => record.name !== MIGRATION_LOCK_NAME);\n }\n\n /**\n * Record a migration.\n */\n private async recordMigration(\n name: string,\n batch: number,\n createdAt?: Date,\n dataSource: DataSource = this.getDataSource(),\n ): Promise<void> {\n const driver = dataSource.driver;\n const migrationDriver = this.getMigrationDriver(dataSource);\n\n // Ensure migrations table exists\n await migrationDriver.ensureMigrationsTable(this.migrationsTable);\n\n await driver.insert(this.migrationsTable, {\n name,\n batch,\n executedAt: new Date(),\n createdAt,\n });\n }\n\n /**\n * Remove a migration record.\n */\n private async removeMigrationRecord(\n name: string,\n dataSource: DataSource = this.getDataSource(),\n ): Promise<void> {\n const driver = dataSource.driver;\n await driver.delete(this.migrationsTable, { name });\n }\n\n /**\n * Get next batch number.\n */\n private async getNextBatchNumber(\n dataSource: DataSource = this.getDataSource(),\n readOnly = false,\n ): Promise<number> {\n const executed = await this.getExecutedMigrations(dataSource, { readOnly });\n if (executed.length === 0) return 1;\n return Math.max(...executed.map((r) => r.batch)) + 1;\n }\n}\n\nexport const migrationRunner = new MigrationRunner();\n"],"mappings":";;;;;;;;;;;AA8EA,MAAM,sBAAsB;AAC5B,MAAM,qBAAqB;AAC3B,MAAM,wBAAwB,MAAU;AACxC,MAAM,yBAAyB,KAAK;AACpC,MAAM,0BAA0B;AAsBhC,MAAM,SAAS,OAAe,IAAI,SAAe,YAAY,WAAW,SAAS,EAAE,CAAC;;;;;;AAOpF,SAAgB,mBAAmB,MAAsB;CAGvD,QAFiB,KAAK,SAAS,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,GAEvC,CAAC,QAAQ,eAAe,EAAE,CAAC,CAAC,QAAQ,eAAe,EAAE;AACtE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,IAAa,kBAAb,MAA6B;;CAE3B,AAAgB,aAA+B,CAAC;;CAGhD,AAAQ;;CAGR,AAAiB,mCAAmB,IAAI,IAAyC;;CAGjF,AAAiB;;CAGjB,AAAiB;;;;;;CAOjB,AAAO,YACL,UAII,CAAC,GACL;EACA,KAAK,aAAa,QAAQ;EAC1B,KAAK,kBAAkB,QAAQ,mBAAmB;EAClD,KAAK,UAAU,QAAQ,WAAW;CACpC;;;;CASA,AAAO,cAAc,YAA8B;EACjD,KAAK,aAAa;EAClB,KAAK,iBAAiB,MAAM;EAC5B,OAAO;CACT;;;;CAKA,AAAQ,gBAA4B;EAClC,IAAI,CAAC,KAAK,YACR,KAAK,aAAa,mBAAmB,IAAI;EAE3C,OAAO,KAAK;CACd;;;;CAKA,AAAQ,mBAC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/sql-grammar.ts"],"sourcesContent":["import { compareCreatedAt } from \"./parse-created-at\";\r\nimport type { SQLStatementType, TaggedSQL } from \"./types\";\r\n\r\n/**\r\n * Parses and sorts SQL statements globally based on their execution phase,\r\n * and classifies statements by their semantic type.\r\n */\r\nexport class SQLGrammar {\r\n /**\r\n * Determine the execution phase of a SQL statement (1–6).\r\n *\r\n * Phase ordering ensures DDL operations run in dependency-safe order\r\n * regardless of which migration file they originate from.\r\n *\r\n * | Phase | Statements |\r\n * |-------|-----------------------------------------------|\r\n * | 1 | CREATE EXTENSION, TYPE, DOMAIN, SCHEMA |\r\n * | 2 | CREATE TABLE |\r\n * | 3 | ADD COLUMN, ADD PRIMARY KEY |\r\n * | 4 | CREATE INDEX, ADD FOREIGN KEY |\r\n * | 5 | DROP COLUMN, ALTER COLUMN, DROP TABLE |\r\n * | 6 | Raw / unclassified |\r\n *\r\n * @example\r\n * SQLGrammar.phase(\"CREATE EXTENSION IF NOT EXISTS vector\"); // => 1\r\n * SQLGrammar.phase(\"ALTER TABLE users ADD COLUMN email TEXT\"); // => 3\r\n */\r\n public static phase(sql: string): 1 | 2 | 3 | 4 | 5 | 6 {\r\n const s = sql.trim().toUpperCase();\r\n\r\n // Phase 1: Preparation (Extensions, Enums, Domains, Types)\r\n if (\r\n s.startsWith(\"CREATE EXTENSION\") ||\r\n s.startsWith(\"CREATE TYPE\") ||\r\n s.startsWith(\"CREATE DOMAIN\") ||\r\n s.startsWith(\"CREATE SCHEMA\")\r\n ) {\r\n return 1;\r\n }\r\n\r\n // Phase 2: Table Creation\r\n if (s.startsWith(\"CREATE TABLE\")) {\r\n return 2;\r\n }\r\n\r\n // Phase 3 / 4 / 5: ALTER TABLE — broken down by clause\r\n if (s.startsWith(\"ALTER TABLE\")) {\r\n if (s.includes(\"ADD CONSTRAINT\") && s.includes(\"FOREIGN KEY\")) return 4;\r\n if (s.includes(\"DROP COLUMN\") || s.includes(\"DROP CONSTRAINT\") || s.includes(\"ALTER COLUMN\")) return 5;\r\n if (s.includes(\"ADD COLUMN\")) return 3;\r\n if (s.includes(\"ADD CONSTRAINT\") && s.includes(\"PRIMARY KEY\")) return 3;\r\n }\r\n\r\n // Phase 4: Index Creation\r\n if (s.startsWith(\"CREATE INDEX\") || s.startsWith(\"CREATE UNIQUE INDEX\")) {\r\n return 4;\r\n }\r\n\r\n // Phase 5: Destructive table/index operations\r\n if (s.startsWith(\"DROP TABLE\") || s.startsWith(\"TRUNCATE TABLE\") || s.startsWith(\"DROP INDEX\")) {\r\n return 5;\r\n }\r\n\r\n // Phase 6: Raw / unclassified (DML, triggers, procedures, views)\r\n return 6;\r\n }\r\n\r\n /**\r\n * Classify a SQL statement into its semantic statement type.\r\n *\r\n * This is independent of execution phase — it identifies *what* a statement\r\n * does, not *when* it should run. Use this for pre-flight checks, dry-run\r\n * display, selective filtering, and extension detection.\r\n *\r\n * @example\r\n * SQLGrammar.classify(\"CREATE EXTENSION IF NOT EXISTS vector\");\r\n * // => \"CREATE_EXTENSION\"\r\n *\r\n * SQLGrammar.classify(\"ALTER TABLE users ADD COLUMN email TEXT\");\r\n * // => \"ADD_COLUMN\"\r\n */\r\n public static classify(sql: string): SQLStatementType {\r\n const s = sql.trim().toUpperCase();\r\n\r\n if (s.startsWith(\"CREATE EXTENSION\")) return \"CREATE_EXTENSION\";\r\n if (s.startsWith(\"CREATE SCHEMA\")) return \"CREATE_SCHEMA\";\r\n if (s.startsWith(\"CREATE TYPE\")) return \"CREATE_TYPE\";\r\n if (s.startsWith(\"CREATE DOMAIN\")) return \"CREATE_DOMAIN\";\r\n if (s.startsWith(\"CREATE TABLE\")) return \"CREATE_TABLE\";\r\n if (s.startsWith(\"DROP TABLE\")) return \"DROP_TABLE\";\r\n if (s.startsWith(\"TRUNCATE TABLE\")) return \"TRUNCATE_TABLE\";\r\n if (s.startsWith(\"DROP INDEX\")) return \"DROP_INDEX\";\r\n\r\n // Must check UNIQUE before plain INDEX to avoid misclassification\r\n if (s.startsWith(\"CREATE UNIQUE INDEX\")) return \"CREATE_UNIQUE_INDEX\";\r\n if (s.startsWith(\"CREATE INDEX\")) return \"CREATE_INDEX\";\r\n\r\n if (s.startsWith(\"ALTER TABLE\")) {\r\n if (s.includes(\"ADD CONSTRAINT\") && s.includes(\"FOREIGN KEY\")) return \"ADD_FOREIGN_KEY\";\r\n if (s.includes(\"DROP CONSTRAINT\") && s.includes(\"FOREIGN KEY\")) return \"DROP_FOREIGN_KEY\";\r\n if (s.includes(\"ADD CONSTRAINT\") && s.includes(\"PRIMARY KEY\")) return \"ADD_PRIMARY_KEY\";\r\n if (s.includes(\"DROP CONSTRAINT\")) return \"DROP_PRIMARY_KEY\";\r\n if (s.includes(\"ADD COLUMN\")) return \"ADD_COLUMN\";\r\n if (s.includes(\"DROP COLUMN\")) return \"DROP_COLUMN\";\r\n if (s.includes(\"RENAME COLUMN\")) return \"RENAME_COLUMN\";\r\n if (s.includes(\"RENAME TO\")) return \"RENAME_TABLE\";\r\n if (s.includes(\"ALTER COLUMN\")) return \"MODIFY_COLUMN\";\r\n }\r\n\r\n return \"RAW\";\r\n }\r\n\r\n /**\r\n * Extract the extension name from a CREATE EXTENSION statement.\r\n *\r\n * Returns undefined if the statement is not a CREATE EXTENSION statement\r\n * or the name cannot be parsed.\r\n *\r\n * @example\r\n * SQLGrammar.extractExtensionName(\"CREATE EXTENSION IF NOT EXISTS vector\");\r\n * // => \"vector\"\r\n *\r\n * SQLGrammar.extractExtensionName(\"CREATE EXTENSION postgis\");\r\n * // => \"postgis\"\r\n */\r\n public static extractExtensionName(sql: string): string | undefined {\r\n // Matches: CREATE EXTENSION [IF NOT EXISTS] <name>\r\n const match = sql\r\n .trim()\r\n .match(/^CREATE\\s+EXTENSION\\s+(?:IF\\s+NOT\\s+EXISTS\\s+)?(\\w+)/i);\r\n\r\n return match?.[1]?.toLowerCase();\r\n }\r\n\r\n /**\r\n * Sort an array of tagged SQL statements by phase, then creation date, then migration name.\r\n *\r\n * This is the sort that decides **execution order** — it runs across every\r\n * pending migration's statements at once, after the migration list itself\r\n * has been ordered, so whatever it decides is final.\r\n *\r\n * The creation date must go through {@link compareCreatedAt}. Parsing these\r\n * stamps with `new Date()` returns `Invalid Date` for the framework's own\r\n * `MM-DD-YYYY_HH-MM-SS` filename format, which collapsed every timestamp to\r\n * the same value and quietly handed the whole ordering to the alphabetical\r\n * tiebreaker below — running, for example, a January 2026 migration before a\r\n * December 2025 one.\r\n */\r\n public static sort(statements: TaggedSQL[]): TaggedSQL[] {\r\n return statements.slice().sort((a, b) => {\r\n // 1. Sort by Phase (ascending)\r\n if (a.phase !== b.phase) {\r\n return a.phase - b.phase;\r\n }\r\n\r\n // 2. Sort by CreatedAt (within same phase)\r\n const byCreatedAt = compareCreatedAt(a.createdAt, b.createdAt);\r\n\r\n if (byCreatedAt !== undefined) {\r\n return byCreatedAt;\r\n }\r\n\r\n // 3. Sort by Migration Name (tie-breaker)\r\n const nameA = a.migrationName || \"\";\r\n const nameB = b.migrationName || \"\";\r\n return nameA.localeCompare(nameB);\r\n });\r\n }\r\n}\r\n"],"mappings":";;;;;;;AAOA,IAAa,aAAb,MAAwB;;;;;;;;;;;;;;;;;;;;CAoBtB,OAAc,MAAM,KAAoC;EACtD,MAAM,IAAI,IAAI,KAAK,CAAC,CAAC,YAAY;EAGjC,IACE,EAAE,WAAW,kBAAkB,KAC/B,EAAE,WAAW,aAAa,KAC1B,EAAE,WAAW,eAAe,KAC5B,EAAE,WAAW,eAAe,GAE5B,OAAO;EAIT,IAAI,EAAE,WAAW,cAAc,GAC7B,OAAO;EAIT,IAAI,EAAE,WAAW,aAAa,GAAG;GAC/B,IAAI,EAAE,SAAS,gBAAgB,KAAK,EAAE,SAAS,aAAa,GAAG,OAAO;GACtE,IAAI,EAAE,SAAS,aAAa,KAAK,EAAE,SAAS,iBAAiB,KAAK,EAAE,SAAS,cAAc,GAAG,OAAO;GACrG,IAAI,EAAE,SAAS,YAAY,GAAG,OAAO;GACrC,IAAI,EAAE,SAAS,gBAAgB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cAAc,GAAG,OAAO;EACzC;EAEA,OAAO;CACT;;;;;;;;;;;;;;CAeA,OAAc,qBAAqB,KAAiC;EAMlE,OAJc,IACX,KAAK,CAAC,CACN,MAAM,uDAEE,CAAC,GAAG,EAAE,EAAE,YAAY;CACjC;;;;;;;;;;;;;;;CAgBA,OAAc,KAAK,YAAsC;EACvD,OAAO,WAAW,MAAM,CAAC,CAAC,MAAM,GAAG,MAAM;GAEvC,IAAI,EAAE,UAAU,EAAE,OAChB,OAAO,EAAE,QAAQ,EAAE;GAIrB,MAAM,cAAc,iBAAiB,EAAE,WAAW,EAAE,SAAS;GAE7D,IAAI,gBAAgB,QAClB,OAAO;GAIT,MAAM,QAAQ,EAAE,iBAAiB;GACjC,MAAM,QAAQ,EAAE,iBAAiB;GACjC,OAAO,MAAM,cAAc,KAAK;EAClC,CAAC;CACH;AACF"}
1
+ {"version":3,"file":"sql-grammar.mjs","names":[],"sources":["../../../../../../../cascade/src/migration/sql-grammar.ts"],"sourcesContent":["import { compareCreatedAt } from \"./parse-created-at\";\nimport type { SQLStatementType, TaggedSQL } from \"./types\";\n\n/**\n * Parses and sorts SQL statements globally based on their execution phase,\n * and classifies statements by their semantic type.\n */\nexport class SQLGrammar {\n /**\n * Determine the execution phase of a SQL statement (1–6).\n *\n * Phase ordering ensures DDL operations run in dependency-safe order\n * regardless of which migration file they originate from.\n *\n * | Phase | Statements |\n * |-------|-----------------------------------------------|\n * | 1 | CREATE EXTENSION, TYPE, DOMAIN, SCHEMA |\n * | 2 | CREATE TABLE |\n * | 3 | ADD COLUMN, ADD PRIMARY KEY |\n * | 4 | CREATE INDEX, ADD FOREIGN KEY |\n * | 5 | DROP COLUMN, ALTER COLUMN, DROP TABLE |\n * | 6 | Raw / unclassified |\n *\n * @example\n * SQLGrammar.phase(\"CREATE EXTENSION IF NOT EXISTS vector\"); // => 1\n * SQLGrammar.phase(\"ALTER TABLE users ADD COLUMN email TEXT\"); // => 3\n */\n public static phase(sql: string): 1 | 2 | 3 | 4 | 5 | 6 {\n const s = sql.trim().toUpperCase();\n\n // Phase 1: Preparation (Extensions, Enums, Domains, Types)\n if (\n s.startsWith(\"CREATE EXTENSION\") ||\n s.startsWith(\"CREATE TYPE\") ||\n s.startsWith(\"CREATE DOMAIN\") ||\n s.startsWith(\"CREATE SCHEMA\")\n ) {\n return 1;\n }\n\n // Phase 2: Table Creation\n if (s.startsWith(\"CREATE TABLE\")) {\n return 2;\n }\n\n // Phase 3 / 4 / 5: ALTER TABLE — broken down by clause\n if (s.startsWith(\"ALTER TABLE\")) {\n if (s.includes(\"ADD CONSTRAINT\") && s.includes(\"FOREIGN KEY\")) return 4;\n if (s.includes(\"DROP COLUMN\") || s.includes(\"DROP CONSTRAINT\") || s.includes(\"ALTER COLUMN\")) return 5;\n if (s.includes(\"ADD COLUMN\")) return 3;\n if (s.includes(\"ADD CONSTRAINT\") && s.includes(\"PRIMARY KEY\")) return 3;\n }\n\n // Phase 4: Index Creation\n if (s.startsWith(\"CREATE INDEX\") || s.startsWith(\"CREATE UNIQUE INDEX\")) {\n return 4;\n }\n\n // Phase 5: Destructive table/index operations\n if (s.startsWith(\"DROP TABLE\") || s.startsWith(\"TRUNCATE TABLE\") || s.startsWith(\"DROP INDEX\")) {\n return 5;\n }\n\n // Phase 6: Raw / unclassified (DML, triggers, procedures, views)\n return 6;\n }\n\n /**\n * Classify a SQL statement into its semantic statement type.\n *\n * This is independent of execution phase — it identifies *what* a statement\n * does, not *when* it should run. Use this for pre-flight checks, dry-run\n * display, selective filtering, and extension detection.\n *\n * @example\n * SQLGrammar.classify(\"CREATE EXTENSION IF NOT EXISTS vector\");\n * // => \"CREATE_EXTENSION\"\n *\n * SQLGrammar.classify(\"ALTER TABLE users ADD COLUMN email TEXT\");\n * // => \"ADD_COLUMN\"\n */\n public static classify(sql: string): SQLStatementType {\n const s = sql.trim().toUpperCase();\n\n if (s.startsWith(\"CREATE EXTENSION\")) return \"CREATE_EXTENSION\";\n if (s.startsWith(\"CREATE SCHEMA\")) return \"CREATE_SCHEMA\";\n if (s.startsWith(\"CREATE TYPE\")) return \"CREATE_TYPE\";\n if (s.startsWith(\"CREATE DOMAIN\")) return \"CREATE_DOMAIN\";\n if (s.startsWith(\"CREATE TABLE\")) return \"CREATE_TABLE\";\n if (s.startsWith(\"DROP TABLE\")) return \"DROP_TABLE\";\n if (s.startsWith(\"TRUNCATE TABLE\")) return \"TRUNCATE_TABLE\";\n if (s.startsWith(\"DROP INDEX\")) return \"DROP_INDEX\";\n\n // Must check UNIQUE before plain INDEX to avoid misclassification\n if (s.startsWith(\"CREATE UNIQUE INDEX\")) return \"CREATE_UNIQUE_INDEX\";\n if (s.startsWith(\"CREATE INDEX\")) return \"CREATE_INDEX\";\n\n if (s.startsWith(\"ALTER TABLE\")) {\n if (s.includes(\"ADD CONSTRAINT\") && s.includes(\"FOREIGN KEY\")) return \"ADD_FOREIGN_KEY\";\n if (s.includes(\"DROP CONSTRAINT\") && s.includes(\"FOREIGN KEY\")) return \"DROP_FOREIGN_KEY\";\n if (s.includes(\"ADD CONSTRAINT\") && s.includes(\"PRIMARY KEY\")) return \"ADD_PRIMARY_KEY\";\n if (s.includes(\"DROP CONSTRAINT\")) return \"DROP_PRIMARY_KEY\";\n if (s.includes(\"ADD COLUMN\")) return \"ADD_COLUMN\";\n if (s.includes(\"DROP COLUMN\")) return \"DROP_COLUMN\";\n if (s.includes(\"RENAME COLUMN\")) return \"RENAME_COLUMN\";\n if (s.includes(\"RENAME TO\")) return \"RENAME_TABLE\";\n if (s.includes(\"ALTER COLUMN\")) return \"MODIFY_COLUMN\";\n }\n\n return \"RAW\";\n }\n\n /**\n * Extract the extension name from a CREATE EXTENSION statement.\n *\n * Returns undefined if the statement is not a CREATE EXTENSION statement\n * or the name cannot be parsed.\n *\n * @example\n * SQLGrammar.extractExtensionName(\"CREATE EXTENSION IF NOT EXISTS vector\");\n * // => \"vector\"\n *\n * SQLGrammar.extractExtensionName(\"CREATE EXTENSION postgis\");\n * // => \"postgis\"\n */\n public static extractExtensionName(sql: string): string | undefined {\n // Matches: CREATE EXTENSION [IF NOT EXISTS] <name>\n const match = sql\n .trim()\n .match(/^CREATE\\s+EXTENSION\\s+(?:IF\\s+NOT\\s+EXISTS\\s+)?(\\w+)/i);\n\n return match?.[1]?.toLowerCase();\n }\n\n /**\n * Sort an array of tagged SQL statements by phase, then creation date, then migration name.\n *\n * This is the sort that decides **execution order** — it runs across every\n * pending migration's statements at once, after the migration list itself\n * has been ordered, so whatever it decides is final.\n *\n * The creation date must go through {@link compareCreatedAt}. Parsing these\n * stamps with `new Date()` returns `Invalid Date` for the framework's own\n * `MM-DD-YYYY_HH-MM-SS` filename format, which collapsed every timestamp to\n * the same value and quietly handed the whole ordering to the alphabetical\n * tiebreaker below — running, for example, a January 2026 migration before a\n * December 2025 one.\n */\n public static sort(statements: TaggedSQL[]): TaggedSQL[] {\n return statements.slice().sort((a, b) => {\n // 1. Sort by Phase (ascending)\n if (a.phase !== b.phase) {\n return a.phase - b.phase;\n }\n\n // 2. Sort by CreatedAt (within same phase)\n const byCreatedAt = compareCreatedAt(a.createdAt, b.createdAt);\n\n if (byCreatedAt !== undefined) {\n return byCreatedAt;\n }\n\n // 3. Sort by Migration Name (tie-breaker)\n const nameA = a.migrationName || \"\";\n const nameB = b.migrationName || \"\";\n return nameA.localeCompare(nameB);\n });\n }\n}\n"],"mappings":";;;;;;;AAOA,IAAa,aAAb,MAAwB;;;;;;;;;;;;;;;;;;;;CAoBtB,OAAc,MAAM,KAAoC;EACtD,MAAM,IAAI,IAAI,KAAK,CAAC,CAAC,YAAY;EAGjC,IACE,EAAE,WAAW,kBAAkB,KAC/B,EAAE,WAAW,aAAa,KAC1B,EAAE,WAAW,eAAe,KAC5B,EAAE,WAAW,eAAe,GAE5B,OAAO;EAIT,IAAI,EAAE,WAAW,cAAc,GAC7B,OAAO;EAIT,IAAI,EAAE,WAAW,aAAa,GAAG;GAC/B,IAAI,EAAE,SAAS,gBAAgB,KAAK,EAAE,SA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,IAAI,EAAE,SAAS,YAAY,GAAG,OAAO;GACrC,IAAI,EAAE,SAAS,aAAa,GAAG,OAAO;GACtC,IAAI,EAAE,SAAS,eAAe,GAAG,OAAO;GACxC,IAAI,EAAE,SAAS,WAAW,GAAG,OAAO;GACpC,IAAI,EAAE,SAAS,cAAc,GAAG,OAAO;EACzC;EAEA,OAAO;CACT;;;;;;;;;;;;;;CAeA,OAAc,qBAAqB,KAAiC;EAMlE,OAJc,IACX,KAAK,CAAC,CACN,MAAM,uDAEE,CAAC,GAAG,EAAE,EAAE,YAAY;CACjC;;;;;;;;;;;;;;;CAgBA,OAAc,KAAK,YAAsC;EACvD,OAAO,WAAW,MAAM,CAAC,CAAC,MAAM,GAAG,MAAM;GAEvC,IAAI,EAAE,UAAU,EAAE,OAChB,OAAO,EAAE,QAAQ,EAAE;GAIrB,MAAM,cAAc,iBAAiB,EAAE,WAAW,EAAE,SAAS;GAE7D,IAAI,gBAAgB,QAClB,OAAO;GAIT,MAAM,QAAQ,EAAE,iBAAiB;GACjC,MAAM,QAAQ,EAAE,iBAAiB;GACjC,OAAO,MAAM,cAAc,KAAK;EAClC,CAAC;CACH;AACF"}
@@ -0,0 +1,23 @@
1
+ //#region ../cascade/src/model/generate-model-id.ts
2
+ /**
3
+ * Generate the next model id using the model's configured sequence options.
4
+ *
5
+ * This is shared by normal inserts and MongoDB upserts so both write paths
6
+ * resolve random and configured sequence settings identically.
7
+ */
8
+ async function generateModelId(ModelClass) {
9
+ const idGenerator = ModelClass.getDataSource().idGenerator;
10
+ if (!idGenerator) return;
11
+ const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
12
+ const initialId = ModelClass.initialId ? ModelClass.initialId : ModelClass.randomInitialId ? typeof ModelClass.randomInitialId === "function" ? ModelClass.randomInitialId() : randomInt(1e4, 499999) : 1;
13
+ const incrementIdBy = ModelClass.randomIncrement ? typeof ModelClass.randomIncrement === "function" ? ModelClass.randomIncrement() : randomInt(1, 10) : ModelClass.incrementIdBy || 1;
14
+ return await idGenerator.generateNextId({
15
+ table: ModelClass.table,
16
+ initialId,
17
+ incrementIdBy
18
+ });
19
+ }
20
+
21
+ //#endregion
22
+ export { generateModelId };
23
+ //# sourceMappingURL=generate-model-id.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate-model-id.mjs","names":[],"sources":["../../../../../../../cascade/src/model/generate-model-id.ts"],"sourcesContent":["import type { ChildModel, Model } from \"./model\";\n\n/**\n * Generate the next model id using the model's configured sequence options.\n *\n * This is shared by normal inserts and MongoDB upserts so both write paths\n * resolve random and configured sequence settings identically.\n */\nexport async function generateModelId<TModel extends Model>(\n ModelClass: ChildModel<TModel>,\n): Promise<number | undefined> {\n const idGenerator = ModelClass.getDataSource().idGenerator;\n\n if (!idGenerator) {\n return undefined;\n }\n\n const randomInt = (min: number, max: number) =>\n Math.floor(Math.random() * (max - min + 1)) + min;\n const initialId = ModelClass.initialId\n ? ModelClass.initialId\n : ModelClass.randomInitialId\n ? typeof ModelClass.randomInitialId === \"function\"\n ? ModelClass.randomInitialId()\n : randomInt(10000, 499999)\n : 1;\n const incrementIdBy = ModelClass.randomIncrement\n ? typeof ModelClass.randomIncrement === \"function\"\n ? ModelClass.randomIncrement()\n : randomInt(1, 10)\n : ModelClass.incrementIdBy || 1;\n\n return await idGenerator.generateNextId({\n table: ModelClass.table,\n initialId,\n incrementIdBy,\n });\n}\n"],"mappings":";;;;;;;AAQA,eAAsB,gBACpB,YAC6B;CAC7B,MAAM,cAAc,WAAW,cAAc,CAAC,CAAC;CAE/C,IAAI,CAAC,aACH;CAGF,MAAM,aAAa,KAAa,QAC9B,KAAK,MAAM,KAAK,OAAO,KAAK,MAAM,MAAM,EAAE,IAAI;CAChD,MAAM,YAAY,WAAW,YACzB,WAAW,YACX,WAAW,kBACT,OAAO,WAAW,oBAAoB,aACpC,WAAW,gBAAgB,IAC3B,UAAU,KAAO,MAAM,IACzB;CACN,MAAM,gBAAgB,WAAW,kBAC7B,OAAO,WAAW,oBAAoB,aACpC,WAAW,gBAAgB,IAC3B,UAAU,GAAG,EAAE,IACjB,WAAW,iBAAiB;CAEhC,OAAO,MAAM,YAAY,eAAe;EACtC,OAAO,WAAW;EAClB;EACA;CACF,CAAC;AACH"}
@@ -1 +1 @@
1
- {"version":3,"file":"delete-methods.mjs","names":[],"sources":["../../../../../../../../cascade/src/model/methods/delete-methods.ts"],"sourcesContent":["import { DatabaseRemover } from \"../../remover/database-remover\";\r\nimport type { DeleteStrategy } from \"../../types\";\r\nimport type { RemoverResult } from \"../../contracts\";\r\nimport { sanitizeFilter } from \"../../utils/sanitize-filter\";\r\nimport type { ChildModel, Model } from \"../model\";\r\n\r\nexport async function destroyModel(\r\n model: Model,\r\n options?: { strategy?: DeleteStrategy; skipEvents?: boolean },\r\n): Promise<RemoverResult> {\r\n const remover = new DatabaseRemover(model);\r\n return remover.destroy(options);\r\n}\r\n\r\nexport async function deleteRecords(\r\n ModelClass: ChildModel<any>,\r\n filter?: Record<string, unknown>,\r\n): Promise<number> {\r\n const sanitized = filter ? sanitizeFilter(filter) : undefined;\r\n\r\n if (!sanitized || Object.keys(sanitized).length === 0) {\r\n throw new Error(\r\n `${ModelClass.name}.delete() requires a filter: pass an explicit filter, or use ${ModelClass.name}.deleteAll() / ${ModelClass.name}.query().delete() to delete every row.`,\r\n );\r\n }\r\n\r\n // Through the query builder so global scopes (tenant, soft delete) apply.\r\n return ModelClass.query().where(sanitized).delete();\r\n}\r\n\r\n/** Delete every row visible to the model's global scopes. Explicit on purpose. */\r\nexport async function deleteAllRecords(ModelClass: ChildModel<any>): Promise<number> {\r\n return ModelClass.query().delete();\r\n}\r\n\r\nexport async function deleteOneRecord(\r\n ModelClass: ChildModel<any>,\r\n filter?: Record<string, unknown>,\r\n): Promise<number> {\r\n const query = ModelClass.query();\r\n\r\n if (filter && Object.keys(filter).length > 0) {\r\n query.where(sanitizeFilter(filter));\r\n }\r\n\r\n return query.deleteOne();\r\n}\r\n"],"mappings":";;;;AAMA,eAAsB,aACpB,OACA,SACwB;CAExB,OAAO,IADa,gBAAgB,KACvB,CAAC,CAAC,QAAQ,OAAO;AAChC;AAEA,eAAsB,cACpB,YACA,QACiB;CACjB,MAAM,YAAY,SAAS,eAAe,MAAM,IAAI;CAEpD,IAAI,CAAC,aAAa,OAAO,KAAK,SAAS,CAAC,CAAC,WAAW,GAClD,MAAM,IAAI,MACR,GAAG,WAAW,KAAK,+DAA+D,WAAW,KAAK,iBAAiB,WAAW,KAAK,uCACrI;CAIF,OAAO,WAAW,MAAM,CAAC,CAAC,MAAM,SAAS,CAAC,CAAC,OAAO;AACpD;AAOA,eAAsB,gBACpB,YACA,QACiB;CACjB,MAAM,QAAQ,WAAW,MAAM;CAE/B,IAAI,UAAU,OAAO,KAAK,MAAM,CAAC,CAAC,SAAS,GACzC,MAAM,MAAM,eAAe,MAAM,CAAC;CAGpC,OAAO,MAAM,UAAU;AACzB"}
1
+ {"version":3,"file":"delete-methods.mjs","names":[],"sources":["../../../../../../../../cascade/src/model/methods/delete-methods.ts"],"sourcesContent":["import { DatabaseRemover } from \"../../remover/database-remover\";\nimport type { DeleteStrategy } from \"../../types\";\nimport type { RemoverResult } from \"../../contracts\";\nimport { sanitizeFilter } from \"../../utils/sanitize-filter\";\nimport type { ChildModel, Model } from \"../model\";\n\nexport async function destroyModel(\n model: Model,\n options?: { strategy?: DeleteStrategy; skipEvents?: boolean },\n): Promise<RemoverResult> {\n const remover = new DatabaseRemover(model);\n return remover.destroy(options);\n}\n\nexport async function deleteRecords(\n ModelClass: ChildModel<any>,\n filter?: Record<string, unknown>,\n): Promise<number> {\n const sanitized = filter ? sanitizeFilter(filter) : undefined;\n\n if (!sanitized || Object.keys(sanitized).length === 0) {\n throw new Error(\n `${ModelClass.name}.delete() requires a filter: pass an explicit filter, or use ${ModelClass.name}.deleteAll() / ${ModelClass.name}.query().delete() to delete every row.`,\n );\n }\n\n // Through the query builder so global scopes (tenant, soft delete) apply.\n return ModelClass.query().where(sanitized).delete();\n}\n\n/** Delete every row visible to the model's global scopes. Explicit on purpose. */\nexport async function deleteAllRecords(ModelClass: ChildModel<any>): Promise<number> {\n return ModelClass.query().delete();\n}\n\nexport async function deleteOneRecord(\n ModelClass: ChildModel<any>,\n filter?: Record<string, unknown>,\n): Promise<number> {\n const query = ModelClass.query();\n\n if (filter && Object.keys(filter).length > 0) {\n query.where(sanitizeFilter(filter));\n }\n\n return query.deleteOne();\n}\n"],"mappings":";;;;AAMA,eAAsB,aACpB,OACA,SACwB;CAExB,OAAO,IADa,gBAAgB,KACvB,CAAC,CAAC,QAAQ,OAAO;AAChC;AAEA,eAAsB,cACpB,YACA,QACiB;CACjB,MAAM,YAAY,SAAS,eAAe,MAAM,IAAI;CAEpD,IAAI,CAAC,aAAa,OAAO,KAAK,SAAS,CAAC,CAAC,WAAW,GAClD,MAAM,IAAI,MACR,GAAG,WAAW,KAAK,+DAA+D,WAAW,KAAK,iBAAiB,WAAW,KAAK,uCACrI;CAIF,OAAO,WAAW,MAAM,CAAC,CAAC,MAAM,SAAS,CAAC,CAAC,OAAO;AACpD;AAOA,eAAsB,gBACpB,YACA,QACiB;CACjB,MAAM,QAAQ,WAAW,MAAM;CAE/B,IAAI,UAAU,OAAO,KAAK,MAAM,CAAC,CAAC,SAAS,GACzC,MAAM,MAAM,eAAe,MAAM,CAAC;CAGpC,OAAO,MAAM,UAAU;AACzB"}
@@ -1 +1 @@
1
- {"version":3,"file":"meta-methods.mjs","names":[],"sources":["../../../../../../../../cascade/src/model/methods/meta-methods.ts"],"sourcesContent":["import { get, set } from \"@mongez/reinforcements\";\r\nimport { DatabaseWriter } from \"../../writer/database-writer\";\r\nimport type { Model } from \"../model\";\r\n\r\n/**\r\n * Whether a subclass (or an intermediate base below `Model`) set the static\r\n * itself. The base `Model` initialises several statics (strictMode,\r\n * autoGenerateId, incrementIdBy, deletedAtColumn), so comparing to\r\n * `undefined` would never let data-source defaults apply.\r\n */\r\nfunction isExplicitStatic(ModelClass: any, key: string): boolean {\r\n let current = ModelClass;\r\n\r\n // The base Model is the class whose parent is `Function.prototype`; its own\r\n // initialisers are framework fallbacks, not explicit choices.\r\n while (current && Object.getPrototypeOf(current) !== Function.prototype) {\r\n if (Object.prototype.hasOwnProperty.call(current, key) && current[key] !== undefined) {\r\n return true;\r\n }\r\n\r\n current = Object.getPrototypeOf(current);\r\n }\r\n\r\n return false;\r\n}\r\n\r\nexport function applyDefaultsToModel(ModelClass: any, defaults: any): void {\r\n // Only apply defaults if model doesn't have its own value\r\n\r\n // ============================================================================\r\n // ID Generation\r\n // ============================================================================\r\n if (defaults.autoGenerateId !== undefined && !isExplicitStatic(ModelClass, \"autoGenerateId\")) {\r\n ModelClass.autoGenerateId = defaults.autoGenerateId;\r\n }\r\n if (defaults.initialId !== undefined && !isExplicitStatic(ModelClass, \"initialId\")) {\r\n ModelClass.initialId = defaults.initialId;\r\n }\r\n if (defaults.randomInitialId !== undefined && !isExplicitStatic(ModelClass, \"randomInitialId\")) {\r\n ModelClass.randomInitialId = defaults.randomInitialId;\r\n }\r\n if (defaults.incrementIdBy !== undefined && !isExplicitStatic(ModelClass, \"incrementIdBy\")) {\r\n ModelClass.incrementIdBy = defaults.incrementIdBy;\r\n }\r\n if (defaults.randomIncrement !== undefined && !isExplicitStatic(ModelClass, \"randomIncrement\")) {\r\n ModelClass.randomIncrement = defaults.randomIncrement;\r\n }\r\n\r\n // ============================================================================\r\n // Timestamps\r\n // ============================================================================\r\n if (defaults.createdAtColumn !== undefined && !isExplicitStatic(ModelClass, \"createdAtColumn\")) {\r\n ModelClass.createdAtColumn = defaults.createdAtColumn;\r\n }\r\n\r\n if (defaults.updatedAtColumn !== undefined && !isExplicitStatic(ModelClass, \"updatedAtColumn\")) {\r\n ModelClass.updatedAtColumn = defaults.updatedAtColumn;\r\n }\r\n\r\n // ============================================================================\r\n // Deletion\r\n // ============================================================================\r\n if (defaults.deleteStrategy !== undefined && !isExplicitStatic(ModelClass, \"deleteStrategy\")) {\r\n ModelClass.deleteStrategy = defaults.deleteStrategy;\r\n }\r\n if (defaults.deletedAtColumn !== undefined && !isExplicitStatic(ModelClass, \"deletedAtColumn\")) {\r\n ModelClass.deletedAtColumn = defaults.deletedAtColumn;\r\n }\r\n if (defaults.trashTable !== undefined && !isExplicitStatic(ModelClass, \"trashTable\")) {\r\n // Handle function-based trash table\r\n if (typeof defaults.trashTable === \"function\") {\r\n ModelClass.trashTable = defaults.trashTable(ModelClass.table);\r\n } else {\r\n ModelClass.trashTable = defaults.trashTable;\r\n }\r\n }\r\n\r\n // ============================================================================\r\n // Validation\r\n // ============================================================================\r\n if (defaults.strictMode !== undefined && !isExplicitStatic(ModelClass, \"strictMode\")) {\r\n ModelClass.strictMode = defaults.strictMode;\r\n }\r\n}\r\n\r\nexport async function generateModelNextId(model: Model): Promise<number | string> {\r\n const writer = new DatabaseWriter(model);\r\n await writer.generateNextId();\r\n return model.id!;\r\n}\r\n\r\nexport async function performAtomicUpdate(\r\n model: Model,\r\n operations: Record<string, unknown>,\r\n): Promise<number> {\r\n return model.self().atomic({ [model.self().primaryKey]: model.trustedPrimaryKey }, operations);\r\n}\r\n\r\n/**\r\n * Apply an atomic delta to the local value without marking the field dirty.\r\n * The database already holds the change, so a later `save()` must not send a\r\n * stale `$set` for it. Other pending changes stay dirty.\r\n */\r\nfunction applyLocalDelta(model: Model, field: string, delta: number): void {\r\n const tracker = model.dirtyTracker;\r\n const fieldWasDirty = tracker.isDirty(field);\r\n const value = (get(model.data, field, 0) as number) + delta;\r\n\r\n set(model.data, field, value);\r\n\r\n // A removed column can't be replayed after a baseline reset, so fall back to\r\n // plain dirty tracking for that rare case.\r\n if (fieldWasDirty || tracker.getRemovedColumns().length > 0) {\r\n tracker.setAtPath(field, value);\r\n return;\r\n }\r\n\r\n const pending = tracker.getDirtyColumnsWithValues();\r\n const baseline = structuredClone(model.data) as Record<string, unknown>;\r\n\r\n for (const [column, values] of Object.entries(pending)) {\r\n set(baseline, column, values.oldValue);\r\n }\r\n\r\n tracker.reset(baseline);\r\n\r\n for (const [column, values] of Object.entries(pending)) {\r\n tracker.setAtPath(column, values.newValue);\r\n }\r\n}\r\n\r\nexport async function performAtomicIncrement<T extends string>(\r\n model: Model,\r\n field: T,\r\n amount = 1,\r\n): Promise<number> {\r\n const result = await performAtomicUpdate(model, { $inc: { [field]: amount } });\r\n applyLocalDelta(model, field, amount);\r\n return result;\r\n}\r\n\r\nexport async function performAtomicDecrement<T extends string>(\r\n model: Model,\r\n field: T,\r\n amount = 1,\r\n): Promise<number> {\r\n const result = await performAtomicUpdate(model, { $inc: { [field]: -amount } });\r\n applyLocalDelta(model, field, -amount);\r\n return result;\r\n}\r\n"],"mappings":";;;;;;;;;;AAUA,SAAS,iBAAiB,YAAiB,KAAsB;CAC/D,IAAI,UAAU;CAId,OAAO,WAAW,OAAO,eAAe,OAAO,MAAM,SAAS,WAAW;EACvE,IAAI,OAAO,UAAU,eAAe,KAAK,SAAS,GAAG,KAAK,QAAQ,SAAS,QACzE,OAAO;EAGT,UAAU,OAAO,eAAe,OAAO;CACzC;CAEA,OAAO;AACT;AAEA,SAAgB,qBAAqB,YAAiB,UAAqB;CAMzE,IAAI,SAAS,mBAAmB,UAAa,CAAC,iBAAiB,YAAY,gBAAgB,GACzF,WAAW,iBAAiB,SAAS;CAEvC,IAAI,SAAS,cAAc,UAAa,CAAC,iBAAiB,YAAY,WAAW,GAC/E,WAAW,YAAY,SAAS;CAElC,IAAI,SAAS,oBAAoB,UAAa,CAAC,iBAAiB,YAAY,iBAAiB,GAC3F,WAAW,kBAAkB,SAAS;CAExC,IAAI,SAAS,kBAAkB,UAAa,CAAC,iBAAiB,YAAY,eAAe,GACvF,WAAW,gBAAgB,SAAS;CAEtC,IAAI,SAAS,oBAAoB,UAAa,CAAC,iBAAiB,YAAY,iBAAiB,GAC3F,WAAW,kBAAkB,SAAS;CAMxC,IAAI,SAAS,oBAAoB,UAAa,CAAC,iBAAiB,YAAY,iBAAiB,GAC3F,WAAW,kBAAkB,SAA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AQ;CAGvC,QAAQ,MAAM,QAAQ;CAEtB,KAAK,MAAM,CAAC,QAAQ,WAAW,OAAO,QAAQ,OAAO,GACnD,QAAQ,UAAU,QAAQ,OAAO,QAAQ;AAE7C;AAEA,eAAsB,uBACpB,OACA,OACA,SAAS,GACQ;CACjB,MAAM,SAAS,MAAM,oBAAoB,OAAO,EAAE,MAAM,GAAG,QAAQ,OAAO,EAAE,CAAC;CAC7E,gBAAgB,OAAO,OAAO,MAAM;CACpC,OAAO;AACT;AAEA,eAAsB,uBACpB,OACA,OACA,SAAS,GACQ;CACjB,MAAM,SAAS,MAAM,oBAAoB,OAAO,EAAE,MAAM,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAC;CAC9E,gBAAgB,OAAO,OAAO,CAAC,MAAM;CACrC,OAAO;AACT"}
1
+ {"version":3,"file":"meta-methods.mjs","names":[],"sources":["../../../../../../../../cascade/src/model/methods/meta-methods.ts"],"sourcesContent":["import { get, set } from \"@mongez/reinforcements\";\nimport { DatabaseWriter } from \"../../writer/database-writer\";\nimport type { Model } from \"../model\";\n\n/**\n * Whether a subclass (or an intermediate base below `Model`) set the static\n * itself. The base `Model` initialises several statics (strictMode,\n * autoGenerateId, incrementIdBy, deletedAtColumn), so comparing to\n * `undefined` would never let data-source defaults apply.\n */\nfunction isExplicitStatic(ModelClass: any, key: string): boolean {\n let current = ModelClass;\n\n // The base Model is the class whose parent is `Function.prototype`; its own\n // initialisers are framework fallbacks, not explicit choices.\n while (current && Object.getPrototypeOf(current) !== Function.prototype) {\n if (Object.prototype.hasOwnProperty.call(current, key) && current[key] !== undefined) {\n return true;\n }\n\n current = Object.getPrototypeOf(current);\n }\n\n return false;\n}\n\nexport function applyDefaultsToModel(ModelClass: any, defaults: any): void {\n // Only apply defaults if model doesn't have its own value\n\n // ============================================================================\n // ID Generation\n // ============================================================================\n if (defaults.autoGenerateId !== undefined && !isExplicitStatic(ModelClass, \"autoGenerateId\")) {\n ModelClass.autoGenerateId = defaults.autoGenerateId;\n }\n if (defaults.initialId !== undefined && !isExplicitStatic(ModelClass, \"initialId\")) {\n ModelClass.initialId = defaults.initialId;\n }\n if (defaults.randomInitialId !== undefined && !isExplicitStatic(ModelClass, \"randomInitialId\")) {\n ModelClass.randomInitialId = defaults.randomInitialId;\n }\n if (defaults.incrementIdBy !== undefined && !isExplicitStatic(ModelClass, \"incrementIdBy\")) {\n ModelClass.incrementIdBy = defaults.incrementIdBy;\n }\n if (defaults.randomIncrement !== undefined && !isExplicitStatic(ModelClass, \"randomIncrement\")) {\n ModelClass.randomIncrement = defaults.randomIncrement;\n }\n\n // ============================================================================\n // Timestamps\n // ============================================================================\n if (defaults.createdAtColumn !== undefined && !isExplicitStatic(ModelClass, \"createdAtColumn\")) {\n ModelClass.createdAtColumn = defaults.createdAtColumn;\n }\n\n if (defaults.updatedAtColumn !== undefined && !isExplicitStatic(ModelClass, \"updatedAtColumn\")) {\n ModelClass.updatedAtColumn = defaults.updatedAtColumn;\n }\n\n // ============================================================================\n // Deletion\n // ============================================================================\n if (defaults.deleteStrategy !== undefined && !isExplicitStatic(ModelClass, \"deleteStrategy\")) {\n ModelClass.deleteStrategy = defaults.deleteStrategy;\n }\n if (defaults.deletedAtColumn !== undefined && !isExplicitStatic(ModelClass, \"deletedAtColumn\")) {\n ModelClass.deletedAtColumn = defaults.deletedAtColumn;\n }\n if (defaults.trashTable !== undefined && !isExplicitStatic(ModelClass, \"trashTable\")) {\n // Handle function-based trash table\n if (typeof defaults.trashTable === \"function\") {\n ModelClass.trashTable = defaults.trashTable(ModelClass.table);\n } else {\n ModelClass.trashTable = defaults.trashTable;\n }\n }\n\n // ============================================================================\n // Validation\n // ============================================================================\n if (defaults.strictMode !== undefined && !isExplicitStatic(ModelClass, \"strictMode\")) {\n ModelClass.strictMode = defaults.strictMode;\n }\n}\n\nexport async function generateModelNextId(model: Model): Promise<number | string> {\n const writer = new DatabaseWriter(model);\n await writer.generateNextId();\n return model.id!;\n}\n\nexport async function performAtomicUpdate(\n model: Model,\n operations: Record<string, unknown>,\n): Promise<number> {\n return model.self().atomic({ [model.self().primaryKey]: model.trustedPrimaryKey }, operations);\n}\n\n/**\n * Apply an atomic delta to the local value without marking the field dirty.\n * The database already holds the change, so a later `save()` must not send a\n * stale `$set` for it. Other pending changes stay dirty.\n */\nfunction applyLocalDelta(model: Model, field: string, delta: number): void {\n const tracker = model.dirtyTracker;\n const fieldWasDirty = tracker.isDirty(field);\n const value = (get(model.data, field, 0) as number) + delta;\n\n set(model.data, field, value);\n\n // A removed column can't be replayed after a baseline reset, so fall back to\n // plain dirty tracking for that rare case.\n if (fieldWasDirty || tracker.getRemovedColumns().length > 0) {\n tracker.setAtPath(field, value);\n return;\n }\n\n const pending = tracker.getDirtyColumnsWithValues();\n const baseline = structuredClone(model.data) as Record<string, unknown>;\n\n for (const [column, values] of Object.entries(pending)) {\n set(baseline, column, values.oldValue);\n }\n\n tracker.reset(baseline);\n\n for (const [column, values] of Object.entries(pending)) {\n tracker.setAtPath(column, values.newValue);\n }\n}\n\nexport async function performAtomicIncrement<T extends string>(\n model: Model,\n field: T,\n amount = 1,\n): Promise<number> {\n const result = await performAtomicUpdate(model, { $inc: { [field]: amount } });\n applyLocalDelta(model, field, amount);\n return result;\n}\n\nexport async function performAtomicDecrement<T extends string>(\n model: Model,\n field: T,\n amount = 1,\n): Promise<number> {\n const result = await performAtomicUpdate(model, { $inc: { [field]: -amount } });\n applyLocalDelta(model, field, -amount);\n return result;\n}\n"],"mappings":";;;;;;;;;;AAUA,SAAS,iBAAiB,YAAiB,KAAsB;CAC/D,IAAI,UAAU;CAId,OAAO,WAAW,OAAO,eAAe,OAAO,MAAM,SAAS,WAAW;EACvE,IAAI,OAAO,UAAU,eAAe,KAAK,SAAS,GAAG,KAAK,QAAQ,SAAS,QACzE,OAAO;EAGT,UAAU,OAAO,eAAe,OAAO;CACzC;CAEA,OAAO;AACT;AAEA,SAAgB,qBAAqB,YAAiB,UAAqB;CAMzE,IAAI,SAAS,mBAAmB,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AkB,GAAG,UAAU;AAC/F;;;;;;AAOA,SAAS,gBAAgB,OAAc,OAAe,OAAqB;CACzE,MAAM,UAAU,MAAM;CACtB,MAAM,gBAAgB,QAAQ,QAAQ,KAAK;CAC3C,MAAM,QAAS,IAAI,MAAM,MAAM,OAAO,CAAC,IAAe;CAEtD,IAAI,MAAM,MAAM,OAAO,KAAK;CAI5B,IAAI,iBAAiB,QAAQ,kBAAkB,CAAC,CAAC,SAAS,GAAG;EAC3D,QAAQ,UAAU,OAAO,KAAK;EAC9B;CACF;CAEA,MAAM,UAAU,QAAQ,0BAA0B;CAClD,MAAM,WAAW,gBAAgB,MAAM,IAAI;CAE3C,KAAK,MAAM,CAAC,QAAQ,WAAW,OAAO,QAAQ,OAAO,GACnD,IAAI,UAAU,QAAQ,OAAO,QAAQ;CAGvC,QAAQ,MAAM,QAAQ;CAEtB,KAAK,MAAM,CAAC,QAAQ,WAAW,OAAO,QAAQ,OAAO,GACnD,QAAQ,UAAU,QAAQ,OAAO,QAAQ;AAE7C;AAEA,eAAsB,uBACpB,OACA,OACA,SAAS,GACQ;CACjB,MAAM,SAAS,MAAM,oBAAoB,OAAO,EAAE,MAAM,GAAG,QAAQ,OAAO,EAAE,CAAC;CAC7E,gBAAgB,OAAO,OAAO,MAAM;CACpC,OAAO;AACT;AAEA,eAAsB,uBACpB,OACA,OACA,SAAS,GACQ;CACjB,MAAM,SAAS,MAAM,oBAAoB,OAAO,EAAE,MAAM,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAC;CAC9E,gBAAgB,OAAO,OAAO,CAAC,MAAM;CACrC,OAAO;AACT"}
@@ -1,6 +1,7 @@
1
1
  import { dataSourceRegistry } from "../../data-source/data-source-registry.mjs";
2
2
  import { sanitizeFilter } from "../../utils/sanitize-filter.mjs";
3
3
  import { emitStaticEvent } from "./static-event-methods.mjs";
4
+ import { generateModelId } from "../generate-model-id.mjs";
4
5
  import { warnUndeclaredSensitiveFields } from "./serialization-methods.mjs";
5
6
 
6
7
  //#region ../cascade/src/model/methods/query-methods.ts
@@ -96,6 +97,26 @@ function toDriverOptions(options) {
96
97
  return rest;
97
98
  }
98
99
  /**
100
+ * Add the same generated `id` that normal Mongo inserts receive to an upsert's
101
+ * insert branch. `$setOnInsert` keeps an existing document's id immutable.
102
+ */
103
+ async function withAutoGeneratedUpsertId(ModelClass, filter, operations, options) {
104
+ if (!options?.upsert || !ModelClass.autoGenerateId || ModelClass.getDriver().name !== "mongodb" || Array.isArray(operations)) return operations;
105
+ const filterId = filter.id;
106
+ const filterConstrainsId = Object.hasOwn(filter, "id") && (filterId === null || typeof filterId !== "object" || !Array.isArray(filterId) && Object.hasOwn(filterId, "$eq"));
107
+ const updateSuppliesId = Object.hasOwn(operations.$set ?? {}, "id") || Object.hasOwn(operations.$setOnInsert ?? {}, "id");
108
+ if (filterConstrainsId || updateSuppliesId) return operations;
109
+ const id = await generateModelId(ModelClass);
110
+ if (id === void 0) return operations;
111
+ return {
112
+ ...operations,
113
+ $setOnInsert: {
114
+ ...operations.$setOnInsert,
115
+ id
116
+ }
117
+ };
118
+ }
119
+ /**
99
120
  * Pin a driver-level write to the rows the model's global scopes (tenant, soft
100
121
  * delete) allow. The driver write APIs take a raw filter and never see scopes,
101
122
  * so the target ids are resolved through the scoped query builder and added to
@@ -129,7 +150,8 @@ async function performAtomic(ModelClass, filter, operations, options) {
129
150
  const resolved = resolveFilter(filter, options);
130
151
  const scoped = options?.upsert ? await scopeUpsertFilter(ModelClass, resolved) : await scopeWriteFilter(ModelClass, resolved, false);
131
152
  if (!scoped) return 0;
132
- const result = await ModelClass.getDriver().atomic(ModelClass.table, scoped, operations, toDriverOptions(options));
153
+ const upsertOperations = await withAutoGeneratedUpsertId(ModelClass, scoped, operations, options);
154
+ const result = await ModelClass.getDriver().atomic(ModelClass.table, scoped, upsertOperations, toDriverOptions(options));
133
155
  return result.modifiedCount + (result.upsertedCount ?? 0);
134
156
  }
135
157
  /**
@@ -166,7 +188,8 @@ async function findOneAndUpdateRecord(ModelClass, filter, update, options) {
166
188
  const resolved = resolveFilter(filter, options);
167
189
  const scoped = options?.upsert ? await scopeUpsertFilter(ModelClass, resolved) : await scopeWriteFilter(ModelClass, resolved, true);
168
190
  if (!scoped) return null;
169
- const result = await ModelClass.getDriver().findOneAndUpdate(ModelClass.table, scoped, update, toDriverOptions(options));
191
+ const upsertUpdate = await withAutoGeneratedUpsertId(ModelClass, scoped, update, options);
192
+ const result = await ModelClass.getDriver().findOneAndUpdate(ModelClass.table, scoped, upsertUpdate, toDriverOptions(options));
170
193
  if (!result) return null;
171
194
  return ModelClass.hydrate(result);
172
195
  }