@javalabs/prisma-client 1.0.16 → 1.0.19
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/.dockerignore +14 -0
- package/Dockerfile +23 -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/package.json +44 -44
- package/prisma/migrations/add_accepts_partial_payments_to_users.sql +19 -0
- package/prisma/migrations/add_amount_received_to_manual_payments.sql +19 -0
- package/prisma/migrations/add_commission_fields.sql +33 -0
- package/prisma/migrations/add_uuid_to_transactions.sql +13 -13
- package/prisma/migrations/complete_partial_payments_migration.sql +53 -0
- package/prisma/migrations/create_settlements_table.sql +60 -0
- package/prisma/schema.prisma +56 -4
- 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
|
@@ -9,21 +9,21 @@ const prisma = new client_1.PrismaClient();
|
|
|
9
9
|
async function addUuidToTable({ tableName, columnName = "uuid", makeRequired = false, addDefault = false, }) {
|
|
10
10
|
try {
|
|
11
11
|
console.log(`🚀 Iniciando proceso de añadir UUID a la tabla ${tableName}...`);
|
|
12
|
-
const tableExists = await prisma.$queryRaw `
|
|
13
|
-
SELECT EXISTS (
|
|
14
|
-
SELECT FROM information_schema.tables
|
|
15
|
-
WHERE table_name = ${tableName}
|
|
16
|
-
);
|
|
12
|
+
const tableExists = await prisma.$queryRaw `
|
|
13
|
+
SELECT EXISTS (
|
|
14
|
+
SELECT FROM information_schema.tables
|
|
15
|
+
WHERE table_name = ${tableName}
|
|
16
|
+
);
|
|
17
17
|
`;
|
|
18
18
|
if (!tableExists[0].exists) {
|
|
19
19
|
throw new Error(`La tabla ${tableName} no existe`);
|
|
20
20
|
}
|
|
21
|
-
const columnExists = await prisma.$queryRaw `
|
|
22
|
-
SELECT EXISTS (
|
|
23
|
-
SELECT FROM information_schema.columns
|
|
24
|
-
WHERE table_name = ${tableName}
|
|
25
|
-
AND column_name = ${columnName}
|
|
26
|
-
);
|
|
21
|
+
const columnExists = await prisma.$queryRaw `
|
|
22
|
+
SELECT EXISTS (
|
|
23
|
+
SELECT FROM information_schema.columns
|
|
24
|
+
WHERE table_name = ${tableName}
|
|
25
|
+
AND column_name = ${columnName}
|
|
26
|
+
);
|
|
27
27
|
`;
|
|
28
28
|
if (columnExists[0].exists) {
|
|
29
29
|
console.log(`⚠️ La columna ${columnName} ya existe en la tabla ${tableName}`);
|
|
@@ -32,40 +32,40 @@ async function addUuidToTable({ tableName, columnName = "uuid", makeRequired = f
|
|
|
32
32
|
console.log("📦 Habilitando extensión uuid-ossp...");
|
|
33
33
|
await prisma.$executeRaw `CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`;
|
|
34
34
|
console.log(`📝 Añadiendo columna ${columnName}...`);
|
|
35
|
-
await prisma.$executeRaw `
|
|
36
|
-
ALTER TABLE ${tableName}
|
|
37
|
-
ADD COLUMN ${columnName} VARCHAR(255);
|
|
35
|
+
await prisma.$executeRaw `
|
|
36
|
+
ALTER TABLE ${tableName}
|
|
37
|
+
ADD COLUMN ${columnName} VARCHAR(255);
|
|
38
38
|
`;
|
|
39
39
|
console.log("🔄 Actualizando registros existentes con UUIDs...");
|
|
40
|
-
await prisma.$executeRaw `
|
|
41
|
-
UPDATE ${tableName}
|
|
42
|
-
SET ${columnName} = uuid_generate_v4()
|
|
43
|
-
WHERE ${columnName} IS NULL;
|
|
40
|
+
await prisma.$executeRaw `
|
|
41
|
+
UPDATE ${tableName}
|
|
42
|
+
SET ${columnName} = uuid_generate_v4()
|
|
43
|
+
WHERE ${columnName} IS NULL;
|
|
44
44
|
`;
|
|
45
45
|
console.log("📊 Creando índice...");
|
|
46
|
-
await prisma.$executeRaw `
|
|
47
|
-
CREATE INDEX idx_${tableName}_${columnName}
|
|
48
|
-
ON ${tableName}(${columnName});
|
|
46
|
+
await prisma.$executeRaw `
|
|
47
|
+
CREATE INDEX idx_${tableName}_${columnName}
|
|
48
|
+
ON ${tableName}(${columnName});
|
|
49
49
|
`;
|
|
50
50
|
console.log("🔒 Añadiendo restricción unique...");
|
|
51
|
-
await prisma.$executeRaw `
|
|
52
|
-
ALTER TABLE ${tableName}
|
|
53
|
-
ADD CONSTRAINT ${tableName}_${columnName}_key
|
|
54
|
-
UNIQUE (${columnName});
|
|
51
|
+
await prisma.$executeRaw `
|
|
52
|
+
ALTER TABLE ${tableName}
|
|
53
|
+
ADD CONSTRAINT ${tableName}_${columnName}_key
|
|
54
|
+
UNIQUE (${columnName});
|
|
55
55
|
`;
|
|
56
56
|
if (makeRequired) {
|
|
57
57
|
console.log("⚠️ Haciendo la columna NOT NULL...");
|
|
58
|
-
await prisma.$executeRaw `
|
|
59
|
-
ALTER TABLE ${tableName}
|
|
60
|
-
ALTER COLUMN ${columnName} SET NOT NULL;
|
|
58
|
+
await prisma.$executeRaw `
|
|
59
|
+
ALTER TABLE ${tableName}
|
|
60
|
+
ALTER COLUMN ${columnName} SET NOT NULL;
|
|
61
61
|
`;
|
|
62
62
|
}
|
|
63
63
|
if (addDefault) {
|
|
64
64
|
console.log("⚡ Añadiendo valor por defecto...");
|
|
65
|
-
await prisma.$executeRaw `
|
|
66
|
-
ALTER TABLE ${tableName}
|
|
67
|
-
ALTER COLUMN ${columnName}
|
|
68
|
-
SET DEFAULT uuid_generate_v4();
|
|
65
|
+
await prisma.$executeRaw `
|
|
66
|
+
ALTER TABLE ${tableName}
|
|
67
|
+
ALTER COLUMN ${columnName}
|
|
68
|
+
SET DEFAULT uuid_generate_v4();
|
|
69
69
|
`;
|
|
70
70
|
}
|
|
71
71
|
console.log(`✅ Proceso completado exitosamente para la tabla ${tableName}`);
|
|
@@ -64,13 +64,13 @@ class BatchMigrator {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
async fetchBatch(sourceSchema, tableConfig, offset, limit) {
|
|
67
|
-
const query = `
|
|
68
|
-
SELECT *
|
|
69
|
-
FROM "${sourceSchema}"."${tableConfig.sourceTable}"
|
|
70
|
-
${this.buildWhereClause(tableConfig)}
|
|
71
|
-
ORDER BY "${tableConfig.idField}"
|
|
72
|
-
LIMIT ${limit}
|
|
73
|
-
OFFSET ${offset}
|
|
67
|
+
const query = `
|
|
68
|
+
SELECT *
|
|
69
|
+
FROM "${sourceSchema}"."${tableConfig.sourceTable}"
|
|
70
|
+
${this.buildWhereClause(tableConfig)}
|
|
71
|
+
ORDER BY "${tableConfig.idField}"
|
|
72
|
+
LIMIT ${limit}
|
|
73
|
+
OFFSET ${offset}
|
|
74
74
|
`;
|
|
75
75
|
const result = await this.connections.sourcePool.query(query);
|
|
76
76
|
return result.rows;
|
|
@@ -121,11 +121,11 @@ class BatchMigrator {
|
|
|
121
121
|
const columns = Object.keys(record);
|
|
122
122
|
const values = Object.values(record);
|
|
123
123
|
const placeholders = values.map((_, i) => `$${i + 1}`).join(", ");
|
|
124
|
-
const query = `
|
|
125
|
-
INSERT INTO "${schema}"."${table}"
|
|
126
|
-
(${columns.map((col) => `"${col}"`).join(", ")})
|
|
127
|
-
VALUES (${placeholders})
|
|
128
|
-
ON CONFLICT DO NOTHING
|
|
124
|
+
const query = `
|
|
125
|
+
INSERT INTO "${schema}"."${table}"
|
|
126
|
+
(${columns.map((col) => `"${col}"`).join(", ")})
|
|
127
|
+
VALUES (${placeholders})
|
|
128
|
+
ON CONFLICT DO NOTHING
|
|
129
129
|
`;
|
|
130
130
|
await client.query(query, values);
|
|
131
131
|
}
|
|
@@ -55,12 +55,12 @@ class DataTransformer {
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
async getEnumTypeName(tableName, columnName) {
|
|
58
|
-
const result = await this.connections.sourcePool.query(`
|
|
59
|
-
SELECT udt_name
|
|
60
|
-
FROM information_schema.columns
|
|
61
|
-
WHERE table_name = $1
|
|
62
|
-
AND column_name = $2
|
|
63
|
-
AND table_schema = 'public'
|
|
58
|
+
const result = await this.connections.sourcePool.query(`
|
|
59
|
+
SELECT udt_name
|
|
60
|
+
FROM information_schema.columns
|
|
61
|
+
WHERE table_name = $1
|
|
62
|
+
AND column_name = $2
|
|
63
|
+
AND table_schema = 'public'
|
|
64
64
|
`, [tableName.replace(/^public\./, ""), columnName]);
|
|
65
65
|
if (result.rows.length === 0) {
|
|
66
66
|
throw new Error(`No se encontró el tipo de enum para ${tableName}.${columnName}`);
|
|
@@ -88,14 +88,14 @@ class DataTransformer {
|
|
|
88
88
|
if (enumDefaults[enumType]) {
|
|
89
89
|
return enumDefaults[enumType];
|
|
90
90
|
}
|
|
91
|
-
const query = `
|
|
92
|
-
SELECT e.enumlabel
|
|
93
|
-
FROM pg_type t
|
|
94
|
-
JOIN pg_enum e ON t.oid = e.enumtypid
|
|
95
|
-
JOIN pg_namespace n ON n.oid = t.typnamespace
|
|
96
|
-
WHERE t.typname = $1 AND n.nspname = $2
|
|
97
|
-
ORDER BY e.enumsortorder
|
|
98
|
-
LIMIT 1
|
|
91
|
+
const query = `
|
|
92
|
+
SELECT e.enumlabel
|
|
93
|
+
FROM pg_type t
|
|
94
|
+
JOIN pg_enum e ON t.oid = e.enumtypid
|
|
95
|
+
JOIN pg_namespace n ON n.oid = t.typnamespace
|
|
96
|
+
WHERE t.typname = $1 AND n.nspname = $2
|
|
97
|
+
ORDER BY e.enumsortorder
|
|
98
|
+
LIMIT 1
|
|
99
99
|
`;
|
|
100
100
|
const result = await this.schemaUtils.queryTargetDb(query, [
|
|
101
101
|
enumType,
|
|
@@ -74,11 +74,11 @@ class DependencyResolver {
|
|
|
74
74
|
this.tableOrder.push(table);
|
|
75
75
|
}
|
|
76
76
|
async getAllTables() {
|
|
77
|
-
const query = `
|
|
78
|
-
SELECT table_name
|
|
79
|
-
FROM information_schema.tables
|
|
80
|
-
WHERE table_schema = 'public'
|
|
81
|
-
AND table_type = 'BASE TABLE'
|
|
77
|
+
const query = `
|
|
78
|
+
SELECT table_name
|
|
79
|
+
FROM information_schema.tables
|
|
80
|
+
WHERE table_schema = 'public'
|
|
81
|
+
AND table_type = 'BASE TABLE'
|
|
82
82
|
`;
|
|
83
83
|
const result = await this.sourcePool.query(query);
|
|
84
84
|
return result.rows.map((row) => row.table_name);
|
|
@@ -88,17 +88,17 @@ class DependencyResolver {
|
|
|
88
88
|
const dependencies = new Set();
|
|
89
89
|
const configDeps = ((_c = (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.tables) === null || _b === void 0 ? void 0 : _b[table]) === null || _c === void 0 ? void 0 : _c.dependencies) || [];
|
|
90
90
|
configDeps.forEach((dep) => dependencies.add(dep));
|
|
91
|
-
const query = `
|
|
92
|
-
SELECT
|
|
93
|
-
ccu.table_name AS foreign_table
|
|
94
|
-
FROM
|
|
95
|
-
information_schema.table_constraints tc
|
|
96
|
-
JOIN information_schema.constraint_column_usage ccu
|
|
97
|
-
ON ccu.constraint_name = tc.constraint_name
|
|
98
|
-
WHERE
|
|
99
|
-
tc.constraint_type = 'FOREIGN KEY'
|
|
100
|
-
AND tc.table_name = $1
|
|
101
|
-
AND tc.table_schema = 'public'
|
|
91
|
+
const query = `
|
|
92
|
+
SELECT
|
|
93
|
+
ccu.table_name AS foreign_table
|
|
94
|
+
FROM
|
|
95
|
+
information_schema.table_constraints tc
|
|
96
|
+
JOIN information_schema.constraint_column_usage ccu
|
|
97
|
+
ON ccu.constraint_name = tc.constraint_name
|
|
98
|
+
WHERE
|
|
99
|
+
tc.constraint_type = 'FOREIGN KEY'
|
|
100
|
+
AND tc.table_name = $1
|
|
101
|
+
AND tc.table_schema = 'public'
|
|
102
102
|
`;
|
|
103
103
|
try {
|
|
104
104
|
const result = await this.sourcePool.query(query, [table]);
|
|
@@ -111,13 +111,13 @@ class DependencyResolver {
|
|
|
111
111
|
}
|
|
112
112
|
async validateDependencies(table, dependencies) {
|
|
113
113
|
for (const dep of dependencies) {
|
|
114
|
-
const query = `
|
|
115
|
-
SELECT EXISTS (
|
|
116
|
-
SELECT 1
|
|
117
|
-
FROM information_schema.tables
|
|
118
|
-
WHERE table_schema = 'public'
|
|
119
|
-
AND table_name = $1
|
|
120
|
-
)
|
|
114
|
+
const query = `
|
|
115
|
+
SELECT EXISTS (
|
|
116
|
+
SELECT 1
|
|
117
|
+
FROM information_schema.tables
|
|
118
|
+
WHERE table_schema = 'public'
|
|
119
|
+
AND table_name = $1
|
|
120
|
+
)
|
|
121
121
|
`;
|
|
122
122
|
const result = await this.sourcePool.query(query, [dep]);
|
|
123
123
|
if (!result.rows[0].exists) {
|
|
@@ -43,13 +43,13 @@ class EntityDiscovery {
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
async getPrimaryKey(schemaName, tableName) {
|
|
46
|
-
const query = `
|
|
47
|
-
SELECT a.attname
|
|
48
|
-
FROM pg_index i
|
|
49
|
-
JOIN pg_attribute a ON a.attrelid = i.indrelid
|
|
50
|
-
AND a.attnum = ANY(i.indkey)
|
|
51
|
-
WHERE i.indrelid = '"${schemaName}"."${tableName}"'::regclass
|
|
52
|
-
AND i.indisprimary;
|
|
46
|
+
const query = `
|
|
47
|
+
SELECT a.attname
|
|
48
|
+
FROM pg_index i
|
|
49
|
+
JOIN pg_attribute a ON a.attrelid = i.indrelid
|
|
50
|
+
AND a.attnum = ANY(i.indkey)
|
|
51
|
+
WHERE i.indrelid = '"${schemaName}"."${tableName}"'::regclass
|
|
52
|
+
AND i.indisprimary;
|
|
53
53
|
`;
|
|
54
54
|
try {
|
|
55
55
|
const { rows } = await this.targetPool.query(query);
|
|
@@ -61,23 +61,23 @@ class EntityDiscovery {
|
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
async getForeignKeys(schemaName, tableName) {
|
|
64
|
-
const query = `
|
|
65
|
-
SELECT
|
|
66
|
-
kcu.column_name,
|
|
67
|
-
ccu.table_name AS foreign_table_name,
|
|
68
|
-
ccu.column_name AS foreign_column_name
|
|
69
|
-
FROM
|
|
70
|
-
information_schema.table_constraints tc
|
|
71
|
-
JOIN information_schema.key_column_usage kcu
|
|
72
|
-
ON tc.constraint_name = kcu.constraint_name
|
|
73
|
-
AND tc.table_schema = kcu.table_schema
|
|
74
|
-
JOIN information_schema.constraint_column_usage ccu
|
|
75
|
-
ON ccu.constraint_name = tc.constraint_name
|
|
76
|
-
AND ccu.table_schema = tc.table_schema
|
|
77
|
-
WHERE
|
|
78
|
-
tc.constraint_type = 'FOREIGN KEY'
|
|
79
|
-
AND tc.table_schema = $1
|
|
80
|
-
AND tc.table_name = $2;
|
|
64
|
+
const query = `
|
|
65
|
+
SELECT
|
|
66
|
+
kcu.column_name,
|
|
67
|
+
ccu.table_name AS foreign_table_name,
|
|
68
|
+
ccu.column_name AS foreign_column_name
|
|
69
|
+
FROM
|
|
70
|
+
information_schema.table_constraints tc
|
|
71
|
+
JOIN information_schema.key_column_usage kcu
|
|
72
|
+
ON tc.constraint_name = kcu.constraint_name
|
|
73
|
+
AND tc.table_schema = kcu.table_schema
|
|
74
|
+
JOIN information_schema.constraint_column_usage ccu
|
|
75
|
+
ON ccu.constraint_name = tc.constraint_name
|
|
76
|
+
AND ccu.table_schema = tc.table_schema
|
|
77
|
+
WHERE
|
|
78
|
+
tc.constraint_type = 'FOREIGN KEY'
|
|
79
|
+
AND tc.table_schema = $1
|
|
80
|
+
AND tc.table_name = $2;
|
|
81
81
|
`;
|
|
82
82
|
try {
|
|
83
83
|
const { rows } = await this.targetPool.query(query, [schemaName, tableName]);
|
|
@@ -89,50 +89,50 @@ class EntityDiscovery {
|
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
async findJoinTables(schemaName, sourceTable, targetTable) {
|
|
92
|
-
const query = `
|
|
93
|
-
WITH source_fks AS (
|
|
94
|
-
SELECT
|
|
95
|
-
tc.table_name,
|
|
96
|
-
kcu.column_name,
|
|
97
|
-
ccu.table_name AS referenced_table
|
|
98
|
-
FROM
|
|
99
|
-
information_schema.table_constraints tc
|
|
100
|
-
JOIN information_schema.key_column_usage kcu
|
|
101
|
-
ON tc.constraint_name = kcu.constraint_name
|
|
102
|
-
AND tc.table_schema = kcu.table_schema
|
|
103
|
-
JOIN information_schema.constraint_column_usage ccu
|
|
104
|
-
ON ccu.constraint_name = tc.constraint_name
|
|
105
|
-
AND ccu.table_schema = tc.table_schema
|
|
106
|
-
WHERE
|
|
107
|
-
tc.constraint_type = 'FOREIGN KEY'
|
|
108
|
-
AND tc.table_schema = $1
|
|
109
|
-
AND ccu.table_name = $2
|
|
110
|
-
),
|
|
111
|
-
target_fks AS (
|
|
112
|
-
SELECT
|
|
113
|
-
tc.table_name,
|
|
114
|
-
kcu.column_name,
|
|
115
|
-
ccu.table_name AS referenced_table
|
|
116
|
-
FROM
|
|
117
|
-
information_schema.table_constraints tc
|
|
118
|
-
JOIN information_schema.key_column_usage kcu
|
|
119
|
-
ON tc.constraint_name = kcu.constraint_name
|
|
120
|
-
AND tc.table_schema = kcu.table_schema
|
|
121
|
-
JOIN information_schema.constraint_column_usage ccu
|
|
122
|
-
ON ccu.constraint_name = tc.constraint_name
|
|
123
|
-
AND ccu.table_schema = tc.table_schema
|
|
124
|
-
WHERE
|
|
125
|
-
tc.constraint_type = 'FOREIGN KEY'
|
|
126
|
-
AND tc.table_schema = $1
|
|
127
|
-
AND ccu.table_name = $3
|
|
128
|
-
)
|
|
129
|
-
SELECT
|
|
130
|
-
s.table_name AS join_table,
|
|
131
|
-
s.column_name AS source_column,
|
|
132
|
-
t.column_name AS target_column
|
|
133
|
-
FROM
|
|
134
|
-
source_fks s
|
|
135
|
-
JOIN target_fks t ON s.table_name = t.table_name
|
|
92
|
+
const query = `
|
|
93
|
+
WITH source_fks AS (
|
|
94
|
+
SELECT
|
|
95
|
+
tc.table_name,
|
|
96
|
+
kcu.column_name,
|
|
97
|
+
ccu.table_name AS referenced_table
|
|
98
|
+
FROM
|
|
99
|
+
information_schema.table_constraints tc
|
|
100
|
+
JOIN information_schema.key_column_usage kcu
|
|
101
|
+
ON tc.constraint_name = kcu.constraint_name
|
|
102
|
+
AND tc.table_schema = kcu.table_schema
|
|
103
|
+
JOIN information_schema.constraint_column_usage ccu
|
|
104
|
+
ON ccu.constraint_name = tc.constraint_name
|
|
105
|
+
AND ccu.table_schema = tc.table_schema
|
|
106
|
+
WHERE
|
|
107
|
+
tc.constraint_type = 'FOREIGN KEY'
|
|
108
|
+
AND tc.table_schema = $1
|
|
109
|
+
AND ccu.table_name = $2
|
|
110
|
+
),
|
|
111
|
+
target_fks AS (
|
|
112
|
+
SELECT
|
|
113
|
+
tc.table_name,
|
|
114
|
+
kcu.column_name,
|
|
115
|
+
ccu.table_name AS referenced_table
|
|
116
|
+
FROM
|
|
117
|
+
information_schema.table_constraints tc
|
|
118
|
+
JOIN information_schema.key_column_usage kcu
|
|
119
|
+
ON tc.constraint_name = kcu.constraint_name
|
|
120
|
+
AND tc.table_schema = kcu.table_schema
|
|
121
|
+
JOIN information_schema.constraint_column_usage ccu
|
|
122
|
+
ON ccu.constraint_name = tc.constraint_name
|
|
123
|
+
AND ccu.table_schema = tc.table_schema
|
|
124
|
+
WHERE
|
|
125
|
+
tc.constraint_type = 'FOREIGN KEY'
|
|
126
|
+
AND tc.table_schema = $1
|
|
127
|
+
AND ccu.table_name = $3
|
|
128
|
+
)
|
|
129
|
+
SELECT
|
|
130
|
+
s.table_name AS join_table,
|
|
131
|
+
s.column_name AS source_column,
|
|
132
|
+
t.column_name AS target_column
|
|
133
|
+
FROM
|
|
134
|
+
source_fks s
|
|
135
|
+
JOIN target_fks t ON s.table_name = t.table_name
|
|
136
136
|
`;
|
|
137
137
|
try {
|
|
138
138
|
const { rows } = await this.targetPool.query(query, [
|
|
@@ -15,23 +15,23 @@ class ForeignKeyManager {
|
|
|
15
15
|
}
|
|
16
16
|
try {
|
|
17
17
|
this.logger.log(`Obteniendo dependencias de llaves foráneas para ${schemaName}.${tableName}`);
|
|
18
|
-
const query = `
|
|
19
|
-
SELECT
|
|
20
|
-
kcu.column_name,
|
|
21
|
-
ccu.table_name AS foreign_table_name,
|
|
22
|
-
ccu.column_name AS foreign_column_name
|
|
23
|
-
FROM
|
|
24
|
-
information_schema.table_constraints tc
|
|
25
|
-
JOIN information_schema.key_column_usage kcu
|
|
26
|
-
ON tc.constraint_name = kcu.constraint_name
|
|
27
|
-
AND tc.table_schema = kcu.table_schema
|
|
28
|
-
JOIN information_schema.constraint_column_usage ccu
|
|
29
|
-
ON ccu.constraint_name = tc.constraint_name
|
|
30
|
-
AND ccu.table_schema = tc.table_schema
|
|
31
|
-
WHERE
|
|
32
|
-
tc.constraint_type = 'FOREIGN KEY'
|
|
33
|
-
AND tc.table_schema = $1
|
|
34
|
-
AND tc.table_name = $2;
|
|
18
|
+
const query = `
|
|
19
|
+
SELECT
|
|
20
|
+
kcu.column_name,
|
|
21
|
+
ccu.table_name AS foreign_table_name,
|
|
22
|
+
ccu.column_name AS foreign_column_name
|
|
23
|
+
FROM
|
|
24
|
+
information_schema.table_constraints tc
|
|
25
|
+
JOIN information_schema.key_column_usage kcu
|
|
26
|
+
ON tc.constraint_name = kcu.constraint_name
|
|
27
|
+
AND tc.table_schema = kcu.table_schema
|
|
28
|
+
JOIN information_schema.constraint_column_usage ccu
|
|
29
|
+
ON ccu.constraint_name = tc.constraint_name
|
|
30
|
+
AND ccu.table_schema = tc.table_schema
|
|
31
|
+
WHERE
|
|
32
|
+
tc.constraint_type = 'FOREIGN KEY'
|
|
33
|
+
AND tc.table_schema = $1
|
|
34
|
+
AND tc.table_name = $2;
|
|
35
35
|
`;
|
|
36
36
|
const { rows } = await this.connections.targetPool.query(query, [
|
|
37
37
|
schemaName,
|
|
@@ -42,7 +42,7 @@ class ForeignKeyManager {
|
|
|
42
42
|
foreignTable: row.foreign_table_name,
|
|
43
43
|
foreignColumn: row.foreign_column_name,
|
|
44
44
|
}));
|
|
45
|
-
this.logger.log(`Encontradas ${dependencies.length} dependencias para ${schemaName}.${tableName}:
|
|
45
|
+
this.logger.log(`Encontradas ${dependencies.length} dependencias para ${schemaName}.${tableName}:
|
|
46
46
|
${dependencies
|
|
47
47
|
.map((d) => `\n - ${d.column} -> ${d.foreignTable}.${d.foreignColumn}`)
|
|
48
48
|
.join("")}`);
|
|
@@ -81,7 +81,7 @@ class ForeignKeyManager {
|
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
if (missingDependencies.length > 0) {
|
|
84
|
-
this.logger.debug(`Registro ${recordId}: Faltan ${missingDependencies.length} dependencias:
|
|
84
|
+
this.logger.debug(`Registro ${recordId}: Faltan ${missingDependencies.length} dependencias:
|
|
85
85
|
${missingDependencies.map((dep) => `\n - ${dep}`).join("")}`);
|
|
86
86
|
}
|
|
87
87
|
else {
|
|
@@ -141,10 +141,10 @@ class ForeignKeyManager {
|
|
|
141
141
|
}
|
|
142
142
|
async checkTableExists(tableName) {
|
|
143
143
|
try {
|
|
144
|
-
const result = await this.connections.sourcePool.query(`SELECT EXISTS (
|
|
145
|
-
SELECT FROM information_schema.tables
|
|
146
|
-
WHERE table_schema = 'public'
|
|
147
|
-
AND table_name = $1
|
|
144
|
+
const result = await this.connections.sourcePool.query(`SELECT EXISTS (
|
|
145
|
+
SELECT FROM information_schema.tables
|
|
146
|
+
WHERE table_schema = 'public'
|
|
147
|
+
AND table_name = $1
|
|
148
148
|
)`, [tableName]);
|
|
149
149
|
const exists = result.rows[0].exists;
|
|
150
150
|
this.logger.debug(`Verificación de existencia de tabla ${tableName}: ${exists ? "EXISTE" : "NO EXISTE"}`);
|
|
@@ -113,11 +113,11 @@ class DataMigrationTool {
|
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
this.logger.debug(`Validation check: Using ${transactionTableKey}.${transactionProviderFk} referencing ${providerTableKey}.${providerIdColumn}`);
|
|
116
|
-
const query = `
|
|
117
|
-
SELECT t."${tablesConfig[transactionTableKey].idField}" AS transaction_id, t."${transactionProviderFk}"
|
|
118
|
-
FROM "${transactionTableKey}" t
|
|
119
|
-
LEFT JOIN "${providerTableKey}" p ON t."${transactionProviderFk}" = p."${providerIdColumn}"
|
|
120
|
-
WHERE t."${transactionProviderFk}" IS NOT NULL AND p."${providerIdColumn}" IS NULL;
|
|
116
|
+
const query = `
|
|
117
|
+
SELECT t."${tablesConfig[transactionTableKey].idField}" AS transaction_id, t."${transactionProviderFk}"
|
|
118
|
+
FROM "${transactionTableKey}" t
|
|
119
|
+
LEFT JOIN "${providerTableKey}" p ON t."${transactionProviderFk}" = p."${providerIdColumn}"
|
|
120
|
+
WHERE t."${transactionProviderFk}" IS NOT NULL AND p."${providerIdColumn}" IS NULL;
|
|
121
121
|
`;
|
|
122
122
|
const invalidTransactions = (await this.connections.sourcePrisma.$queryRawUnsafe(query));
|
|
123
123
|
if (invalidTransactions.length > 0) {
|