@javalabs/prisma-client 1.0.18 → 1.0.20

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 (101) hide show
  1. package/.github/CODEOWNERS +1 -0
  2. package/README.md +269 -269
  3. package/dist/index.d.ts +1 -1
  4. package/dist/prisma.service.d.ts +1 -1
  5. package/dist/scripts/add-uuid-to-table.js +32 -32
  6. package/dist/scripts/data-migration/batch-migrator.js +12 -12
  7. package/dist/scripts/data-migration/data-transformer.js +14 -14
  8. package/dist/scripts/data-migration/dependency-resolver.js +23 -23
  9. package/dist/scripts/data-migration/entity-discovery.js +68 -68
  10. package/dist/scripts/data-migration/foreign-key-manager.js +23 -23
  11. package/dist/scripts/data-migration/migration-tool.js +5 -5
  12. package/dist/scripts/data-migration/schema-utils.js +74 -74
  13. package/dist/scripts/data-migration/typecast-manager.js +4 -4
  14. package/dist/scripts/database-initializer.js +5 -5
  15. package/dist/scripts/drop-database.js +5 -5
  16. package/dist/scripts/fix-data-types.js +53 -53
  17. package/dist/scripts/fix-enum-values.js +34 -34
  18. package/dist/scripts/fix-schema-discrepancies.js +40 -40
  19. package/dist/scripts/fix-table-indexes.js +81 -81
  20. package/dist/scripts/migrate-schema-structure.js +4 -4
  21. package/dist/scripts/migrate-uuid.js +19 -19
  22. package/dist/scripts/post-migration-validator.js +49 -49
  23. package/dist/scripts/pre-migration-validator.js +107 -107
  24. package/dist/scripts/reset-database.js +21 -21
  25. package/dist/scripts/retry-failed-migrations.js +28 -28
  26. package/dist/scripts/run-migration.js +5 -5
  27. package/dist/scripts/schema-sync.js +18 -18
  28. package/dist/scripts/sequence-sync-cli.js +55 -55
  29. package/dist/scripts/sequence-synchronizer.js +20 -20
  30. package/dist/scripts/sync-enum-types.js +30 -30
  31. package/dist/scripts/sync-enum-values.js +52 -52
  32. package/dist/scripts/truncate-database.js +10 -10
  33. package/dist/scripts/verify-migration-setup.js +10 -10
  34. package/dist/tsconfig.tsbuildinfo +1 -1
  35. package/migration-config.json +63 -63
  36. package/migration-config.json.bk +95 -95
  37. package/migrations/add_reserved_amount.sql +8 -0
  38. package/package.json +44 -44
  39. package/prisma/migrations/add_uuid_to_transactions.sql +13 -13
  40. package/prisma/schema.prisma +601 -554
  41. package/src/index.ts +23 -23
  42. package/src/prisma-factory.service.ts +40 -40
  43. package/src/prisma.module.ts +9 -9
  44. package/src/prisma.service.ts +16 -16
  45. package/src/scripts/add-uuid-to-table.ts +138 -138
  46. package/src/scripts/create-tenant-schemas.ts +145 -145
  47. package/src/scripts/data-migration/batch-migrator.ts +248 -248
  48. package/src/scripts/data-migration/data-transformer.ts +426 -426
  49. package/src/scripts/data-migration/db-connector.ts +120 -120
  50. package/src/scripts/data-migration/dependency-resolver.ts +174 -174
  51. package/src/scripts/data-migration/entity-discovery.ts +196 -196
  52. package/src/scripts/data-migration/foreign-key-manager.ts +277 -277
  53. package/src/scripts/data-migration/migration-config.json +63 -63
  54. package/src/scripts/data-migration/migration-tool.ts +509 -509
  55. package/src/scripts/data-migration/schema-utils.ts +248 -248
  56. package/src/scripts/data-migration/tenant-migrator.ts +201 -201
  57. package/src/scripts/data-migration/typecast-manager.ts +193 -193
  58. package/src/scripts/data-migration/types.ts +113 -113
  59. package/src/scripts/database-initializer.ts +49 -49
  60. package/src/scripts/drop-database.ts +104 -104
  61. package/src/scripts/dump-source-db.sh +61 -61
  62. package/src/scripts/encrypt-user-passwords.ts +36 -36
  63. package/src/scripts/error-handler.ts +117 -117
  64. package/src/scripts/fix-data-types.ts +241 -241
  65. package/src/scripts/fix-enum-values.ts +357 -357
  66. package/src/scripts/fix-schema-discrepancies.ts +317 -317
  67. package/src/scripts/fix-table-indexes.ts +601 -601
  68. package/src/scripts/migrate-schema-structure.ts +90 -90
  69. package/src/scripts/migrate-uuid.ts +76 -76
  70. package/src/scripts/post-migration-validator.ts +526 -526
  71. package/src/scripts/pre-migration-validator.ts +610 -610
  72. package/src/scripts/reset-database.ts +263 -263
  73. package/src/scripts/retry-failed-migrations.ts +416 -416
  74. package/src/scripts/run-migration.ts +707 -707
  75. package/src/scripts/schema-sync.ts +128 -128
  76. package/src/scripts/sequence-sync-cli.ts +416 -416
  77. package/src/scripts/sequence-synchronizer.ts +127 -127
  78. package/src/scripts/sync-enum-types.ts +170 -170
  79. package/src/scripts/sync-enum-values.ts +563 -563
  80. package/src/scripts/truncate-database.ts +123 -123
  81. package/src/scripts/verify-migration-setup.ts +135 -135
  82. package/tsconfig.json +17 -17
  83. package/dist/scripts/data-migration/dependency-manager.d.ts +0 -9
  84. package/dist/scripts/data-migration/dependency-manager.js +0 -86
  85. package/dist/scripts/data-migration/dependency-manager.js.map +0 -1
  86. package/dist/scripts/data-migration/migration-config.json +0 -63
  87. package/dist/scripts/data-migration/migration-phases.d.ts +0 -5
  88. package/dist/scripts/data-migration/migration-phases.js +0 -55
  89. package/dist/scripts/data-migration/migration-phases.js.map +0 -1
  90. package/dist/scripts/data-migration/multi-source-migrator.d.ts +0 -17
  91. package/dist/scripts/data-migration/multi-source-migrator.js +0 -130
  92. package/dist/scripts/data-migration/multi-source-migrator.js.map +0 -1
  93. package/dist/scripts/data-migration/phase-generator.d.ts +0 -15
  94. package/dist/scripts/data-migration/phase-generator.js +0 -187
  95. package/dist/scripts/data-migration/phase-generator.js.map +0 -1
  96. package/dist/scripts/data-migration.d.ts +0 -22
  97. package/dist/scripts/data-migration.js +0 -593
  98. package/dist/scripts/data-migration.js.map +0 -1
  99. package/dist/scripts/multi-db-migration.d.ts +0 -1
  100. package/dist/scripts/multi-db-migration.js +0 -55
  101. package/dist/scripts/multi-db-migration.js.map +0 -1
