@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.
- package/.github/CODEOWNERS +1 -0
- package/README.md +269 -269
- package/dist/index.d.ts +1 -1
- package/dist/prisma.service.d.ts +1 -1
- package/dist/scripts/add-uuid-to-table.js +32 -32
- package/dist/scripts/data-migration/batch-migrator.js +12 -12
- package/dist/scripts/data-migration/data-transformer.js +14 -14
- package/dist/scripts/data-migration/dependency-resolver.js +23 -23
- package/dist/scripts/data-migration/entity-discovery.js +68 -68
- package/dist/scripts/data-migration/foreign-key-manager.js +23 -23
- package/dist/scripts/data-migration/migration-tool.js +5 -5
- package/dist/scripts/data-migration/schema-utils.js +74 -74
- package/dist/scripts/data-migration/typecast-manager.js +4 -4
- package/dist/scripts/database-initializer.js +5 -5
- package/dist/scripts/drop-database.js +5 -5
- package/dist/scripts/fix-data-types.js +53 -53
- package/dist/scripts/fix-enum-values.js +34 -34
- package/dist/scripts/fix-schema-discrepancies.js +40 -40
- package/dist/scripts/fix-table-indexes.js +81 -81
- package/dist/scripts/migrate-schema-structure.js +4 -4
- package/dist/scripts/migrate-uuid.js +19 -19
- package/dist/scripts/post-migration-validator.js +49 -49
- package/dist/scripts/pre-migration-validator.js +107 -107
- package/dist/scripts/reset-database.js +21 -21
- package/dist/scripts/retry-failed-migrations.js +28 -28
- package/dist/scripts/run-migration.js +5 -5
- package/dist/scripts/schema-sync.js +18 -18
- package/dist/scripts/sequence-sync-cli.js +55 -55
- package/dist/scripts/sequence-synchronizer.js +20 -20
- package/dist/scripts/sync-enum-types.js +30 -30
- package/dist/scripts/sync-enum-values.js +52 -52
- package/dist/scripts/truncate-database.js +10 -10
- package/dist/scripts/verify-migration-setup.js +10 -10
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/migration-config.json +63 -63
- package/migration-config.json.bk +95 -95
- package/migrations/add_reserved_amount.sql +8 -0
- package/package.json +44 -44
- package/prisma/migrations/add_uuid_to_transactions.sql +13 -13
- package/prisma/schema.prisma +601 -554
- package/src/index.ts +23 -23
- package/src/prisma-factory.service.ts +40 -40
- package/src/prisma.module.ts +9 -9
- package/src/prisma.service.ts +16 -16
- package/src/scripts/add-uuid-to-table.ts +138 -138
- package/src/scripts/create-tenant-schemas.ts +145 -145
- package/src/scripts/data-migration/batch-migrator.ts +248 -248
- package/src/scripts/data-migration/data-transformer.ts +426 -426
- package/src/scripts/data-migration/db-connector.ts +120 -120
- package/src/scripts/data-migration/dependency-resolver.ts +174 -174
- package/src/scripts/data-migration/entity-discovery.ts +196 -196
- package/src/scripts/data-migration/foreign-key-manager.ts +277 -277
- package/src/scripts/data-migration/migration-config.json +63 -63
- package/src/scripts/data-migration/migration-tool.ts +509 -509
- package/src/scripts/data-migration/schema-utils.ts +248 -248
- package/src/scripts/data-migration/tenant-migrator.ts +201 -201
- package/src/scripts/data-migration/typecast-manager.ts +193 -193
- package/src/scripts/data-migration/types.ts +113 -113
- package/src/scripts/database-initializer.ts +49 -49
- package/src/scripts/drop-database.ts +104 -104
- package/src/scripts/dump-source-db.sh +61 -61
- package/src/scripts/encrypt-user-passwords.ts +36 -36
- package/src/scripts/error-handler.ts +117 -117
- package/src/scripts/fix-data-types.ts +241 -241
- package/src/scripts/fix-enum-values.ts +357 -357
- package/src/scripts/fix-schema-discrepancies.ts +317 -317
- package/src/scripts/fix-table-indexes.ts +601 -601
- package/src/scripts/migrate-schema-structure.ts +90 -90
- package/src/scripts/migrate-uuid.ts +76 -76
- package/src/scripts/post-migration-validator.ts +526 -526
- package/src/scripts/pre-migration-validator.ts +610 -610
- package/src/scripts/reset-database.ts +263 -263
- package/src/scripts/retry-failed-migrations.ts +416 -416
- package/src/scripts/run-migration.ts +707 -707
- package/src/scripts/schema-sync.ts +128 -128
- package/src/scripts/sequence-sync-cli.ts +416 -416
- package/src/scripts/sequence-synchronizer.ts +127 -127
- package/src/scripts/sync-enum-types.ts +170 -170
- package/src/scripts/sync-enum-values.ts +563 -563
- package/src/scripts/truncate-database.ts +123 -123
- package/src/scripts/verify-migration-setup.ts +135 -135
- package/tsconfig.json +17 -17
- package/dist/scripts/data-migration/dependency-manager.d.ts +0 -9
- package/dist/scripts/data-migration/dependency-manager.js +0 -86
- package/dist/scripts/data-migration/dependency-manager.js.map +0 -1
- package/dist/scripts/data-migration/migration-config.json +0 -63
- package/dist/scripts/data-migration/migration-phases.d.ts +0 -5
- package/dist/scripts/data-migration/migration-phases.js +0 -55
- package/dist/scripts/data-migration/migration-phases.js.map +0 -1
- package/dist/scripts/data-migration/multi-source-migrator.d.ts +0 -17
- package/dist/scripts/data-migration/multi-source-migrator.js +0 -130
- package/dist/scripts/data-migration/multi-source-migrator.js.map +0 -1
- package/dist/scripts/data-migration/phase-generator.d.ts +0 -15
- package/dist/scripts/data-migration/phase-generator.js +0 -187
- package/dist/scripts/data-migration/phase-generator.js.map +0 -1
- package/dist/scripts/data-migration.d.ts +0 -22
- package/dist/scripts/data-migration.js +0 -593
- package/dist/scripts/data-migration.js.map +0 -1
- package/dist/scripts/multi-db-migration.d.ts +0 -1
- package/dist/scripts/multi-db-migration.js +0 -55
- package/dist/scripts/multi-db-migration.js.map +0 -1
|
@@ -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
|
+
}
|