@javalabs/prisma-client 1.0.27 → 1.0.30

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 (168) hide show
  1. package/.github/CODEOWNERS +1 -1
  2. package/README.md +269 -269
  3. package/dist/index.d.ts +7 -0
  4. package/dist/index.js +34 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/prisma-factory.service.d.ts +9 -0
  7. package/dist/prisma-factory.service.js +47 -0
  8. package/dist/prisma-factory.service.js.map +1 -0
  9. package/dist/prisma.module.d.ts +2 -0
  10. package/dist/prisma.module.js +23 -0
  11. package/dist/prisma.module.js.map +1 -0
  12. package/dist/prisma.service.d.ts +6 -0
  13. package/dist/prisma.service.js +27 -0
  14. package/dist/prisma.service.js.map +1 -0
  15. package/dist/scripts/add-uuid-to-table.d.ts +8 -0
  16. package/dist/scripts/add-uuid-to-table.js +98 -0
  17. package/dist/scripts/add-uuid-to-table.js.map +1 -0
  18. package/dist/scripts/create-tenant-schemas.d.ts +1 -0
  19. package/dist/scripts/create-tenant-schemas.js +117 -0
  20. package/dist/scripts/create-tenant-schemas.js.map +1 -0
  21. package/dist/scripts/data-migration/batch-migrator.d.ts +20 -0
  22. package/dist/scripts/data-migration/batch-migrator.js +134 -0
  23. package/dist/scripts/data-migration/batch-migrator.js.map +1 -0
  24. package/dist/scripts/data-migration/data-transformer.d.ts +26 -0
  25. package/dist/scripts/data-migration/data-transformer.js +278 -0
  26. package/dist/scripts/data-migration/data-transformer.js.map +1 -0
  27. package/dist/scripts/data-migration/db-connector.d.ts +12 -0
  28. package/dist/scripts/data-migration/db-connector.js +94 -0
  29. package/dist/scripts/data-migration/db-connector.js.map +1 -0
  30. package/dist/scripts/data-migration/dependency-resolver.d.ts +18 -0
  31. package/dist/scripts/data-migration/dependency-resolver.js +132 -0
  32. package/dist/scripts/data-migration/dependency-resolver.js.map +1 -0
  33. package/dist/scripts/data-migration/entity-discovery.d.ts +11 -0
  34. package/dist/scripts/data-migration/entity-discovery.js +152 -0
  35. package/dist/scripts/data-migration/entity-discovery.js.map +1 -0
  36. package/dist/scripts/data-migration/foreign-key-manager.d.ts +18 -0
  37. package/dist/scripts/data-migration/foreign-key-manager.js +160 -0
  38. package/dist/scripts/data-migration/foreign-key-manager.js.map +1 -0
  39. package/dist/scripts/data-migration/migration-tool.d.ts +48 -0
  40. package/dist/scripts/data-migration/migration-tool.js +290 -0
  41. package/dist/scripts/data-migration/migration-tool.js.map +1 -0
  42. package/dist/scripts/data-migration/schema-utils.d.ts +18 -0
  43. package/dist/scripts/data-migration/schema-utils.js +207 -0
  44. package/dist/scripts/data-migration/schema-utils.js.map +1 -0
  45. package/dist/scripts/data-migration/tenant-migrator.d.ts +15 -0
  46. package/dist/scripts/data-migration/tenant-migrator.js +117 -0
  47. package/dist/scripts/data-migration/tenant-migrator.js.map +1 -0
  48. package/dist/scripts/data-migration/typecast-manager.d.ts +9 -0
  49. package/dist/scripts/data-migration/typecast-manager.js +179 -0
  50. package/dist/scripts/data-migration/typecast-manager.js.map +1 -0
  51. package/dist/scripts/data-migration/types.d.ts +100 -0
  52. package/dist/scripts/data-migration/types.js +3 -0
  53. package/dist/scripts/data-migration/types.js.map +1 -0
  54. package/dist/scripts/database-initializer.d.ts +5 -0
  55. package/dist/scripts/database-initializer.js +45 -0
  56. package/dist/scripts/database-initializer.js.map +1 -0
  57. package/dist/scripts/drop-database.d.ts +10 -0
  58. package/dist/scripts/drop-database.js +81 -0
  59. package/dist/scripts/drop-database.js.map +1 -0
  60. package/dist/scripts/encrypt-user-passwords.d.ts +1 -0
  61. package/dist/scripts/encrypt-user-passwords.js +33 -0
  62. package/dist/scripts/encrypt-user-passwords.js.map +1 -0
  63. package/dist/scripts/error-handler.d.ts +12 -0
  64. package/dist/scripts/error-handler.js +82 -0
  65. package/dist/scripts/error-handler.js.map +1 -0
  66. package/dist/scripts/fix-data-types.d.ts +10 -0
  67. package/dist/scripts/fix-data-types.js +185 -0
  68. package/dist/scripts/fix-data-types.js.map +1 -0
  69. package/dist/scripts/fix-enum-values.d.ts +17 -0
  70. package/dist/scripts/fix-enum-values.js +234 -0
  71. package/dist/scripts/fix-enum-values.js.map +1 -0
  72. package/dist/scripts/fix-schema-discrepancies.d.ts +21 -0
  73. package/dist/scripts/fix-schema-discrepancies.js +240 -0
  74. package/dist/scripts/fix-schema-discrepancies.js.map +1 -0
  75. package/dist/scripts/fix-table-indexes.d.ts +26 -0
  76. package/dist/scripts/fix-table-indexes.js +460 -0
  77. package/dist/scripts/fix-table-indexes.js.map +1 -0
  78. package/dist/scripts/migrate-schema-structure.d.ts +1 -0
  79. package/dist/scripts/migrate-schema-structure.js +76 -0
  80. package/dist/scripts/migrate-schema-structure.js.map +1 -0
  81. package/dist/scripts/migrate-uuid.d.ts +2 -0
  82. package/dist/scripts/migrate-uuid.js +57 -0
  83. package/dist/scripts/migrate-uuid.js.map +1 -0
  84. package/dist/scripts/post-migration-validator.d.ts +34 -0
  85. package/dist/scripts/post-migration-validator.js +363 -0
  86. package/dist/scripts/post-migration-validator.js.map +1 -0
  87. package/dist/scripts/pre-migration-validator.d.ts +25 -0
  88. package/dist/scripts/pre-migration-validator.js +491 -0
  89. package/dist/scripts/pre-migration-validator.js.map +1 -0
  90. package/dist/scripts/reset-database.d.ts +17 -0
  91. package/dist/scripts/reset-database.js +202 -0
  92. package/dist/scripts/reset-database.js.map +1 -0
  93. package/dist/scripts/retry-failed-migrations.d.ts +14 -0
  94. package/dist/scripts/retry-failed-migrations.js +301 -0
  95. package/dist/scripts/retry-failed-migrations.js.map +1 -0
  96. package/dist/scripts/run-migration.d.ts +1 -0
  97. package/dist/scripts/run-migration.js +512 -0
  98. package/dist/scripts/run-migration.js.map +1 -0
  99. package/dist/scripts/schema-sync.d.ts +1 -0
  100. package/dist/scripts/schema-sync.js +85 -0
  101. package/dist/scripts/schema-sync.js.map +1 -0
  102. package/dist/scripts/sequence-sync-cli.d.ts +2 -0
  103. package/dist/scripts/sequence-sync-cli.js +287 -0
  104. package/dist/scripts/sequence-sync-cli.js.map +1 -0
  105. package/dist/scripts/sequence-synchronizer.d.ts +8 -0
  106. package/dist/scripts/sequence-synchronizer.js +88 -0
  107. package/dist/scripts/sequence-synchronizer.js.map +1 -0
  108. package/dist/scripts/sync-enum-types.d.ts +13 -0
  109. package/dist/scripts/sync-enum-types.js +139 -0
  110. package/dist/scripts/sync-enum-types.js.map +1 -0
  111. package/dist/scripts/sync-enum-values.d.ts +20 -0
  112. package/dist/scripts/sync-enum-values.js +336 -0
  113. package/dist/scripts/sync-enum-values.js.map +1 -0
  114. package/dist/scripts/truncate-database.d.ts +10 -0
  115. package/dist/scripts/truncate-database.js +100 -0
  116. package/dist/scripts/truncate-database.js.map +1 -0
  117. package/dist/scripts/verify-migration-setup.d.ts +11 -0
  118. package/dist/scripts/verify-migration-setup.js +120 -0
  119. package/dist/scripts/verify-migration-setup.js.map +1 -0
  120. package/dist/tsconfig.tsbuildinfo +1 -0
  121. package/migration-config.json +63 -63
  122. package/migration-config.json.bk +95 -95
  123. package/migrations/add_reserved_amount.sql +7 -7
  124. package/package.json +44 -44
  125. package/prisma/migrations/add_uuid_to_transactions.sql +13 -13
  126. package/prisma/schema.prisma +609 -601
  127. package/src/index.ts +23 -23
  128. package/src/prisma-factory.service.ts +40 -40
  129. package/src/prisma.module.ts +9 -9
  130. package/src/prisma.service.ts +16 -16
  131. package/src/scripts/add-uuid-to-table.ts +138 -138
  132. package/src/scripts/create-tenant-schemas.ts +145 -145
  133. package/src/scripts/data-migration/batch-migrator.ts +248 -248
  134. package/src/scripts/data-migration/data-transformer.ts +426 -426
  135. package/src/scripts/data-migration/db-connector.ts +120 -120
  136. package/src/scripts/data-migration/dependency-resolver.ts +174 -174
  137. package/src/scripts/data-migration/entity-discovery.ts +196 -196
  138. package/src/scripts/data-migration/foreign-key-manager.ts +277 -277
  139. package/src/scripts/data-migration/migration-config.json +63 -63
  140. package/src/scripts/data-migration/migration-tool.ts +509 -509
  141. package/src/scripts/data-migration/schema-utils.ts +248 -248
  142. package/src/scripts/data-migration/tenant-migrator.ts +201 -201
  143. package/src/scripts/data-migration/typecast-manager.ts +193 -193
  144. package/src/scripts/data-migration/types.ts +113 -113
  145. package/src/scripts/database-initializer.ts +49 -49
  146. package/src/scripts/drop-database.ts +104 -104
  147. package/src/scripts/dump-source-db.sh +61 -61
  148. package/src/scripts/encrypt-user-passwords.ts +36 -36
  149. package/src/scripts/error-handler.ts +117 -117
  150. package/src/scripts/fix-data-types.ts +241 -241
  151. package/src/scripts/fix-enum-values.ts +357 -357
  152. package/src/scripts/fix-schema-discrepancies.ts +317 -317
  153. package/src/scripts/fix-table-indexes.ts +601 -601
  154. package/src/scripts/migrate-schema-structure.ts +90 -90
  155. package/src/scripts/migrate-uuid.ts +76 -76
  156. package/src/scripts/post-migration-validator.ts +526 -526
  157. package/src/scripts/pre-migration-validator.ts +610 -610
  158. package/src/scripts/reset-database.ts +263 -263
  159. package/src/scripts/retry-failed-migrations.ts +416 -416
  160. package/src/scripts/run-migration.ts +707 -707
  161. package/src/scripts/schema-sync.ts +128 -128
  162. package/src/scripts/sequence-sync-cli.ts +416 -416
  163. package/src/scripts/sequence-synchronizer.ts +127 -127
  164. package/src/scripts/sync-enum-types.ts +170 -170
  165. package/src/scripts/sync-enum-values.ts +563 -563
  166. package/src/scripts/truncate-database.ts +123 -123
  167. package/src/scripts/verify-migration-setup.ts +135 -135
  168. package/tsconfig.json +17 -17