@@ -9,37 +9,37 @@ async function migrateUuid() {
9
9
  console.log("\n📦 Habilitando extensión uuid-ossp...");
10
10
  await prisma.$executeRaw `CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`;
11
11
  console.log("\n🔍 Verificando registros sin UUID...");
12
- const nullUuids = await prisma.$queryRaw `
13
- SELECT COUNT(*) as count
14
- FROM transactions
15
- WHERE uuid IS NULL;
12
+ const nullUuids = await prisma.$queryRaw `
13
+ SELECT COUNT(*) as count
14
+ FROM transactions
15
+ WHERE uuid IS NULL;
16
16
  `;
17
17
  if (nullUuids[0].count > 0) {
18
18
  console.log(`\n📝 Actualizando ${nullUuids[0].count} registros sin UUID...`);
19
- await prisma.$executeRaw `
20
- UPDATE transactions
21
- SET uuid = uuid_generate_v4()
22
- WHERE uuid IS NULL;
19
+ await prisma.$executeRaw `
20
+ UPDATE transactions
21
+ SET uuid = uuid_generate_v4()
22
+ WHERE uuid IS NULL;
23
23
  `;
24
24
  }
25
25
  console.log("\n🔍 Verificando registros después de la actualización...");
26
- const remainingNullUuids = await prisma.$queryRaw `
27
- SELECT COUNT(*) as count
28
- FROM transactions
29
- WHERE uuid IS NULL;
26
+ const remainingNullUuids = await prisma.$queryRaw `
27
+ SELECT COUNT(*) as count
28
+ FROM transactions
29
+ WHERE uuid IS NULL;
30
30
  `;
31
31
  if (remainingNullUuids[0].count > 0) {
32
32
  throw new Error(`Aún hay ${remainingNullUuids[0].count} registros sin UUID después de la actualización`);
33
33
  }
34
34
  console.log("\n📝 Haciendo UUID requerido y añadiendo valor por defecto...");
35
- await prisma.$executeRaw `
36
- ALTER TABLE transactions
37
- ALTER COLUMN uuid SET NOT NULL;
35
+ await prisma.$executeRaw `
36
+ ALTER TABLE transactions
37
+ ALTER COLUMN uuid SET NOT NULL;
38
38
  `;
