@argos-js/data 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +799 -2
  3. package/dist/connection/index.d.ts +2 -0
  4. package/dist/connection/index.d.ts.map +1 -0
  5. package/dist/connection/index.js +18 -0
  6. package/dist/connection/index.js.map +1 -0
  7. package/dist/connection/manager.d.ts +93 -0
  8. package/dist/connection/manager.d.ts.map +1 -0
  9. package/dist/connection/manager.js +194 -0
  10. package/dist/connection/manager.js.map +1 -0
  11. package/dist/database.d.ts +29 -0
  12. package/dist/database.d.ts.map +1 -0
  13. package/dist/database.js +276 -0
  14. package/dist/database.js.map +1 -0
  15. package/dist/drivers/base-driver.d.ts +18 -0
  16. package/dist/drivers/base-driver.d.ts.map +1 -0
  17. package/dist/drivers/base-driver.js +211 -0
  18. package/dist/drivers/base-driver.js.map +1 -0
  19. package/dist/drivers/index.d.ts +11 -0
  20. package/dist/drivers/index.d.ts.map +1 -0
  21. package/dist/drivers/index.js +55 -0
  22. package/dist/drivers/index.js.map +1 -0
  23. package/dist/drivers/mavedb-edge.d.ts +8 -0
  24. package/dist/drivers/mavedb-edge.d.ts.map +1 -0
  25. package/dist/drivers/mavedb-edge.js +18 -0
  26. package/dist/drivers/mavedb-edge.js.map +1 -0
  27. package/dist/drivers/mongodb.d.ts +8 -0
  28. package/dist/drivers/mongodb.d.ts.map +1 -0
  29. package/dist/drivers/mongodb.js +18 -0
  30. package/dist/drivers/mongodb.js.map +1 -0
  31. package/dist/drivers/mssql.d.ts +8 -0
  32. package/dist/drivers/mssql.d.ts.map +1 -0
  33. package/dist/drivers/mssql.js +18 -0
  34. package/dist/drivers/mssql.js.map +1 -0
  35. package/dist/drivers/mysql.d.ts +8 -0
  36. package/dist/drivers/mysql.d.ts.map +1 -0
  37. package/dist/drivers/mysql.js +18 -0
  38. package/dist/drivers/mysql.js.map +1 -0
  39. package/dist/drivers/postgres.d.ts +8 -0
  40. package/dist/drivers/postgres.d.ts.map +1 -0
  41. package/dist/drivers/postgres.js +18 -0
  42. package/dist/drivers/postgres.js.map +1 -0
  43. package/dist/drivers/redis.d.ts +9 -0
  44. package/dist/drivers/redis.d.ts.map +1 -0
  45. package/dist/drivers/redis.js +50 -0
  46. package/dist/drivers/redis.js.map +1 -0
  47. package/dist/drivers/sqlite.d.ts +8 -0
  48. package/dist/drivers/sqlite.d.ts.map +1 -0
  49. package/dist/drivers/sqlite.js +18 -0
  50. package/dist/drivers/sqlite.js.map +1 -0
  51. package/dist/index.d.ts +14 -0
  52. package/dist/index.d.ts.map +1 -0
  53. package/dist/index.js +49 -0
  54. package/dist/index.js.map +1 -0
  55. package/dist/migrations/index.d.ts +7 -0
  56. package/dist/migrations/index.d.ts.map +1 -0
  57. package/dist/migrations/index.js +23 -0
  58. package/dist/migrations/index.js.map +1 -0
  59. package/dist/migrations/migrator.d.ts +79 -0
  60. package/dist/migrations/migrator.d.ts.map +1 -0
  61. package/dist/migrations/migrator.js +274 -0
  62. package/dist/migrations/migrator.js.map +1 -0
  63. package/dist/migrations/schema-builder.d.ts +318 -0
  64. package/dist/migrations/schema-builder.d.ts.map +1 -0
  65. package/dist/migrations/schema-builder.js +957 -0
  66. package/dist/migrations/schema-builder.js.map +1 -0
  67. package/dist/migrations/types.d.ts +58 -0
  68. package/dist/migrations/types.d.ts.map +1 -0
  69. package/dist/migrations/types.js +3 -0
  70. package/dist/migrations/types.js.map +1 -0
  71. package/dist/model.d.ts +107 -0
  72. package/dist/model.d.ts.map +1 -0
  73. package/dist/model.js +333 -0
  74. package/dist/model.js.map +1 -0
  75. package/dist/plugin.d.ts +38 -0
  76. package/dist/plugin.d.ts.map +1 -0
  77. package/dist/plugin.js +83 -0
  78. package/dist/plugin.js.map +1 -0
  79. package/dist/query-builder.d.ts +58 -0
  80. package/dist/query-builder.d.ts.map +1 -0
  81. package/dist/query-builder.js +534 -0
  82. package/dist/query-builder.js.map +1 -0
  83. package/dist/schema-adapter.d.ts +34 -0
  84. package/dist/schema-adapter.d.ts.map +1 -0
  85. package/dist/schema-adapter.js +511 -0
  86. package/dist/schema-adapter.js.map +1 -0
  87. package/dist/schema.d.ts +104 -0
  88. package/dist/schema.d.ts.map +1 -0
  89. package/dist/schema.js +190 -0
  90. package/dist/schema.js.map +1 -0
  91. package/dist/seeds/index.d.ts +3 -0
  92. package/dist/seeds/index.d.ts.map +1 -0
  93. package/dist/seeds/index.js +19 -0
  94. package/dist/seeds/index.js.map +1 -0
  95. package/dist/seeds/seeder.d.ts +27 -0
  96. package/dist/seeds/seeder.d.ts.map +1 -0
  97. package/dist/seeds/seeder.js +138 -0
  98. package/dist/seeds/seeder.js.map +1 -0
  99. package/dist/seeds/types.d.ts +60 -0
  100. package/dist/seeds/types.d.ts.map +1 -0
  101. package/dist/seeds/types.js +3 -0
  102. package/dist/seeds/types.js.map +1 -0
  103. package/dist/sync/dump.d.ts +80 -0
  104. package/dist/sync/dump.d.ts.map +1 -0
  105. package/dist/sync/dump.js +655 -0
  106. package/dist/sync/dump.js.map +1 -0
  107. package/dist/sync/index.d.ts +5 -0
  108. package/dist/sync/index.d.ts.map +1 -0
  109. package/dist/sync/index.js +21 -0
  110. package/dist/sync/index.js.map +1 -0
  111. package/dist/sync/introspector.d.ts +49 -0
  112. package/dist/sync/introspector.d.ts.map +1 -0
  113. package/dist/sync/introspector.js +750 -0
  114. package/dist/sync/introspector.js.map +1 -0
  115. package/dist/sync/synchronizer.d.ts +50 -0
  116. package/dist/sync/synchronizer.d.ts.map +1 -0
  117. package/dist/sync/synchronizer.js +506 -0
  118. package/dist/sync/synchronizer.js.map +1 -0
  119. package/dist/sync/types.d.ts +83 -0
  120. package/dist/sync/types.d.ts.map +1 -0
  121. package/dist/sync/types.js +3 -0
  122. package/dist/sync/types.js.map +1 -0
  123. package/dist/tenant/sequence.d.ts +36 -0
  124. package/dist/tenant/sequence.d.ts.map +1 -0
  125. package/dist/tenant/sequence.js +130 -0
  126. package/dist/tenant/sequence.js.map +1 -0
  127. package/dist/types.d.ts +81 -0
  128. package/dist/types.d.ts.map +1 -0
  129. package/dist/types.js +3 -0
  130. package/dist/types.js.map +1 -0
  131. package/package.json +38 -3
