@javalabs/prisma-client 1.0.3 → 1.0.6

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 (61) hide show
  1. package/dist/scripts/data-migration/batch-migrator.d.ts +14 -19
  2. package/dist/scripts/data-migration/batch-migrator.js +98 -297
  3. package/dist/scripts/data-migration/batch-migrator.js.map +1 -1
  4. package/dist/scripts/data-migration/data-transformer.d.ts +16 -7
  5. package/dist/scripts/data-migration/data-transformer.js +169 -133
  6. package/dist/scripts/data-migration/data-transformer.js.map +1 -1
  7. package/dist/scripts/data-migration/db-connector.d.ts +6 -1
  8. package/dist/scripts/data-migration/db-connector.js +44 -8
  9. package/dist/scripts/data-migration/db-connector.js.map +1 -1
  10. package/dist/scripts/data-migration/dependency-resolver.d.ts +10 -10
  11. package/dist/scripts/data-migration/dependency-resolver.js +92 -211
  12. package/dist/scripts/data-migration/dependency-resolver.js.map +1 -1
  13. package/dist/scripts/data-migration/foreign-key-manager.d.ts +6 -5
  14. package/dist/scripts/data-migration/foreign-key-manager.js +108 -18
  15. package/dist/scripts/data-migration/foreign-key-manager.js.map +1 -1
  16. package/dist/scripts/data-migration/migration-config.json +63 -0
  17. package/dist/scripts/data-migration/migration-tool.d.ts +25 -6
  18. package/dist/scripts/data-migration/migration-tool.js +78 -38
  19. package/dist/scripts/data-migration/migration-tool.js.map +1 -1
  20. package/dist/scripts/data-migration/multi-source-migrator.d.ts +17 -0
  21. package/dist/scripts/data-migration/multi-source-migrator.js +130 -0
  22. package/dist/scripts/data-migration/multi-source-migrator.js.map +1 -0
  23. package/dist/scripts/data-migration/schema-utils.d.ts +3 -3
  24. package/dist/scripts/data-migration/schema-utils.js +62 -19
  25. package/dist/scripts/data-migration/schema-utils.js.map +1 -1
  26. package/dist/scripts/data-migration/tenant-migrator.js +9 -2
  27. package/dist/scripts/data-migration/tenant-migrator.js.map +1 -1
  28. package/dist/scripts/data-migration/typecast-manager.d.ts +7 -3
  29. package/dist/scripts/data-migration/typecast-manager.js +169 -25
  30. package/dist/scripts/data-migration/typecast-manager.js.map +1 -1
  31. package/dist/scripts/data-migration/types.d.ts +68 -2
  32. package/dist/scripts/fix-table-indexes.d.ts +26 -0
  33. package/dist/scripts/fix-table-indexes.js +460 -0
  34. package/dist/scripts/fix-table-indexes.js.map +1 -0
  35. package/dist/scripts/multi-db-migration.d.ts +1 -0
  36. package/dist/scripts/multi-db-migration.js +55 -0
  37. package/dist/scripts/multi-db-migration.js.map +1 -0
  38. package/dist/scripts/run-migration.js +41 -75
  39. package/dist/scripts/run-migration.js.map +1 -1
  40. package/dist/tsconfig.tsbuildinfo +1 -1
  41. package/migration-config.json +40 -72
  42. package/{migration-config-public.json → migration-config.json.bk} +14 -14
  43. package/package.json +6 -3
  44. package/prisma/migrations/20250422001248_add_bank_account_id/migration.sql +458 -0
  45. package/prisma/migrations/20250422001957_add_bank_account_id_relations/migration.sql +14 -0
  46. package/prisma/schema.prisma +13 -12
  47. package/src/scripts/data-migration/batch-migrator.ts +192 -513
  48. package/src/scripts/data-migration/data-transformer.ts +252 -203
  49. package/src/scripts/data-migration/db-connector.ts +66 -13
  50. package/src/scripts/data-migration/dependency-resolver.ts +121 -266
  51. package/src/scripts/data-migration/foreign-key-manager.ts +214 -32
  52. package/src/scripts/data-migration/migration-config.json +63 -0
  53. package/src/scripts/data-migration/migration-tool.ts +377 -225
  54. package/src/scripts/data-migration/schema-utils.ts +94 -32
  55. package/src/scripts/data-migration/tenant-migrator.ts +12 -5
  56. package/src/scripts/data-migration/typecast-manager.ts +186 -31
  57. package/src/scripts/data-migration/types.ts +78 -5
  58. package/src/scripts/dumps/source_dump_20250428_145606.sql +323 -0
  59. package/src/scripts/fix-table-indexes.ts +602 -0
  60. package/src/scripts/post-migration-validator.ts +206 -107
  61. package/src/scripts/run-migration.ts +87 -101