39
- await prisma.$executeRaw `
40
- ALTER TABLE transactions
41
- ALTER COLUMN uuid
42
- SET DEFAULT uuid_generate_v4();
39
+ await prisma.$executeRaw `
40
+ ALTER TABLE transactions
41
+ ALTER COLUMN uuid
42
+ SET DEFAULT uuid_generate_v4();
43
43
  `;
44
44
  console.log("\n✅ Migración completada exitosamente");
45
45
  }
@@ -45,9 +45,9 @@ class PostMigrationValidator {
45
45
  };
46
46
  }
47
47
  this.logger.log("Starting post-migration validation");
48
- const apiKeysResult = await this.sourcePool.query(`
49
- SELECT api_key, provider_id
50
- FROM api_keys
48
+ const apiKeysResult = await this.sourcePool.query(`
49
+ SELECT api_key, provider_id
50
+ FROM api_keys
51
51
  `);
52
52
  this.logger.log(`Found ${apiKeysResult.rows.length} API keys to validate`);
53
53
  for (const apiKey of apiKeysResult.rows) {
@@ -101,11 +101,11 @@ class PostMigrationValidator {
101
101
  async validateTenantData(tenantId, providerId) {
102
102
  this.logger.log(`Validating data for tenant: ${tenantId} (Provider ID: ${providerId})`);
103
103
  try {
104
- const schemaExistsResult = await this.targetPool.query(`
105
- SELECT 1
106
- FROM information_schema.schemata
107
- WHERE schema_name = $1
108
- LIMIT 1
104
+ const schemaExistsResult = await this.targetPool.query(`
105
+ SELECT 1
106
+ FROM information_schema.schemata
107
+ WHERE schema_name = $1
108
+ LIMIT 1
109
109
  `, [tenantId]);
110
110
  if (schemaExistsResult.rows.length === 0) {
111
111
  this.logger.warn(`Schema ${tenantId} does not exist in target database`);
@@ -117,12 +117,12 @@ class PostMigrationValidator {
117
117
  });
118
118
  return;
119
119
  }
120
- const tablesResult = await this.sourcePool.query(`
121
- SELECT table_name
122
- FROM information_schema.tables
123
- WHERE table_schema = 'public'
124
- AND table_type = 'BASE TABLE'
125
- AND table_name NOT IN ('_prisma_migrations', 'api_keys')
120
+ const tablesResult = await this.sourcePool.query(`
121
+ SELECT table_name
122
+ FROM information_schema.tables
123
+ WHERE table_schema = 'public'
124
+ AND table_type = 'BASE TABLE'
125
+ AND table_name NOT IN ('_prisma_migrations', 'api_keys')
126
126
  `);
127
127
  for (const tableRow of tablesResult.rows) {
128
128
  const tableName = tableRow.table_name;
@@ -142,13 +142,13 @@ class PostMigrationValidator {
142
142
  async validateTableData(tenantId, tableName, providerId) {
143
143
  try {
144
144
  let sourceCountQuery = `SELECT COUNT(*) as count FROM ${tableName}`;
145
- const hasProviderIdResult = await this.sourcePool.query(`
146
- SELECT 1
147
- FROM information_schema.columns
148
- WHERE table_schema = 'public'
149
- AND table_name = $1
150
- AND column_name = 'provider_id'
151
- LIMIT 1
145
+ const hasProviderIdResult = await this.sourcePool.query(`
146
+ SELECT 1
147
+ FROM information_schema.columns
148
+ WHERE table_schema = 'public'
149
+ AND table_name = $1
150
+ AND column_name = 'provider_id'
151
+ LIMIT 1
152
152
  `, [tableName]);
153
153
  if (hasProviderIdResult.rows.length > 0) {
154
154
  sourceCountQuery += ` WHERE provider_id = ${providerId}`;
@@ -197,49 +197,49 @@ class PostMigrationValidator {
197
197
  }
198
198
  async validateRandomRecords(tenantId, tableName, providerId) {
199
199
  try {
200
- const primaryKeyResult = await this.sourcePool.query(`
201
- SELECT kcu.column_name
202
- FROM information_schema.table_constraints tc
203
- JOIN information_schema.key_column_usage kcu
204
- ON tc.constraint_name = kcu.constraint_name
205
- AND tc.table_schema = kcu.table_schema
206
- WHERE tc.constraint_type = 'PRIMARY KEY'
207
- AND tc.table_schema = 'public'
208
- AND tc.table_name = $1
209
- LIMIT 1
200
+ const primaryKeyResult = await this.sourcePool.query(`
201
+ SELECT kcu.column_name
202
+ FROM information_schema.table_constraints tc
203
+ JOIN information_schema.key_column_usage kcu
204
+ ON tc.constraint_name = kcu.constraint_name
205
+ AND tc.table_schema = kcu.table_schema
206
+ WHERE tc.constraint_type = 'PRIMARY KEY'
207
+ AND tc.table_schema = 'public'
208
+ AND tc.table_name = $1
209
+ LIMIT 1
210
210
  `, [tableName]);
211
211
  if (primaryKeyResult.rows.length === 0) {
212
212
  this.logger.warn(`No primary key found for table ${tableName}`);
213
213
  return;
214
214
  }
215
215
  const primaryKeyColumn = primaryKeyResult.rows[0].column_name;
216
- let randomRecordsQuery = `
217
- SELECT *
218
- FROM ${tableName}
216
+ let randomRecordsQuery = `
217
+ SELECT *
218
+ FROM ${tableName}
219
219
  `;
220
- const hasProviderIdResult = await this.sourcePool.query(`
221
- SELECT 1
222
- FROM information_schema.columns
223
- WHERE table_schema = 'public'
224
- AND table_name = $1
225
- AND column_name = 'provider_id'
226
- LIMIT 1
220
+ const hasProviderIdResult = await this.sourcePool.query(`
221
+ SELECT 1
222
+ FROM information_schema.columns
223
+ WHERE table_schema = 'public'
224
+ AND table_name = $1
225
+ AND column_name = 'provider_id'
226
+ LIMIT 1
227
227
  `, [tableName]);
228
228
  if (hasProviderIdResult.rows.length > 0) {
229
229
  randomRecordsQuery += ` WHERE provider_id = ${providerId}`;
230
230
  }
231
- randomRecordsQuery += `
232
- ORDER BY RANDOM()
233
- LIMIT 5
231
+ randomRecordsQuery += `
232
+ ORDER BY RANDOM()
233
+ LIMIT 5
234
234
  `;
235
235
  const randomRecordsResult = await this.sourcePool.query(randomRecordsQuery);
236
236
  for (const sourceRecord of randomRecordsResult.rows) {
237
237
  const primaryKeyValue = sourceRecord[primaryKeyColumn];
238
- const targetRecordResult = await this.targetPool.query(`
239
- SELECT *
240
- FROM "${tenantId}"."${tableName}"
241
- WHERE "${primaryKeyColumn}" = $1
242
- LIMIT 1
238
+ const targetRecordResult = await this.targetPool.query(`
239
+ SELECT *
240
+ FROM "${tenantId}"."${tableName}"
241
+ WHERE "${primaryKeyColumn}" = $1
242
+ LIMIT 1
243
243
  `, [primaryKeyValue]);
244
244
  if (targetRecordResult.rows.length === 0) {
245
245
  this.issues.push({
@@ -103,8 +103,8 @@ class PreMigrationValidator {
103
103
  async validateApiKeys() {
104
104
  this.logger.log('Validating API keys');
105
105
  try {
106
- const result = await this.sourcePool.query(`
107
- SELECT COUNT(*) as count FROM api_keys
106
+ const result = await this.sourcePool.query(`
107
+ SELECT COUNT(*) as count FROM api_keys
108
108
  `);
109
109
  const count = parseInt(result.rows[0].count);
110
110
  if (count === 0) {
@@ -129,35 +129,35 @@ class PreMigrationValidator {
129
129
  async validateDataTypeCompatibility() {
130
130
  this.logger.log('Validating data type compatibility');
131
131
  try {
132
- const tablesResult = await this.sourcePool.query(`
133
- SELECT table_name
134
- FROM information_schema.tables
135
- WHERE table_schema = 'public'
136
- AND table_type = 'BASE TABLE'
132
+ const tablesResult = await this.sourcePool.query(`
133
+ SELECT table_name
134
+ FROM information_schema.tables
135
+ WHERE table_schema = 'public'
136
+ AND table_type = 'BASE TABLE'
137
137
  `);
138
138
  for (const tableRow of tablesResult.rows) {
139
139
  const tableName = tableRow.table_name;
140
- const sourceColumnsResult = await this.sourcePool.query(`
141
- SELECT column_name, data_type, udt_name
142
- FROM information_schema.columns
143
- WHERE table_schema = 'public'
144
- AND table_name = $1
140
+ const sourceColumnsResult = await this.sourcePool.query(`
141
+ SELECT column_name, data_type, udt_name
142
+ FROM information_schema.columns
143
+ WHERE table_schema = 'public'
144
+ AND table_name = $1
145
145
  `, [tableName]);
146
- const targetTableExists = await this.targetPool.query(`
147
- SELECT 1
148
- FROM information_schema.tables
149
- WHERE table_schema = 'public'
150
- AND table_name = $1
151
- LIMIT 1
146
+ const targetTableExists = await this.targetPool.query(`
147
+ SELECT 1
148
+ FROM information_schema.tables
149
+ WHERE table_schema = 'public'
150
+ AND table_name = $1
151
+ LIMIT 1
152
152
  `, [tableName]);
153
153
  if (targetTableExists.rows.length === 0) {
154
154
  continue;
155
155
  }
156
- const targetColumnsResult = await this.targetPool.query(`
157
- SELECT column_name, data_type, udt_name
158
- FROM information_schema.columns
159
- WHERE table_schema = 'public'
160
- AND table_name = $1
156
+ const targetColumnsResult = await this.targetPool.query(`
157
+ SELECT column_name, data_type, udt_name
158
+ FROM information_schema.columns
159
+ WHERE table_schema = 'public'
160
+ AND table_name = $1
161
161
  `, [tableName]);
162
162
  const sourceColumns = sourceColumnsResult.rows.reduce((map, col) => {
163
163
  map[col.column_name] = col;
@@ -217,28 +217,28 @@ class PreMigrationValidator {
217
217
  async validateColumnSizes() {
218
218
  this.logger.log('Validating column sizes');
219
219
  try {
220
- const tablesResult = await this.sourcePool.query(`
221
- SELECT table_name
222
- FROM information_schema.tables
223
- WHERE table_schema = 'public'
224
- AND table_type = 'BASE TABLE'
220
+ const tablesResult = await this.sourcePool.query(`
221
+ SELECT table_name
222
+ FROM information_schema.tables
223
+ WHERE table_schema = 'public'
224
+ AND table_type = 'BASE TABLE'
225
225
  `);
226
226
  for (const tableRow of tablesResult.rows) {
227
227
  const tableName = tableRow.table_name;
228
- const sourceColumnsResult = await this.sourcePool.query(`
229
- SELECT column_name, data_type, character_maximum_length
230
- FROM information_schema.columns
231
- WHERE table_schema = 'public'
232
- AND table_name = $1
233
- AND data_type IN ('character varying', 'character', 'varchar', 'char')
234
- AND character_maximum_length IS NOT NULL
228
+ const sourceColumnsResult = await this.sourcePool.query(`
229
+ SELECT column_name, data_type, character_maximum_length
230
+ FROM information_schema.columns
231
+ WHERE table_schema = 'public'
232
+ AND table_name = $1
233
+ AND data_type IN ('character varying', 'character', 'varchar', 'char')
234
+ AND character_maximum_length IS NOT NULL
235
235
  `, [tableName]);
236
- const targetTableExists = await this.targetPool.query(`
237
- SELECT 1
238
- FROM information_schema.tables
239
- WHERE table_schema = 'public'
240
- AND table_name = $1
241
- LIMIT 1
236
+ const targetTableExists = await this.targetPool.query(`
237
+ SELECT 1
238
+ FROM information_schema.tables
239
+ WHERE table_schema = 'public'
240
+ AND table_name = $1
241
+ LIMIT 1
242
242
  `, [tableName]);
243
243
  if (targetTableExists.rows.length === 0) {
244
244
  continue;
@@ -246,13 +246,13 @@ class PreMigrationValidator {
246
246
  for (const sourceColumn of sourceColumnsResult.rows) {
247
247
  const columnName = sourceColumn.column_name;
248
248
  const sourceMaxLength = sourceColumn.character_maximum_length;
249
- const targetColumnResult = await this.targetPool.query(`
250
- SELECT character_maximum_length
251
- FROM information_schema.columns
252
- WHERE table_schema = 'public'
253
- AND table_name = $1
254
- AND column_name = $2
255
- AND data_type IN ('character varying', 'character', 'varchar', 'char')
249
+ const targetColumnResult = await this.targetPool.query(`
250
+ SELECT character_maximum_length
251
+ FROM information_schema.columns
252
+ WHERE table_schema = 'public'
253
+ AND table_name = $1
254
+ AND column_name = $2
255
+ AND data_type IN ('character varying', 'character', 'varchar', 'char')
256
256
  `, [tableName, columnName]);
257
257
  if (targetColumnResult.rows.length > 0) {
258
258
  const targetMaxLength = targetColumnResult.rows[0].character_maximum_length;
@@ -282,41 +282,41 @@ class PreMigrationValidator {
282
282
  async validateEnumValues() {
283
283
  this.logger.log('Validating enum values');
284
284
  try {
285
- const sourceEnumsResult = await this.sourcePool.query(`
286
- SELECT t.typname AS enum_name
287
- FROM pg_type t
288
- JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
289
- WHERE t.typtype = 'e'
290
- AND n.nspname = 'public'
285
+ const sourceEnumsResult = await this.sourcePool.query(`
286
+ SELECT t.typname AS enum_name
287
+ FROM pg_type t
288
+ JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
289
+ WHERE t.typtype = 'e'
290
+ AND n.nspname = 'public'
291
291
  `);
292
292
  for (const enumRow of sourceEnumsResult.rows) {
293
293
  const enumName = enumRow.enum_name;
294
- const sourceEnumValuesResult = await this.sourcePool.query(`
295
- SELECT e.enumlabel
296
- FROM pg_enum e
297
- JOIN pg_type t ON e.enumtypid = t.oid
298
- WHERE t.typname = $1
299
- ORDER BY e.enumsortorder
294
+ const sourceEnumValuesResult = await this.sourcePool.query(`
295
+ SELECT e.enumlabel
296
+ FROM pg_enum e
297
+ JOIN pg_type t ON e.enumtypid = t.oid
298
+ WHERE t.typname = $1
299
+ ORDER BY e.enumsortorder
300
300
  `, [enumName]);
301
301
  const sourceEnumValues = sourceEnumValuesResult.rows.map(row => row.enumlabel);
302
- const targetEnumExistsResult = await this.targetPool.query(`
303
- SELECT 1
304
- FROM pg_type t
305
- JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
306
- WHERE t.typtype = 'e'
307
- AND t.typname = $1
308
- AND n.nspname = 'public'
309
- LIMIT 1
302
+ const targetEnumExistsResult = await this.targetPool.query(`
303
+ SELECT 1
304
+ FROM pg_type t
305
+ JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
306
+ WHERE t.typtype = 'e'
307
+ AND t.typname = $1
308
+ AND n.nspname = 'public'
309
+ LIMIT 1
310
310
  `, [enumName]);
311
311
  if (targetEnumExistsResult.rows.length === 0) {
312
312
  continue;
313
313
  }
314
- const targetEnumValuesResult = await this.targetPool.query(`
315
- SELECT e.enumlabel
316
- FROM pg_enum e
317
- JOIN pg_type t ON e.enumtypid = t.oid
318
- WHERE t.typname = $1
319
- ORDER BY e.enumsortorder
314
+ const targetEnumValuesResult = await this.targetPool.query(`
315
+ SELECT e.enumlabel
316
+ FROM pg_enum e
317
+ JOIN pg_type t ON e.enumtypid = t.oid
318
+ WHERE t.typname = $1
319
+ ORDER BY e.enumsortorder
320
320
  `, [enumName]);
321
321
  const targetEnumValues = targetEnumValuesResult.rows.map(row => row.enumlabel);
322
322
  for (const sourceValue of sourceEnumValues) {
@@ -343,22 +343,22 @@ class PreMigrationValidator {
343
343
  async validateForeignKeyConstraints() {
344
344
  this.logger.log('Validating foreign key constraints');
345
345
  try {
346
- const foreignKeysResult = await this.sourcePool.query(`
347
- SELECT
348
- tc.table_name,
349
- kcu.column_name,
350
- ccu.table_name AS foreign_table_name,
351
- ccu.column_name AS foreign_column_name
352
- FROM
353
- information_schema.table_constraints AS tc
354
- JOIN information_schema.key_column_usage AS kcu
355
- ON tc.constraint_name = kcu.constraint_name
356
- AND tc.table_schema = kcu.table_schema
357
- JOIN information_schema.constraint_column_usage AS ccu
358
- ON ccu.constraint_name = tc.constraint_name
359
- AND ccu.table_schema = tc.table_schema
360
- WHERE tc.constraint_type = 'FOREIGN KEY'
361
- AND tc.table_schema = 'public'
346
+ const foreignKeysResult = await this.sourcePool.query(`
347
+ SELECT
348
+ tc.table_name,
349
+ kcu.column_name,
350
+ ccu.table_name AS foreign_table_name,
351
+ ccu.column_name AS foreign_column_name
352
+ FROM
353
+ information_schema.table_constraints AS tc
354
+ JOIN information_schema.key_column_usage AS kcu
355
+ ON tc.constraint_name = kcu.constraint_name
356
+ AND tc.table_schema = kcu.table_schema
357
+ JOIN information_schema.constraint_column_usage AS ccu
358
+ ON ccu.constraint_name = tc.constraint_name
359
+ AND ccu.table_schema = tc.table_schema
360
+ WHERE tc.constraint_type = 'FOREIGN KEY'
361
+ AND tc.table_schema = 'public'
362
362
  `);
363
363
  this.logger.log(`Found ${foreignKeysResult.rows.length} foreign key constraints in source database`);
364
364
  }
@@ -373,28 +373,28 @@ class PreMigrationValidator {
373
373
  async validateUniqueConstraints() {
374
374
  this.logger.log('Validating unique constraints...');
375
375
  try {
376
- const sourceConstraintsQuery = `
377
- SELECT
378
- tc.table_name,
379
- kcu.column_name
380
- FROM
381
- information_schema.table_constraints tc
382
- JOIN information_schema.key_column_usage kcu
383
- ON tc.constraint_name = kcu.constraint_name
384
- WHERE
385
- tc.constraint_type = 'UNIQUE'
386
- AND tc.table_schema = 'public'
376
+ const sourceConstraintsQuery = `
377
+ SELECT
378
+ tc.table_name,
379
+ kcu.column_name
380
+ FROM
381
+ information_schema.table_constraints tc
382
+ JOIN information_schema.key_column_usage kcu
383
+ ON tc.constraint_name = kcu.constraint_name
384
+ WHERE
385
+ tc.constraint_type = 'UNIQUE'
386
+ AND tc.table_schema = 'public'
387
387
  `;
388
388
  const sourceConstraints = await this.sourcePool.query(sourceConstraintsQuery);
389
389
  for (const constraint of sourceConstraints.rows) {
390
390
  const { table_name, column_name } = constraint;
391
391
  try {
392
- const columnExistsQuery = `
393
- SELECT column_name
394
- FROM information_schema.columns
395
- WHERE table_name = $1
396
- AND column_name = $2
397
- AND table_schema = 'public'
392
+ const columnExistsQuery = `
393
+ SELECT column_name
394
+ FROM information_schema.columns
395
+ WHERE table_name = $1
396
+ AND column_name = $2
397
+ AND table_schema = 'public'
398
398
  `;
399
399
  const columnExists = await this.targetPool.query(columnExistsQuery, [table_name, column_name]);
400
400
  if (columnExists.rows.length === 0) {
@@ -105,12 +105,12 @@ class DatabaseResetTool {
105
105
  async dropAllCustomTypes() {
106
106
  this.logger.log("Dropping all custom types (enums, domains, etc.)");
107
107
  try {
108
- const typesQuery = `
109
- SELECT t.typname AS name
110
- FROM pg_type t
111
- JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
112
- WHERE n.nspname = 'public'
113
- AND t.typtype = 'e' -- enum types
108
+ const typesQuery = `
109
+ SELECT t.typname AS name
110
+ FROM pg_type t
111
+ JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
112
+ WHERE n.nspname = 'public'
113
+ AND t.typtype = 'e' -- enum types
114
114
  `;
115
115
  const typesResult = await this.pool.query(typesQuery);
116
116
  const types = typesResult.rows.map(row => row.name);
@@ -130,20 +130,20 @@ class DatabaseResetTool {
130
130
  }
131
131
  }
132
132
  async getCustomSchemas() {
133
- const result = await this.pool.query(`
134
- SELECT schema_name
135
- FROM information_schema.schemata
136
- WHERE schema_name NOT IN ('public', 'information_schema', 'pg_catalog', 'pg_toast')
137
- AND schema_name NOT LIKE 'pg_%'
133
+ const result = await this.pool.query(`
134
+ SELECT schema_name
135
+ FROM information_schema.schemata
136
+ WHERE schema_name NOT IN ('public', 'information_schema', 'pg_catalog', 'pg_toast')
137
+ AND schema_name NOT LIKE 'pg_%'
138
138
  `);
139
139
  return result.rows.map(row => row.schema_name);
140
140
  }
141
141
  async getPublicTables() {
142
- const result = await this.pool.query(`
143
- SELECT table_name
144
- FROM information_schema.tables
145
- WHERE table_schema = 'public'
146
- AND table_type = 'BASE TABLE'
142
+ const result = await this.pool.query(`
143
+ SELECT table_name
144
+ FROM information_schema.tables
145
+ WHERE table_schema = 'public'
146
+ AND table_type = 'BASE TABLE'
147
147
  `);
148
148
  return result.rows.map(row => row.table_name);
149
149
  }
@@ -154,11 +154,11 @@ class DatabaseResetTool {
154
154
  async terminateConnections(dbName) {
155
155
  this.logger.log(`Terminating all connections to database: ${dbName}`);
156
156
  try {
157
- await this.adminPool.query(`
158
- SELECT pg_terminate_backend(pg_stat_activity.pid)
159
- FROM pg_stat_activity
160
- WHERE pg_stat_activity.datname = $1
161
- AND pid <> pg_backend_pid()
157
+ await this.adminPool.query(`
158
+ SELECT pg_terminate_backend(pg_stat_activity.pid)
159
+ FROM pg_stat_activity
160
+ WHERE pg_stat_activity.datname = $1
161
+ AND pid <> pg_backend_pid()
162
162
  `, [dbName]);
163
163
  this.logger.log("All connections terminated successfully");
164
164
  }
@@ -131,10 +131,10 @@ class FailedMigrationRetry {
131
131
  if (columnNames.includes(recordId)) {
132
132
  createData[recordId] = data[recordId];
133
133
  }
134
- const checkExistQuery = `
135
- SELECT 1 FROM "${tenantId}"."${entity}"
136
- WHERE "${recordId}" = $1
137
- LIMIT 1
134
+ const checkExistQuery = `
135
+ SELECT 1 FROM "${tenantId}"."${entity}"
136
+ WHERE "${recordId}" = $1
137
+ LIMIT 1
138
138
  `;
139
139
  const existResult = await this.targetPool.query(checkExistQuery, [
140
140
  data[recordId],
@@ -150,10 +150,10 @@ class FailedMigrationRetry {
150
150
  return `"${key}" = $${index + 2}`;
151
151
  });
152
152
  if (updateFields.length > 0) {
153
- const updateQuery = `
154
- UPDATE "${tenantId}"."${entity}"
155
- SET ${updateFields.join(", ")}
156
- WHERE "${recordId}" = $1
153
+ const updateQuery = `
154
+ UPDATE "${tenantId}"."${entity}"
155
+ SET ${updateFields.join(", ")}
156
+ WHERE "${recordId}" = $1
157
157
  `;
158
158
  const updateValues = [
159
159
  data[recordId],
@@ -176,9 +176,9 @@ class FailedMigrationRetry {
176
176
  }
177
177
  return `$${index + 1}`;
178
178
  });
179
- const insertQuery = `
180
- INSERT INTO "${tenantId}"."${entity}" (${insertFields.join(", ")})
181
- VALUES (${insertPlaceholders.join(", ")})
179
+ const insertQuery = `
180
+ INSERT INTO "${tenantId}"."${entity}" (${insertFields.join(", ")})
181
+ VALUES (${insertPlaceholders.join(", ")})
182
182
  `;
183
183
  const insertValues = Object.keys(createData)
184
184
  .filter(key => key !== '_enumTypes')
@@ -196,11 +196,11 @@ class FailedMigrationRetry {
196
196
  }
197
197
  await tenantPrisma.$disconnect();
198
198
  }
199
- this.logger.log(`
200
- Retry Summary:
201
- - Total errors: ${totalErrors}
202
- - Successfully retried: ${retrySuccessCount}
203
- - Failed retries: ${retryFailCount}
199
+ this.logger.log(`
200
+ Retry Summary:
201
+ - Total errors: ${totalErrors}
202
+ - Successfully retried: ${retrySuccessCount}
203
+ - Failed retries: ${retryFailCount}
204
204
  `);
205
205
  const remainingErrorsReport = this.errorHandler.generateErrorReport();
206
206
  this.logger.log("Remaining Errors Report:\n" + remainingErrorsReport);
@@ -214,12 +214,12 @@ Retry Summary:
214
214
  }
215
215
  async getTableSchema(tableName, schemaName) {
216
216
  try {
217
- const schemaQuery = `
218
- SELECT column_name, data_type, column_default, is_nullable, udt_name
219
- FROM information_schema.columns
220
- WHERE table_name = $1
221
- AND table_schema = $2
222
- ORDER BY ordinal_position
217
+ const schemaQuery = `
218
+ SELECT column_name, data_type, column_default, is_nullable, udt_name
219
+ FROM information_schema.columns
220
+ WHERE table_name = $1
221
+ AND table_schema = $2
222
+ ORDER BY ordinal_position
223
223
  `;
224
224
  const result = await this.targetPool.query(schemaQuery, [
225
225
  tableName,
@@ -234,12 +234,12 @@ Retry Summary:
234
234
  }
235
235
  async getEnumValues(enumTypeName) {
236
236
  try {
237
- const enumQuery = `
238
- SELECT e.enumlabel
239
- FROM pg_enum e
240
- JOIN pg_type t ON e.enumtypid = t.oid
241
- WHERE t.typname = $1
242
- ORDER BY e.enumsortorder
237
+ const enumQuery = `
238
+ SELECT e.enumlabel
239
+ FROM pg_enum e
240
+ JOIN pg_type t ON e.enumtypid = t.oid
241
+ WHERE t.typname = $1
242
+ ORDER BY e.enumsortorder
243
243
  `;
244
244
  const result = await this.targetPool.query(enumQuery, [enumTypeName]);
245
245
  return result.rows.map((row) => row.enumlabel);