@@ -0,0 +1,655 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.DatabaseRestorer = exports.DatabaseDumper = void 0;
37
+ const node_fs_1 = require("node:fs");
38
+ const node_path_1 = require("node:path");
39
+ const readline = __importStar(require("node:readline"));
40
+ const node_perf_hooks_1 = require("node:perf_hooks");
41
+ const manager_1 = require("../connection/manager");
42
+ const schema_adapter_1 = require("../schema-adapter");
43
+ const introspector_1 = require("./introspector");
44
+ const schema_builder_1 = require("../migrations/schema-builder");
45
+ /**
46
+ * Agnostic Database Dumper.
47
+ * Introspects source database schema and exports a collation-safe canonical bundle (.argos)
48
+ * containing manifest.json, schema.json, and streaming chunked data/*.ndjson files.
49
+ */
50
+ class DatabaseDumper {
51
+ async dump(options = {}) {
52
+ const startTime = node_perf_hooks_1.performance.now();
53
+ // 1. Resolve source database client
54
+ const client = this.resolveClient(options.connection);
55
+ if (!client.isConnected) {
56
+ await client.connect();
57
+ }
58
+ const sourceDialect = (0, schema_adapter_1.normalizeDialect)(client.driverName);
59
+ // 2. Resolve output path (default: ./storage/dumps/dump_<timestamp>.argos)
60
+ let outputPath = options.outputPath;
61
+ if (!outputPath) {
62
+ const timestamp = new Date()
63
+ .toISOString()
64
+ .replace(/[-:]/g, '')
65
+ .replace('T', '')
66
+ .replace(/\..+/, '');
67
+ outputPath = (0, node_path_1.resolve)(process.cwd(), `storage/dumps/dump_${timestamp}.argos`);
68
+ }
69
+ else {
70
+ outputPath = (0, node_path_1.resolve)(process.cwd(), outputPath);
71
+ }
72
+ // 3. Introspect schema
73
+ if (options.onProgress) {
74
+ options.onProgress({
75
+ tableName: '*',
76
+ transferredRows: 0,
77
+ totalRows: 0,
78
+ phase: 'introspecting',
79
+ });
80
+ }
81
+ const introspector = new introspector_1.DatabaseIntrospector(client);
82
+ const fullSchema = await introspector.introspect();
83
+ // Filter tables if requested
84
+ let tablesToDump = fullSchema.tables;
85
+ if (options.tables && options.tables.length > 0) {
86
+ const allowed = new Set(options.tables.map((t) => t.toLowerCase().trim()));
87
+ tablesToDump = tablesToDump.filter((t) => allowed.has(t.tableName.toLowerCase()));
88
+ }
89
+ // Ensure output directories exist
90
+ (0, node_fs_1.mkdirSync)(outputPath, { recursive: true });
91
+ const dataDir = (0, node_path_1.join)(outputPath, 'data');
92
+ (0, node_fs_1.mkdirSync)(dataDir, { recursive: true });
93
+ // 4. Sanitize and write schema.json (strip vendor collations like utf8mb4_0900_ai_ci)
94
+ const sanitizedSchema = this.sanitizeSchema(fullSchema, tablesToDump);
95
+ (0, node_fs_1.writeFileSync)((0, node_path_1.join)(outputPath, 'schema.json'), JSON.stringify(sanitizedSchema, null, 2), 'utf8');
96
+ // 5. Stream data for each table into data/<tableName>.ndjson
97
+ const batchSize = options.batchSize && options.batchSize > 0 ? options.batchSize : 1000;
98
+ const tableManifests = {};
99
+ let totalRecords = 0;
100
+ for (const table of tablesToDump) {
101
+ const quotedTable = this.quoteIdentifier(table.tableName, sourceDialect);
102
+ // Get count
103
+ let recordCount = 0;
104
+ try {
105
+ const countRes = await client.execute(`SELECT COUNT(*) as count FROM ${quotedTable};`);
106
+ recordCount = Number(countRes.rows[0]?.count ?? 0);
107
+ }
108
+ catch {
109
+ recordCount = 0;
110
+ }
111
+ tableManifests[table.tableName] = { recordCount };
112
+ totalRecords += recordCount;
113
+ const ndjsonPath = (0, node_path_1.join)(dataDir, `${table.tableName}.ndjson`);
114
+ const writeStream = (0, node_fs_1.createWriteStream)(ndjsonPath, { encoding: 'utf8' });
115
+ let transferredRows = 0;
116
+ // Stream data in paginated batches
117
+ while (transferredRows < recordCount || (recordCount === 0 && transferredRows === 0)) {
118
+ if (recordCount === 0)
119
+ break;
120
+ const selectSql = `SELECT * FROM ${quotedTable} LIMIT ${batchSize} OFFSET ${transferredRows};`;
121
+ const res = await client.execute(selectSql);
122
+ const rows = res.rows || [];
123
+ if (rows.length === 0)
124
+ break;
125
+ for (const row of rows) {
126
+ writeStream.write(JSON.stringify(row) + '\n');
127
+ }
128
+ transferredRows += rows.length;
129
+ if (options.onProgress) {
130
+ options.onProgress({
131
+ tableName: table.tableName,
132
+ transferredRows,
133
+ totalRows: recordCount,
134
+ phase: 'dumping',
135
+ });
136
+ }
137
+ }
138
+ await new Promise((resStream) => {
139
+ writeStream.end(() => resStream());
140
+ });
141
+ }
142
+ // 6. Write manifest.json
143
+ const manifest = {
144
+ version: '1.0.0',
145
+ format: 'argos-canonical-dump',
146
+ createdAt: new Date().toISOString(),
147
+ sourceDialect,
148
+ totalRecords,
149
+ tableCount: tablesToDump.length,
150
+ tables: tableManifests,
151
+ };
152
+ (0, node_fs_1.writeFileSync)((0, node_path_1.join)(outputPath, 'manifest.json'), JSON.stringify(manifest, null, 2), 'utf8');
153
+ if (options.onProgress) {
154
+ options.onProgress({
155
+ tableName: '*',
156
+ transferredRows: totalRecords,
157
+ totalRows: totalRecords,
158
+ phase: 'completed',
159
+ });
160
+ }
161
+ const durationMs = Math.round(node_perf_hooks_1.performance.now() - startTime);
162
+ return {
163
+ outputPath,
164
+ tablesDumped: tablesToDump.length,
165
+ totalRecords,
166
+ durationMs,
167
+ manifest,
168
+ };
169
+ }
170
+ resolveClient(connection) {
171
+ if (connection && typeof connection !== 'string') {
172
+ return connection;
173
+ }
174
+ return manager_1.databaseManager.connection(connection);
175
+ }
176
+ sanitizeSchema(fullSchema, tables) {
177
+ return {
178
+ dialect: fullSchema.dialect,
179
+ tables: tables.map((t) => ({
180
+ tableName: t.tableName,
181
+ primaryKeys: [...t.primaryKeys],
182
+ indices: t.indices.map((idx) => ({ ...idx })),
183
+ foreignKeys: t.foreignKeys.map((fk) => ({ ...fk })),
184
+ columns: t.columns.map((col) => {
185
+ let cleanDefault = col.defaultValue;
186
+ if (typeof cleanDefault === 'string') {
187
+ // Strip problematic MySQL 8 collation clauses
188
+ cleanDefault = cleanDefault.replace(/COLLATE\s+[\w_]+/i, '').trim();
189
+ }
190
+ return {
191
+ ...col,
192
+ defaultValue: cleanDefault,
193
+ };
194
+ }),
195
+ })),
196
+ };
197
+ }
198
+ quoteIdentifier(name, dialect) {
199
+ if (dialect === 'mysql' || dialect === 'mariadb') {
200
+ return `\`${name}\``;
201
+ }
202
+ if (dialect === 'mssql') {
203
+ return `[${name}]`;
204
+ }
205
+ return `"${name}"`;
206
+ }
207
+ }
208
+ exports.DatabaseDumper = DatabaseDumper;
209
+ /**
210
+ * Agnostic Database Restorer.
211
+ * Reads canonical .argos dump package, adapts schema to active target dialect,
212
+ * disables foreign key constraints, streams .ndjson records with normalization,
213
+ * and recalibrates dialect auto-increment/sequences.
214
+ */
215
+ class DatabaseRestorer {
216
+ async restore(options) {
217
+ const startTime = node_perf_hooks_1.performance.now();
218
+ const inputPath = (0, node_path_1.resolve)(process.cwd(), options.inputPath);
219
+ if (!(0, node_fs_1.existsSync)(inputPath)) {
220
+ throw new Error(`[Argos Restorer] Dump path does not exist: "${inputPath}".`);
221
+ }
222
+ const manifestPath = (0, node_path_1.join)(inputPath, 'manifest.json');
223
+ const schemaPath = (0, node_path_1.join)(inputPath, 'schema.json');
224
+ const dataDir = (0, node_path_1.join)(inputPath, 'data');
225
+ if (!(0, node_fs_1.existsSync)(manifestPath) || !(0, node_fs_1.existsSync)(schemaPath)) {
226
+ throw new Error(`[Argos Restorer] Invalid dump package at "${inputPath}". Missing manifest.json or schema.json.`);
227
+ }
228
+ // 1. Read manifest and schema
229
+ const manifest = JSON.parse((0, node_fs_1.readFileSync)(manifestPath, 'utf8'));
230
+ const schema = JSON.parse((0, node_fs_1.readFileSync)(schemaPath, 'utf8'));
231
+ // 2. Resolve target database client
232
+ const targetDb = this.resolveClient(options.connection);
233
+ if (!targetDb.isConnected) {
234
+ await targetDb.connect();
235
+ }
236
+ const targetDialect = (0, schema_adapter_1.normalizeDialect)(targetDb.driverName);
237
+ const truncate = options.truncate !== false;
238
+ const batchSize = options.batchSize && options.batchSize > 0 ? options.batchSize : 1000;
239
+ if (options.onProgress) {
240
+ options.onProgress({
241
+ tableName: '*',
242
+ transferredRows: 0,
243
+ totalRows: manifest.totalRecords,
244
+ phase: 'preparing',
245
+ });
246
+ }
247
+ // 3. Disable foreign key constraints during restore
248
+ await this.setConstraintsEnabled(targetDb, targetDialect, false, schema.tables);
249
+ let totalRestoredRecords = 0;
250
+ let tablesRestored = 0;
251
+ try {
252
+ // 4. Ensure tables exist in target database using TableBlueprint
253
+ for (const table of schema.tables) {
254
+ await this.createTableInTarget(targetDb, table, targetDialect);
255
+ }
256
+ // 5. Truncate tables if requested
257
+ if (truncate) {
258
+ for (const table of schema.tables) {
259
+ await this.truncateTable(targetDb, table.tableName, targetDialect);
260
+ }
261
+ }
262
+ // 6. Stream and insert data from .ndjson files
263
+ for (const table of schema.tables) {
264
+ const ndjsonFile = (0, node_path_1.join)(dataDir, `${table.tableName}.ndjson`);
265
+ if (!(0, node_fs_1.existsSync)(ndjsonFile))
266
+ continue;
267
+ const tableTotalRecords = manifest.tables[table.tableName]?.recordCount ?? 0;
268
+ let tableTransferred = 0;
269
+ const insertColumns = table.columns.filter((c) => !c.isGenerated);
270
+ const colNames = insertColumns.map((c) => c.name);
271
+ const colMap = new Map(table.columns.map((c) => [c.name, c]));
272
+ const fileStream = (0, node_fs_1.createReadStream)(ndjsonFile, { encoding: 'utf8' });
273
+ const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity });
274
+ let currentBatch = [];
275
+ for await (const line of rl) {
276
+ if (!line || !line.trim())
277
+ continue;
278
+ const parsed = JSON.parse(line);
279
+ const normalized = this.normalizeRow(parsed, colMap, targetDialect);
280
+ currentBatch.push(normalized);
281
+ if (currentBatch.length >= batchSize) {
282
+ await this.insertBatch(targetDb, table.tableName, colNames, currentBatch, targetDialect);
283
+ tableTransferred += currentBatch.length;
284
+ totalRestoredRecords += currentBatch.length;
285
+ currentBatch = [];
286
+ if (options.onProgress) {
287
+ options.onProgress({
288
+ tableName: table.tableName,
289
+ transferredRows: tableTransferred,
290
+ totalRows: tableTotalRecords,
291
+ phase: 'restoring',
292
+ });
293
+ }
294
+ }
295
+ }
296
+ if (currentBatch.length > 0) {
297
+ await this.insertBatch(targetDb, table.tableName, colNames, currentBatch, targetDialect);
298
+ tableTransferred += currentBatch.length;
299
+ totalRestoredRecords += currentBatch.length;
300
+ currentBatch = [];
301
+ if (options.onProgress) {
302
+ options.onProgress({
303
+ tableName: table.tableName,
304
+ transferredRows: tableTransferred,
305
+ totalRows: tableTotalRecords,
306
+ phase: 'restoring',
307
+ });
308
+ }
309
+ }
310
+ tablesRestored++;
311
+ // 7. Sync auto-increment sequences for this table
312
+ await this.syncSequences(targetDb, table, targetDialect);
313
+ }
314
+ }
315
+ finally {
316
+ // 8. Re-enable foreign key constraints
317
+ await this.setConstraintsEnabled(targetDb, targetDialect, true, schema.tables);
318
+ }
319
+ if (options.onProgress) {
320
+ options.onProgress({
321
+ tableName: '*',
322
+ transferredRows: totalRestoredRecords,
323
+ totalRows: totalRestoredRecords,
324
+ phase: 'completed',
325
+ });
326
+ }
327
+ const durationMs = Math.round(node_perf_hooks_1.performance.now() - startTime);
328
+ return {
329
+ tablesRestored,
330
+ recordsRestored: totalRestoredRecords,
331
+ durationMs,
332
+ };
333
+ }
334
+ resolveClient(connection) {
335
+ if (connection && typeof connection !== 'string') {
336
+ return connection;
337
+ }
338
+ return manager_1.databaseManager.connection(connection);
339
+ }
340
+ async setConstraintsEnabled(db, dialect, enabled, tables) {
341
+ try {
342
+ if (dialect === 'sqlite') {
343
+ await db.execute(`PRAGMA foreign_keys = ${enabled ? 'ON' : 'OFF'};`);
344
+ }
345
+ else if (dialect === 'postgres') {
346
+ const mode = enabled ? 'origin' : 'replica';
347
+ await db.execute(`SET session_replication_role = '${mode}';`);
348
+ }
349
+ else if (dialect === 'mysql' || dialect === 'mariadb') {
350
+ await db.execute(`SET FOREIGN_KEY_CHECKS = ${enabled ? 1 : 0};`);
351
+ }
352
+ else if (dialect === 'mssql') {
353
+ for (const t of tables) {
354
+ const quoted = this.quoteIdentifier(t.tableName, dialect);
355
+ const cmd = enabled ? 'CHECK CONSTRAINT ALL' : 'NOCHECK CONSTRAINT ALL';
356
+ await db.execute(`ALTER TABLE ${quoted} ${cmd};`);
357
+ }
358
+ }
359
+ else if (dialect === 'oracle') {
360
+ for (const t of tables) {
361
+ for (const fk of t.foreignKeys) {
362
+ if (fk.name) {
363
+ const cmd = enabled ? 'ENABLE' : 'DISABLE';
364
+ await db.execute(`ALTER TABLE "${t.tableName}" ${cmd} CONSTRAINT "${fk.name}";`);
365
+ }
366
+ }
367
+ }
368
+ }
369
+ }
370
+ catch {
371
+ // Best-effort constraint isolation
372
+ }
373
+ }
374
+ async truncateTable(db, tableName, dialect) {
375
+ const quoted = this.quoteIdentifier(tableName, dialect);
376
+ try {
377
+ if (dialect === 'postgres') {
378
+ await db.execute(`TRUNCATE TABLE ${quoted} CASCADE;`);
379
+ }
380
+ else if (dialect === 'mysql' || dialect === 'mariadb') {
381
+ await db.execute(`TRUNCATE TABLE ${quoted};`);
382
+ }
383
+ else {
384
+ await db.execute(`DELETE FROM ${quoted};`);
385
+ }
386
+ }
387
+ catch {
388
+ await db.execute(`DELETE FROM ${quoted};`);
389
+ }
390
+ }
391
+ async createTableInTarget(db, table, targetDialect) {
392
+ const blueprint = new schema_builder_1.TableBlueprint(table.tableName, false);
393
+ for (const col of table.columns) {
394
+ let colBp;
395
+ switch (col.type) {
396
+ case 'id':
397
+ colBp = blueprint.id(col.name);
398
+ break;
399
+ case 'uuid':
400
+ colBp = blueprint.uuid(col.name);
401
+ break;
402
+ case 'uuidv7':
403
+ colBp = blueprint.uuidV7(col.name);
404
+ break;
405
+ case 'string':
406
+ colBp = blueprint.string(col.name, col.length || 255);
407
+ break;
408
+ case 'text':
409
+ colBp = blueprint.text(col.name);
410
+ break;
411
+ case 'integer':
412
+ colBp = blueprint.integer(col.name);
413
+ break;
414
+ case 'bigint':
415
+ colBp = blueprint.bigint(col.name);
416
+ break;
417
+ case 'decimal':
418
+ colBp = blueprint.decimal(col.name, col.precision ?? 10, col.scale ?? 2);
419
+ break;
420
+ case 'float':
421
+ colBp = blueprint.float(col.name);
422
+ break;
423
+ case 'boolean':
424
+ colBp = blueprint.boolean(col.name);
425
+ break;
426
+ case 'timestamp':
427
+ colBp = blueprint.timestamp(col.name);
428
+ break;
429
+ case 'timestamp_created':
430
+ colBp = blueprint.timestamp(col.name);
431
+ break;
432
+ case 'timestamp_updated':
433
+ colBp = blueprint.timestamp(col.name);
434
+ break;
435
+ case 'soft_delete':
436
+ colBp = blueprint.softDeletes(col.name);
437
+ break;
438
+ case 'datetime':
439
+ colBp = blueprint.datetime(col.name);
440
+ break;
441
+ case 'json':
442
+ colBp = blueprint.json(col.name);
443
+ break;
444
+ case 'jsonb':
445
+ colBp = blueprint.jsonb(col.name);
446
+ break;
447
+ case 'vector':
448
+ colBp = blueprint.vector(col.name, col.dimensions || 1536);
449
+ break;
450
+ case 'inet':
451
+ colBp = blueprint.inet(col.name);
452
+ break;
453
+ case 'macaddr':
454
+ colBp = blueprint.macAddress(col.name);
455
+ break;
456
+ case 'enum':
457
+ colBp = blueprint.enum(col.name, col.enumValues || ['default']);
458
+ break;
459
+ case 'money':
460
+ colBp = blueprint.money(col.name, col.precision || 19, col.scale || 4);
461
+ break;
462
+ case 'date':
463
+ colBp = blueprint.date(col.name);
464
+ break;
465
+ case 'time':
466
+ colBp = blueprint.time(col.name);
467
+ break;
468
+ case 'binary':
469
+ colBp = blueprint.binary(col.name);
470
+ break;
471
+ default:
472
+ colBp = blueprint.string(col.name, 255);
473
+ }
474
+ if (col.isGenerated && col.generatedExpression) {
475
+ colBp.as(col.generatedExpression, col.generatedMode ? { mode: col.generatedMode } : undefined);
476
+ }
477
+ if (!col.isNullable)
478
+ colBp.notNull();
479
+ if (col.isUnique && !col.isPrimaryKey)
480
+ colBp.unique();
481
+ if (col.hasDefault && col.defaultValue !== undefined)
482
+ colBp.default(col.defaultValue);
483
+ }
484
+ // Add indexes
485
+ for (const idx of table.indices) {
486
+ if (!idx.isPrimary && idx.columns.length > 0) {
487
+ blueprint.index(idx.columns, idx.name);
488
+ }
489
+ }
490
+ const statements = blueprint.toSql(targetDialect);
491
+ for (const sql of statements) {
492
+ try {
493
+ await db.execute(sql);
494
+ }
495
+ catch {
496
+ // Table or index may already exist
497
+ }
498
+ }
499
+ }
500
+ normalizeRow(row, colMap, targetDialect) {
501
+ const normalized = {};
502
+ for (const [key, val] of Object.entries(row)) {
503
+ const col = colMap.get(key);
504
+ if (!col) {
505
+ normalized[key] = val;
506
+ continue;
507
+ }
508
+ if (val === null || val === undefined) {
509
+ normalized[key] = null;
510
+ continue;
511
+ }
512
+ // Boolean normalization
513
+ if (col.type === 'boolean') {
514
+ const isTrue = val === true || val === 1 || val === '1' || val === 'true';
515
+ if (targetDialect === 'sqlite' || targetDialect === 'mssql' || targetDialect === 'oracle') {
516
+ normalized[key] = isTrue ? 1 : 0;
517
+ }
518
+ else {
519
+ normalized[key] = isTrue;
520
+ }
521
+ continue;
522
+ }
523
+ // JSON normalization
524
+ if (col.type === 'json' || col.type === 'jsonb') {
525
+ if (typeof val === 'object' && val !== null) {
526
+ normalized[key] = JSON.stringify(val);
527
+ }
528
+ else {
529
+ normalized[key] = val;
530
+ }
531
+ continue;
532
+ }
533
+ // Vector normalization
534
+ if (col.type === 'vector') {
535
+ if (Array.isArray(val)) {
536
+ normalized[key] = `[${val.join(',')}]`;
537
+ }
538
+ else if (typeof val === 'object' && val !== null) {
539
+ normalized[key] = JSON.stringify(val);
540
+ }
541
+ else {
542
+ normalized[key] = val;
543
+ }
544
+ continue;
545
+ }
546
+ // Date & Time normalization
547
+ if (col.type === 'date') {
548
+ if (val instanceof Date) {
549
+ normalized[key] = val.toISOString().slice(0, 10);
550
+ }
551
+ else if (typeof val === 'string' && val.includes('T')) {
552
+ normalized[key] = val.slice(0, 10);
553
+ }
554
+ else {
555
+ normalized[key] = String(val);
556
+ }
557
+ continue;
558
+ }
559
+ if (col.type === 'time') {
560
+ if (val instanceof Date) {
561
+ normalized[key] = val.toTimeString().slice(0, 8);
562
+ }
563
+ else {
564
+ normalized[key] = String(val);
565
+ }
566
+ continue;
567
+ }
568
+ // Oracle empty strings become NULL
569
+ if (targetDialect === 'oracle' && val === '') {
570
+ normalized[key] = null;
571
+ continue;
572
+ }
573
+ normalized[key] = val;
574
+ }
575
+ return normalized;
576
+ }
577
+ async insertBatch(db, tableName, colNames, rows, dialect) {
578
+ if (rows.length === 0 || colNames.length === 0)
579
+ return;
580
+ const maxParamsPerQuery = 500;
581
+ const chunkSize = Math.max(1, Math.floor(maxParamsPerQuery / colNames.length));
582
+ for (let i = 0; i < rows.length; i += chunkSize) {
583
+ const chunk = rows.slice(i, i + chunkSize);
584
+ const quotedCols = colNames.map((c) => this.quoteIdentifier(c, dialect)).join(', ');
585
+ const quotedTable = this.quoteIdentifier(tableName, dialect);
586
+ const valuePlaceholders = [];
587
+ const params = [];
588
+ let paramIndex = 1;
589
+ for (const row of chunk) {
590
+ const rowPlaceholders = [];
591
+ for (const col of colNames) {
592
+ params.push(row[col]);
593
+ if (dialect === 'postgres') {
594
+ rowPlaceholders.push(`$${paramIndex++}`);
595
+ }
596
+ else {
597
+ rowPlaceholders.push('?');
598
+ }
599
+ }
600
+ valuePlaceholders.push(`(${rowPlaceholders.join(', ')})`);
601
+ }
602
+ const sql = `INSERT INTO ${quotedTable} (${quotedCols}) VALUES ${valuePlaceholders.join(', ')};`;
603
+ await db.execute(sql, params);
604
+ }
605
+ }
606
+ async syncSequences(db, table, dialect) {
607
+ const idCol = table.columns.find((c) => c.type === 'id' || c.isAutoIncrement);
608
+ if (!idCol)
609
+ return;
610
+ try {
611
+ const quotedTable = this.quoteIdentifier(table.tableName, dialect);
612
+ const quotedCol = this.quoteIdentifier(idCol.name, dialect);
613
+ if (dialect === 'postgres') {
614
+ const seqSql = `
615
+ SELECT setval(
616
+ pg_get_serial_sequence('${table.tableName}', '${idCol.name}'),
617
+ COALESCE((SELECT MAX(${quotedCol}) FROM ${quotedTable}), 1)
618
+ );
619
+ `;
620
+ await db.execute(seqSql);
621
+ }
622
+ else if (dialect === 'sqlite') {
623
+ const seqSql = `
624
+ INSERT OR REPLACE INTO sqlite_sequence (name, seq)
625
+ VALUES ('${table.tableName}', COALESCE((SELECT MAX(${quotedCol}) FROM ${quotedTable}), 0));
626
+ `;
627
+ await db.execute(seqSql);
628
+ }
629
+ else if (dialect === 'mysql' || dialect === 'mariadb') {
630
+ const maxRes = await db.execute(`SELECT COALESCE(MAX(${quotedCol}), 0) + 1 as max_val FROM ${quotedTable};`);
631
+ const nextVal = Number(maxRes.rows[0]?.max_val || 1);
632
+ await db.execute(`ALTER TABLE ${quotedTable} AUTO_INCREMENT = ${nextVal};`);
633
+ }
634
+ else if (dialect === 'mssql') {
635
+ const maxRes = await db.execute(`SELECT COALESCE(MAX(${quotedCol}), 0) as max_val FROM ${quotedTable};`);
636
+ const maxVal = Number(maxRes.rows[0]?.max_val || 0);
637
+ await db.execute(`DBCC CHECKIDENT ('${table.tableName}', RESEED, ${maxVal});`);
638
+ }
639
+ }
640
+ catch {
641
+ // Best effort sequence realignment
642
+ }
643
+ }
644
+ quoteIdentifier(name, dialect) {
645
+ if (dialect === 'mysql' || dialect === 'mariadb') {
646
+ return `\`${name}\``;
647
+ }
648
+ if (dialect === 'mssql') {
649
+ return `[${name}]`;
650
+ }
651
+ return `"${name}"`;
652
+ }
653
+ }
654
+ exports.DatabaseRestorer = DatabaseRestorer;
655
+ //# sourceMappingURL=dump.js.map