@@ -5,12 +5,35 @@ import { promisify } from "util";
5
5
  import { SchemaDiscrepancyFixer } from "./fix-schema-discrepancies";
6
6
  import { EnumSynchronizer } from "./sync-enum-types";
7
7
  import { PrismaClient } from "@prisma/client";
8
+ import { MigrationConfig } from "./data-migration/types";
9
+ import * as fs from "fs";
10
+ import * as path from "path";
8
11
 
9
12
  dotenv.config();
10
13
 
11
14
  const execAsync = promisify(exec);
12
15
  const program = new Command();
13
16
 
17
+ // Función para cargar la configuración de migración
18
+ async function loadMigrationConfig(
19
+ configPath?: string
20
+ ): Promise<MigrationConfig> {
21
+ const defaultPath = path.join(
22
+ __dirname,
23
+ "data-migration",
24
+ "migration-config.json"
25
+ );
26
+ const filePath = configPath || defaultPath;
27
+
28
+ try {
29
+ const configContent = await fs.promises.readFile(filePath, "utf8");
30
+ return JSON.parse(configContent);
31
+ } catch (error) {
32
+ console.error(`Error loading migration config from ${filePath}:`, error);
33
+ throw error;
34
+ }
35
+ }
36
+
14
37
  // Añadir esta función después de las importaciones
