@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
|
@@ -1,197 +1,197 @@
|
|
|
1
|
-
import { Logger } from "@nestjs/common";
|
|
2
|
-
import { Pool } from "pg";
|
|
3
|
-
import { EntityType } from "./types";
|
|
4
|
-
|
|
5
|
-
export class EntityDiscovery {
|
|
6
|
-
private readonly logger = new Logger("EntityDiscovery");
|
|
7
|
-
|
|
8
|
-
constructor(private readonly targetPool: Pool) {}
|
|
9
|
-
|
|
10
|
-
async discoverEntityTypes(
|
|
11
|
-
schemaName: string,
|
|
12
|
-
tables: string[]
|
|
13
|
-
): Promise<EntityType[]> {
|
|
14
|
-
try {
|
|
15
|
-
const entityTypes: EntityType[] = [];
|
|
16
|
-
|
|
17
|
-
for (const tableName of tables) {
|
|
18
|
-
// Obtener la clave primaria de la tabla
|
|
19
|
-
const primaryKey = await this.getPrimaryKey(schemaName, tableName);
|
|
20
|
-
|
|
21
|
-
if (!primaryKey) {
|
|
22
|
-
this.logger.warn(`No primary key found for table ${tableName}, skipping`);
|
|
23
|
-
continue;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// Obtener las claves foráneas que podrían conectar con un proveedor
|
|
27
|
-
const foreignKeys = await this.getForeignKeys(schemaName, tableName);
|
|
28
|
-
|
|
29
|
-
// Buscar una clave foránea que podría ser provider_id
|
|
30
|
-
const providerForeignKey = foreignKeys.find(fk =>
|
|
31
|
-
fk.column_name.includes('provider_id') ||
|
|
32
|
-
fk.foreign_table_name === 'providers'
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
// Crear la entidad
|
|
36
|
-
const entity: EntityType = {
|
|
37
|
-
name: tableName,
|
|
38
|
-
idField: primaryKey,
|
|
39
|
-
foreignKey: providerForeignKey?.column_name,
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
// Si no hay una clave foránea directa a providers, buscar relaciones indirectas
|
|
43
|
-
if (!entity.foreignKey) {
|
|
44
|
-
// Buscar tablas de unión que podrían conectar esta tabla con providers
|
|
45
|
-
const joinTables = await this.findJoinTables(
|
|
46
|
-
schemaName,
|
|
47
|
-
tableName,
|
|
48
|
-
'providers'
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
if (joinTables.length > 0) {
|
|
52
|
-
const joinTable = joinTables[0];
|
|
53
|
-
entity.joinTable = joinTable.join_table;
|
|
54
|
-
entity.joinField = joinTable.source_column;
|
|
55
|
-
entity.joinForeignKey = joinTable.target_column;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
entityTypes.push(entity);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return entityTypes;
|
|
63
|
-
} catch (error) {
|
|
64
|
-
this.logger.error(`Error discovering entity types: ${error.message}`);
|
|
65
|
-
return [];
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
private async getPrimaryKey(
|
|
70
|
-
schemaName: string,
|
|
71
|
-
tableName: string
|
|
72
|
-
): Promise<string | null> {
|
|
73
|
-
const query = `
|
|
74
|
-
SELECT a.attname
|
|
75
|
-
FROM pg_index i
|
|
76
|
-
JOIN pg_attribute a ON a.attrelid = i.indrelid
|
|
77
|
-
AND a.attnum = ANY(i.indkey)
|
|
78
|
-
WHERE i.indrelid = '"${schemaName}"."${tableName}"'::regclass
|
|
79
|
-
AND i.indisprimary;
|
|
80
|
-
`;
|
|
81
|
-
|
|
82
|
-
try {
|
|
83
|
-
const { rows } = await this.targetPool.query(query);
|
|
84
|
-
return rows.length > 0 ? rows[0].attname : null;
|
|
85
|
-
} catch (error) {
|
|
86
|
-
this.logger.error(`Error getting primary key for ${tableName}: ${error.message}`);
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
private async getForeignKeys(
|
|
92
|
-
schemaName: string,
|
|
93
|
-
tableName: string
|
|
94
|
-
): Promise<Array<{
|
|
95
|
-
column_name: string;
|
|
96
|
-
foreign_table_name: string;
|
|
97
|
-
foreign_column_name: string;
|
|
98
|
-
}>> {
|
|
99
|
-
const query = `
|
|
100
|
-
SELECT
|
|
101
|
-
kcu.column_name,
|
|
102
|
-
ccu.table_name AS foreign_table_name,
|
|
103
|
-
ccu.column_name AS foreign_column_name
|
|
104
|
-
FROM
|
|
105
|
-
information_schema.table_constraints tc
|
|
106
|
-
JOIN information_schema.key_column_usage kcu
|
|
107
|
-
ON tc.constraint_name = kcu.constraint_name
|
|
108
|
-
AND tc.table_schema = kcu.table_schema
|
|
109
|
-
JOIN information_schema.constraint_column_usage ccu
|
|
110
|
-
ON ccu.constraint_name = tc.constraint_name
|
|
111
|
-
AND ccu.table_schema = tc.table_schema
|
|
112
|
-
WHERE
|
|
113
|
-
tc.constraint_type = 'FOREIGN KEY'
|
|
114
|
-
AND tc.table_schema = $1
|
|
115
|
-
AND tc.table_name = $2;
|
|
116
|
-
`;
|
|
117
|
-
|
|
118
|
-
try {
|
|
119
|
-
const { rows } = await this.targetPool.query(query, [schemaName, tableName]);
|
|
120
|
-
return rows;
|
|
121
|
-
} catch (error) {
|
|
122
|
-
this.logger.error(`Error getting foreign keys for ${tableName}: ${error.message}`);
|
|
123
|
-
return [];
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
private async findJoinTables(
|
|
128
|
-
schemaName: string,
|
|
129
|
-
sourceTable: string,
|
|
130
|
-
targetTable: string
|
|
131
|
-
): Promise<Array<{
|
|
132
|
-
join_table: string;
|
|
133
|
-
source_column: string;
|
|
134
|
-
target_column: string;
|
|
135
|
-
}>> {
|
|
136
|
-
// Buscar tablas que tienen claves foráneas tanto a sourceTable como a targetTable
|
|
137
|
-
const query = `
|
|
138
|
-
WITH source_fks AS (
|
|
139
|
-
SELECT
|
|
140
|
-
tc.table_name,
|
|
141
|
-
kcu.column_name,
|
|
142
|
-
ccu.table_name AS referenced_table
|
|
143
|
-
FROM
|
|
144
|
-
information_schema.table_constraints tc
|
|
145
|
-
JOIN information_schema.key_column_usage kcu
|
|
146
|
-
ON tc.constraint_name = kcu.constraint_name
|
|
147
|
-
AND tc.table_schema = kcu.table_schema
|
|
148
|
-
JOIN information_schema.constraint_column_usage ccu
|
|
149
|
-
ON ccu.constraint_name = tc.constraint_name
|
|
150
|
-
AND ccu.table_schema = tc.table_schema
|
|
151
|
-
WHERE
|
|
152
|
-
tc.constraint_type = 'FOREIGN KEY'
|
|
153
|
-
AND tc.table_schema = $1
|
|
154
|
-
AND ccu.table_name = $2
|
|
155
|
-
),
|
|
156
|
-
target_fks AS (
|
|
157
|
-
SELECT
|
|
158
|
-
tc.table_name,
|
|
159
|
-
kcu.column_name,
|
|
160
|
-
ccu.table_name AS referenced_table
|
|
161
|
-
FROM
|
|
162
|
-
information_schema.table_constraints tc
|
|
163
|
-
JOIN information_schema.key_column_usage kcu
|
|
164
|
-
ON tc.constraint_name = kcu.constraint_name
|
|
165
|
-
AND tc.table_schema = kcu.table_schema
|
|
166
|
-
JOIN information_schema.constraint_column_usage ccu
|
|
167
|
-
ON ccu.constraint_name = tc.constraint_name
|
|
168
|
-
AND ccu.table_schema = tc.table_schema
|
|
169
|
-
WHERE
|
|
170
|
-
tc.constraint_type = 'FOREIGN KEY'
|
|
171
|
-
AND tc.table_schema = $1
|
|
172
|
-
AND ccu.table_name = $3
|
|
173
|
-
)
|
|
174
|
-
SELECT
|
|
175
|
-
s.table_name AS join_table,
|
|
176
|
-
s.column_name AS source_column,
|
|
177
|
-
t.column_name AS target_column
|
|
178
|
-
FROM
|
|
179
|
-
source_fks s
|
|
180
|
-
JOIN target_fks t ON s.table_name = t.table_name
|
|
181
|
-
`;
|
|
182
|
-
|
|
183
|
-
try {
|
|
184
|
-
const { rows } = await this.targetPool.query(query, [
|
|
185
|
-
schemaName,
|
|
186
|
-
sourceTable,
|
|
187
|
-
targetTable,
|
|
188
|
-
]);
|
|
189
|
-
return rows;
|
|
190
|
-
} catch (error) {
|
|
191
|
-
this.logger.error(
|
|
192
|
-
`Error finding join tables between ${sourceTable} and ${targetTable}: ${error.message}`
|
|
193
|
-
);
|
|
194
|
-
return [];
|
|
195
|
-
}
|
|
196
|
-
}
|
|
1
|
+
import { Logger } from "@nestjs/common";
|
|
2
|
+
import { Pool } from "pg";
|
|
3
|
+
import { EntityType } from "./types";
|
|
4
|
+
|
|
5
|
+
export class EntityDiscovery {
|
|
6
|
+
private readonly logger = new Logger("EntityDiscovery");
|
|
7
|
+
|
|
8
|
+
constructor(private readonly targetPool: Pool) {}
|
|
9
|
+
|
|
10
|
+
async discoverEntityTypes(
|
|
11
|
+
schemaName: string,
|
|
12
|
+
tables: string[]
|
|
13
|
+
): Promise<EntityType[]> {
|
|
14
|
+
try {
|
|
15
|
+
const entityTypes: EntityType[] = [];
|
|
16
|
+
|
|
17
|
+
for (const tableName of tables) {
|
|
18
|
+
// Obtener la clave primaria de la tabla
|
|
19
|
+
const primaryKey = await this.getPrimaryKey(schemaName, tableName);
|
|
20
|
+
|
|
21
|
+
if (!primaryKey) {
|
|
22
|
+
this.logger.warn(`No primary key found for table ${tableName}, skipping`);
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Obtener las claves foráneas que podrían conectar con un proveedor
|
|
27
|
+
const foreignKeys = await this.getForeignKeys(schemaName, tableName);
|
|
28
|
+
|
|
29
|
+
// Buscar una clave foránea que podría ser provider_id
|
|
30
|
+
const providerForeignKey = foreignKeys.find(fk =>
|
|
31
|
+
fk.column_name.includes('provider_id') ||
|
|
32
|
+
fk.foreign_table_name === 'providers'
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
// Crear la entidad
|
|
36
|
+
const entity: EntityType = {
|
|
37
|
+
name: tableName,
|
|
38
|
+
idField: primaryKey,
|
|
39
|
+
foreignKey: providerForeignKey?.column_name,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Si no hay una clave foránea directa a providers, buscar relaciones indirectas
|
|
43
|
+
if (!entity.foreignKey) {
|
|
44
|
+
// Buscar tablas de unión que podrían conectar esta tabla con providers
|
|
45
|
+
const joinTables = await this.findJoinTables(
|
|
46
|
+
schemaName,
|
|
47
|
+
tableName,
|
|
48
|
+
'providers'
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
if (joinTables.length > 0) {
|
|
52
|
+
const joinTable = joinTables[0];
|
|
53
|
+
entity.joinTable = joinTable.join_table;
|
|
54
|
+
entity.joinField = joinTable.source_column;
|
|
55
|
+
entity.joinForeignKey = joinTable.target_column;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
entityTypes.push(entity);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return entityTypes;
|
|
63
|
+
} catch (error) {
|
|
64
|
+
this.logger.error(`Error discovering entity types: ${error.message}`);
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private async getPrimaryKey(
|
|
70
|
+
schemaName: string,
|
|
71
|
+
tableName: string
|
|
72
|
+
): Promise<string | null> {
|
|
73
|
+
const query = `
|
|
74
|
+
SELECT a.attname
|
|
75
|
+
FROM pg_index i
|
|
76
|
+
JOIN pg_attribute a ON a.attrelid = i.indrelid
|
|
77
|
+
AND a.attnum = ANY(i.indkey)
|
|
78
|
+
WHERE i.indrelid = '"${schemaName}"."${tableName}"'::regclass
|
|
79
|
+
AND i.indisprimary;
|
|
80
|
+
`;
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
const { rows } = await this.targetPool.query(query);
|
|
84
|
+
return rows.length > 0 ? rows[0].attname : null;
|
|
85
|
+
} catch (error) {
|
|
86
|
+
this.logger.error(`Error getting primary key for ${tableName}: ${error.message}`);
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private async getForeignKeys(
|
|
92
|
+
schemaName: string,
|
|
93
|
+
tableName: string
|
|
94
|
+
): Promise<Array<{
|
|
95
|
+
column_name: string;
|
|
96
|
+
foreign_table_name: string;
|
|
97
|
+
foreign_column_name: string;
|
|
98
|
+
}>> {
|
|
99
|
+
const query = `
|
|
100
|
+
SELECT
|
|
101
|
+
kcu.column_name,
|
|
102
|
+
ccu.table_name AS foreign_table_name,
|
|
103
|
+
ccu.column_name AS foreign_column_name
|
|
104
|
+
FROM
|
|
105
|
+
information_schema.table_constraints tc
|
|
106
|
+
JOIN information_schema.key_column_usage kcu
|
|
107
|
+
ON tc.constraint_name = kcu.constraint_name
|
|
108
|
+
AND tc.table_schema = kcu.table_schema
|
|
109
|
+
JOIN information_schema.constraint_column_usage ccu
|
|
110
|
+
ON ccu.constraint_name = tc.constraint_name
|
|
111
|
+
AND ccu.table_schema = tc.table_schema
|
|
112
|
+
WHERE
|
|
113
|
+
tc.constraint_type = 'FOREIGN KEY'
|
|
114
|
+
AND tc.table_schema = $1
|
|
115
|
+
AND tc.table_name = $2;
|
|
116
|
+
`;
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
const { rows } = await this.targetPool.query(query, [schemaName, tableName]);
|
|
120
|
+
return rows;
|
|
121
|
+
} catch (error) {
|
|
122
|
+
this.logger.error(`Error getting foreign keys for ${tableName}: ${error.message}`);
|
|
123
|
+
return [];
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private async findJoinTables(
|
|
128
|
+
schemaName: string,
|
|
129
|
+
sourceTable: string,
|
|
130
|
+
targetTable: string
|
|
131
|
+
): Promise<Array<{
|
|
132
|
+
join_table: string;
|
|
133
|
+
source_column: string;
|
|
134
|
+
target_column: string;
|
|
135
|
+
}>> {
|
|
136
|
+
// Buscar tablas que tienen claves foráneas tanto a sourceTable como a targetTable
|
|
137
|
+
const query = `
|
|
138
|
+
WITH source_fks AS (
|
|
139
|
+
SELECT
|
|
140
|
+
tc.table_name,
|
|
141
|
+
kcu.column_name,
|
|
142
|
+
ccu.table_name AS referenced_table
|
|
143
|
+
FROM
|
|
144
|
+
information_schema.table_constraints tc
|
|
145
|
+
JOIN information_schema.key_column_usage kcu
|
|
146
|
+
ON tc.constraint_name = kcu.constraint_name
|
|
147
|
+
AND tc.table_schema = kcu.table_schema
|
|
148
|
+
JOIN information_schema.constraint_column_usage ccu
|
|
149
|
+
ON ccu.constraint_name = tc.constraint_name
|
|
150
|
+
AND ccu.table_schema = tc.table_schema
|
|
151
|
+
WHERE
|
|
152
|
+
tc.constraint_type = 'FOREIGN KEY'
|
|
153
|
+
AND tc.table_schema = $1
|
|
154
|
+
AND ccu.table_name = $2
|
|
155
|
+
),
|
|
156
|
+
target_fks AS (
|
|
157
|
+
SELECT
|
|
158
|
+
tc.table_name,
|
|
159
|
+
kcu.column_name,
|
|
160
|
+
ccu.table_name AS referenced_table
|
|
161
|
+
FROM
|
|
162
|
+
information_schema.table_constraints tc
|
|
163
|
+
JOIN information_schema.key_column_usage kcu
|
|
164
|
+
ON tc.constraint_name = kcu.constraint_name
|
|
165
|
+
AND tc.table_schema = kcu.table_schema
|
|
166
|
+
JOIN information_schema.constraint_column_usage ccu
|
|
167
|
+
ON ccu.constraint_name = tc.constraint_name
|
|
168
|
+
AND ccu.table_schema = tc.table_schema
|
|
169
|
+
WHERE
|
|
170
|
+
tc.constraint_type = 'FOREIGN KEY'
|
|
171
|
+
AND tc.table_schema = $1
|
|
172
|
+
AND ccu.table_name = $3
|
|
173
|
+
)
|
|
174
|
+
SELECT
|
|
175
|
+
s.table_name AS join_table,
|
|
176
|
+
s.column_name AS source_column,
|
|
177
|
+
t.column_name AS target_column
|
|
178
|
+
FROM
|
|
179
|
+
source_fks s
|
|
180
|
+
JOIN target_fks t ON s.table_name = t.table_name
|
|
181
|
+
`;
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
const { rows } = await this.targetPool.query(query, [
|
|
185
|
+
schemaName,
|
|
186
|
+
sourceTable,
|
|
187
|
+
targetTable,
|
|
188
|
+
]);
|
|
189
|
+
return rows;
|
|
190
|
+
} catch (error) {
|
|
191
|
+
this.logger.error(
|
|
192
|
+
`Error finding join tables between ${sourceTable} and ${targetTable}: ${error.message}`
|
|
193
|
+
);
|
|
194
|
+
return [];
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
197
|
}
|