@@ -1,526 +1,526 @@
1
- import * as pg from "pg";
2
- import * as dotenv from "dotenv";
3
- import { Logger } from "@nestjs/common";
4
- import * as fs from "fs";
5
- import * as path from "path";
6
-
7
- dotenv.config();
8
-
9
- interface ValidationIssue {
10
- type: string;
11
- tenantId?: string;
12
- providerId?: number | null;
13
- message: string;
14
- }
15
-
16
- interface ValidationResult {
17
- success: boolean;
18
- issueCount: number;
19
- timestamp: string;
20
- sourceDatabase: string;
21
- targetDatabase: string;
22
- issues: ValidationIssue[];
23
- }
24
-
25
- export class PostMigrationValidator {
26
- private readonly logger = new Logger("PostMigrationValidator");
27
- private readonly sourcePool: pg.Pool;
28
- private readonly targetPool: pg.Pool;
29
- private readonly reportPath: string;
30
- private issues: any[] = [];
31
-
32
- constructor(
33
- private readonly sourceUrl: string = process.env.SOURCE_DATABASE_URL,
34
- private readonly targetUrl: string = process.env.DATABASE_URL
35
- ) {
36
- this.sourcePool = new pg.Pool({
37
- connectionString: this.sourceUrl,
38
- });
39
-
40
- this.targetPool = new pg.Pool({
41
- connectionString: this.targetUrl,
42
- });
43
-
44
- // Crear directorio para reportes si no existe
45
- const reportsDir = path.join(process.cwd(), "migration-logs");
46
- if (!fs.existsSync(reportsDir)) {
47
- fs.mkdirSync(reportsDir, { recursive: true });
48
- }
49
-
50
- // Archivo para guardar el reporte
51
- const timestamp = new Date()
52
- .toISOString()
53
- .replace(/:/g, "-")
54
- .replace(/\..+/, "");
55
- this.reportPath = path.join(
56
- reportsDir,
57
- `post-migration-report-${timestamp}.json`
58
- );
59
- }
60
-
61
- async validate(options?: {
62
- publicOnly?: boolean;
63
- }): Promise<ValidationResult> {
64
- const issues: ValidationIssue[] = [];
65
- const timestamp = new Date().toISOString();
66
-
67
- try {
68
- // Si estamos en modo public-only, solo validamos el esquema público
69
- if (options?.publicOnly) {
70
- this.logger.log("Running validation in public-only mode");
71
- // Aquí solo validaríamos las tablas públicas
72
- return {
73
- success: true,
74
- issueCount: 0,
75
- timestamp,
76
- sourceDatabase: process.env.SOURCE_DATABASE_URL,
77
- targetDatabase: process.env.DATABASE_URL,
78
- issues: [],
79
- };
80
- }
81
-
82
- // Si no es public-only, procedemos con la validación normal de todos los schemas
83
- this.logger.log("Starting post-migration validation");
84
-
85
- // Obtener todos los API keys para determinar los schemas de tenant
86
- const apiKeysResult = await this.sourcePool.query(`
87
- SELECT api_key, provider_id
88
- FROM api_keys
89
- `);
90
-
91
- this.logger.log(
92
- `Found ${apiKeysResult.rows.length} API keys to validate`
93
- );
94
-
95
- // Para cada API key (tenant), validar los datos migrados
96
- for (const apiKey of apiKeysResult.rows) {
97
- const tenantId = apiKey.api_key;
98
- const providerId = apiKey.provider_id;
99
-
100
- await this.validateTenantData(tenantId, providerId);
101
- }
102
-
103
- // Guardar el reporte
104
- this.saveReport();
105
-
106
- // Mostrar resumen
107
- this.logger.log(
108
- `Validation completed with ${this.issues.length} issues found`
109
- );
110
- if (this.issues.length > 0) {
111
- this.logger.log(`Check the full report at: ${this.reportPath}`);
112
-
113
- // Mostrar los primeros 5 problemas como ejemplo
114
- this.logger.log("Sample issues:");
115
- for (let i = 0; i < Math.min(5, this.issues.length); i++) {
116
- const issue = this.issues[i];
117
- this.logger.log(`- ${issue.type}: ${issue.message}`);
118
- }
119
- } else {
120
- this.logger.log("No issues found. Migration was successful.");
121
- }
122
-
123
- return {
124
- success: this.issues.length === 0,
125
- issueCount: this.issues.length,
126
- timestamp,
127
- sourceDatabase: this.sourceUrl,
128
- targetDatabase: this.targetUrl,
129
- issues: this.issues,
130
- };
131
- } catch (error) {
132
- this.logger.error(
133
- `Error during validation: ${error.message}`,
134
- error.stack
135
- );
136
- this.issues.push({
137
- type: "VALIDATION_ERROR",
138
- message: `Validation process failed: ${error.message}`,
139
- details: error.stack,
140
- });
141
- this.saveReport();
142
- return {
143
- success: false,
144
- issueCount: this.issues.length,
145
- timestamp,
146
- sourceDatabase: this.sourceUrl,
147
- targetDatabase: this.targetUrl,
148
- issues: this.issues,
149
- };
150
- } finally {
151
- await this.cleanup();
152
- }
153
- }
154
-
155
- private async validateTenantData(tenantId: string, providerId: number) {
156
- this.logger.log(
157
- `Validating data for tenant: ${tenantId} (Provider ID: ${providerId})`
158
- );
159
-
160
- try {
161
- // Verificar si el schema existe en la base de datos de destino
162
- const schemaExistsResult = await this.targetPool.query(
163
- `
164
- SELECT 1
165
- FROM information_schema.schemata
166
- WHERE schema_name = $1
167
- LIMIT 1
168
- `,
169
- [tenantId]
170
- );
171
-
172
- if (schemaExistsResult.rows.length === 0) {
173
- this.logger.warn(
174
- `Schema ${tenantId} does not exist in target database`
175
- );
176
- this.issues.push({
177
- type: "MISSING_SCHEMA",
178
- tenantId,
179
- providerId,
180
- message: `Schema ${tenantId} does not exist in target database. Migration failed for this tenant.`,
181
- });
182
- return;
183
- }
184
-
185
- // Obtener todas las tablas de la base de datos de origen
186
- const tablesResult = await this.sourcePool.query(`
187
- SELECT table_name
188
- FROM information_schema.tables
189
- WHERE table_schema = 'public'
190
- AND table_type = 'BASE TABLE'
191
- AND table_name NOT IN ('_prisma_migrations', 'api_keys')
192
- `);
193
-
194
- // Para cada tabla, comparar el número de registros
195
- for (const tableRow of tablesResult.rows) {
196
- const tableName = tableRow.table_name;
197
- await this.validateTableData(tenantId, tableName, providerId);
198
- }
199
- } catch (error) {
200
- this.logger.error(
201
- `Error validating tenant ${tenantId}: ${error.message}`
202
- );
203
- this.issues.push({
204
- type: "TENANT_VALIDATION_ERROR",
205
- tenantId,
206
- providerId,
207
- message: `Error validating tenant ${tenantId}: ${error.message}`,
208
- });
209
- }
210
- }
211
-
212
- private async validateTableData(
213
- tenantId: string,
214
- tableName: string,
215
- providerId: number
216
- ) {
217
- try {
218
- // Contar registros en la base de datos de origen
219
- let sourceCountQuery = `SELECT COUNT(*) as count FROM ${tableName}`;
220
-
221
- // Añadir filtro por provider_id si la tabla tiene esa columna
222
- const hasProviderIdResult = await this.sourcePool.query(
223
- `
224
- SELECT 1
225
- FROM information_schema.columns
226
- WHERE table_schema = 'public'
227
- AND table_name = $1
228
- AND column_name = 'provider_id'
229
- LIMIT 1
230
- `,
231
- [tableName]
232
- );
233
-
234
- if (hasProviderIdResult.rows.length > 0) {
235
- sourceCountQuery += ` WHERE provider_id = ${providerId}`;
236
- }
237
-
238
- const sourceCountResult = await this.sourcePool.query(sourceCountQuery);
239
- const sourceCount = parseInt(sourceCountResult.rows[0].count);
240
-
241
- // Contar registros en la base de datos de destino
242
- const targetCountQuery = `SELECT COUNT(*) as count FROM "${tenantId}"."${tableName}"`;
243
- const targetCountResult = await this.targetPool.query(targetCountQuery);
244
- const targetCount = parseInt(targetCountResult.rows[0].count);
245
-
246
- // Calcular el porcentaje de migración
247
- const migrationPercentage =
248
- sourceCount > 0 ? (targetCount / sourceCount) * 100 : 100;
249
-
250
- this.logger.log(
251
- `Table ${tableName} for tenant ${tenantId}: ${targetCount}/${sourceCount} records migrated (${migrationPercentage.toFixed(
252
- 2
253
- )}%)`
254
- );
255
-
256
- // Si hay una discrepancia significativa, registrarla como un problema
257
- if (migrationPercentage < 95) {
258
- this.issues.push({
259
- type: "DATA_MISMATCH",
260
- tenantId,
261
- tableName,
262
- sourceCount,
263
- targetCount,
264
- migrationPercentage: parseFloat(migrationPercentage.toFixed(2)),
265
- message: `Table ${tableName} for tenant ${tenantId}: Only ${migrationPercentage.toFixed(
266
- 2
267
- )}% of records were migrated (${targetCount}/${sourceCount})`,
268
- });
269
- }
270
-
271
- // Si hay más registros en el destino que en el origen, también es un problema
272
- if (targetCount > sourceCount) {
273
- this.issues.push({
274
- type: "EXCESS_DATA",
275
- tenantId,
276
- tableName,
277
- sourceCount,
278
- targetCount,
279
- message: `Table ${tableName} for tenant ${tenantId}: Target has more records (${targetCount}) than source (${sourceCount})`,
280
- });
281
- }
282
-
283
- // Validar algunos registros aleatorios para verificar la integridad de los datos
284
- if (sourceCount > 0 && targetCount > 0) {
285
- await this.validateRandomRecords(tenantId, tableName, providerId);
286
- }
287
- } catch (error) {
288
- this.logger.error(
289
- `Error validating table ${tableName} for tenant ${tenantId}: ${error.message}`
290
- );
291
- this.issues.push({
292
- type: "TABLE_VALIDATION_ERROR",
293
- tenantId,
294
- tableName,
295
- message: `Error validating table ${tableName} for tenant ${tenantId}: ${error.message}`,
296
- });
297
- }
298
- }
299
-
300
- private async validateRandomRecords(
301
- tenantId: string,
302
- tableName: string,
303
- providerId: number
304
- ) {
305
- try {
306
- // Obtener la clave primaria de la tabla
307
- const primaryKeyResult = await this.sourcePool.query(
308
- `
309
- SELECT kcu.column_name
310
- FROM information_schema.table_constraints tc
311
- JOIN information_schema.key_column_usage kcu
312
- ON tc.constraint_name = kcu.constraint_name
313
- AND tc.table_schema = kcu.table_schema
314
- WHERE tc.constraint_type = 'PRIMARY KEY'
315
- AND tc.table_schema = 'public'
316
- AND tc.table_name = $1
317
- LIMIT 1
318
- `,
319
- [tableName]
320
- );
321
-
322
- if (primaryKeyResult.rows.length === 0) {
323
- this.logger.warn(`No primary key found for table ${tableName}`);
324
- return;
325
- }
326
-
327
- const primaryKeyColumn = primaryKeyResult.rows[0].column_name;
328
-
329
- // Obtener hasta 5 registros aleatorios de la base de datos de origen
330
- let randomRecordsQuery = `
331
- SELECT *
332
- FROM ${tableName}
333
- `;
334
-
335
- // Añadir filtro por provider_id si la tabla tiene esa columna
336
- const hasProviderIdResult = await this.sourcePool.query(
337
- `
338
- SELECT 1
339
- FROM information_schema.columns
340
- WHERE table_schema = 'public'
341
- AND table_name = $1
342
- AND column_name = 'provider_id'
343
- LIMIT 1
344
- `,
345
- [tableName]
346
- );
347
-
348
- if (hasProviderIdResult.rows.length > 0) {
349
- randomRecordsQuery += ` WHERE provider_id = ${providerId}`;
350
- }
351
-
352
- randomRecordsQuery += `
353
- ORDER BY RANDOM()
354
- LIMIT 5
355
- `;
356
-
357
- const randomRecordsResult = await this.sourcePool.query(
358
- randomRecordsQuery
359
- );
360
-
361
- // Para cada registro aleatorio, verificar si existe en la base de datos de destino
362
- for (const sourceRecord of randomRecordsResult.rows) {
363
- const primaryKeyValue = sourceRecord[primaryKeyColumn];
364
-
365
- const targetRecordResult = await this.targetPool.query(
366
- `
367
- SELECT *
368
- FROM "${tenantId}"."${tableName}"
369
- WHERE "${primaryKeyColumn}" = $1
370
- LIMIT 1
371
- `,
372
- [primaryKeyValue]
373
- );
374
-
375
- if (targetRecordResult.rows.length === 0) {
376
- this.issues.push({
377
- type: "MISSING_RECORD",
378
- tenantId,
379
- tableName,
380
- recordId: primaryKeyValue,
381
- message: `Record with ID ${primaryKeyValue} in table ${tableName} for tenant ${tenantId} was not migrated`,
382
- });
383
- continue;
384
- }
385
-
386
- const targetRecord = targetRecordResult.rows[0];
387
-
388
- // Comparar los valores de las columnas
389
- for (const columnName in sourceRecord) {
390
- // Ignorar columnas específicas de la base de datos de origen
391
- if (columnName === "provider_id") {
392
- continue;
393
- }
394
-
395
- // Verificar si la columna existe en el registro de destino
396
- if (!(columnName in targetRecord)) {
397
- this.issues.push({
398
- type: "MISSING_COLUMN",
399
- tenantId,
400
- tableName,
401
- recordId: primaryKeyValue,
402
- columnName,
403
- message: `Column ${columnName} is missing in record with ID ${primaryKeyValue} in table ${tableName} for tenant ${tenantId}`,
404
- });
405
- continue;
406
- }
407
-
408
- // Comparar los valores (con manejo especial para tipos de datos específicos)
409
- const sourceValue = sourceRecord[columnName];
410
- const targetValue = targetRecord[columnName];
411
-
412
- if (this.areValuesDifferent(sourceValue, targetValue)) {
413
- this.issues.push({
414
- type: "VALUE_MISMATCH",
415
- tenantId,
416
- tableName,
417
- recordId: primaryKeyValue,
418
- columnName,
419
- sourceValue,
420
- targetValue,
421
- message: `Value mismatch for column ${columnName} in record with ID ${primaryKeyValue} in table ${tableName} for tenant ${tenantId}: ${sourceValue} (source) vs ${targetValue} (target)`,
422
- });
423
- }
424
- }
425
- }
426
- } catch (error) {
427
- this.logger.error(
428
- `Error validating random records for table ${tableName} in tenant ${tenantId}: ${error.message}`
429
- );
430
- this.issues.push({
431
- type: "RECORD_VALIDATION_ERROR",
432
- tenantId,
433
- tableName,
434
- message: `Error validating random records for table ${tableName} in tenant ${tenantId}: ${error.message}`,
435
- });
436
- }
437
- }
438
-
439
- private areValuesDifferent(sourceValue: any, targetValue: any): boolean {
440
- // Si ambos son null o undefined, son iguales
441
- if (sourceValue == null && targetValue == null) {
442
- return false;
443
- }
444
-
445
- // Si uno es null y el otro no, son diferentes
446
- if (sourceValue == null || targetValue == null) {
447
- return true;
448
- }
449
-
450
- // Para fechas, comparar como strings ISO
451
- if (sourceValue instanceof Date && targetValue instanceof Date) {
452
- return sourceValue.toISOString() !== targetValue.toISOString();
453
- }
454
-
455
- // Para números, comparar con tolerancia para punto flotante
456
- if (typeof sourceValue === "number" && typeof targetValue === "number") {
457
- const epsilon = 0.0001;
458
- return Math.abs(sourceValue - targetValue) > epsilon;
459
- }
460
-
461
- // Para strings, comparar directamente
462
- if (typeof sourceValue === "string" && typeof targetValue === "string") {
463
- return sourceValue !== targetValue;
464
- }
465
-
466
- // Para booleanos, comparar directamente
467
- if (typeof sourceValue === "boolean" && typeof targetValue === "boolean") {
468
- return sourceValue !== targetValue;
469
- }
470
-
471
- // Para otros tipos, convertir a string y comparar
472
- return String(sourceValue) !== String(targetValue);
473
- }
474
-
475
- private saveReport() {
476
- try {
477
- const report = {
478
- timestamp: new Date().toISOString(),
479
- sourceDatabase: this.sourceUrl,
480
- targetDatabase: this.targetUrl,
481
- issueCount: this.issues.length,
482
- issues: this.issues,
483
- };
484
-
485
- fs.writeFileSync(
486
- this.reportPath,
487
- JSON.stringify(report, null, 2),
488
- "utf8"
489
- );
490
- } catch (error) {
491
- this.logger.error(`Error saving validation report: ${error.message}`);
492
- }
493
- }
494
-
495
- private async cleanup() {
496
- this.logger.log("Cleaning up database connections");
497
- await this.sourcePool.end();
498
- await this.targetPool.end();
499
- }
500
- }
501
-
502
- // Script para ejecutar desde línea de comandos
503
- if (require.main === module) {
504
- const run = async () => {
505
- try {
506
- const validator = new PostMigrationValidator();
507
- const result = await validator.validate();
508
-
509
- if (result.success) {
510
- console.log("Post-migration validation successful! No issues found.");
511
- process.exit(0);
512
- } else {
513
- console.log(
514
- `Post-migration validation completed with ${result.issueCount} issues found.`
515
- );
516
- console.log(JSON.stringify(result, null, 2));
517
- process.exit(1);
518
- }
519
- } catch (error) {
520
- console.error("Error:", error.message);
521
- process.exit(1);
522
- }
523
- };
524
-
525
- run();
526
- }
1
+ import * as pg from "pg";
2
+ import * as dotenv from "dotenv";
3
+ import { Logger } from "@nestjs/common";
4
+ import * as fs from "fs";
5
+ import * as path from "path";
6
+
7
+ dotenv.config();
8
+
9
+ interface ValidationIssue {
10
+ type: string;
11
+ tenantId?: string;
12
+ providerId?: number | null;
13
+ message: string;
14
+ }
15
+
16
+ interface ValidationResult {
17
+ success: boolean;
18
+ issueCount: number;
19
+ timestamp: string;
20
+ sourceDatabase: string;
21
+ targetDatabase: string;
22
+ issues: ValidationIssue[];
23
+ }
24
+
25
+ export class PostMigrationValidator {
26
+ private readonly logger = new Logger("PostMigrationValidator");
27
+ private readonly sourcePool: pg.Pool;
28
+ private readonly targetPool: pg.Pool;
29
+ private readonly reportPath: string;
30
+ private issues: any[] = [];
31
+
32
+ constructor(
33
+ private readonly sourceUrl: string = process.env.SOURCE_DATABASE_URL,
34
+ private readonly targetUrl: string = process.env.DATABASE_URL
35
+ ) {
36
+ this.sourcePool = new pg.Pool({
37
+ connectionString: this.sourceUrl,
38
+ });
39
+
40
+ this.targetPool = new pg.Pool({
41
+ connectionString: this.targetUrl,
42
+ });
43
+
44
+ // Crear directorio para reportes si no existe
45
+ const reportsDir = path.join(process.cwd(), "migration-logs");
46
+ if (!fs.existsSync(reportsDir)) {
47
+ fs.mkdirSync(reportsDir, { recursive: true });
48
+ }
49
+
50
+ // Archivo para guardar el reporte
51
+ const timestamp = new Date()
52
+ .toISOString()
53
+ .replace(/:/g, "-")
54
+ .replace(/\..+/, "");
55
+ this.reportPath = path.join(
56
+ reportsDir,
57
+ `post-migration-report-${timestamp}.json`
58
+ );
59
+ }
60
+
61
+ async validate(options?: {
62
+ publicOnly?: boolean;
63
+ }): Promise<ValidationResult> {
64
+ const issues: ValidationIssue[] = [];
65
+ const timestamp = new Date().toISOString();
66
+
67
+ try {
68
+ // Si estamos en modo public-only, solo validamos el esquema público
69
+ if (options?.publicOnly) {
70
+ this.logger.log("Running validation in public-only mode");
71
+ // Aquí solo validaríamos las tablas públicas
72
+ return {
73
+ success: true,
74
+ issueCount: 0,
75
+ timestamp,
76
+ sourceDatabase: process.env.SOURCE_DATABASE_URL,
77
+ targetDatabase: process.env.DATABASE_URL,
78
+ issues: [],
79
+ };
80
+ }
81
+
82
+ // Si no es public-only, procedemos con la validación normal de todos los schemas
83
+ this.logger.log("Starting post-migration validation");
84
+
85
+ // Obtener todos los API keys para determinar los schemas de tenant
86
+ const apiKeysResult = await this.sourcePool.query(`
87
+ SELECT api_key, provider_id
88
+ FROM api_keys
89
+ `);
90
+
91
+ this.logger.log(
92
+ `Found ${apiKeysResult.rows.length} API keys to validate`
93
+ );
94
+
95
+ // Para cada API key (tenant), validar los datos migrados
96
+ for (const apiKey of apiKeysResult.rows) {
97
+ const tenantId = apiKey.api_key;
98
+ const providerId = apiKey.provider_id;
99
+
100
+ await this.validateTenantData(tenantId, providerId);
101
+ }
102
+
103
+ // Guardar el reporte
104
+ this.saveReport();
105
+
106
+ // Mostrar resumen
107
+ this.logger.log(
108
+ `Validation completed with ${this.issues.length} issues found`
109
+ );
110
+ if (this.issues.length > 0) {
111
+ this.logger.log(`Check the full report at: ${this.reportPath}`);
112
+
113
+ // Mostrar los primeros 5 problemas como ejemplo
114
+ this.logger.log("Sample issues:");
115
+ for (let i = 0; i < Math.min(5, this.issues.length); i++) {
116
+ const issue = this.issues[i];
117
+ this.logger.log(`- ${issue.type}: ${issue.message}`);
118
+ }
119
+ } else {
120
+ this.logger.log("No issues found. Migration was successful.");
121
+ }
122
+
123
+ return {
124
+ success: this.issues.length === 0,
125
+ issueCount: this.issues.length,
126
+ timestamp,
127
+ sourceDatabase: this.sourceUrl,
128
+ targetDatabase: this.targetUrl,
129
+ issues: this.issues,
130
+ };
131
+ } catch (error) {
132
+ this.logger.error(
133
+ `Error during validation: ${error.message}`,
134
+ error.stack
135
+ );
136
+ this.issues.push({
137
+ type: "VALIDATION_ERROR",
138
+ message: `Validation process failed: ${error.message}`,
139
+ details: error.stack,
140
+ });
141
+ this.saveReport();
142
+ return {
143
+ success: false,
144
+ issueCount: this.issues.length,
145
+ timestamp,
146
+ sourceDatabase: this.sourceUrl,
147
+ targetDatabase: this.targetUrl,
148
+ issues: this.issues,
149
+ };
150
+ } finally {
151
+ await this.cleanup();
152
+ }
153
+ }
154
+
155
+ private async validateTenantData(tenantId: string, providerId: number) {
156
+ this.logger.log(
157
+ `Validating data for tenant: ${tenantId} (Provider ID: ${providerId})`
158
+ );
159
+
160
+ try {
161
+ // Verificar si el schema existe en la base de datos de destino
162
+ const schemaExistsResult = await this.targetPool.query(
163
+ `
164
+ SELECT 1
165
+ FROM information_schema.schemata
166
+ WHERE schema_name = $1
167
+ LIMIT 1
168
+ `,
169
+ [tenantId]
170
+ );
171
+
172
+ if (schemaExistsResult.rows.length === 0) {
173
+ this.logger.warn(
174
+ `Schema ${tenantId} does not exist in target database`
175
+ );
176
+ this.issues.push({
177
+ type: "MISSING_SCHEMA",
178
+ tenantId,
179
+ providerId,
180
+ message: `Schema ${tenantId} does not exist in target database. Migration failed for this tenant.`,
181
+ });
182
+ return;
183
+ }
184
+
185
+ // Obtener todas las tablas de la base de datos de origen
186
+ const tablesResult = await this.sourcePool.query(`
187
+ SELECT table_name
188
+ FROM information_schema.tables
189
+ WHERE table_schema = 'public'
190
+ AND table_type = 'BASE TABLE'
191
+ AND table_name NOT IN ('_prisma_migrations', 'api_keys')
192
+ `);
193
+
194
+ // Para cada tabla, comparar el número de registros
195
+ for (const tableRow of tablesResult.rows) {
196
+ const tableName = tableRow.table_name;
197
+ await this.validateTableData(tenantId, tableName, providerId);
198
+ }
199
+ } catch (error) {
200
+ this.logger.error(
201
+ `Error validating tenant ${tenantId}: ${error.message}`
202
+ );
203
+ this.issues.push({
204
+ type: "TENANT_VALIDATION_ERROR",
205
+ tenantId,
206
+ providerId,
207
+ message: `Error validating tenant ${tenantId}: ${error.message}`,
208
+ });
209
+ }
210
+ }
211
+
212
+ private async validateTableData(
213
+ tenantId: string,
214
+ tableName: string,
215
+ providerId: number
216
+ ) {
217
+ try {
218
+ // Contar registros en la base de datos de origen
219
+ let sourceCountQuery = `SELECT COUNT(*) as count FROM ${tableName}`;
220
+
221
+ // Añadir filtro por provider_id si la tabla tiene esa columna
222
+ const hasProviderIdResult = await this.sourcePool.query(
223
+ `
224
+ SELECT 1
225
+ FROM information_schema.columns
226
+ WHERE table_schema = 'public'
227
+ AND table_name = $1
228
+ AND column_name = 'provider_id'
229
+ LIMIT 1
230
+ `,
231
+ [tableName]
232
+ );
233
+
234
+ if (hasProviderIdResult.rows.length > 0) {
235
+ sourceCountQuery += ` WHERE provider_id = ${providerId}`;
236
+ }
237
+
238
+ const sourceCountResult = await this.sourcePool.query(sourceCountQuery);
239
+ const sourceCount = parseInt(sourceCountResult.rows[0].count);
240
+
241
+ // Contar registros en la base de datos de destino
242
+ const targetCountQuery = `SELECT COUNT(*) as count FROM "${tenantId}"."${tableName}"`;
243
+ const targetCountResult = await this.targetPool.query(targetCountQuery);
244
+ const targetCount = parseInt(targetCountResult.rows[0].count);
245
+
246
+ // Calcular el porcentaje de migración
247
+ const migrationPercentage =
248
+ sourceCount > 0 ? (targetCount / sourceCount) * 100 : 100;
249
+
250
+ this.logger.log(
251
+ `Table ${tableName} for tenant ${tenantId}: ${targetCount}/${sourceCount} records migrated (${migrationPercentage.toFixed(
252
+ 2
253
+ )}%)`
254
+ );
255
+
256
+ // Si hay una discrepancia significativa, registrarla como un problema
257
+ if (migrationPercentage < 95) {
258
+ this.issues.push({
259
+ type: "DATA_MISMATCH",
260
+ tenantId,
261
+ tableName,
262
+ sourceCount,
263
+ targetCount,
264
+ migrationPercentage: parseFloat(migrationPercentage.toFixed(2)),
265
+ message: `Table ${tableName} for tenant ${tenantId}: Only ${migrationPercentage.toFixed(
266
+ 2
267
+ )}% of records were migrated (${targetCount}/${sourceCount})`,
268
+ });
269
+ }
270
+
271
+ // Si hay más registros en el destino que en el origen, también es un problema
272
+ if (targetCount > sourceCount) {
273
+ this.issues.push({
274
+ type: "EXCESS_DATA",
275
+ tenantId,
276
+ tableName,
277
+ sourceCount,
278
+ targetCount,
279
+ message: `Table ${tableName} for tenant ${tenantId}: Target has more records (${targetCount}) than source (${sourceCount})`,
280
+ });
281
+ }
282
+
283
+ // Validar algunos registros aleatorios para verificar la integridad de los datos
284
+ if (sourceCount > 0 && targetCount > 0) {
285
+ await this.validateRandomRecords(tenantId, tableName, providerId);
286
+ }
287
+ } catch (error) {
288
+ this.logger.error(
289
+ `Error validating table ${tableName} for tenant ${tenantId}: ${error.message}`
290
+ );
291
+ this.issues.push({
292
+ type: "TABLE_VALIDATION_ERROR",
293
+ tenantId,
294
+ tableName,
295
+ message: `Error validating table ${tableName} for tenant ${tenantId}: ${error.message}`,
296
+ });
297
+ }
298
+ }
299
+
300
+ private async validateRandomRecords(
301
+ tenantId: string,
302
+ tableName: string,
303
+ providerId: number
304
+ ) {
305
+ try {
306
+ // Obtener la clave primaria de la tabla
307
+ const primaryKeyResult = await this.sourcePool.query(
308
+ `
309
+ SELECT kcu.column_name
310
+ FROM information_schema.table_constraints tc
311
+ JOIN information_schema.key_column_usage kcu
312
+ ON tc.constraint_name = kcu.constraint_name
313
+ AND tc.table_schema = kcu.table_schema
314
+ WHERE tc.constraint_type = 'PRIMARY KEY'
315
+ AND tc.table_schema = 'public'
316
+ AND tc.table_name = $1
317
+ LIMIT 1
318
+ `,
319
+ [tableName]
320
+ );
321
+
322
+ if (primaryKeyResult.rows.length === 0) {
323
+ this.logger.warn(`No primary key found for table ${tableName}`);
324
+ return;
325
+ }
326
+
327
+ const primaryKeyColumn = primaryKeyResult.rows[0].column_name;
328
+
329
+ // Obtener hasta 5 registros aleatorios de la base de datos de origen
330
+ let randomRecordsQuery = `
331
+ SELECT *
332
+ FROM ${tableName}
333
+ `;
334
+
335
+ // Añadir filtro por provider_id si la tabla tiene esa columna
336
+ const hasProviderIdResult = await this.sourcePool.query(
337
+ `
338
+ SELECT 1
339
+ FROM information_schema.columns
340
+ WHERE table_schema = 'public'
341
+ AND table_name = $1
342
+ AND column_name = 'provider_id'
343
+ LIMIT 1
344
+ `,
345
+ [tableName]
346
+ );
347
+
348
+ if (hasProviderIdResult.rows.length > 0) {
349
+ randomRecordsQuery += ` WHERE provider_id = ${providerId}`;
350
+ }
351
+
352
+ randomRecordsQuery += `
353
+ ORDER BY RANDOM()
354
+ LIMIT 5
355
+ `;
356
+
357
+ const randomRecordsResult = await this.sourcePool.query(
358
+ randomRecordsQuery
359
+ );
360
+
361
+ // Para cada registro aleatorio, verificar si existe en la base de datos de destino
362
+ for (const sourceRecord of randomRecordsResult.rows) {
363
+ const primaryKeyValue = sourceRecord[primaryKeyColumn];
364
+
365
+ const targetRecordResult = await this.targetPool.query(
366
+ `
367
+ SELECT *
368
+ FROM "${tenantId}"."${tableName}"
369
+ WHERE "${primaryKeyColumn}" = $1
370
+ LIMIT 1
371
+ `,
372
+ [primaryKeyValue]
373
+ );
374
+
375
+ if (targetRecordResult.rows.length === 0) {
376
+ this.issues.push({
377
+ type: "MISSING_RECORD",
378
+ tenantId,
379
+ tableName,
380
+ recordId: primaryKeyValue,
381
+ message: `Record with ID ${primaryKeyValue} in table ${tableName} for tenant ${tenantId} was not migrated`,
382
+ });
383
+ continue;
384
+ }
385
+
386
+ const targetRecord = targetRecordResult.rows[0];
387
+
388
+ // Comparar los valores de las columnas
389
+ for (const columnName in sourceRecord) {
390
+ // Ignorar columnas específicas de la base de datos de origen
391
+ if (columnName === "provider_id") {
392
+ continue;
393
+ }
394
+
395
+ // Verificar si la columna existe en el registro de destino
396
+ if (!(columnName in targetRecord)) {
397
+ this.issues.push({
398
+ type: "MISSING_COLUMN",
399
+ tenantId,
400
+ tableName,
401
+ recordId: primaryKeyValue,
402
+ columnName,
403
+ message: `Column ${columnName} is missing in record with ID ${primaryKeyValue} in table ${tableName} for tenant ${tenantId}`,
404
+ });
405
+ continue;
406
+ }
407
+
408
+ // Comparar los valores (con manejo especial para tipos de datos específicos)
409
+ const sourceValue = sourceRecord[columnName];
410
+ const targetValue = targetRecord[columnName];
411
+
412
+ if (this.areValuesDifferent(sourceValue, targetValue)) {
413
+ this.issues.push({
414
+ type: "VALUE_MISMATCH",
415
+ tenantId,
416
+ tableName,
417
+ recordId: primaryKeyValue,
418
+ columnName,
419
+ sourceValue,
420
+ targetValue,
421
+ message: `Value mismatch for column ${columnName} in record with ID ${primaryKeyValue} in table ${tableName} for tenant ${tenantId}: ${sourceValue} (source) vs ${targetValue} (target)`,
422
+ });
423
+ }
424
+ }
425
+ }
426
+ } catch (error) {
427
+ this.logger.error(
428
+ `Error validating random records for table ${tableName} in tenant ${tenantId}: ${error.message}`
429
+ );
430
+ this.issues.push({
431
+ type: "RECORD_VALIDATION_ERROR",
432
+ tenantId,
433
+ tableName,
434
+ message: `Error validating random records for table ${tableName} in tenant ${tenantId}: ${error.message}`,
435
+ });
436
+ }
437
+ }
438
+
439
+ private areValuesDifferent(sourceValue: any, targetValue: any): boolean {
440
+ // Si ambos son null o undefined, son iguales
441
+ if (sourceValue == null && targetValue == null) {
442
+ return false;
443
+ }
444
+
445
+ // Si uno es null y el otro no, son diferentes
446
+ if (sourceValue == null || targetValue == null) {
447
+ return true;
448
+ }
449
+
450
+ // Para fechas, comparar como strings ISO
451
+ if (sourceValue instanceof Date && targetValue instanceof Date) {
452
+ return sourceValue.toISOString() !== targetValue.toISOString();
453
+ }
454
+
455
+ // Para números, comparar con tolerancia para punto flotante
456
+ if (typeof sourceValue === "number" && typeof targetValue === "number") {
457
+ const epsilon = 0.0001;
458
+ return Math.abs(sourceValue - targetValue) > epsilon;
459
+ }
460
+
461
+ // Para strings, comparar directamente
462
+ if (typeof sourceValue === "string" && typeof targetValue === "string") {
463
+ return sourceValue !== targetValue;
464
+ }
465
+
466
+ // Para booleanos, comparar directamente
467
+ if (typeof sourceValue === "boolean" && typeof targetValue === "boolean") {
468
+ return sourceValue !== targetValue;
469
+ }
470
+
471
+ // Para otros tipos, convertir a string y comparar
472
+ return String(sourceValue) !== String(targetValue);
473
+ }
474
+
475
+ private saveReport() {
476
+ try {
477
+ const report = {
478
+ timestamp: new Date().toISOString(),
479
+ sourceDatabase: this.sourceUrl,
480
+ targetDatabase: this.targetUrl,
481
+ issueCount: this.issues.length,
482
+ issues: this.issues,
483
+ };
484
+
485
+ fs.writeFileSync(
486
+ this.reportPath,
487
+ JSON.stringify(report, null, 2),
488
+ "utf8"
489
+ );
490
+ } catch (error) {
491
+ this.logger.error(`Error saving validation report: ${error.message}`);
492
+ }
493
+ }
494
+
495
+ private async cleanup() {
496
+ this.logger.log("Cleaning up database connections");
497
+ await this.sourcePool.end();
498
+ await this.targetPool.end();
499
+ }
500
+ }
501
+
502
+ // Script para ejecutar desde línea de comandos
503
+ if (require.main === module) {
504
+ const run = async () => {
505
+ try {
506
+ const validator = new PostMigrationValidator();
507
+ const result = await validator.validate();
508
+
509
+ if (result.success) {
510
+ console.log("Post-migration validation successful! No issues found.");
511
+ process.exit(0);
512
+ } else {
513
+ console.log(
514
+ `Post-migration validation completed with ${result.issueCount} issues found.`
515
+ );
516
+ console.log(JSON.stringify(result, null, 2));
517
+ process.exit(1);
518
+ }
519
+ } catch (error) {
520
+ console.error("Error:", error.message);
521
+ process.exit(1);
522
+ }
523
+ };
524
+
525
+ run();
526
+ }