15
38
  async function testDatabaseConnection(
16
39
  url: string,
@@ -36,85 +59,6 @@ async function testDatabaseConnection(
36
59
  }
37
60
 
38
61
  // Modificar la acción del comando migrate para incluir la prueba de conexión
39
- async function initializeDatabase(options: any) {
40
- try {
41
- const { Pool } = await import("pg");
42
- const pool = new Pool({ connectionString: process.env.DATABASE_URL });
43
-
44
- // Check if database is already initialized
45
- const tablesExist = await checkIfTablesExist(pool);
46
-
47
- if (!tablesExist) {
48
- // Only create tables if they don't exist
49
- console.log("Database empty, creating initial schema...");
50
- await execAsync("npx prisma db push --skip-generate");
51
- } else {
52
- console.log("Database schema already exists, skipping schema creation");
53
- // Create baseline if it doesn't exist
54
- await execAsync(
55
- "npx prisma migrate reset --force --skip-generate --skip-seed"
56
- );
57
- }
58
-
59
- if (!options.skipEnumSync) {
60
- console.log("Synchronizing enum types...");
61
- const enumSynchronizer = new EnumSynchronizer(
62
- process.env.SOURCE_DATABASE_URL,
63
- process.env.DATABASE_URL
64
- );
65
- await enumSynchronizer.synchronizeEnums();
66
- }
67
-
68
- // Proceed with data migration if not skipped
69
- if (!options.skipDataMigration) {
70
- console.log("Starting data migration...");
71
- const { DataMigrationTool } = await import(
72
- "./data-migration/migration-tool"
73
- );
74
- const migrationTool = new DataMigrationTool({
75
- sourcePool: new Pool({
76
- connectionString: process.env.SOURCE_DATABASE_URL,
77
- }),
78
- targetPool: pool,
79
- sourcePrisma: new PrismaClient({
80
- datasources: { db: { url: process.env.SOURCE_DATABASE_URL } },
81
- }),
82
- targetPrisma: new PrismaClient({
83
- datasources: { db: { url: process.env.DATABASE_URL } },
84
- }),
85
- });
86
-
87
- await migrationTool.migrateData({
88
- publicOnly: options.mode === "public-only",
89
- });
90
- }
91
-
92
- await pool.end();
93
- return true;
94
- } catch (error) {
95
- console.error("Error initializing database:", error);
96
- return false;
97
- }
98
- }
99
-
100
- // Helper function to check if tables exist
101
- async function checkIfTablesExist(pool: any): Promise<boolean> {
102
- try {
103
- const result = await pool.query(`
104
- SELECT COUNT(*)
105
- FROM information_schema.tables
106
- WHERE table_schema = 'public'
107
- AND table_type = 'BASE TABLE'
108
- `);
109
-
110
- return parseInt(result.rows[0].count) > 0;
111
- } catch (error) {
112
- console.error("Error checking tables:", error);
113
- return false;
114
- }
115
- }
116
-
117
- // Modify the existing action handler
118
62
  program
119
63
  .command("migrate")
120
64
  .description("Run the data migration process")
@@ -134,6 +78,7 @@ program
134
78
  .option("--auto-fix-schema", "Automatically fix schema discrepancies", false)
135
79
  .option("--force", "Force migration even if validation fails", false)
136
80
  .option("-y, --yes", "Automatically answer yes to all prompts", false)
81
+ .option("--config-path <path>", "Path to migration config file")
137
82
  .action(async (options) => {
138
83
  // Set environment variables for the migration
139
84
  if (options.source) {
@@ -182,9 +127,6 @@ program
182
127
  }
183
128
 
184
129
  try {
185
- // Initialize database if empty
186
- await initializeDatabase(options);
187
-
188
130
  // Step 0: Pre-migration validation
189
131
  if (!options.skipValidation) {
190
132
  console.log("Step 0: Running pre-migration validation...");
@@ -198,9 +140,7 @@ program
198
140
  console.log(
199
141
  `Pre-migration validation found ${validationResult.issueCount} issues.`
200
142
  );
201
- console.log(
202
- `Check the full report at: ${validationResult.reportPath}`
203
- );
143
+ console.log(JSON.stringify(validationResult, null, 2));
204
144
 
205
145
  // Si la opción --yes o --force está activada, continuar automáticamente
206
146
  if (options.yes) {
@@ -253,6 +193,24 @@ program
253
193
  } else {
254
194
  const { Pool } = await import("pg");
255
195
  const pool = new Pool({ connectionString: process.env.DATABASE_URL });
196
+
197
+ // Helper function to check if tables exist
198
+ async function checkIfTablesExist(pool: any): Promise<boolean> {
199
+ try {
200
+ const result = await pool.query(`
201
+ SELECT COUNT(*)
202
+ FROM information_schema.tables
203
+ WHERE table_schema = 'public'
204
+ AND table_type = 'BASE TABLE'
205
+ `);
206
+
207
+ return parseInt(result.rows[0].count) > 0;
208
+ } catch (error) {
209
+ console.error("Error checking tables:", error);
210
+ return false;
211
+ }
212
+ }
213
+
256
214
  const tablesExist = await checkIfTablesExist(pool);
257
215
  if (!tablesExist) {
258
216
  await execAsync("npx prisma migrate deploy");
@@ -276,7 +234,6 @@ program
276
234
  }
277
235
 
278
236
  // Step 3: Migrate data with transformation
279
- // In the migrate command action
280
237
  if (!options.skipDataMigration) {
281
238
  console.log("Step 3: Migrating data with transformation...");
282
239
  const { DataMigrationTool } = await import(
@@ -285,6 +242,9 @@ program
285
242
  const { PrismaClient } = await import("@prisma/client");
286
243
  const { Pool } = await import("pg");
287
244
 
245
+ // Cargar la configuración de migración
246
+ const migrationConfig = await loadMigrationConfig(options.configPath);
247
+
288
248
  // Create connections with increased pool size and timeout
289
249
  const connections = {
290
250
  sourcePool: new Pool({
@@ -313,20 +273,44 @@ program
313
273
  "SET session_replication_role = 'replica';"
314
274
  );
315
275
 
316
- const migrationTool = new DataMigrationTool(connections);
317
-
276
+ // Si es public-only, modificar la configuración para solo incluir tablas públicas
318
277
  if (options.mode === "public-only") {
319
- await migrationTool.migrateData({
320
- publicOnly: true,
278
+ migrationConfig.tables = Object.entries(migrationConfig.tables)
279
+ .filter(([_, config]) => config.type === "public")
280
+ .reduce(
281
+ (acc, [key, value]) => ({
282
+ ...acc,
283
+ [key]: value,
284
+ }),
285
+ {}
286
+ );
287
+ }
288
+
289
+ const migrationTool = new DataMigrationTool(
290
+ connections,
291
+ {
292
+ ...migrationConfig,
293
+ tables: Object.entries(migrationConfig.tables).reduce(
294
+ (acc, [key, value]) => ({
295
+ ...acc,
296
+ [key]: {
297
+ ...value,
298
+ sourceTable: value.sourceTable || key,
299
+ targetTable: value.targetTable || key,
300
+ },
301
+ }),
302
+ {}
303
+ ),
304
+ },
305
+ {
306
+ publicOnly: options.mode === "public-only",
321
307
  targetSchema: "public",
322
308
  sourceSchema: "public",
323
- });
324
- } else {
325
- await migrationTool.migrateData({
326
- forceSingleTenant: false,
327
- multiTenant: true,
328
- });
329
- }
309
+ multiTenant: options.mode !== "public-only",
310
+ }
311
+ );
312
+
313
+ await migrationTool.migrate();
330
314
  } finally {
331
315
  // Re-enable foreign key constraints
332
316
  await connections.targetPool.query(
@@ -362,7 +346,9 @@ program
362
346
  "./post-migration-validator"
363
347
  );
364
348
  const validator = new PostMigrationValidator();
365
- const result = await validator.validate();
349
+ const result = await validator.validate({
350
+ publicOnly: options.mode === "public-only",
351
+ });
366
352
 
367
353
  if (result.success) {
368
354
  console.log("Post-migration validation successful!");
@@ -370,7 +356,7 @@ program
370
356
  console.log(
371
357
  `Post-migration validation found ${result.issueCount} issues.`
372
358
  );
373
- console.log(`Check the full report at: ${result.reportPath}`);
359
+ console.log(JSON.stringify(result, null, 2));
374
360
  console.log(
375
361
  "You may need to run 'retry-failed' command to fix these issues."
376
362
  );
@@ -537,7 +523,7 @@ program
537
523
  console.log(
538
524
  `Pre-migration validation found ${result.issueCount} issues.`
539
525
  );
540
- console.log(`Check the full report at: ${result.reportPath}`);
526
+ console.log(JSON.stringify(result, null, 2));
541
527
 
542
528
  // Si la opción --yes está activada, continuar automáticamente
543
529
  if (options.yes) {
@@ -643,7 +629,7 @@ program
643
629
  console.log(
644
630
  `Post-migration validation found ${result.issueCount} issues.`
645
631
  );
646
- console.log(`Check the full report at: ${result.reportPath}`);
632
+ console.log(JSON.stringify(result, null, 2));
647
633
  }
648
634
  } catch (error) {
649
635
  console.error("Error during post-migration validation:", error);