@danceroutine/tango-migrations 1.11.8 → 1.11.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{CompilerStrategy-Dc5_3-nw.js → CompilerStrategy-CosQJr-g.js} +3 -8
- package/dist/CompilerStrategy-CosQJr-g.js.map +1 -0
- package/dist/InternalDialect-DDW2l3mz.js +9 -0
- package/dist/InternalDialect-DDW2l3mz.js.map +1 -0
- package/dist/{IntrospectorStrategy-BCTs_4wl.js → IntrospectorStrategy-D_9umttn.js} +2 -2
- package/dist/{IntrospectorStrategy-BCTs_4wl.js.map → IntrospectorStrategy-D_9umttn.js.map} +1 -1
- package/dist/{MigrationRunner-B9F-yxGV.js → MigrationRunner-DCCeU1kq.js} +3 -2
- package/dist/{MigrationRunner-B9F-yxGV.js.map → MigrationRunner-DCCeU1kq.js.map} +1 -1
- package/dist/{cli-kbb2TSGO.js → cli-Dhp2BZsB.js} +6 -6
- package/dist/{cli-kbb2TSGO.js.map → cli-Dhp2BZsB.js.map} +1 -1
- package/dist/cli.js +1 -1
- package/dist/commands/index.js +1 -1
- package/dist/diff/index.d.ts +1 -1
- package/dist/diff/index.js +1 -1
- package/dist/{diff-BsQr9Sl8.js → diff-DdwU_MyI.js} +44 -14
- package/dist/diff-DdwU_MyI.js.map +1 -0
- package/dist/{index-CzdR_Ig9.d.ts → index-i1MsTVXm.d.ts} +6 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +5 -5
- package/dist/runner/index.js +1 -1
- package/dist/strategies/index.js +2 -2
- package/package.json +7 -7
- package/dist/CompilerStrategy-Dc5_3-nw.js.map +0 -1
- package/dist/diff-BsQr9Sl8.js.map +0 -1
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
import { r as PostgresCompilerFactory, t as SqliteCompilerFactory } from "./SqliteCompilerFactory-C_56xoQM.js";
|
|
2
|
-
|
|
3
|
-
const InternalDialect = {
|
|
4
|
-
POSTGRES: "postgres",
|
|
5
|
-
SQLITE: "sqlite"
|
|
6
|
-
};
|
|
7
|
-
//#endregion
|
|
2
|
+
import { t as InternalDialect } from "./InternalDialect-DDW2l3mz.js";
|
|
8
3
|
//#region src/strategies/CompilerStrategy.ts
|
|
9
4
|
/**
|
|
10
5
|
* Dialect-aware SQL compiler orchestration with optional custom-op handlers.
|
|
@@ -72,6 +67,6 @@ function createDefaultCompilerStrategy() {
|
|
|
72
67
|
});
|
|
73
68
|
}
|
|
74
69
|
//#endregion
|
|
75
|
-
export { createDefaultCompilerStrategy as n,
|
|
70
|
+
export { createDefaultCompilerStrategy as n, CompilerStrategy as t };
|
|
76
71
|
|
|
77
|
-
//# sourceMappingURL=CompilerStrategy-
|
|
72
|
+
//# sourceMappingURL=CompilerStrategy-CosQJr-g.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CompilerStrategy-CosQJr-g.js","names":[],"sources":["../src/strategies/CompilerStrategy.ts"],"sourcesContent":["import { PostgresCompilerFactory } from '../compilers/factories/PostgresCompilerFactory';\nimport { SqliteCompilerFactory } from '../compilers/factories/SqliteCompilerFactory';\nimport type { CompilerFactory } from '../compilers/contracts/CompilerFactory';\nimport type { Dialect } from '../domain/Dialect';\nimport type { CustomMigrationOperation, MigrationOperation } from '../domain/MigrationOperation';\nimport type { SQL } from '../compilers/contracts/SQL';\nimport type { SQLCompiler } from '../compilers/contracts/SQLCompiler';\nimport { InternalDialect } from '../domain/internal/InternalDialect';\n\ntype CompilerFactoryRegistry = Record<Dialect, CompilerFactory>;\n\n/**\n * Dialect-aware SQL compiler orchestration with optional custom-op handlers.\n */\nexport class CompilerStrategy {\n static readonly BRAND = 'tango.migrations.compiler_strategy' as const;\n readonly __tangoBrand: typeof CompilerStrategy.BRAND = CompilerStrategy.BRAND;\n private readonly compilerCache = new Map<Dialect, SQLCompiler>();\n private readonly customHandlers = new Map<string, (dialect: Dialect, op: CustomMigrationOperation) => SQL[]>();\n\n constructor(private readonly factories: CompilerFactoryRegistry) {}\n\n /**\n * Narrow an unknown value to the dialect-aware migration compiler strategy.\n */\n static isCompilerStrategy(value: unknown): value is CompilerStrategy {\n return (\n typeof value === 'object' &&\n value !== null &&\n (value as { __tangoBrand?: unknown }).__tangoBrand === CompilerStrategy.BRAND\n );\n }\n\n /**\n * Prepare a batch of migration operations for a target dialect.\n */\n prepareOperations(dialect: Dialect, operations: MigrationOperation[]): MigrationOperation[] {\n const compiler = this.getCompiler(dialect);\n return compiler.prepareOperations ? compiler.prepareOperations(operations) : operations;\n }\n\n /**\n * Compile a migration operation to SQL for a target dialect.\n */\n compile(dialect: Dialect, operation: MigrationOperation): SQL[] {\n if (operation.kind === 'custom') {\n const handler = this.customHandlers.get(operation.name);\n if (!handler) {\n throw new Error(`Unsupported custom migration op: ${operation.name}`);\n }\n return handler(dialect, operation);\n }\n const compiler = this.getCompiler(dialect);\n return compiler.compile(operation);\n }\n\n /**\n * Register a handler for custom migration operations.\n */\n registerCustomHandler<TName extends string, TArgs extends object>(\n name: TName,\n handler: (dialect: Dialect, op: CustomMigrationOperation<TName, TArgs>) => SQL[]\n ): this {\n this.customHandlers.set(name, handler as (dialect: Dialect, op: CustomMigrationOperation) => SQL[]);\n return this;\n }\n\n /**\n * Resolve and cache a compiler instance for a dialect.\n */\n getCompiler(dialect: Dialect): SQLCompiler {\n const cached = this.compilerCache.get(dialect);\n if (cached) {\n return cached;\n }\n\n const factory = this.factories[dialect];\n if (!factory) {\n throw new Error(`No SQL compiler factory registered for dialect: ${String(dialect)}`);\n }\n const compiler = factory.create();\n this.compilerCache.set(dialect, compiler);\n return compiler;\n }\n}\n\n/**\n * Create the default compiler strategy with built-in dialect factories.\n */\nexport function createDefaultCompilerStrategy(): CompilerStrategy {\n return new CompilerStrategy({\n [InternalDialect.POSTGRES]: new PostgresCompilerFactory(),\n [InternalDialect.SQLITE]: new SqliteCompilerFactory(),\n });\n}\n"],"mappings":";;;;;;AAcA,IAAa,mBAAb,MAAa,iBAAiB;CAMG;CA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InternalDialect.ts"],"sourcesContent":["export const InternalDialect = {\n POSTGRES: 'postgres',\n SQLITE: 'sqlite',\n} as const;\n\nexport type InternalDialect = (typeof InternalDialect)[keyof typeof InternalDialect];\n"],"mappings":";AAAA,MAAa,kBAAkB;CAC3B,UAAU;CACV,QAAQ;AACZ"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as InternalDialect } from "./InternalDialect-DDW2l3mz.js";
|
|
2
2
|
import { n as PostgresIntrospector, t as SqliteIntrospector } from "./SqliteIntrospector-CfItmGgA.js";
|
|
3
3
|
//#region src/strategies/IntrospectorStrategy.ts
|
|
4
4
|
/**
|
|
@@ -49,4 +49,4 @@ function createDefaultIntrospectorStrategy() {
|
|
|
49
49
|
//#endregion
|
|
50
50
|
export { createDefaultIntrospectorStrategy as n, IntrospectorStrategy as t };
|
|
51
51
|
|
|
52
|
-
//# sourceMappingURL=IntrospectorStrategy-
|
|
52
|
+
//# sourceMappingURL=IntrospectorStrategy-D_9umttn.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IntrospectorStrategy-
|
|
1
|
+
{"version":3,"file":"IntrospectorStrategy-D_9umttn.js","names":[],"sources":["../src/strategies/IntrospectorStrategy.ts"],"sourcesContent":["import type { DBClient, DatabaseIntrospector } from '../introspect/DatabaseIntrospector';\nimport type { Dialect } from '../domain/Dialect';\nimport { InternalDialect } from '../domain/internal/InternalDialect';\nimport type { DbSchema } from '../introspect/PostgresIntrospector';\nimport { PostgresIntrospector } from '../introspect/PostgresIntrospector';\nimport { SqliteIntrospector } from '../introspect/SqliteIntrospector';\n\ntype IntrospectorFactory = {\n create(): DatabaseIntrospector;\n};\n\ntype IntrospectorFactoryRegistry = Record<Dialect, IntrospectorFactory>;\n\n/**\n * Dialect-aware schema introspection orchestration.\n */\nexport class IntrospectorStrategy {\n static readonly BRAND = 'tango.migrations.introspector_strategy' as const;\n readonly __tangoBrand: typeof IntrospectorStrategy.BRAND = IntrospectorStrategy.BRAND;\n private readonly introspectorCache = new Map<Dialect, DatabaseIntrospector>();\n\n constructor(private readonly factories: IntrospectorFactoryRegistry) {}\n\n /**\n * Narrow an unknown value to the dialect-aware schema introspection strategy.\n */\n static isIntrospectorStrategy(value: unknown): value is IntrospectorStrategy {\n return (\n typeof value === 'object' &&\n value !== null &&\n (value as { __tangoBrand?: unknown }).__tangoBrand === IntrospectorStrategy.BRAND\n );\n }\n\n /**\n * Introspect database schema using a dialect-specific introspector.\n */\n introspect(dialect: Dialect, client: DBClient): Promise<DbSchema> {\n const introspector = this.getIntrospector(dialect);\n return introspector.introspect(client);\n }\n\n /**\n * Resolve and cache an introspector instance for a dialect.\n */\n getIntrospector(dialect: Dialect): DatabaseIntrospector {\n const cached = this.introspectorCache.get(dialect);\n if (cached) {\n return cached;\n }\n\n const factory = this.factories[dialect];\n if (!factory) {\n throw new Error(`No database introspector factory registered for dialect: ${String(dialect)}`);\n }\n\n const introspector = factory.create();\n this.introspectorCache.set(dialect, introspector);\n return introspector;\n }\n}\n\n/**\n * Create the default introspector strategy with built-in dialect support.\n */\nexport function createDefaultIntrospectorStrategy(): IntrospectorStrategy {\n return new IntrospectorStrategy({\n [InternalDialect.POSTGRES]: { create: () => new PostgresIntrospector() },\n [InternalDialect.SQLITE]: { create: () => new SqliteIntrospector() },\n });\n}\n"],"mappings":";;;;;;AAgBA,IAAa,uBAAb,MAAa,qBAAqB;CAKD;CAJ7B,OAAgB,QAAQ;CACxB,eAA2D,qBAAqB;CAChF,oCAAqC,IAAI,IAAmC;CAE5E,YAAY,WAAyD;EAAxC,KAAA,YAAA;CAAyC;;;;CAKtE,OAAO,uBAAuB,OAA+C;EACzE,OACI,OAAO,UAAU,YACjB,UAAU,QACT,MAAqC,iBAAiB,qBAAqB;CAEpF;;;;CAKA,WAAW,SAAkB,QAAqC;EAE9D,OADqB,KAAK,gBAAgB,OACxB,EAAE,WAAW,MAAM;CACzC;;;;CAKA,gBAAgB,SAAwC;EACpD,MAAM,SAAS,KAAK,kBAAkB,IAAI,OAAO;EACjD,IAAI,QACA,OAAO;EAGX,MAAM,UAAU,KAAK,UAAU;EAC/B,IAAI,CAAC,SACD,MAAM,IAAI,MAAM,4DAA4D,OAAO,OAAO,GAAG;EAGjG,MAAM,eAAe,QAAQ,OAAO;EACpC,KAAK,kBAAkB,IAAI,SAAS,YAAY;EAChD,OAAO;CACX;AACJ;;;;AAKA,SAAgB,oCAA0D;CACtE,OAAO,IAAI,qBAAqB;GAC3B,gBAAgB,WAAW,EAAE,cAAc,IAAI,qBAAqB,EAAE;GACtE,gBAAgB,SAAS,EAAE,cAAc,IAAI,mBAAmB,EAAE;CACvE,CAAC;AACL"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { t as Migration } from "./Migration-DxHHPyzn.js";
|
|
2
2
|
import { t as CollectingBuilder } from "./CollectingBuilder-BIfAKs_x.js";
|
|
3
|
-
import {
|
|
3
|
+
import { t as InternalDialect } from "./InternalDialect-DDW2l3mz.js";
|
|
4
|
+
import { n as createDefaultCompilerStrategy } from "./CompilerStrategy-CosQJr-g.js";
|
|
4
5
|
import { isError } from "@danceroutine/tango-core";
|
|
5
6
|
import { readdir } from "node:fs/promises";
|
|
6
7
|
import { extname, resolve } from "node:path";
|
|
@@ -246,4 +247,4 @@ var MigrationRunner = class MigrationRunner {
|
|
|
246
247
|
//#endregion
|
|
247
248
|
export { loadModule as n, MigrationRunner as t };
|
|
248
249
|
|
|
249
|
-
//# sourceMappingURL=MigrationRunner-
|
|
250
|
+
//# sourceMappingURL=MigrationRunner-DCCeU1kq.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MigrationRunner-B9F-yxGV.js","names":[],"sources":["../src/runtime/loadModule.ts","../src/runner/MigrationRunner.ts"],"sourcesContent":["import { resolve, extname } from 'node:path';\nimport { pathToFileURL } from 'node:url';\nimport { createJiti } from 'jiti';\nimport { ModelRegistry } from '@danceroutine/tango-schema';\n\nconst TS_EXTENSIONS = new Set(['.ts', '.tsx', '.mts', '.cts']);\n\nfunction toAbsolutePath(modulePath: string, projectRoot: string): string {\n return resolve(projectRoot, modulePath);\n}\n\nfunction isTypeScriptModule(modulePath: string): boolean {\n return TS_EXTENSIONS.has(extname(modulePath).toLowerCase());\n}\n\n/**\n * Load a module from a Tango app project root.\n *\n * TypeScript modules are loaded through jiti and JavaScript/ESM modules are loaded\n * through native dynamic import so published installs behave like end-user runtime.\n */\nexport async function loadModule(\n modulePath: string,\n options?: { projectRoot?: string; registry?: ModelRegistry; moduleCache?: boolean }\n): Promise<Record<string, unknown>> {\n const projectRoot = options?.projectRoot ?? process.cwd();\n const absolutePath = toAbsolutePath(modulePath, projectRoot);\n const executeImport = async (): Promise<Record<string, unknown>> => {\n if (isTypeScriptModule(absolutePath)) {\n const jiti = createJiti(resolve(projectRoot, 'tango.config.ts'), {\n interopDefault: true,\n moduleCache: options?.moduleCache ?? true,\n });\n return (await jiti.import<Record<string, unknown>>(absolutePath)) as Record<string, unknown>;\n }\n\n return (await import(pathToFileURL(absolutePath).href)) as Record<string, unknown>;\n };\n\n if (options?.registry) {\n return ModelRegistry.runWithRegistry(options.registry, executeImport);\n }\n\n return executeImport();\n}\n\n/**\n * Load a module and return default export when present.\n */\nexport async function loadDefaultExport(modulePath: string, options?: { projectRoot?: string }): Promise<unknown> {\n const loaded = await loadModule(modulePath, options);\n return loaded.default ?? loaded;\n}\n","import { CollectingBuilder } from '../builder/runtime/CollectingBuilder';\nimport type { Dialect } from '../domain/Dialect';\nimport { Migration } from '../domain/Migration';\nimport type { SQL } from '../compilers/contracts/SQL';\nimport type { MigrationOperation } from '../domain/MigrationOperation';\nimport type { CompilerStrategy } from '../strategies/CompilerStrategy';\nimport { createDefaultCompilerStrategy } from '../strategies/CompilerStrategy';\nimport { InternalDialect } from '../domain/internal/InternalDialect';\nimport { isError } from '@danceroutine/tango-core';\nimport { readdir } from 'node:fs/promises';\nimport { resolve } from 'node:path';\nimport { loadDefaultExport } from '../runtime/loadModule';\nimport { webcrypto } from 'node:crypto';\n\nconst JOURNAL = '_tango_migrations';\n\n/** DB client contract required by migration execution. */\ninterface DBClient {\n /** Execute SQL with optional parameters. */\n query<T = unknown>(sql: string, params?: readonly unknown[]): Promise<{ rows: T[] }>;\n /** Release underlying database resources. */\n close(): Promise<void>;\n}\n\ninterface AppliedMigrationRecord {\n id: string;\n checksum: string;\n}\n\ninterface MigrationChecksums {\n current: string;\n legacyOperations: string;\n}\n\n/**\n * Manages the lifecycle of database migrations: applying, planning, and tracking status.\n *\n * The runner reads migration files from a directory, compiles operations to SQL\n * for the target dialect, and maintains a journal table to track which migrations\n * have been applied. Each applied migration is checksummed to detect tampering.\n *\n * @example\n * ```typescript\n * const runner = new MigrationRunner(client, 'postgres', './migrations');\n *\n * // Apply all pending migrations\n * await runner.apply();\n *\n * // Apply up to a specific migration\n * await runner.apply('003_add_indexes');\n *\n * // Preview the SQL that would be generated\n * const sql = await runner.plan();\n *\n * // Check which migrations are applied\n * const statuses = await runner.status();\n * ```\n */\nexport class MigrationRunner {\n static readonly BRAND = 'tango.migrations.runner' as const;\n readonly __tangoBrand: typeof MigrationRunner.BRAND = MigrationRunner.BRAND;\n private compilerStrategy: CompilerStrategy;\n\n constructor(\n private client: DBClient,\n private dialect: Dialect,\n private migrationsDir: string = 'migrations',\n compilerStrategy?: CompilerStrategy\n ) {\n this.compilerStrategy = compilerStrategy ?? createDefaultCompilerStrategy();\n }\n\n /**\n * Narrow an unknown value to `MigrationRunner`.\n */\n static isMigrationRunner(value: unknown): value is MigrationRunner {\n return (\n typeof value === 'object' &&\n value !== null &&\n (value as { __tangoBrand?: unknown }).__tangoBrand === MigrationRunner.BRAND\n );\n }\n\n /**\n * Apply all pending migrations, optionally stopping at a specific migration ID.\n * Migrations are applied in file-sort order. Already-applied migrations are skipped.\n * Non-online migrations are wrapped in a transaction on Postgres.\n */\n async apply(toId?: string): Promise<void> {\n await this.ensureJournal();\n const applied = await this.listApplied();\n const migrations = await this.loadMigrations();\n\n for (const migration of migrations) {\n if (toId && migration.id > toId) {\n break;\n }\n const storedChecksum = applied.get(migration.id);\n if (storedChecksum !== undefined) {\n await this.assertAppliedChecksum(migration, storedChecksum);\n continue;\n }\n\n await this.applyMigration(migration);\n }\n }\n\n /**\n * Generate a dry-run SQL plan for all migrations without executing anything.\n * Useful for reviewing what SQL would be run before applying.\n */\n async plan(): Promise<string> {\n const migrations = await this.loadMigrations();\n let output = '';\n\n migrations.forEach((migration) => {\n const builder = new CollectingBuilder();\n migration.up(builder);\n const preparedOps = this.compilerStrategy.prepareOperations(this.dialect, builder.ops);\n const sqls = preparedOps.flatMap((op) => this.compileOperation(op));\n\n output += `# ${migration.id}\\n`;\n sqls.forEach((statement) => {\n output += statement.sql + ';\\n';\n });\n if (builder.dataFns.length) {\n output += '-- (data step present)\\n';\n }\n output += '\\n';\n });\n\n return output;\n }\n\n /**\n * Return the applied/pending status of every migration found on disk.\n */\n async status(): Promise<{ id: string; applied: boolean }[]> {\n const applied = await this.listApplied();\n const migrations = await this.loadMigrations();\n\n const statuses: { id: string; applied: boolean }[] = [];\n\n for (const migration of migrations) {\n const storedChecksum = applied.get(migration.id);\n if (storedChecksum !== undefined) {\n await this.assertAppliedChecksum(migration, storedChecksum);\n }\n\n statuses.push({\n id: migration.id,\n applied: storedChecksum !== undefined,\n });\n }\n\n return statuses;\n }\n\n private async ensureJournal(): Promise<void> {\n const sql =\n this.dialect === InternalDialect.POSTGRES\n ? `CREATE TABLE IF NOT EXISTS \"${JOURNAL}\" (\n id TEXT PRIMARY KEY,\n applied_at TIMESTAMPTZ NOT NULL DEFAULT now(),\n checksum TEXT NOT NULL\n )`\n : `CREATE TABLE IF NOT EXISTS ${JOURNAL} (\n id TEXT PRIMARY KEY,\n applied_at TEXT NOT NULL DEFAULT (datetime('now')),\n checksum TEXT NOT NULL\n )`;\n\n await this.client.query(sql);\n }\n\n private async listApplied(): Promise<Map<string, string>> {\n const table = this.dialect === InternalDialect.POSTGRES ? `\"${JOURNAL}\"` : JOURNAL;\n const { rows } = await this.client.query<AppliedMigrationRecord>(`SELECT id, checksum FROM ${table}`);\n return new Map(rows.map((record) => [record.id, record.checksum]));\n }\n\n private async loadMigrations(): Promise<Migration[]> {\n const files = (await readdir(this.migrationsDir)).filter((f) => f.endsWith('.ts') || f.endsWith('.js')).sort();\n\n const migrations: Migration[] = [];\n\n for (const file of files) {\n const absolutePath = resolve(this.migrationsDir, file);\n let loaded: unknown;\n try {\n loaded = await loadDefaultExport(absolutePath, { projectRoot: process.cwd() });\n } catch (error) {\n const reason = isError(error) ? error.message : String(error);\n throw new Error(`Failed to load migration module '${file}': ${reason}`, { cause: error });\n }\n\n if (Migration.isMigration(loaded)) {\n migrations.push(loaded);\n continue;\n }\n\n if (Migration.isMigrationConstructor(loaded)) {\n migrations.push(new loaded());\n continue;\n }\n\n throw new Error(\n `Invalid migration module '${file}'. Default export must be a Migration subclass or instance.`\n );\n }\n\n return migrations;\n }\n\n private async applyMigration(migration: Migration): Promise<void> {\n const builder = new CollectingBuilder();\n await migration.up(builder);\n\n const preparedOps = this.compilerStrategy.prepareOperations(this.dialect, builder.ops);\n const sqls = preparedOps.flatMap((op) => this.compileOperation(op));\n const checksum = await this.calculateBuilderChecksum(builder);\n\n const isOnline = (migration.mode ?? builder.getMode()) === 'online';\n\n if (!isOnline && this.dialect === InternalDialect.POSTGRES) {\n await this.client.query('BEGIN');\n }\n\n try {\n for (const statement of sqls) {\n await this.client.query(statement.sql, statement.params);\n }\n\n for (const fn of builder.dataFns) {\n await fn({ query: (sql, params) => this.client.query(sql, params).then(() => {}) });\n }\n\n const table = this.dialect === InternalDialect.POSTGRES ? `\"${JOURNAL}\"` : JOURNAL;\n const placeholder = this.dialect === InternalDialect.POSTGRES ? '$1, $2' : '?, ?';\n await this.client.query(`INSERT INTO ${table} (id, checksum) VALUES (${placeholder})`, [\n migration.id,\n checksum,\n ]);\n\n if (!isOnline && this.dialect === InternalDialect.POSTGRES) {\n await this.client.query('COMMIT');\n }\n } catch (error) {\n if (!isOnline && this.dialect === InternalDialect.POSTGRES) {\n await this.client.query('ROLLBACK');\n }\n throw error;\n }\n }\n\n private async assertAppliedChecksum(migration: Migration, storedChecksum: string): Promise<void> {\n const checksums = await this.calculateChecksums(migration);\n\n // Older Tango versions stored operation-only checksums. Keep those\n // journals readable while writing data-aware checksums for new rows.\n if (checksums.current !== storedChecksum && checksums.legacyOperations !== storedChecksum) {\n throw new Error(\n `Applied migration '${migration.id}' checksum mismatch. The migration file may have been modified after it was applied.`\n );\n }\n }\n\n private async calculateChecksums(migration: Migration): Promise<MigrationChecksums> {\n const builder = new CollectingBuilder();\n await migration.up(builder);\n\n return {\n current: await this.calculateBuilderChecksum(builder),\n legacyOperations: await this.calculateLegacyOperationsChecksum(builder),\n };\n }\n\n private async calculateBuilderChecksum(builder: CollectingBuilder): Promise<string> {\n return this.hashPayload({\n operations: builder.ops,\n dataFns: builder.dataFns.map((fn) => fn.toString()),\n });\n }\n\n private async calculateLegacyOperationsChecksum(builder: CollectingBuilder): Promise<string> {\n return this.hashPayload(builder.ops);\n }\n\n /**\n * Compute a SHA-256 digest of a migration checksum payload.\n * Stored alongside each applied migration in the journal table to detect\n * if a migration file has been modified after it was already applied.\n */\n private async hashPayload(payload: unknown): Promise<string> {\n const digest = await webcrypto.subtle.digest('SHA-256', Buffer.from(JSON.stringify(payload), 'utf8'));\n return Buffer.from(digest).toString('hex');\n }\n\n private compileOperation(op: MigrationOperation): SQL[] {\n return this.compilerStrategy.compile(this.dialect, op);\n }\n}\n"],"mappings":";;;;;;;;;;;AAKA,MAAM,gBAAgB,IAAI,IAAI;CAAC;CAAO;CAAQ;CAAQ;AAAM,CAAC;AAE7D,SAAS,eAAe,YAAoB,aAA6B;CACrE,OAAO,QAAQ,aAAa,UAAU;AAC1C;AAEA,SAAS,mBAAmB,YAA6B;CACrD,OAAO,cAAc,IAAI,QAAQ,UAAU,EAAE,YAAY,CAAC;AAC9D;;;;;;;AAQA,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AO,WAAW,OAAO,KAAK,KAAK,UAAU,OAAO,GAAG,MAAM,CAAC;EACpG,OAAO,OAAO,KAAK,MAAM,EAAE,SAAS,KAAK;CAC7C;CAEA,iBAAyB,IAA+B;EACpD,OAAO,KAAK,iBAAiB,QAAQ,KAAK,SAAS,EAAE;CACzD;AACJ"}
|
|
1
|
+
{"version":3,"file":"MigrationRunner-DCCeU1kq.js","names":[],"sources":["../src/runtime/loadModule.ts","../src/runner/MigrationRunner.ts"],"sourcesContent":["import { resolve, extname } from 'node:path';\nimport { pathToFileURL } from 'node:url';\nimport { createJiti } from 'jiti';\nimport { ModelRegistry } from '@danceroutine/tango-schema';\n\nconst TS_EXTENSIONS = new Set(['.ts', '.tsx', '.mts', '.cts']);\n\nfunction toAbsolutePath(modulePath: string, projectRoot: string): string {\n return resolve(projectRoot, modulePath);\n}\n\nfunction isTypeScriptModule(modulePath: string): boolean {\n return TS_EXTENSIONS.has(extname(modulePath).toLowerCase());\n}\n\n/**\n * Load a module from a Tango app project root.\n *\n * TypeScript modules are loaded through jiti and JavaScript/ESM modules are loaded\n * through native dynamic import so published installs behave like end-user runtime.\n */\nexport async function loadModule(\n modulePath: string,\n options?: { projectRoot?: string; registry?: ModelRegistry; moduleCache?: boolean }\n): Promise<Record<string, unknown>> {\n const projectRoot = options?.projectRoot ?? process.cwd();\n const absolutePath = toAbsolutePath(modulePath, projectRoot);\n const executeImport = async (): Promise<Record<string, unknown>> => {\n if (isTypeScriptModule(absolutePath)) {\n const jiti = createJiti(resolve(projectRoot, 'tango.config.ts'), {\n interopDefault: true,\n moduleCache: options?.moduleCache ?? true,\n });\n return (await jiti.import<Record<string, unknown>>(absolutePath)) as Record<string, unknown>;\n }\n\n return (await import(pathToFileURL(absolutePath).href)) as Record<string, unknown>;\n };\n\n if (options?.registry) {\n return ModelRegistry.runWithRegistry(options.registry, executeImport);\n }\n\n return executeImport();\n}\n\n/**\n * Load a module and return default export when present.\n */\nexport async function loadDefaultExport(modulePath: string, options?: { projectRoot?: string }): Promise<unknown> {\n const loaded = await loadModule(modulePath, options);\n return loaded.default ?? loaded;\n}\n","import { CollectingBuilder } from '../builder/runtime/CollectingBuilder';\nimport type { Dialect } from '../domain/Dialect';\nimport { Migration } from '../domain/Migration';\nimport type { SQL } from '../compilers/contracts/SQL';\nimport type { MigrationOperation } from '../domain/MigrationOperation';\nimport type { CompilerStrategy } from '../strategies/CompilerStrategy';\nimport { createDefaultCompilerStrategy } from '../strategies/CompilerStrategy';\nimport { InternalDialect } from '../domain/internal/InternalDialect';\nimport { isError } from '@danceroutine/tango-core';\nimport { readdir } from 'node:fs/promises';\nimport { resolve } from 'node:path';\nimport { loadDefaultExport } from '../runtime/loadModule';\nimport { webcrypto } from 'node:crypto';\n\nconst JOURNAL = '_tango_migrations';\n\n/** DB client contract required by migration execution. */\ninterface DBClient {\n /** Execute SQL with optional parameters. */\n query<T = unknown>(sql: string, params?: readonly unknown[]): Promise<{ rows: T[] }>;\n /** Release underlying database resources. */\n close(): Promise<void>;\n}\n\ninterface AppliedMigrationRecord {\n id: string;\n checksum: string;\n}\n\ninterface MigrationChecksums {\n current: string;\n legacyOperations: string;\n}\n\n/**\n * Manages the lifecycle of database migrations: applying, planning, and tracking status.\n *\n * The runner reads migration files from a directory, compiles operations to SQL\n * for the target dialect, and maintains a journal table to track which migrations\n * have been applied. Each applied migration is checksummed to detect tampering.\n *\n * @example\n * ```typescript\n * const runner = new MigrationRunner(client, 'postgres', './migrations');\n *\n * // Apply all pending migrations\n * await runner.apply();\n *\n * // Apply up to a specific migration\n * await runner.apply('003_add_indexes');\n *\n * // Preview the SQL that would be generated\n * const sql = await runner.plan();\n *\n * // Check which migrations are applied\n * const statuses = await runner.status();\n * ```\n */\nexport class MigrationRunner {\n static readonly BRAND = 'tango.migrations.runner' as const;\n readonly __tangoBrand: typeof MigrationRunner.BRAND = MigrationRunner.BRAND;\n private compilerStrategy: CompilerStrategy;\n\n constructor(\n private client: DBClient,\n private dialect: Dialect,\n private migrationsDir: string = 'migrations',\n compilerStrategy?: CompilerStrategy\n ) {\n this.compilerStrategy = compilerStrategy ?? createDefaultCompilerStrategy();\n }\n\n /**\n * Narrow an unknown value to `MigrationRunner`.\n */\n static isMigrationRunner(value: unknown): value is MigrationRunner {\n return (\n typeof value === 'object' &&\n value !== null &&\n (value as { __tangoBrand?: unknown }).__tangoBrand === MigrationRunner.BRAND\n );\n }\n\n /**\n * Apply all pending migrations, optionally stopping at a specific migration ID.\n * Migrations are applied in file-sort order. Already-applied migrations are skipped.\n * Non-online migrations are wrapped in a transaction on Postgres.\n */\n async apply(toId?: string): Promise<void> {\n await this.ensureJournal();\n const applied = await this.listApplied();\n const migrations = await this.loadMigrations();\n\n for (const migration of migrations) {\n if (toId && migration.id > toId) {\n break;\n }\n const storedChecksum = applied.get(migration.id);\n if (storedChecksum !== undefined) {\n await this.assertAppliedChecksum(migration, storedChecksum);\n continue;\n }\n\n await this.applyMigration(migration);\n }\n }\n\n /**\n * Generate a dry-run SQL plan for all migrations without executing anything.\n * Useful for reviewing what SQL would be run before applying.\n */\n async plan(): Promise<string> {\n const migrations = await this.loadMigrations();\n let output = '';\n\n migrations.forEach((migration) => {\n const builder = new CollectingBuilder();\n migration.up(builder);\n const preparedOps = this.compilerStrategy.prepareOperations(this.dialect, builder.ops);\n const sqls = preparedOps.flatMap((op) => this.compileOperation(op));\n\n output += `# ${migration.id}\\n`;\n sqls.forEach((statement) => {\n output += statement.sql + ';\\n';\n });\n if (builder.dataFns.length) {\n output += '-- (data step present)\\n';\n }\n output += '\\n';\n });\n\n return output;\n }\n\n /**\n * Return the applied/pending status of every migration found on disk.\n */\n async status(): Promise<{ id: string; applied: boolean }[]> {\n const applied = await this.listApplied();\n const migrations = await this.loadMigrations();\n\n const statuses: { id: string; applied: boolean }[] = [];\n\n for (const migration of migrations) {\n const storedChecksum = applied.get(migration.id);\n if (storedChecksum !== undefined) {\n await this.assertAppliedChecksum(migration, storedChecksum);\n }\n\n statuses.push({\n id: migration.id,\n applied: storedChecksum !== undefined,\n });\n }\n\n return statuses;\n }\n\n private async ensureJournal(): Promise<void> {\n const sql =\n this.dialect === InternalDialect.POSTGRES\n ? `CREATE TABLE IF NOT EXISTS \"${JOURNAL}\" (\n id TEXT PRIMARY KEY,\n applied_at TIMESTAMPTZ NOT NULL DEFAULT now(),\n checksum TEXT NOT NULL\n )`\n : `CREATE TABLE IF NOT EXISTS ${JOURNAL} (\n id TEXT PRIMARY KEY,\n applied_at TEXT NOT NULL DEFAULT (datetime('now')),\n checksum TEXT NOT NULL\n )`;\n\n await this.client.query(sql);\n }\n\n private async listApplied(): Promise<Map<string, string>> {\n const table = this.dialect === InternalDialect.POSTGRES ? `\"${JOURNAL}\"` : JOURNAL;\n const { rows } = await this.client.query<AppliedMigrationRecord>(`SELECT id, checksum FROM ${table}`);\n return new Map(rows.map((record) => [record.id, record.checksum]));\n }\n\n private async loadMigrations(): Promise<Migration[]> {\n const files = (await readdir(this.migrationsDir)).filter((f) => f.endsWith('.ts') || f.endsWith('.js')).sort();\n\n const migrations: Migration[] = [];\n\n for (const file of files) {\n const absolutePath = resolve(this.migrationsDir, file);\n let loaded: unknown;\n try {\n loaded = await loadDefaultExport(absolutePath, { projectRoot: process.cwd() });\n } catch (error) {\n const reason = isError(error) ? error.message : String(error);\n throw new Error(`Failed to load migration module '${file}': ${reason}`, { cause: error });\n }\n\n if (Migration.isMigration(loaded)) {\n migrations.push(loaded);\n continue;\n }\n\n if (Migration.isMigrationConstructor(loaded)) {\n migrations.push(new loaded());\n continue;\n }\n\n throw new Error(\n `Invalid migration module '${file}'. Default export must be a Migration subclass or instance.`\n );\n }\n\n return migrations;\n }\n\n private async applyMigration(migration: Migration): Promise<void> {\n const builder = new CollectingBuilder();\n await migration.up(builder);\n\n const preparedOps = this.compilerStrategy.prepareOperations(this.dialect, builder.ops);\n const sqls = preparedOps.flatMap((op) => this.compileOperation(op));\n const checksum = await this.calculateBuilderChecksum(builder);\n\n const isOnline = (migration.mode ?? builder.getMode()) === 'online';\n\n if (!isOnline && this.dialect === InternalDialect.POSTGRES) {\n await this.client.query('BEGIN');\n }\n\n try {\n for (const statement of sqls) {\n await this.client.query(statement.sql, statement.params);\n }\n\n for (const fn of builder.dataFns) {\n await fn({ query: (sql, params) => this.client.query(sql, params).then(() => {}) });\n }\n\n const table = this.dialect === InternalDialect.POSTGRES ? `\"${JOURNAL}\"` : JOURNAL;\n const placeholder = this.dialect === InternalDialect.POSTGRES ? '$1, $2' : '?, ?';\n await this.client.query(`INSERT INTO ${table} (id, checksum) VALUES (${placeholder})`, [\n migration.id,\n checksum,\n ]);\n\n if (!isOnline && this.dialect === InternalDialect.POSTGRES) {\n await this.client.query('COMMIT');\n }\n } catch (error) {\n if (!isOnline && this.dialect === InternalDialect.POSTGRES) {\n await this.client.query('ROLLBACK');\n }\n throw error;\n }\n }\n\n private async assertAppliedChecksum(migration: Migration, storedChecksum: string): Promise<void> {\n const checksums = await this.calculateChecksums(migration);\n\n // Older Tango versions stored operation-only checksums. Keep those\n // journals readable while writing data-aware checksums for new rows.\n if (checksums.current !== storedChecksum && checksums.legacyOperations !== storedChecksum) {\n throw new Error(\n `Applied migration '${migration.id}' checksum mismatch. The migration file may have been modified after it was applied.`\n );\n }\n }\n\n private async calculateChecksums(migration: Migration): Promise<MigrationChecksums> {\n const builder = new CollectingBuilder();\n await migration.up(builder);\n\n return {\n current: await this.calculateBuilderChecksum(builder),\n legacyOperations: await this.calculateLegacyOperationsChecksum(builder),\n };\n }\n\n private async calculateBuilderChecksum(builder: CollectingBuilder): Promise<string> {\n return this.hashPayload({\n operations: builder.ops,\n dataFns: builder.dataFns.map((fn) => fn.toString()),\n });\n }\n\n private async calculateLegacyOperationsChecksum(builder: CollectingBuilder): Promise<string> {\n return this.hashPayload(builder.ops);\n }\n\n /**\n * Compute a SHA-256 digest of a migration checksum payload.\n * Stored alongside each applied migration in the journal table to detect\n * if a migration file has been modified after it was already applied.\n */\n private async hashPayload(payload: unknown): Promise<string> {\n const digest = await webcrypto.subtle.digest('SHA-256', Buffer.from(JSON.stringify(payload), 'utf8'));\n return Buffer.from(digest).toString('hex');\n }\n\n private compileOperation(op: MigrationOperation): SQL[] {\n return this.compilerStrategy.compile(this.dialect, op);\n }\n}\n"],"mappings":";;;;;;;;;;;;AAKA,MAAM,gBAAgB,IAAI,IAAI;CAAC;CAAO;CAAQ;CAAQ;AAAM,CAAC;AAE7D,SAAS,eAAe,YAAoB,aAA6B;CACrE,OAAO,QAAQ,aAAa,UAAU;AAC1C;AAEA,SAAS,mBAAmB,YAA6B;CACrD,OAAO,cAAc,IAAI,QAAQ,UAAU,EAAE,YAAY,CAAC;AAC9D;;;;;;;AAQA,eAAsB,WAClB,YACA,SACgC;CAChC,MAAM,cAAc,SAAS,eAAe,QAAQ,IAAI;CACxD,MAAM,eAAe,eAAe,YAAY,WAAW;CAC3D,MAAM,gBAAgB,YAA8C;EAChE,IAAI,mBAAmB,YAAY,GAK/B,OAAQ,MAJK,WAAW,QAAQ,aAAa,iBAAiB,GAAG;GAC7D,gBAAgB;GAChB,aAAa,SAAS,eAAe;EACzC,CACiB,EAAE,OAAgC,YAAY;EAGnE,OAAQ,MAAM,OAAO,cAAc,YAAY,EAAE;CACrD;CAEA,IAAI,SAAS,UACT,OAAO,cAAc,gBAAgB,QAAQ,UAAU,aAAa;CAGxE,OAAO,cAAc;AACzB;;;;AAKA,eAAsB,kBAAkB,YAAoB,SAAsD;CAC9G,MAAM,SAAS,MAAM,WAAW,YAAY,OAAO;CACnD,OAAO,OAAO,WAAW;AAC7B;;;ACtCA,MAAM,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;AA4ChB,IAAa,kBAAb,MAAa,gBAAgB;CAMb;CACA;CACA;CAPZ,OAAgB,QAAQ;CACxB,eAAsD,gBAAgB;CACtE;CAEA,YACI,QACA,SACA,gBAAgC,cAChC,kBACF;EAJU,KAAA,SAAA;EACA,KAAA,UAAA;EACA,KAAA,gBAAA;EAGR,KAAK,mBAAmB,oBAAoB,8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EACtC,MAAM,UAAU,GAAG,OAAO;EAE1B,OAAO;GACH,SAAS,MAAM,KAAK,yBAAyB,OAAO;GACpD,kBAAkB,MAAM,KAAK,kCAAkC,OAAO;EAC1E;CACJ;CAEA,MAAc,yBAAyB,SAA6C;EAChF,OAAO,KAAK,YAAY;GACpB,YAAY,QAAQ;GACpB,SAAS,QAAQ,QAAQ,KAAK,OAAO,GAAG,SAAS,CAAC;EACtD,CAAC;CACL;CAEA,MAAc,kCAAkC,SAA6C;EACzF,OAAO,KAAK,YAAY,QAAQ,GAAG;CACvC;;;;;;CAOA,MAAc,YAAY,SAAmC;EACzD,MAAM,SAAS,MAAM,UAAU,OAAO,OAAO,WAAW,OAAO,KAAK,KAAK,UAAU,OAAO,GAAG,MAAM,CAAC;EACpG,OAAO,OAAO,KAAK,MAAM,EAAE,SAAS,KAAK;CAC7C;CAEA,iBAAyB,IAA+B;EACpD,OAAO,KAAK,iBAAiB,QAAQ,KAAK,SAAS,EAAE;CACzD;AACJ"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { n as loadModule, t as MigrationRunner } from "./MigrationRunner-
|
|
1
|
+
import { t as InternalDialect } from "./InternalDialect-DDW2l3mz.js";
|
|
2
|
+
import { n as loadModule, t as MigrationRunner } from "./MigrationRunner-DCCeU1kq.js";
|
|
3
3
|
import { t as MigrationGenerator } from "./MigrationGenerator-D-sSbsFG.js";
|
|
4
|
-
import { n as buildMigrationModelMetadataProjection, r as diffSchema } from "./diff-
|
|
5
|
-
import { n as createDefaultIntrospectorStrategy } from "./IntrospectorStrategy-
|
|
4
|
+
import { n as buildMigrationModelMetadataProjection, r as diffSchema } from "./diff-DdwU_MyI.js";
|
|
5
|
+
import { n as createDefaultIntrospectorStrategy } from "./IntrospectorStrategy-D_9umttn.js";
|
|
6
6
|
import { getLogger } from "@danceroutine/tango-core";
|
|
7
7
|
import { access, mkdir } from "node:fs/promises";
|
|
8
8
|
import { dirname, resolve } from "node:path";
|
|
@@ -222,7 +222,7 @@ function registerMigrationsCommands(yargsBuilder) {
|
|
|
222
222
|
env: argv.env
|
|
223
223
|
});
|
|
224
224
|
const loaded = await loadModels(argv.models);
|
|
225
|
-
const projectedModels = buildMigrationModelMetadataProjection(loaded.registry);
|
|
225
|
+
const projectedModels = buildMigrationModelMetadataProjection(loaded.registry, { dialect: resolved.dialect });
|
|
226
226
|
logger.info(`Found ${loaded.models.length} exported model(s); ${projectedModels.length} model(s) after projection: ${projectedModels.map((m) => m.table).join(", ")}`);
|
|
227
227
|
try {
|
|
228
228
|
await writeRelationRegistryArtifacts({
|
|
@@ -324,4 +324,4 @@ function registerMigrationsCommands(yargsBuilder) {
|
|
|
324
324
|
//#endregion
|
|
325
325
|
export { registerMigrationsCommands as t };
|
|
326
326
|
|
|
327
|
-
//# sourceMappingURL=cli-
|
|
327
|
+
//# sourceMappingURL=cli-Dhp2BZsB.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli-kbb2TSGO.js","names":["fsConstants"],"sources":["../src/commands/cli.ts"],"sourcesContent":["import { access, mkdir } from 'node:fs/promises';\nimport { constants as fsConstants } from 'node:fs';\nimport { dirname, resolve } from 'node:path';\nimport type { Argv } from 'yargs';\nimport { MigrationRunner } from '../runner/MigrationRunner';\nimport { MigrationGenerator } from '../generator/MigrationGenerator';\nimport { buildMigrationModelMetadataProjection, diffSchema } from '../diff/index';\nimport type { DbSchema } from '../introspect/PostgresIntrospector';\nimport type { Dialect } from '../domain/Dialect';\nimport type { ColumnType } from '../builder/contracts/ColumnType';\nimport type { DeleteReferentialAction } from '../builder/contracts/DeleteReferentialAction';\nimport type { UpdateReferentialAction } from '../builder/contracts/UpdateReferentialAction';\nimport { createDefaultIntrospectorStrategy } from '../strategies/IntrospectorStrategy';\nimport { InternalDialect } from '../domain/internal/InternalDialect';\nimport { loadConfig } from '@danceroutine/tango-config';\nimport { loadProjectModule } from '@danceroutine/tango-codegen/commands';\nimport { writeRelationRegistryArtifacts } from '@danceroutine/tango-codegen/generators';\nimport { getLogger } from '@danceroutine/tango-core';\nimport { GENERATED_RELATION_REGISTRY_DIRNAME, ModelRegistry } from '@danceroutine/tango-schema';\nimport { loadModule } from '../runtime/loadModule';\n\nconst logger = getLogger('tango.migrations');\n\ntype ConfigEnvironment = 'development' | 'test' | 'production';\n\ntype OptionalMigrationDefaults = {\n dialect?: Dialect;\n db?: string;\n dir?: string;\n autoApply?: boolean;\n};\n\ntype ModelMetadataLike = {\n table: string;\n fields: Array<{\n name: string;\n type: ColumnType;\n notNull?: boolean;\n default?: string | { now: true } | null;\n primaryKey?: boolean;\n unique?: boolean;\n references?: {\n table: string;\n column: string;\n onDelete?: DeleteReferentialAction;\n onUpdate?: UpdateReferentialAction;\n };\n }>;\n indexes?: Array<{ name: string; on: string[]; unique?: boolean }>;\n};\n\ntype CliDbClient = {\n query<T = unknown>(sql: string, params?: readonly unknown[]): Promise<{ rows: T[] }>;\n close(): Promise<void>;\n};\n\ntype LoadedModelsResult = {\n models: ModelMetadataLike[];\n registry: ModelRegistry;\n modelTypeAccessors: Record<string, string>;\n};\n\ntype ModelContainerLike = {\n metadata: ModelMetadataLike;\n};\n\nasync function importModule(modulePath: string): Promise<Record<string, unknown>> {\n return loadModule(modulePath, { projectRoot: process.cwd() });\n}\n\nasync function tryLoadMigrationDefaults(\n configPathArg: string | undefined,\n configEnvArg: ConfigEnvironment | undefined\n): Promise<OptionalMigrationDefaults> {\n const explicitConfigPath = typeof configPathArg === 'string' && configPathArg.trim().length > 0;\n const resolvedPath = resolve(process.cwd(), configPathArg?.trim() || './tango.config.ts');\n\n try {\n await access(resolvedPath, fsConstants.F_OK);\n } catch (error) {\n if (explicitConfigPath) {\n throw new Error(`Config file not found: ${resolvedPath}`, { cause: error });\n }\n return {};\n }\n\n const module = await importModule(resolvedPath);\n const fileConfig = (module.default ?? module) as { current?: ConfigEnvironment } & Record<string, unknown>;\n\n const loaded = loadConfig(() => ({\n ...fileConfig,\n ...(configEnvArg ? { current: configEnvArg } : {}),\n }));\n\n const { db, migrations } = loaded.current;\n const inferredDialect = db.adapter as Dialect;\n const inferredDb = resolveDbTarget(db);\n\n return {\n dialect: inferredDialect,\n db: inferredDb,\n dir: migrations.dir,\n autoApply: (migrations as { autoApply?: boolean }).autoApply,\n };\n}\n\nfunction resolveDbTarget(db: {\n adapter: string;\n url?: string;\n filename?: string;\n host?: string;\n port?: number;\n database?: string;\n user?: string;\n password?: string;\n}): string | undefined {\n if (db.adapter === InternalDialect.SQLITE) {\n return db.filename ?? db.url;\n }\n\n if (db.url) {\n return db.url;\n }\n\n if (!db.database) {\n return undefined;\n }\n\n const host = db.host ?? 'localhost';\n const port = db.port ?? 5432;\n const encodedUser = db.user ? encodeURIComponent(db.user) : '';\n const encodedPassword = db.password ? encodeURIComponent(db.password) : '';\n const userInfo =\n encodedUser.length > 0\n ? encodedPassword.length > 0\n ? `${encodedUser}:${encodedPassword}@`\n : `${encodedUser}@`\n : '';\n\n return `postgres://${userInfo}${host}:${String(port)}/${db.database}`;\n}\n\nasync function resolveCommandInputs(argv: {\n dialect?: string;\n dir?: string;\n db?: string;\n config?: string;\n env?: ConfigEnvironment;\n}): Promise<{ dialect: Dialect; dir: string; db?: string; autoApply: boolean }> {\n const defaults = await tryLoadMigrationDefaults(argv.config, argv.env);\n\n const resolvedDialect = (argv.dialect as Dialect | undefined) ?? defaults.dialect ?? InternalDialect.POSTGRES;\n const resolvedDir = argv.dir ?? defaults.dir ?? 'migrations';\n const resolvedDb = argv.db ?? defaults.db;\n\n return {\n dialect: resolvedDialect,\n dir: resolvedDir,\n db: resolvedDb,\n autoApply: defaults.autoApply ?? true,\n };\n}\n\nfunction isModelContainerLike(value: unknown): value is ModelContainerLike {\n return typeof value === 'object' && value !== null && 'metadata' in value;\n}\n\nfunction collectExportedModels(moduleValue: unknown): ModelMetadataLike[] {\n if (!moduleValue || typeof moduleValue !== 'object') {\n return [];\n }\n\n const models: ModelMetadataLike[] = [];\n for (const value of Object.values(moduleValue as Record<string, unknown>)) {\n if (isModelContainerLike(value)) {\n models.push(value.metadata);\n continue;\n }\n\n if (!value || typeof value !== 'object') {\n continue;\n }\n\n for (const nestedValue of Object.values(value as Record<string, unknown>)) {\n if (isModelContainerLike(nestedValue)) {\n models.push(nestedValue.metadata);\n }\n }\n }\n\n return models;\n}\n\nasync function loadModels(modelsPath: string): Promise<LoadedModelsResult> {\n const {\n loaded: mod,\n registry,\n modelTypeAccessors,\n } = await loadProjectModule(modelsPath, {\n projectRoot: process.cwd(),\n outputDir: resolve(process.cwd(), GENERATED_RELATION_REGISTRY_DIRNAME),\n });\n const moduleValue = (mod.default ?? mod) as unknown;\n\n const models = isModelContainerLike(moduleValue)\n ? [moduleValue.metadata, ...collectExportedModels(moduleValue)]\n : collectExportedModels(moduleValue);\n\n if (models.length === 0) {\n throw new Error(`No models found in '${modelsPath}'. Ensure the module exports Model() definitions.`);\n }\n\n return {\n models,\n registry,\n modelTypeAccessors,\n };\n}\n\nasync function connectAndIntrospect(dbUrl: string, dialect: string): Promise<DbSchema> {\n const dbClient = await connectDbClient(dbUrl, dialect as Dialect);\n\n try {\n const strategy = createDefaultIntrospectorStrategy();\n return await strategy.introspect(dialect as Dialect, dbClient);\n } finally {\n await dbClient.close();\n }\n}\n\nasync function connectDbClient(db: string, dialect: Dialect): Promise<CliDbClient> {\n if (dialect === InternalDialect.POSTGRES) {\n const pg = await import('pg');\n const client = new pg.default.Client({ connectionString: db });\n await client.connect();\n\n return {\n async query<T = unknown>(sql: string, params?: readonly unknown[]): Promise<{ rows: T[] }> {\n const result = await client.query(sql, params as unknown[] | undefined);\n return { rows: result.rows as T[] };\n },\n async close(): Promise<void> {\n await client.end();\n },\n };\n }\n\n if (dialect === InternalDialect.SQLITE) {\n const sqlite = await import('better-sqlite3');\n const DatabaseCtor = (sqlite.default ?? sqlite) as new (filename: string) => {\n prepare(sql: string): {\n all(...params: unknown[]): unknown[];\n run(...params: unknown[]): unknown;\n };\n close(): void;\n };\n\n const filename = normalizeSqliteFilename(db);\n await ensureSqliteParentDirectory(filename);\n const connection = new DatabaseCtor(filename);\n\n return {\n async query<T = unknown>(sql: string, params?: readonly unknown[]): Promise<{ rows: T[] }> {\n const statement = connection.prepare(sql);\n const values = [...(params ?? [])];\n const isSelectLike = /^\\s*(SELECT|PRAGMA|WITH)\\b/i.test(sql);\n if (isSelectLike) {\n return { rows: statement.all(...values) as T[] };\n }\n\n statement.run(...values);\n return { rows: [] as T[] };\n },\n async close(): Promise<void> {\n connection.close();\n },\n };\n }\n\n throw new Error(`Unsupported dialect: ${dialect}`);\n}\n\nasync function ensureSqliteParentDirectory(filename: string): Promise<void> {\n if (filename === ':memory:' || filename === 'file::memory:') {\n return;\n }\n const directory = dirname(filename);\n if (directory === '.' || directory.length === 0) {\n return;\n }\n await mkdir(directory, { recursive: true });\n}\n\nfunction normalizeSqliteFilename(db: string): string {\n if (db.startsWith('sqlite://')) {\n return db.slice('sqlite://'.length);\n }\n return db;\n}\n\n/**\n * Register Tango's migration commands on an existing yargs parser.\n */\nexport function registerMigrationsCommands(yargsBuilder: Argv): Argv {\n return yargsBuilder\n .command(\n 'migrate',\n 'Apply pending migrations to the database',\n (builder) =>\n builder\n .option('dir', {\n type: 'string',\n describe: 'Migrations directory',\n })\n .option('db', {\n type: 'string',\n describe: 'Database connection URL',\n })\n .option('dialect', {\n type: 'string',\n choices: ['postgres', 'sqlite'] as const,\n describe: 'Database dialect',\n })\n .option('config', {\n type: 'string',\n describe: 'Path to tango.config.ts (auto-loads ./tango.config.ts when present)',\n })\n .option('env', {\n type: 'string',\n choices: ['development', 'test', 'production'] as const,\n describe: 'Config environment override',\n })\n .option('to', {\n type: 'string',\n describe: 'Target migration ID (apply up to this migration)',\n }),\n async (argv) => {\n const resolved = await resolveCommandInputs({\n dialect: argv.dialect as string | undefined,\n dir: argv.dir as string | undefined,\n db: argv.db as string | undefined,\n config: argv.config as string | undefined,\n env: argv.env as ConfigEnvironment | undefined,\n });\n\n if (!resolved.autoApply) {\n logger.info('Auto-migration disabled (autoApply: false). Skipping.');\n return;\n }\n\n if (!resolved.db) {\n throw new Error('No database target provided. Pass --db or define db settings in tango.config.ts.');\n }\n\n const dbClient = await connectDbClient(resolved.db, resolved.dialect);\n\n const runner = new MigrationRunner(dbClient, resolved.dialect, resolved.dir);\n await runner.apply(argv.to);\n\n await dbClient.close();\n logger.info('Migrations applied successfully');\n }\n )\n .command(\n 'make:migrations',\n 'Generate migration file by comparing models to database',\n (builder) =>\n builder\n .option('dir', {\n type: 'string',\n describe: 'Migrations directory',\n })\n .option('name', {\n type: 'string',\n demandOption: true,\n describe: 'Migration name (e.g. \"create_users\")',\n })\n .option('models', {\n type: 'string',\n demandOption: true,\n describe: 'Path to module exporting Model definitions (e.g. \"./src/models.ts\")',\n })\n .option('db', {\n type: 'string',\n describe: 'Database connection URL for introspection (omit for initial migration)',\n })\n .option('dialect', {\n type: 'string',\n choices: ['postgres', 'sqlite'] as const,\n describe: 'Database dialect',\n })\n .option('config', {\n type: 'string',\n describe: 'Path to tango.config.ts (auto-loads ./tango.config.ts when present)',\n })\n .option('env', {\n type: 'string',\n choices: ['development', 'test', 'production'] as const,\n describe: 'Config environment override',\n }),\n async (argv) => {\n const resolved = await resolveCommandInputs({\n dialect: argv.dialect as string | undefined,\n dir: argv.dir as string | undefined,\n db: argv.db as string | undefined,\n config: argv.config as string | undefined,\n env: argv.env as ConfigEnvironment | undefined,\n });\n const loaded = await loadModels(argv.models);\n const projectedModels = buildMigrationModelMetadataProjection(loaded.registry);\n logger.info(\n `Found ${loaded.models.length} exported model(s); ${projectedModels.length} model(s) after projection: ${projectedModels.map((m) => m.table).join(', ')}`\n );\n try {\n await writeRelationRegistryArtifacts({\n registry: loaded.registry,\n modelTypeAccessors: loaded.modelTypeAccessors,\n outputDir: resolve(process.cwd(), GENERATED_RELATION_REGISTRY_DIRNAME),\n });\n } catch (error) {\n logger.warn(\n `Unable to refresh generated relation registry during make:migrations. Continuing without updated relation artifacts: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n\n let dbState: DbSchema;\n if (resolved.db) {\n logger.info('Introspecting database...');\n dbState = await connectAndIntrospect(resolved.db, resolved.dialect);\n } else {\n dbState = { tables: {} };\n }\n\n const operations = diffSchema(dbState, projectedModels);\n\n if (operations.length === 0) {\n logger.info('No changes detected — models and database are in sync');\n return;\n }\n\n const generator = new MigrationGenerator();\n const filepath = await generator.generate({\n name: argv.name,\n operations,\n directory: resolved.dir,\n });\n\n logger.info(`Generated migration: ${filepath}`);\n logger.info(` ${operations.length} operation(s)`);\n }\n )\n .command(\n 'plan',\n 'Print migration SQL without applying',\n (builder) =>\n builder\n .option('dir', {\n type: 'string',\n describe: 'Migrations directory',\n })\n .option('dialect', {\n type: 'string',\n choices: ['postgres', 'sqlite'] as const,\n describe: 'Database dialect',\n })\n .option('config', {\n type: 'string',\n describe: 'Path to tango.config.ts (auto-loads ./tango.config.ts when present)',\n })\n .option('env', {\n type: 'string',\n choices: ['development', 'test', 'production'] as const,\n describe: 'Config environment override',\n }),\n async (argv) => {\n const resolved = await resolveCommandInputs({\n dialect: argv.dialect as string | undefined,\n dir: argv.dir as string | undefined,\n db: argv.db as string | undefined,\n config: argv.config as string | undefined,\n env: argv.env as ConfigEnvironment | undefined,\n });\n const runner = new MigrationRunner(\n { query: async () => ({ rows: [] }), close: async () => {} },\n resolved.dialect,\n resolved.dir\n );\n const output = await runner.plan();\n logger.info(output);\n }\n )\n .command(\n 'status',\n 'Show applied/pending status of all migrations',\n (builder) =>\n builder\n .option('dir', {\n type: 'string',\n describe: 'Migrations directory',\n })\n .option('db', {\n type: 'string',\n describe: 'Database connection URL',\n })\n .option('dialect', {\n type: 'string',\n choices: ['postgres', 'sqlite'] as const,\n describe: 'Database dialect',\n })\n .option('config', {\n type: 'string',\n describe: 'Path to tango.config.ts (auto-loads ./tango.config.ts when present)',\n })\n .option('env', {\n type: 'string',\n choices: ['development', 'test', 'production'] as const,\n describe: 'Config environment override',\n }),\n async (argv) => {\n const resolved = await resolveCommandInputs({\n dialect: argv.dialect as string | undefined,\n dir: argv.dir as string | undefined,\n db: argv.db as string | undefined,\n config: argv.config as string | undefined,\n env: argv.env as ConfigEnvironment | undefined,\n });\n if (!resolved.db) {\n throw new Error('No database target provided. Pass --db or define db settings in tango.config.ts.');\n }\n\n const dbClient = await connectDbClient(resolved.db, resolved.dialect);\n\n const runner = new MigrationRunner(dbClient, resolved.dialect, resolved.dir);\n const statuses = await runner.status();\n\n if (statuses.length === 0) {\n logger.info('No migrations found');\n } else {\n statuses.forEach((statusItem) => {\n const marker = statusItem.applied ? '[x]' : '[ ]';\n logger.info(` ${marker} ${statusItem.id}`);\n });\n }\n\n await dbClient.close();\n }\n );\n}\n"],"mappings":";;;;;;;;;;;;;;AAqBA,MAAM,SAAS,UAAU,kBAAkB;AA6C3C,eAAe,aAAa,YAAsD;CAC9E,OAAO,WAAW,YAAY,EAAE,aAAa,QAAQ,IAAI,EAAE,CAAC;AAChE;AAEA,eAAe,yBACX,eACA,cACkC;CAClC,MAAM,qBAAqB,OAAO,kBAAkB,YAAY,cAAc,KAAK,EAAE,SAAS;CAC9F,MAAM,eAAe,QAAQ,QAAQ,IAAI,GAAG,eAAe,KAAK,KAAK,mBAAmB;CAExF,IAAI;EACA,MAAM,OAAO,cAAcA,UAAY,IAAI;CAC/C,SAAS,OAAO;EACZ,IAAI,oBACA,MAAM,IAAI,MAAM,0BAA0B,gBAAgB,EAAE,OAAO,MAAM,CAAC;EAE9E,OAAO,CAAC;CACZ;CAEA,MAAM,SAAS,MAAM,aAAa,YAAY;CAC9C,MAAM,aAAc,OAAO,WAAW;CAOtC,MAAM,EAAE,IAAI,eALG,kBAAkB;EAC7B,GAAG;EACH,GAAI,eAAe,EAAE,SAAS,aAAa,IAAI,CAAC;CACpD,EAEgC,EAAE;CAIlC,OAAO;EACH,SAJoB,GAAG;EAKvB,IAJe,gBAAgB,EAIlB;EACb,KAAK,WAAW;EAChB,WAAY,WAAuC;CACvD;AAC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,OAAO;EACX,MAAM;EACN,UAAU;CACd,CAAC,EACA,OAAO,MAAM;EACV,MAAM;EACN,UAAU;CACd,CAAC,EACA,OAAO,WAAW;EACf,MAAM;EACN,SAAS,CAAC,YAAY,QAAQ;EAC9B,UAAU;CACd,CAAC,EACA,OAAO,UAAU;EACd,MAAM;EACN,UAAU;CACd,CAAC,EACA,OAAO,OAAO;EACX,MAAM;EACN,SAAS;GAAC;GAAe;GAAQ;EAAY;EAC7C,UAAU;CACd,CAAC,GACT,OAAO,SAAS;EACZ,MAAM,WAAW,MAAM,qBAAqB;GACxC,SAAS,KAAK;GACd,KAAK,KAAK;GACV,IAAI,KAAK;GACT,QAAQ,KAAK;GACb,KAAK,KAAK;EACd,CAAC;EACD,IAAI,CAAC,SAAS,IACV,MAAM,IAAI,MAAM,kFAAkF;EAGtG,MAAM,WAAW,MAAM,gBAAgB,SAAS,IAAI,SAAS,OAAO;EAGpE,MAAM,WAAW,MAAM,IADJ,gBAAgB,UAAU,SAAS,SAAS,SAAS,GAC5C,EAAE,OAAO;EAErC,IAAI,SAAS,WAAW,GACpB,OAAO,KAAK,qBAAqB;OAEjC,SAAS,SAAS,eAAe;GAC7B,MAAM,SAAS,WAAW,UAAU,QAAQ;GAC5C,OAAO,KAAK,KAAK,OAAO,GAAG,WAAW,IAAI;EAC9C,CAAC;EAGL,MAAM,SAAS,MAAM;CACzB,CACJ;AACR"}
|
|
1
|
+
{"version":3,"file":"cli-Dhp2BZsB.js","names":["fsConstants"],"sources":["../src/commands/cli.ts"],"sourcesContent":["import { access, mkdir } from 'node:fs/promises';\nimport { constants as fsConstants } from 'node:fs';\nimport { dirname, resolve } from 'node:path';\nimport type { Argv } from 'yargs';\nimport { MigrationRunner } from '../runner/MigrationRunner';\nimport { MigrationGenerator } from '../generator/MigrationGenerator';\nimport { buildMigrationModelMetadataProjection, diffSchema } from '../diff/index';\nimport type { DbSchema } from '../introspect/PostgresIntrospector';\nimport type { Dialect } from '../domain/Dialect';\nimport type { ColumnType } from '../builder/contracts/ColumnType';\nimport type { DeleteReferentialAction } from '../builder/contracts/DeleteReferentialAction';\nimport type { UpdateReferentialAction } from '../builder/contracts/UpdateReferentialAction';\nimport { createDefaultIntrospectorStrategy } from '../strategies/IntrospectorStrategy';\nimport { InternalDialect } from '../domain/internal/InternalDialect';\nimport { loadConfig } from '@danceroutine/tango-config';\nimport { loadProjectModule } from '@danceroutine/tango-codegen/commands';\nimport { writeRelationRegistryArtifacts } from '@danceroutine/tango-codegen/generators';\nimport { getLogger } from '@danceroutine/tango-core';\nimport { GENERATED_RELATION_REGISTRY_DIRNAME, ModelRegistry } from '@danceroutine/tango-schema';\nimport { loadModule } from '../runtime/loadModule';\n\nconst logger = getLogger('tango.migrations');\n\ntype ConfigEnvironment = 'development' | 'test' | 'production';\n\ntype OptionalMigrationDefaults = {\n dialect?: Dialect;\n db?: string;\n dir?: string;\n autoApply?: boolean;\n};\n\ntype ModelMetadataLike = {\n table: string;\n fields: Array<{\n name: string;\n type: ColumnType;\n notNull?: boolean;\n default?: string | { now: true } | null;\n primaryKey?: boolean;\n unique?: boolean;\n references?: {\n table: string;\n column: string;\n onDelete?: DeleteReferentialAction;\n onUpdate?: UpdateReferentialAction;\n };\n }>;\n indexes?: Array<{ name: string; on: string[]; unique?: boolean }>;\n};\n\ntype CliDbClient = {\n query<T = unknown>(sql: string, params?: readonly unknown[]): Promise<{ rows: T[] }>;\n close(): Promise<void>;\n};\n\ntype LoadedModelsResult = {\n models: ModelMetadataLike[];\n registry: ModelRegistry;\n modelTypeAccessors: Record<string, string>;\n};\n\ntype ModelContainerLike = {\n metadata: ModelMetadataLike;\n};\n\nasync function importModule(modulePath: string): Promise<Record<string, unknown>> {\n return loadModule(modulePath, { projectRoot: process.cwd() });\n}\n\nasync function tryLoadMigrationDefaults(\n configPathArg: string | undefined,\n configEnvArg: ConfigEnvironment | undefined\n): Promise<OptionalMigrationDefaults> {\n const explicitConfigPath = typeof configPathArg === 'string' && configPathArg.trim().length > 0;\n const resolvedPath = resolve(process.cwd(), configPathArg?.trim() || './tango.config.ts');\n\n try {\n await access(resolvedPath, fsConstants.F_OK);\n } catch (error) {\n if (explicitConfigPath) {\n throw new Error(`Config file not found: ${resolvedPath}`, { cause: error });\n }\n return {};\n }\n\n const module = await importModule(resolvedPath);\n const fileConfig = (module.default ?? module) as { current?: ConfigEnvironment } & Record<string, unknown>;\n\n const loaded = loadConfig(() => ({\n ...fileConfig,\n ...(configEnvArg ? { current: configEnvArg } : {}),\n }));\n\n const { db, migrations } = loaded.current;\n const inferredDialect = db.adapter as Dialect;\n const inferredDb = resolveDbTarget(db);\n\n return {\n dialect: inferredDialect,\n db: inferredDb,\n dir: migrations.dir,\n autoApply: (migrations as { autoApply?: boolean }).autoApply,\n };\n}\n\nfunction resolveDbTarget(db: {\n adapter: string;\n url?: string;\n filename?: string;\n host?: string;\n port?: number;\n database?: string;\n user?: string;\n password?: string;\n}): string | undefined {\n if (db.adapter === InternalDialect.SQLITE) {\n return db.filename ?? db.url;\n }\n\n if (db.url) {\n return db.url;\n }\n\n if (!db.database) {\n return undefined;\n }\n\n const host = db.host ?? 'localhost';\n const port = db.port ?? 5432;\n const encodedUser = db.user ? encodeURIComponent(db.user) : '';\n const encodedPassword = db.password ? encodeURIComponent(db.password) : '';\n const userInfo =\n encodedUser.length > 0\n ? encodedPassword.length > 0\n ? `${encodedUser}:${encodedPassword}@`\n : `${encodedUser}@`\n : '';\n\n return `postgres://${userInfo}${host}:${String(port)}/${db.database}`;\n}\n\nasync function resolveCommandInputs(argv: {\n dialect?: string;\n dir?: string;\n db?: string;\n config?: string;\n env?: ConfigEnvironment;\n}): Promise<{ dialect: Dialect; dir: string; db?: string; autoApply: boolean }> {\n const defaults = await tryLoadMigrationDefaults(argv.config, argv.env);\n\n const resolvedDialect = (argv.dialect as Dialect | undefined) ?? defaults.dialect ?? InternalDialect.POSTGRES;\n const resolvedDir = argv.dir ?? defaults.dir ?? 'migrations';\n const resolvedDb = argv.db ?? defaults.db;\n\n return {\n dialect: resolvedDialect,\n dir: resolvedDir,\n db: resolvedDb,\n autoApply: defaults.autoApply ?? true,\n };\n}\n\nfunction isModelContainerLike(value: unknown): value is ModelContainerLike {\n return typeof value === 'object' && value !== null && 'metadata' in value;\n}\n\nfunction collectExportedModels(moduleValue: unknown): ModelMetadataLike[] {\n if (!moduleValue || typeof moduleValue !== 'object') {\n return [];\n }\n\n const models: ModelMetadataLike[] = [];\n for (const value of Object.values(moduleValue as Record<string, unknown>)) {\n if (isModelContainerLike(value)) {\n models.push(value.metadata);\n continue;\n }\n\n if (!value || typeof value !== 'object') {\n continue;\n }\n\n for (const nestedValue of Object.values(value as Record<string, unknown>)) {\n if (isModelContainerLike(nestedValue)) {\n models.push(nestedValue.metadata);\n }\n }\n }\n\n return models;\n}\n\nasync function loadModels(modelsPath: string): Promise<LoadedModelsResult> {\n const {\n loaded: mod,\n registry,\n modelTypeAccessors,\n } = await loadProjectModule(modelsPath, {\n projectRoot: process.cwd(),\n outputDir: resolve(process.cwd(), GENERATED_RELATION_REGISTRY_DIRNAME),\n });\n const moduleValue = (mod.default ?? mod) as unknown;\n\n const models = isModelContainerLike(moduleValue)\n ? [moduleValue.metadata, ...collectExportedModels(moduleValue)]\n : collectExportedModels(moduleValue);\n\n if (models.length === 0) {\n throw new Error(`No models found in '${modelsPath}'. Ensure the module exports Model() definitions.`);\n }\n\n return {\n models,\n registry,\n modelTypeAccessors,\n };\n}\n\nasync function connectAndIntrospect(dbUrl: string, dialect: string): Promise<DbSchema> {\n const dbClient = await connectDbClient(dbUrl, dialect as Dialect);\n\n try {\n const strategy = createDefaultIntrospectorStrategy();\n return await strategy.introspect(dialect as Dialect, dbClient);\n } finally {\n await dbClient.close();\n }\n}\n\nasync function connectDbClient(db: string, dialect: Dialect): Promise<CliDbClient> {\n if (dialect === InternalDialect.POSTGRES) {\n const pg = await import('pg');\n const client = new pg.default.Client({ connectionString: db });\n await client.connect();\n\n return {\n async query<T = unknown>(sql: string, params?: readonly unknown[]): Promise<{ rows: T[] }> {\n const result = await client.query(sql, params as unknown[] | undefined);\n return { rows: result.rows as T[] };\n },\n async close(): Promise<void> {\n await client.end();\n },\n };\n }\n\n if (dialect === InternalDialect.SQLITE) {\n const sqlite = await import('better-sqlite3');\n const DatabaseCtor = (sqlite.default ?? sqlite) as new (filename: string) => {\n prepare(sql: string): {\n all(...params: unknown[]): unknown[];\n run(...params: unknown[]): unknown;\n };\n close(): void;\n };\n\n const filename = normalizeSqliteFilename(db);\n await ensureSqliteParentDirectory(filename);\n const connection = new DatabaseCtor(filename);\n\n return {\n async query<T = unknown>(sql: string, params?: readonly unknown[]): Promise<{ rows: T[] }> {\n const statement = connection.prepare(sql);\n const values = [...(params ?? [])];\n const isSelectLike = /^\\s*(SELECT|PRAGMA|WITH)\\b/i.test(sql);\n if (isSelectLike) {\n return { rows: statement.all(...values) as T[] };\n }\n\n statement.run(...values);\n return { rows: [] as T[] };\n },\n async close(): Promise<void> {\n connection.close();\n },\n };\n }\n\n throw new Error(`Unsupported dialect: ${dialect}`);\n}\n\nasync function ensureSqliteParentDirectory(filename: string): Promise<void> {\n if (filename === ':memory:' || filename === 'file::memory:') {\n return;\n }\n const directory = dirname(filename);\n if (directory === '.' || directory.length === 0) {\n return;\n }\n await mkdir(directory, { recursive: true });\n}\n\nfunction normalizeSqliteFilename(db: string): string {\n if (db.startsWith('sqlite://')) {\n return db.slice('sqlite://'.length);\n }\n return db;\n}\n\n/**\n * Register Tango's migration commands on an existing yargs parser.\n */\nexport function registerMigrationsCommands(yargsBuilder: Argv): Argv {\n return yargsBuilder\n .command(\n 'migrate',\n 'Apply pending migrations to the database',\n (builder) =>\n builder\n .option('dir', {\n type: 'string',\n describe: 'Migrations directory',\n })\n .option('db', {\n type: 'string',\n describe: 'Database connection URL',\n })\n .option('dialect', {\n type: 'string',\n choices: ['postgres', 'sqlite'] as const,\n describe: 'Database dialect',\n })\n .option('config', {\n type: 'string',\n describe: 'Path to tango.config.ts (auto-loads ./tango.config.ts when present)',\n })\n .option('env', {\n type: 'string',\n choices: ['development', 'test', 'production'] as const,\n describe: 'Config environment override',\n })\n .option('to', {\n type: 'string',\n describe: 'Target migration ID (apply up to this migration)',\n }),\n async (argv) => {\n const resolved = await resolveCommandInputs({\n dialect: argv.dialect as string | undefined,\n dir: argv.dir as string | undefined,\n db: argv.db as string | undefined,\n config: argv.config as string | undefined,\n env: argv.env as ConfigEnvironment | undefined,\n });\n\n if (!resolved.autoApply) {\n logger.info('Auto-migration disabled (autoApply: false). Skipping.');\n return;\n }\n\n if (!resolved.db) {\n throw new Error('No database target provided. Pass --db or define db settings in tango.config.ts.');\n }\n\n const dbClient = await connectDbClient(resolved.db, resolved.dialect);\n\n const runner = new MigrationRunner(dbClient, resolved.dialect, resolved.dir);\n await runner.apply(argv.to);\n\n await dbClient.close();\n logger.info('Migrations applied successfully');\n }\n )\n .command(\n 'make:migrations',\n 'Generate migration file by comparing models to database',\n (builder) =>\n builder\n .option('dir', {\n type: 'string',\n describe: 'Migrations directory',\n })\n .option('name', {\n type: 'string',\n demandOption: true,\n describe: 'Migration name (e.g. \"create_users\")',\n })\n .option('models', {\n type: 'string',\n demandOption: true,\n describe: 'Path to module exporting Model definitions (e.g. \"./src/models.ts\")',\n })\n .option('db', {\n type: 'string',\n describe: 'Database connection URL for introspection (omit for initial migration)',\n })\n .option('dialect', {\n type: 'string',\n choices: ['postgres', 'sqlite'] as const,\n describe: 'Database dialect',\n })\n .option('config', {\n type: 'string',\n describe: 'Path to tango.config.ts (auto-loads ./tango.config.ts when present)',\n })\n .option('env', {\n type: 'string',\n choices: ['development', 'test', 'production'] as const,\n describe: 'Config environment override',\n }),\n async (argv) => {\n const resolved = await resolveCommandInputs({\n dialect: argv.dialect as string | undefined,\n dir: argv.dir as string | undefined,\n db: argv.db as string | undefined,\n config: argv.config as string | undefined,\n env: argv.env as ConfigEnvironment | undefined,\n });\n const loaded = await loadModels(argv.models);\n const projectedModels = buildMigrationModelMetadataProjection(loaded.registry, {\n dialect: resolved.dialect,\n });\n logger.info(\n `Found ${loaded.models.length} exported model(s); ${projectedModels.length} model(s) after projection: ${projectedModels.map((m) => m.table).join(', ')}`\n );\n try {\n await writeRelationRegistryArtifacts({\n registry: loaded.registry,\n modelTypeAccessors: loaded.modelTypeAccessors,\n outputDir: resolve(process.cwd(), GENERATED_RELATION_REGISTRY_DIRNAME),\n });\n } catch (error) {\n logger.warn(\n `Unable to refresh generated relation registry during make:migrations. Continuing without updated relation artifacts: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n\n let dbState: DbSchema;\n if (resolved.db) {\n logger.info('Introspecting database...');\n dbState = await connectAndIntrospect(resolved.db, resolved.dialect);\n } else {\n dbState = { tables: {} };\n }\n\n const operations = diffSchema(dbState, projectedModels);\n\n if (operations.length === 0) {\n logger.info('No changes detected — models and database are in sync');\n return;\n }\n\n const generator = new MigrationGenerator();\n const filepath = await generator.generate({\n name: argv.name,\n operations,\n directory: resolved.dir,\n });\n\n logger.info(`Generated migration: ${filepath}`);\n logger.info(` ${operations.length} operation(s)`);\n }\n )\n .command(\n 'plan',\n 'Print migration SQL without applying',\n (builder) =>\n builder\n .option('dir', {\n type: 'string',\n describe: 'Migrations directory',\n })\n .option('dialect', {\n type: 'string',\n choices: ['postgres', 'sqlite'] as const,\n describe: 'Database dialect',\n })\n .option('config', {\n type: 'string',\n describe: 'Path to tango.config.ts (auto-loads ./tango.config.ts when present)',\n })\n .option('env', {\n type: 'string',\n choices: ['development', 'test', 'production'] as const,\n describe: 'Config environment override',\n }),\n async (argv) => {\n const resolved = await resolveCommandInputs({\n dialect: argv.dialect as string | undefined,\n dir: argv.dir as string | undefined,\n db: argv.db as string | undefined,\n config: argv.config as string | undefined,\n env: argv.env as ConfigEnvironment | undefined,\n });\n const runner = new MigrationRunner(\n { query: async () => ({ rows: [] }), close: async () => {} },\n resolved.dialect,\n resolved.dir\n );\n const output = await runner.plan();\n logger.info(output);\n }\n )\n .command(\n 'status',\n 'Show applied/pending status of all migrations',\n (builder) =>\n builder\n .option('dir', {\n type: 'string',\n describe: 'Migrations directory',\n })\n .option('db', {\n type: 'string',\n describe: 'Database connection URL',\n })\n .option('dialect', {\n type: 'string',\n choices: ['postgres', 'sqlite'] as const,\n describe: 'Database dialect',\n })\n .option('config', {\n type: 'string',\n describe: 'Path to tango.config.ts (auto-loads ./tango.config.ts when present)',\n })\n .option('env', {\n type: 'string',\n choices: ['development', 'test', 'production'] as const,\n describe: 'Config environment override',\n }),\n async (argv) => {\n const resolved = await resolveCommandInputs({\n dialect: argv.dialect as string | undefined,\n dir: argv.dir as string | undefined,\n db: argv.db as string | undefined,\n config: argv.config as string | undefined,\n env: argv.env as ConfigEnvironment | undefined,\n });\n if (!resolved.db) {\n throw new Error('No database target provided. Pass --db or define db settings in tango.config.ts.');\n }\n\n const dbClient = await connectDbClient(resolved.db, resolved.dialect);\n\n const runner = new MigrationRunner(dbClient, resolved.dialect, resolved.dir);\n const statuses = await runner.status();\n\n if (statuses.length === 0) {\n logger.info('No migrations found');\n } else {\n statuses.forEach((statusItem) => {\n const marker = statusItem.applied ? '[x]' : '[ ]';\n logger.info(` ${marker} ${statusItem.id}`);\n });\n }\n\n await dbClient.close();\n }\n );\n}\n"],"mappings":";;;;;;;;;;;;;;AAqBA,MAAM,SAAS,UAAU,kBAAkB;AA6C3C,eAAe,aAAa,YAAsD;CAC9E,OAAO,WAAW,YAAY,EAAE,aAAa,QAAQ,IAAI,EAAE,CAAC;AAChE;AAEA,eAAe,yBACX,eACA,cACkC;CAClC,MAAM,qBAAqB,OAAO,kBAAkB,YAAY,cAAc,KAAK,EAAE,SAAS;CAC9F,MAAM,eAAe,QAAQ,QAAQ,IAAI,GAAG,eAAe,KAAK,KAAK,mBAAmB;CAExF,IAAI;EACA,MAAM,OAAO,cAAcA,UAAY,IAAI;CAC/C,SAAS,OAAO;EACZ,IAAI,oBACA,MAAM,IAAI,MAAM,0BAA0B,gBAAgB,EAAE,OAAO,MAAM,CAAC;EAE9E,OAA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/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as __exportAll } from "../chunk-D7D4PA-g.js";
|
|
2
|
-
import { t as registerMigrationsCommands } from "../cli-
|
|
2
|
+
import { t as registerMigrationsCommands } from "../cli-Dhp2BZsB.js";
|
|
3
3
|
//#region src/commands/index.ts
|
|
4
4
|
var commands_exports = /* @__PURE__ */ __exportAll({ registerMigrationsCommands: () => registerMigrationsCommands });
|
|
5
5
|
//#endregion
|
package/dist/diff/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as buildMigrationModelMetadataProjection, r as diffSchema } from "../index-
|
|
1
|
+
import { n as buildMigrationModelMetadataProjection, r as diffSchema } from "../index-i1MsTVXm.js";
|
|
2
2
|
export { buildMigrationModelMetadataProjection, diffSchema };
|
package/dist/diff/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as buildMigrationModelMetadataProjection, r as diffSchema } from "../diff-
|
|
1
|
+
import { n as buildMigrationModelMetadataProjection, r as diffSchema } from "../diff-DdwU_MyI.js";
|
|
2
2
|
export { buildMigrationModelMetadataProjection, diffSchema };
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { t as __exportAll } from "./chunk-D7D4PA-g.js";
|
|
2
2
|
import { a as applyFieldType, i as OpBuilder } from "./builder-Bhm_to6b.js";
|
|
3
|
+
import { n as InternalColumnType } from "./InternalOperationKind-BJ7n-pUH.js";
|
|
4
|
+
import { t as InternalDialect } from "./InternalDialect-DDW2l3mz.js";
|
|
3
5
|
import { trustedSql } from "@danceroutine/tango-core";
|
|
4
6
|
//#region src/diff/diffSchema.ts
|
|
5
7
|
/**
|
|
@@ -75,20 +77,48 @@ function diffSchema(db, models) {
|
|
|
75
77
|
return ops;
|
|
76
78
|
}
|
|
77
79
|
//#endregion
|
|
78
|
-
//#region src/schema/
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
80
|
+
//#region src/schema/field/strategy/ModelFieldMapperStrategy.ts
|
|
81
|
+
var ModelFieldMapperStrategy = class {
|
|
82
|
+
mapField(field) {
|
|
83
|
+
return {
|
|
84
|
+
name: field.name,
|
|
85
|
+
type: this.mapType(field),
|
|
86
|
+
notNull: field.notNull,
|
|
87
|
+
default: field.default,
|
|
88
|
+
primaryKey: field.primaryKey,
|
|
89
|
+
unique: field.unique,
|
|
90
|
+
references: field.references
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
mapType(field) {
|
|
94
|
+
return field.type;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region src/schema/field/strategy/PostgresModelFieldMapperStrategy.ts
|
|
99
|
+
var PostgresModelFieldMapperStrategy = class extends ModelFieldMapperStrategy {
|
|
100
|
+
mapType(field) {
|
|
101
|
+
if (field.primaryKey && field.type === InternalColumnType.INT) return InternalColumnType.SERIAL;
|
|
102
|
+
return super.mapType(field);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region src/schema/field/strategy/SqliteModelFieldMapperStrategy.ts
|
|
107
|
+
var SqliteModelFieldMapperStrategy = class extends ModelFieldMapperStrategy {};
|
|
108
|
+
//#endregion
|
|
109
|
+
//#region src/schema/field/strategy/createModelFieldMapperStrategy.ts
|
|
110
|
+
function createModelFieldMapperStrategy(dialect) {
|
|
111
|
+
switch (dialect) {
|
|
112
|
+
case void 0:
|
|
113
|
+
case InternalDialect.SQLITE: return new SqliteModelFieldMapperStrategy();
|
|
114
|
+
case InternalDialect.POSTGRES: return new PostgresModelFieldMapperStrategy();
|
|
115
|
+
}
|
|
89
116
|
}
|
|
90
|
-
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/schema/buildMigrationModelMetadataProjection.ts
|
|
119
|
+
function buildMigrationModelMetadataProjection(registry, options) {
|
|
91
120
|
registry.finalizeStorageArtifacts();
|
|
121
|
+
const fieldMapperStrategy = createModelFieldMapperStrategy(options?.dialect);
|
|
92
122
|
const projection = [];
|
|
93
123
|
for (const model of registry.values()) {
|
|
94
124
|
const finalized = registry.getFinalizedFields(model.metadata.key);
|
|
@@ -96,7 +126,7 @@ function buildMigrationModelMetadataProjection(registry) {
|
|
|
96
126
|
name: model.metadata.name,
|
|
97
127
|
table: model.metadata.table,
|
|
98
128
|
managed: model.metadata.managed ?? true,
|
|
99
|
-
fields: finalized.map(
|
|
129
|
+
fields: finalized.map((field) => fieldMapperStrategy.mapField(field)),
|
|
100
130
|
indexes: model.metadata.indexes?.map((index) => ({
|
|
101
131
|
name: index.name,
|
|
102
132
|
on: [...index.on],
|
|
@@ -115,4 +145,4 @@ var diff_exports = /* @__PURE__ */ __exportAll({
|
|
|
115
145
|
//#endregion
|
|
116
146
|
export { buildMigrationModelMetadataProjection as n, diffSchema as r, diff_exports as t };
|
|
117
147
|
|
|
118
|
-
//# sourceMappingURL=diff-
|
|
148
|
+
//# sourceMappingURL=diff-DdwU_MyI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diff-DdwU_MyI.js","names":["op"],"sources":["../src/diff/diffSchema.ts","../src/schema/field/strategy/ModelFieldMapperStrategy.ts","../src/schema/field/strategy/PostgresModelFieldMapperStrategy.ts","../src/schema/field/strategy/SqliteModelFieldMapperStrategy.ts","../src/schema/field/strategy/createModelFieldMapperStrategy.ts","../src/schema/buildMigrationModelMetadataProjection.ts","../src/diff/index.ts"],"sourcesContent":["import { trustedSql } from '@danceroutine/tango-core';\nimport type { DbSchema } from '../introspect/PostgresIntrospector';\nimport type { MigrationOperation } from '../domain/MigrationOperation';\nimport type { ColumnType } from '../builder/contracts/ColumnType';\nimport type { DeleteReferentialAction } from '../builder/contracts/DeleteReferentialAction';\nimport type { UpdateReferentialAction } from '../builder/contracts/UpdateReferentialAction';\nimport { op, applyFieldType } from '../builder/index';\n\ntype ModelField = {\n name: string;\n type: ColumnType;\n notNull?: boolean;\n default?: string | { now: true } | null;\n primaryKey?: boolean;\n unique?: boolean;\n references?: {\n table: string;\n column: string;\n onDelete?: DeleteReferentialAction;\n onUpdate?: UpdateReferentialAction;\n };\n};\n\ntype ModelIndex = {\n name: string;\n on: string[];\n unique?: boolean;\n};\n\nexport type ModelMetadataLike = {\n name?: string;\n table: string;\n fields: ModelField[];\n indexes?: ModelIndex[];\n managed?: boolean;\n};\n\n/**\n * Compare model metadata with an introspected database schema and return the\n * operations needed to bring the database into alignment.\n */\nexport function diffSchema(db: DbSchema, models: ModelMetadataLike[]): MigrationOperation[] {\n const ops: MigrationOperation[] = [];\n models.forEach((model) => {\n if (model.managed === false) {\n return;\n }\n\n const dbTable = db.tables[model.table];\n\n if (!dbTable) {\n ops.push(\n op.table(model.table).create((cols) => {\n model.fields.forEach((field) => {\n cols.add(field.name, (builder) => {\n builder = applyFieldType(builder, field.type);\n\n if (field.notNull) {\n builder = builder.notNull();\n }\n\n if (field.default === null) {\n builder = builder.default(null);\n } else if (field.default && typeof field.default === 'object' && 'now' in field.default) {\n builder = builder.defaultNow();\n } else if (typeof field.default === 'string') {\n builder = builder.default(trustedSql(field.default));\n }\n\n if (field.primaryKey) {\n builder = builder.primaryKey();\n }\n\n if (field.unique) {\n builder = builder.unique();\n }\n\n if (field.references) {\n builder = builder.references(field.references.table, field.references.column, {\n onDelete: field.references.onDelete,\n onUpdate: field.references.onUpdate,\n });\n }\n\n return builder;\n });\n });\n })\n );\n\n (model.indexes ?? []).forEach((index) => {\n ops.push(\n op.index.create({\n name: index.name,\n table: model.table,\n on: index.on,\n unique: !!index.unique,\n })\n );\n });\n return;\n }\n\n const modelFieldNames = new Set(model.fields.map((field) => field.name));\n const dbFieldNames = new Set(Object.keys(dbTable.columns));\n\n model.fields.forEach((field) => {\n if (!dbFieldNames.has(field.name)) {\n ops.push(\n op.table(model.table).addColumn(field.name, (builder) => {\n builder = applyFieldType(builder, field.type);\n\n if (field.notNull) {\n builder = builder.notNull();\n }\n if (field.default === null) {\n builder = builder.default(null);\n } else if (field.default && typeof field.default === 'object' && 'now' in field.default) {\n builder = builder.defaultNow();\n } else if (typeof field.default === 'string') {\n builder = builder.default(trustedSql(field.default));\n }\n if (field.primaryKey) {\n builder = builder.primaryKey();\n }\n if (field.unique) {\n builder = builder.unique();\n }\n if (field.references) {\n builder = builder.references(field.references.table, field.references.column, {\n onDelete: field.references.onDelete,\n onUpdate: field.references.onUpdate,\n });\n }\n\n return builder;\n })\n );\n }\n });\n\n dbFieldNames.forEach((dbColumnName) => {\n if (!modelFieldNames.has(dbColumnName)) {\n ops.push(op.table(model.table).dropColumn(dbColumnName));\n }\n });\n\n const modelIndexes = new Map((model.indexes ?? []).map((index) => [index.name, index] as const));\n const dbIndexNames = new Set(Object.keys(dbTable.indexes));\n\n modelIndexes.forEach((index, indexName) => {\n if (!dbIndexNames.has(indexName)) {\n ops.push(\n op.index.create({\n name: index.name,\n table: model.table,\n on: index.on,\n unique: !!index.unique,\n })\n );\n }\n });\n\n // Index drift is intentionally conservative. Extra indexes in the live\n // database may be app-owned or dialect-managed, so migrations only add\n // missing declared indexes and avoid destructive index drops here.\n });\n\n return ops;\n}\n","import type { Field } from '@danceroutine/tango-schema/domain';\nimport type { ColumnType } from '../../../builder/contracts/ColumnType';\nimport type { ModelMetadataLike } from '../../../diff/diffSchema';\n\nexport class ModelFieldMapperStrategy {\n mapField(field: Field): ModelMetadataLike['fields'][number] {\n return {\n name: field.name,\n type: this.mapType(field),\n notNull: field.notNull,\n default: field.default,\n primaryKey: field.primaryKey,\n unique: field.unique,\n references: field.references as ModelMetadataLike['fields'][number]['references'],\n };\n }\n\n protected mapType(field: Field): ColumnType {\n return field.type as ColumnType;\n }\n}\n","import type { Field } from '@danceroutine/tango-schema/domain';\nimport type { ColumnType } from '../../../builder/contracts/ColumnType';\nimport { InternalColumnType } from '../../../domain/internal/InternalColumnType';\nimport { ModelFieldMapperStrategy } from './ModelFieldMapperStrategy';\n\nexport class PostgresModelFieldMapperStrategy extends ModelFieldMapperStrategy {\n protected override mapType(field: Field): ColumnType {\n // Postgres auto-incrementing primary keys are modeled as serial in migrations,\n // while SQLite must preserve the schema-level integer type to avoid churn.\n if (field.primaryKey && field.type === InternalColumnType.INT) {\n return InternalColumnType.SERIAL;\n }\n\n return super.mapType(field);\n }\n}\n","import { ModelFieldMapperStrategy } from './ModelFieldMapperStrategy';\n\nexport class SqliteModelFieldMapperStrategy extends ModelFieldMapperStrategy {}\n","import type { Dialect } from '../../../domain/Dialect';\nimport { InternalDialect } from '../../../domain/internal/InternalDialect';\nimport { ModelFieldMapperStrategy } from './ModelFieldMapperStrategy';\nimport { PostgresModelFieldMapperStrategy } from './PostgresModelFieldMapperStrategy';\nimport { SqliteModelFieldMapperStrategy } from './SqliteModelFieldMapperStrategy';\n\nexport function createModelFieldMapperStrategy(dialect?: Dialect): ModelFieldMapperStrategy {\n switch (dialect) {\n case undefined:\n case InternalDialect.SQLITE:\n return new SqliteModelFieldMapperStrategy();\n case InternalDialect.POSTGRES:\n return new PostgresModelFieldMapperStrategy();\n }\n}\n","import type { ModelRegistry } from '@danceroutine/tango-schema';\nimport type { Dialect } from '../domain/Dialect';\nimport type { ModelMetadataLike } from '../diff/diffSchema';\nimport { createModelFieldMapperStrategy } from './field/strategy/createModelFieldMapperStrategy';\n\nexport type BuildMigrationModelMetadataProjectionOptions = {\n dialect?: Dialect;\n};\n\nexport function buildMigrationModelMetadataProjection(\n registry: ModelRegistry,\n options?: BuildMigrationModelMetadataProjectionOptions\n): ModelMetadataLike[] {\n registry.finalizeStorageArtifacts();\n const fieldMapperStrategy = createModelFieldMapperStrategy(options?.dialect);\n const projection: ModelMetadataLike[] = [];\n for (const model of registry.values()) {\n const finalized = registry.getFinalizedFields(model.metadata.key);\n projection.push({\n name: model.metadata.name,\n table: model.metadata.table,\n managed: model.metadata.managed ?? true,\n fields: finalized.map((field) => fieldMapperStrategy.mapField(field)),\n indexes: model.metadata.indexes?.map((index) => ({\n name: index.name,\n on: [...index.on],\n unique: index.unique,\n })),\n });\n }\n return projection;\n}\n","/**\n * Domain boundary barrel: centralizes this subdomain's public contract.\n */\n\nexport { diffSchema } from './diffSchema';\nexport { buildMigrationModelMetadataProjection } from '../schema/buildMigrationModelMetadataProjection';\n"],"mappings":";;;;;;;;;;AAyCA,SAAgB,WAAW,IAAc,QAAmD;CACxF,MAAM,MAA4B,CAAC;CACnC,OAAO,SAAS,UAAU;EACtB,IAAI,MAAM,YAAY,OAClB;EAGJ,MAAM,UAAU,GAAG,OAAO,MAAM;EAEhC,IAAI,CAAC,SAAS;GACV,IAAI,KACAA,UAAG,MAAM,MAAM,KAAK,EAAE,QAAQ,SAAS;IACnC,MAAM,OAAO,SAAS,UAAU;KAC5B,KAAK,IAAI,MAAM,OAAO,YAAY;MAC9B,UAAU,eAAe,SAAS,MAAM,IAAI;MAE5C,IAAI,MAAM,SACN,UAAU,QAAQ,QAAQ;MAG9B,IAAI,MAAM,YAAY,MAClB,UAAU,QAAQ,QAAQ,IAAI;WAC3B,IAAI,MAAM,WAAW,OAAO,MAAM,YAAY,YAAY,SAAS,MAAM,SAC5E,UAAU,QAAQ,WAAW;WAC1B,IAAI,OAAO,MAAM,YAAY,UAChC,UAAU,QAAQ,QAAQ,WAAW,MAAM,OAAO,CAAC;MAGvD,IAAI,MAAM,YACN,UAAU,QAAQ,WAAW;MAGjC,IAAI,MAAM,QACN,UAAU,QAAQ,OAAO;MAG7B,IAAI,MAAM,YACN,UAAU,QAAQ,WAAW,MAAM,WAAW,OAAO,MAAM,WAAW,QAAQ;OAC1E,UAAU,MAAM,WAAW;OAC3B,UAAU,MAAM,WAAW;MAC/B,CAAC;MAGL,OAAO;KACX,CAAC;IACL,CAAC;GACL,CAAC,CACL;GAEA,CAAC,MAAM,WAAW,CAAC,GAAG,SAAS,UAAU;IACrC,IAAI,KACAA,UAAG,MAAM,OAAO;KACZ,MAAM,MAAM;KACZ,OAAO,MAA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C,QAAQ,UAAU,KAAK,UAAU,oBAAoB,SAAS,KAAK,CAAC;GACpE,SAAS,MAAM,SAAS,SAAS,KAAK,WAAW;IAC7C,MAAM,MAAM;IACZ,IAAI,CAAC,GAAG,MAAM,EAAE;IAChB,QAAQ,MAAM;GAClB,EAAE;EACN,CAAC;CACL;CACA,OAAO;AACX"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { _ as ColumnType, d as MigrationOperation, g as DeleteReferentialAction, h as UpdateReferentialAction } from "./MigrationOperation-qpdhPEs9.js";
|
|
2
2
|
import { i as DbSchema } from "./PostgresIntrospector-DQDTZUW_.js";
|
|
3
|
+
import { t as Dialect } from "./Dialect-Cp4r7UfW.js";
|
|
3
4
|
import { ModelRegistry } from "@danceroutine/tango-schema";
|
|
4
5
|
|
|
5
6
|
//#region src/diff/diffSchema.d.ts
|
|
@@ -38,10 +39,13 @@ type ModelMetadataLike = {
|
|
|
38
39
|
declare function diffSchema(db: DbSchema, models: ModelMetadataLike[]): MigrationOperation[];
|
|
39
40
|
//#endregion
|
|
40
41
|
//#region src/schema/buildMigrationModelMetadataProjection.d.ts
|
|
41
|
-
|
|
42
|
+
type BuildMigrationModelMetadataProjectionOptions = {
|
|
43
|
+
dialect?: Dialect;
|
|
44
|
+
};
|
|
45
|
+
declare function buildMigrationModelMetadataProjection(registry: ModelRegistry, options?: BuildMigrationModelMetadataProjectionOptions): ModelMetadataLike[];
|
|
42
46
|
declare namespace index_d_exports {
|
|
43
47
|
export { buildMigrationModelMetadataProjection, diffSchema };
|
|
44
48
|
}
|
|
45
49
|
//#endregion
|
|
46
50
|
export { buildMigrationModelMetadataProjection as n, diffSchema as r, index_d_exports as t };
|
|
47
|
-
//# sourceMappingURL=index-
|
|
51
|
+
//# sourceMappingURL=index-i1MsTVXm.d.ts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { n as registerMigrationsCommands, t as index_d_exports$1 } from "./index
|
|
|
5
5
|
import { n as SQLCompiler, r as SQL, t as CompilerFactory } from "./CompilerFactory-Czv-zEOS.js";
|
|
6
6
|
import { o as SqliteCompiler, s as PostgresCompiler, t as index_d_exports$2 } from "./index-CsTGwtZ0.js";
|
|
7
7
|
import { a as DbTable, c as DatabaseIntrospector, i as DbSchema, n as DbForeignKey, o as PostgresIntrospector, r as DbIndex, s as DBClient, t as DbColumn } from "./PostgresIntrospector-DQDTZUW_.js";
|
|
8
|
-
import { r as diffSchema, t as index_d_exports$3 } from "./index-
|
|
8
|
+
import { r as diffSchema, t as index_d_exports$3 } from "./index-i1MsTVXm.js";
|
|
9
9
|
import { t as Dialect } from "./Dialect-Cp4r7UfW.js";
|
|
10
10
|
import { n as Migration, t as index_d_exports$4 } from "./index-DdCF5yCg.js";
|
|
11
11
|
import { n as GenerateMigrationOptions, r as MigrationGenerator, t as index_d_exports$5 } from "./index-Dcuh1zcQ.js";
|
package/dist/index.js
CHANGED
|
@@ -3,18 +3,18 @@ import { t as domain_exports } from "./domain/index.js";
|
|
|
3
3
|
import { a as applyFieldType, i as OpBuilder, t as builder_exports } from "./builder-Bhm_to6b.js";
|
|
4
4
|
import { t as CollectingBuilder } from "./CollectingBuilder-BIfAKs_x.js";
|
|
5
5
|
import { i as PostgresCompiler, n as SqliteCompiler } from "./SqliteCompilerFactory-C_56xoQM.js";
|
|
6
|
-
import { n as createDefaultCompilerStrategy, t as CompilerStrategy } from "./CompilerStrategy-
|
|
7
|
-
import { t as MigrationRunner } from "./MigrationRunner-
|
|
6
|
+
import { n as createDefaultCompilerStrategy, t as CompilerStrategy } from "./CompilerStrategy-CosQJr-g.js";
|
|
7
|
+
import { t as MigrationRunner } from "./MigrationRunner-DCCeU1kq.js";
|
|
8
8
|
import { t as runner_exports } from "./runner/index.js";
|
|
9
9
|
import { t as MigrationGenerator } from "./MigrationGenerator-D-sSbsFG.js";
|
|
10
10
|
import { t as generator_exports } from "./generator/index.js";
|
|
11
|
-
import { r as diffSchema, t as diff_exports } from "./diff-
|
|
11
|
+
import { r as diffSchema, t as diff_exports } from "./diff-DdwU_MyI.js";
|
|
12
12
|
import { t as compilers_exports } from "./compilers-BlLQwwmu.js";
|
|
13
13
|
import { n as PostgresIntrospector, t as SqliteIntrospector } from "./SqliteIntrospector-CfItmGgA.js";
|
|
14
14
|
import { t as introspect_exports } from "./introspect/index.js";
|
|
15
|
-
import { n as createDefaultIntrospectorStrategy, t as IntrospectorStrategy } from "./IntrospectorStrategy-
|
|
15
|
+
import { n as createDefaultIntrospectorStrategy, t as IntrospectorStrategy } from "./IntrospectorStrategy-D_9umttn.js";
|
|
16
16
|
import { t as strategies_exports } from "./strategies/index.js";
|
|
17
|
-
import { t as registerMigrationsCommands } from "./cli-
|
|
17
|
+
import { t as registerMigrationsCommands } from "./cli-Dhp2BZsB.js";
|
|
18
18
|
import { t as commands_exports } from "./commands/index.js";
|
|
19
19
|
import { trustedSql } from "@danceroutine/tango-core";
|
|
20
20
|
export { CollectingBuilder, CompilerStrategy, IntrospectorStrategy, Migration, MigrationGenerator, MigrationRunner, OpBuilder, OpBuilder as op, PostgresCompiler, PostgresIntrospector, SqliteCompiler, SqliteIntrospector, applyFieldType, builder_exports as builder, commands_exports as commands, compilers_exports as compilers, createDefaultCompilerStrategy, createDefaultIntrospectorStrategy, diff_exports as diff, diffSchema, domain_exports as domain, generator_exports as generator, introspect_exports as introspect, registerMigrationsCommands, runner_exports as runner, strategies_exports as strategies, trustedSql };
|
package/dist/runner/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as __exportAll } from "../chunk-D7D4PA-g.js";
|
|
2
|
-
import { t as MigrationRunner } from "../MigrationRunner-
|
|
2
|
+
import { t as MigrationRunner } from "../MigrationRunner-DCCeU1kq.js";
|
|
3
3
|
//#region src/runner/index.ts
|
|
4
4
|
var runner_exports = /* @__PURE__ */ __exportAll({ MigrationRunner: () => MigrationRunner });
|
|
5
5
|
//#endregion
|
package/dist/strategies/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { t as __exportAll } from "../chunk-D7D4PA-g.js";
|
|
2
|
-
import { n as createDefaultCompilerStrategy, t as CompilerStrategy } from "../CompilerStrategy-
|
|
3
|
-
import { n as createDefaultIntrospectorStrategy, t as IntrospectorStrategy } from "../IntrospectorStrategy-
|
|
2
|
+
import { n as createDefaultCompilerStrategy, t as CompilerStrategy } from "../CompilerStrategy-CosQJr-g.js";
|
|
3
|
+
import { n as createDefaultIntrospectorStrategy, t as IntrospectorStrategy } from "../IntrospectorStrategy-D_9umttn.js";
|
|
4
4
|
//#region src/strategies/index.ts
|
|
5
5
|
var strategies_exports = /* @__PURE__ */ __exportAll({
|
|
6
6
|
CompilerStrategy: () => CompilerStrategy,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@danceroutine/tango-migrations",
|
|
3
|
-
"version": "1.11.
|
|
3
|
+
"version": "1.11.10",
|
|
4
4
|
"description": "Auto-migration system with ops DSL for Tango",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -67,10 +67,10 @@
|
|
|
67
67
|
"jiti": "^2.7.0",
|
|
68
68
|
"kleur": "^4.1.5",
|
|
69
69
|
"yargs": "^18.0.0",
|
|
70
|
-
"@danceroutine/tango-config": "1.11.
|
|
71
|
-
"@danceroutine/tango-codegen": "1.11.
|
|
72
|
-
"@danceroutine/tango-core": "1.11.
|
|
73
|
-
"@danceroutine/tango-schema": "1.11.
|
|
70
|
+
"@danceroutine/tango-config": "1.11.10",
|
|
71
|
+
"@danceroutine/tango-codegen": "1.11.10",
|
|
72
|
+
"@danceroutine/tango-core": "1.11.10",
|
|
73
|
+
"@danceroutine/tango-schema": "1.11.10"
|
|
74
74
|
},
|
|
75
75
|
"peerDependencies": {
|
|
76
76
|
"pg": "^8.20.0",
|
|
@@ -95,8 +95,8 @@
|
|
|
95
95
|
"typescript": "^6.0.3",
|
|
96
96
|
"vitest": "^4.1.7",
|
|
97
97
|
"zod": "^4.0.0",
|
|
98
|
-
"@danceroutine/tango-testing": "1.11.
|
|
99
|
-
"@danceroutine/tango-orm": "1.11.
|
|
98
|
+
"@danceroutine/tango-testing": "1.11.10",
|
|
99
|
+
"@danceroutine/tango-orm": "1.11.10"
|
|
100
100
|
},
|
|
101
101
|
"scripts": {
|
|
102
102
|
"build": "tsdown",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CompilerStrategy-Dc5_3-nw.js","names":[],"sources":["../src/domain/internal/InternalDialect.ts","../src/strategies/CompilerStrategy.ts"],"sourcesContent":["export const InternalDialect = {\n POSTGRES: 'postgres',\n SQLITE: 'sqlite',\n} as const;\n\nexport type InternalDialect = (typeof InternalDialect)[keyof typeof InternalDialect];\n","import { PostgresCompilerFactory } from '../compilers/factories/PostgresCompilerFactory';\nimport { SqliteCompilerFactory } from '../compilers/factories/SqliteCompilerFactory';\nimport type { CompilerFactory } from '../compilers/contracts/CompilerFactory';\nimport type { Dialect } from '../domain/Dialect';\nimport type { CustomMigrationOperation, MigrationOperation } from '../domain/MigrationOperation';\nimport type { SQL } from '../compilers/contracts/SQL';\nimport type { SQLCompiler } from '../compilers/contracts/SQLCompiler';\nimport { InternalDialect } from '../domain/internal/InternalDialect';\n\ntype CompilerFactoryRegistry = Record<Dialect, CompilerFactory>;\n\n/**\n * Dialect-aware SQL compiler orchestration with optional custom-op handlers.\n */\nexport class CompilerStrategy {\n static readonly BRAND = 'tango.migrations.compiler_strategy' as const;\n readonly __tangoBrand: typeof CompilerStrategy.BRAND = CompilerStrategy.BRAND;\n private readonly compilerCache = new Map<Dialect, SQLCompiler>();\n private readonly customHandlers = new Map<string, (dialect: Dialect, op: CustomMigrationOperation) => SQL[]>();\n\n constructor(private readonly factories: CompilerFactoryRegistry) {}\n\n /**\n * Narrow an unknown value to the dialect-aware migration compiler strategy.\n */\n static isCompilerStrategy(value: unknown): value is CompilerStrategy {\n return (\n typeof value === 'object' &&\n value !== null &&\n (value as { __tangoBrand?: unknown }).__tangoBrand === CompilerStrategy.BRAND\n );\n }\n\n /**\n * Prepare a batch of migration operations for a target dialect.\n */\n prepareOperations(dialect: Dialect, operations: MigrationOperation[]): MigrationOperation[] {\n const compiler = this.getCompiler(dialect);\n return compiler.prepareOperations ? compiler.prepareOperations(operations) : operations;\n }\n\n /**\n * Compile a migration operation to SQL for a target dialect.\n */\n compile(dialect: Dialect, operation: MigrationOperation): SQL[] {\n if (operation.kind === 'custom') {\n const handler = this.customHandlers.get(operation.name);\n if (!handler) {\n throw new Error(`Unsupported custom migration op: ${operation.name}`);\n }\n return handler(dialect, operation);\n }\n const compiler = this.getCompiler(dialect);\n return compiler.compile(operation);\n }\n\n /**\n * Register a handler for custom migration operations.\n */\n registerCustomHandler<TName extends string, TArgs extends object>(\n name: TName,\n handler: (dialect: Dialect, op: CustomMigrationOperation<TName, TArgs>) => SQL[]\n ): this {\n this.customHandlers.set(name, handler as (dialect: Dialect, op: CustomMigrationOperation) => SQL[]);\n return this;\n }\n\n /**\n * Resolve and cache a compiler instance for a dialect.\n */\n getCompiler(dialect: Dialect): SQLCompiler {\n const cached = this.compilerCache.get(dialect);\n if (cached) {\n return cached;\n }\n\n const factory = this.factories[dialect];\n if (!factory) {\n throw new Error(`No SQL compiler factory registered for dialect: ${String(dialect)}`);\n }\n const compiler = factory.create();\n this.compilerCache.set(dialect, compiler);\n return compiler;\n }\n}\n\n/**\n * Create the default compiler strategy with built-in dialect factories.\n */\nexport function createDefaultCompilerStrategy(): CompilerStrategy {\n return new CompilerStrategy({\n [InternalDialect.POSTGRES]: new PostgresCompilerFactory(),\n [InternalDialect.SQLITE]: new SqliteCompilerFactory(),\n });\n}\n"],"mappings":";;AAAA,MAAa,kBAAkB;CAC3B,UAAU;CACV,QAAQ;AACZ;;;;;;ACWA,IAAa,mBAAb,MAAa,iBAAiB;CAMG;CAL7B,OAAgB,QAAQ;CACxB,eAAuD,iBAAiB;CACxE,gCAAiC,IAAI,IAA0B;CAC/D,iCAAkC,IAAI,IAAuE;CAE7G,YAAY,WAAqD;EAApC,KAAA,YAAA;CAAqC;;;;CAKlE,OAAO,mBAAmB,OAA2C;EACjE,OACI,OAAO,UAAU,YACjB,UAAU,QACT,MAAqC,iBAAiB,iBAAiB;CAEhF;;;;CAKA,kBAAkB,SAAkB,YAAwD;EACxF,MAAM,WAAW,KAAK,YAAY,OAAO;EACzC,OAAO,SAAS,oBAAoB,SAAS,kBAAkB,UAAU,IAAI;CACjF;;;;CAKA,QAAQ,SAAkB,WAAsC;EAC5D,IAAI,UAAU,SAAS,UAAU;GAC7B,MAAM,UAAU,KAAK,eAAe,IAAI,UAAU,IAAI;GACtD,IAAI,CAAC,SACD,MAAM,IAAI,MAAM,oCAAoC,UAAU,MAAM;GAExE,OAAO,QAAQ,SAAS,SAAS;EACrC;EAEA,OADiB,KAAK,YAAY,OACpB,EAAE,QAAQ,SAAS;CACrC;;;;CAKA,sBACI,MACA,SACI;EACJ,KAAK,eAAe,IAAI,MAAM,OAAoE;EAClG,OAAO;CACX;;;;CAKA,YAAY,SAA+B;EACvC,MAAM,SAAS,KAAK,cAAc,IAAI,OAAO;EAC7C,IAAI,QACA,OAAO;EAGX,MAAM,UAAU,KAAK,UAAU;EAC/B,IAAI,CAAC,SACD,MAAM,IAAI,MAAM,mDAAmD,OAAO,OAAO,GAAG;EAExF,MAAM,WAAW,QAAQ,OAAO;EAChC,KAAK,cAAc,IAAI,SAAS,QAAQ;EACxC,OAAO;CACX;AACJ;;;;AAKA,SAAgB,gCAAkD;CAC9D,OAAO,IAAI,iBAAiB;GACvB,gBAAgB,WAAW,IAAI,wBAAwB;GACvD,gBAAgB,SAAS,IAAI,sBAAsB;CACxD,CAAC;AACL"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"diff-BsQr9Sl8.js","names":["op"],"sources":["../src/diff/diffSchema.ts","../src/schema/buildMigrationModelMetadataProjection.ts","../src/diff/index.ts"],"sourcesContent":["import { trustedSql } from '@danceroutine/tango-core';\nimport type { DbSchema } from '../introspect/PostgresIntrospector';\nimport type { MigrationOperation } from '../domain/MigrationOperation';\nimport type { ColumnType } from '../builder/contracts/ColumnType';\nimport type { DeleteReferentialAction } from '../builder/contracts/DeleteReferentialAction';\nimport type { UpdateReferentialAction } from '../builder/contracts/UpdateReferentialAction';\nimport { op, applyFieldType } from '../builder/index';\n\ntype ModelField = {\n name: string;\n type: ColumnType;\n notNull?: boolean;\n default?: string | { now: true } | null;\n primaryKey?: boolean;\n unique?: boolean;\n references?: {\n table: string;\n column: string;\n onDelete?: DeleteReferentialAction;\n onUpdate?: UpdateReferentialAction;\n };\n};\n\ntype ModelIndex = {\n name: string;\n on: string[];\n unique?: boolean;\n};\n\nexport type ModelMetadataLike = {\n name?: string;\n table: string;\n fields: ModelField[];\n indexes?: ModelIndex[];\n managed?: boolean;\n};\n\n/**\n * Compare model metadata with an introspected database schema and return the\n * operations needed to bring the database into alignment.\n */\nexport function diffSchema(db: DbSchema, models: ModelMetadataLike[]): MigrationOperation[] {\n const ops: MigrationOperation[] = [];\n models.forEach((model) => {\n if (model.managed === false) {\n return;\n }\n\n const dbTable = db.tables[model.table];\n\n if (!dbTable) {\n ops.push(\n op.table(model.table).create((cols) => {\n model.fields.forEach((field) => {\n cols.add(field.name, (builder) => {\n builder = applyFieldType(builder, field.type);\n\n if (field.notNull) {\n builder = builder.notNull();\n }\n\n if (field.default === null) {\n builder = builder.default(null);\n } else if (field.default && typeof field.default === 'object' && 'now' in field.default) {\n builder = builder.defaultNow();\n } else if (typeof field.default === 'string') {\n builder = builder.default(trustedSql(field.default));\n }\n\n if (field.primaryKey) {\n builder = builder.primaryKey();\n }\n\n if (field.unique) {\n builder = builder.unique();\n }\n\n if (field.references) {\n builder = builder.references(field.references.table, field.references.column, {\n onDelete: field.references.onDelete,\n onUpdate: field.references.onUpdate,\n });\n }\n\n return builder;\n });\n });\n })\n );\n\n (model.indexes ?? []).forEach((index) => {\n ops.push(\n op.index.create({\n name: index.name,\n table: model.table,\n on: index.on,\n unique: !!index.unique,\n })\n );\n });\n return;\n }\n\n const modelFieldNames = new Set(model.fields.map((field) => field.name));\n const dbFieldNames = new Set(Object.keys(dbTable.columns));\n\n model.fields.forEach((field) => {\n if (!dbFieldNames.has(field.name)) {\n ops.push(\n op.table(model.table).addColumn(field.name, (builder) => {\n builder = applyFieldType(builder, field.type);\n\n if (field.notNull) {\n builder = builder.notNull();\n }\n if (field.default === null) {\n builder = builder.default(null);\n } else if (field.default && typeof field.default === 'object' && 'now' in field.default) {\n builder = builder.defaultNow();\n } else if (typeof field.default === 'string') {\n builder = builder.default(trustedSql(field.default));\n }\n if (field.primaryKey) {\n builder = builder.primaryKey();\n }\n if (field.unique) {\n builder = builder.unique();\n }\n if (field.references) {\n builder = builder.references(field.references.table, field.references.column, {\n onDelete: field.references.onDelete,\n onUpdate: field.references.onUpdate,\n });\n }\n\n return builder;\n })\n );\n }\n });\n\n dbFieldNames.forEach((dbColumnName) => {\n if (!modelFieldNames.has(dbColumnName)) {\n ops.push(op.table(model.table).dropColumn(dbColumnName));\n }\n });\n\n const modelIndexes = new Map((model.indexes ?? []).map((index) => [index.name, index] as const));\n const dbIndexNames = new Set(Object.keys(dbTable.indexes));\n\n modelIndexes.forEach((index, indexName) => {\n if (!dbIndexNames.has(indexName)) {\n ops.push(\n op.index.create({\n name: index.name,\n table: model.table,\n on: index.on,\n unique: !!index.unique,\n })\n );\n }\n });\n\n // Index drift is intentionally conservative. Extra indexes in the live\n // database may be app-owned or dialect-managed, so migrations only add\n // missing declared indexes and avoid destructive index drops here.\n });\n\n return ops;\n}\n","import type { ModelRegistry } from '@danceroutine/tango-schema';\nimport type { Field } from '@danceroutine/tango-schema/domain';\nimport type { ColumnType } from '../builder/contracts/ColumnType';\nimport type { ModelMetadataLike } from '../diff/diffSchema';\n\nfunction fieldToModelField(field: Field): ModelMetadataLike['fields'][number] {\n return {\n name: field.name,\n type: field.type as ColumnType,\n notNull: field.notNull,\n default: field.default,\n primaryKey: field.primaryKey,\n unique: field.unique,\n references: field.references as ModelMetadataLike['fields'][number]['references'],\n };\n}\n\nexport function buildMigrationModelMetadataProjection(registry: ModelRegistry): ModelMetadataLike[] {\n registry.finalizeStorageArtifacts();\n const projection: ModelMetadataLike[] = [];\n for (const model of registry.values()) {\n const finalized = registry.getFinalizedFields(model.metadata.key);\n projection.push({\n name: model.metadata.name,\n table: model.metadata.table,\n managed: model.metadata.managed ?? true,\n fields: finalized.map(fieldToModelField),\n indexes: model.metadata.indexes?.map((index) => ({\n name: index.name,\n on: [...index.on],\n unique: index.unique,\n })),\n });\n }\n return projection;\n}\n","/**\n * Domain boundary barrel: centralizes this subdomain's public contract.\n */\n\nexport { diffSchema } from './diffSchema';\nexport { buildMigrationModelMetadataProjection } from '../schema/buildMigrationModelMetadataProjection';\n"],"mappings":";;;;;;;;AAyCA,SAAgB,WAAW,IAAc,QAAmD;CACxF,MAAM,MAA4B,CAAC;CACnC,OAAO,SAAS,UAAU;EACtB,IAAI,MAAM,YAAY,OAClB;EAGJ,MAAM,UAAU,GAAG,OAAO,MAAM;EAEhC,IAAI,CAAC,SAAS;GACV,IAAI,KACAA,UAAG,MAAM,MAAM,KAAK,EAAE,QAAQ,SAAS;IACnC,MAAM,OAAO,SAAS,UAAU;KAC5B,KAAK,IAAI,MAAM,OAAO,YAAY;MAC9B,UAAU,eAAe,SAAS,MAAM,IAAI;MAE5C,IAAI,MAAM,SACN,UAAU,QAAQ,QAAQ;MAG9B,IAAI,MAAM,YAAY,MAClB,UAAU,QAAQ,QAAQ,IAAI;WAC3B,IAAI,MAAM,WAAW,OAAO,MAAM,YAAY,YAAY,SAAS,MAAM,SAC5E,UAAU,QAAQ,WAAW;WAC1B,IAAI,OAAO,MAAM,YAAY,UAChC,UAAU,QAAQ,QAAQ,WAAW,MAAM,OAAO,CAAC;MAGvD,IAAI,MAAM,YACN,UAAU,QAAQ,WAAW;MAGjC,IAAI,MAAM,QACN,UAAU,QAAQ,OAAO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