@javalabs/prisma-client 1.0.18 → 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 +47 -1
- 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,129 +1,129 @@
|
|
|
1
|
-
import { Logger } from "@nestjs/common";
|
|
2
|
-
import { DatabaseConnector } from "./data-migration/db-connector";
|
|
3
|
-
import * as fs from "fs";
|
|
4
|
-
import * as path from "path";
|
|
5
|
-
|
|
6
|
-
async function main() {
|
|
7
|
-
const logger = new Logger("SchemaSyncTool");
|
|
8
|
-
logger.log("Starting schema synchronization");
|
|
9
|
-
|
|
10
|
-
// Crear conexiones a las bases de datos
|
|
11
|
-
const connections = DatabaseConnector.createConnections();
|
|
12
|
-
|
|
13
|
-
try {
|
|
14
|
-
// Obtener todas las tablas de la base de datos origen
|
|
15
|
-
const sourceTables = await getTablesFromDatabase(connections.sourcePool);
|
|
16
|
-
logger.log(`Found ${sourceTables.length} tables in source database`);
|
|
17
|
-
|
|
18
|
-
// Obtener todas las tablas de la base de datos destino
|
|
19
|
-
const targetTables = await getTablesFromDatabase(connections.targetPool);
|
|
20
|
-
logger.log(`Found ${targetTables.length} tables in target database`);
|
|
21
|
-
|
|
22
|
-
// Comparar esquemas y generar SQL para sincronizar
|
|
23
|
-
const syncScript = await generateSyncScript(
|
|
24
|
-
connections.sourcePool,
|
|
25
|
-
connections.targetPool,
|
|
26
|
-
sourceTables,
|
|
27
|
-
targetTables
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
// Guardar el script SQL
|
|
31
|
-
const timestamp = new Date().toISOString().replace(/:/g, "-").split(".")[0];
|
|
32
|
-
const filePath = path.join(
|
|
33
|
-
process.cwd(),
|
|
34
|
-
`schema-sync-${timestamp}.sql`
|
|
35
|
-
);
|
|
36
|
-
fs.writeFileSync(filePath, syncScript);
|
|
37
|
-
|
|
38
|
-
logger.log(`Schema synchronization script saved to ${filePath}`);
|
|
39
|
-
logger.log("Review the script and execute it manually to synchronize schemas");
|
|
40
|
-
|
|
41
|
-
} catch (error) {
|
|
42
|
-
logger.error(`Error in schema synchronization: ${error.message}`, error.stack);
|
|
43
|
-
} finally {
|
|
44
|
-
// Cerrar conexiones
|
|
45
|
-
await DatabaseConnector.cleanup(connections);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async function getTablesFromDatabase(pool: any): Promise<string[]> {
|
|
50
|
-
const query = `
|
|
51
|
-
SELECT table_name
|
|
52
|
-
FROM information_schema.tables
|
|
53
|
-
WHERE table_schema = 'public'
|
|
54
|
-
AND table_type = 'BASE TABLE'
|
|
55
|
-
`;
|
|
56
|
-
|
|
57
|
-
const result = await pool.query(query);
|
|
58
|
-
return result.rows.map(row => row.table_name);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async function generateSyncScript(
|
|
62
|
-
sourcePool: any,
|
|
63
|
-
targetPool: any,
|
|
64
|
-
sourceTables: string[],
|
|
65
|
-
targetTables: string[]
|
|
66
|
-
): Promise<string> {
|
|
67
|
-
let script = "-- Schema synchronization script\n\n";
|
|
68
|
-
|
|
69
|
-
// Procesar cada tabla de la fuente
|
|
70
|
-
for (const table of sourceTables) {
|
|
71
|
-
// Obtener columnas de la tabla fuente
|
|
72
|
-
const sourceColumns = await getTableColumns(sourcePool, table);
|
|
73
|
-
|
|
74
|
-
if (targetTables.includes(table)) {
|
|
75
|
-
// La tabla existe en ambas bases de datos, comparar columnas
|
|
76
|
-
const targetColumns = await getTableColumns(targetPool, table);
|
|
77
|
-
|
|
78
|
-
// Encontrar columnas que faltan en el destino
|
|
79
|
-
const missingColumns = sourceColumns.filter(
|
|
80
|
-
sourceCol => !targetColumns.some(targetCol => targetCol.column_name === sourceCol.column_name)
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
// Generar SQL para agregar columnas faltantes
|
|
84
|
-
for (const column of missingColumns) {
|
|
85
|
-
script += `-- Adding missing column ${column.column_name} to table ${table}\n`;
|
|
86
|
-
script += `ALTER TABLE "${table}" ADD COLUMN "${column.column_name}" ${column.data_type}`;
|
|
87
|
-
|
|
88
|
-
// Agregar restricciones si es necesario
|
|
89
|
-
if (column.is_nullable === 'NO') {
|
|
90
|
-
script += " NOT NULL";
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
script += ";\n\n";
|
|
94
|
-
}
|
|
95
|
-
} else {
|
|
96
|
-
// La tabla no existe en el destino, generar SQL para crearla
|
|
97
|
-
script += `-- Table ${table} does not exist in target database\n`;
|
|
98
|
-
script += `-- Consider running a full schema migration instead\n\n`;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return script;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
async function getTableColumns(pool: any, table: string): Promise<any[]> {
|
|
106
|
-
const query = `
|
|
107
|
-
SELECT
|
|
108
|
-
column_name,
|
|
109
|
-
data_type,
|
|
110
|
-
is_nullable,
|
|
111
|
-
column_default
|
|
112
|
-
FROM
|
|
113
|
-
information_schema.columns
|
|
114
|
-
WHERE
|
|
115
|
-
table_schema = 'public'
|
|
116
|
-
AND table_name = $1
|
|
117
|
-
ORDER BY
|
|
118
|
-
ordinal_position
|
|
119
|
-
`;
|
|
120
|
-
|
|
121
|
-
const result = await pool.query(query, [table]);
|
|
122
|
-
return result.rows;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// Ejecutar el script
|
|
126
|
-
main().catch(e => {
|
|
127
|
-
console.error(e);
|
|
128
|
-
process.exit(1);
|
|
1
|
+
import { Logger } from "@nestjs/common";
|
|
2
|
+
import { DatabaseConnector } from "./data-migration/db-connector";
|
|
3
|
+
import * as fs from "fs";
|
|
4
|
+
import * as path from "path";
|
|
5
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const logger = new Logger("SchemaSyncTool");
|
|
8
|
+
logger.log("Starting schema synchronization");
|
|
9
|
+
|
|
10
|
+
// Crear conexiones a las bases de datos
|
|
11
|
+
const connections = DatabaseConnector.createConnections();
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
// Obtener todas las tablas de la base de datos origen
|
|
15
|
+
const sourceTables = await getTablesFromDatabase(connections.sourcePool);
|
|
16
|
+
logger.log(`Found ${sourceTables.length} tables in source database`);
|
|
17
|
+
|
|
18
|
+
// Obtener todas las tablas de la base de datos destino
|
|
19
|
+
const targetTables = await getTablesFromDatabase(connections.targetPool);
|
|
20
|
+
logger.log(`Found ${targetTables.length} tables in target database`);
|
|
21
|
+
|
|
22
|
+
// Comparar esquemas y generar SQL para sincronizar
|
|
23
|
+
const syncScript = await generateSyncScript(
|
|
24
|
+
connections.sourcePool,
|
|
25
|
+
connections.targetPool,
|
|
26
|
+
sourceTables,
|
|
27
|
+
targetTables
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// Guardar el script SQL
|
|
31
|
+
const timestamp = new Date().toISOString().replace(/:/g, "-").split(".")[0];
|
|
32
|
+
const filePath = path.join(
|
|
33
|
+
process.cwd(),
|
|
34
|
+
`schema-sync-${timestamp}.sql`
|
|
35
|
+
);
|
|
36
|
+
fs.writeFileSync(filePath, syncScript);
|
|
37
|
+
|
|
38
|
+
logger.log(`Schema synchronization script saved to ${filePath}`);
|
|
39
|
+
logger.log("Review the script and execute it manually to synchronize schemas");
|
|
40
|
+
|
|
41
|
+
} catch (error) {
|
|
42
|
+
logger.error(`Error in schema synchronization: ${error.message}`, error.stack);
|
|
43
|
+
} finally {
|
|
44
|
+
// Cerrar conexiones
|
|
45
|
+
await DatabaseConnector.cleanup(connections);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function getTablesFromDatabase(pool: any): Promise<string[]> {
|
|
50
|
+
const query = `
|
|
51
|
+
SELECT table_name
|
|
52
|
+
FROM information_schema.tables
|
|
53
|
+
WHERE table_schema = 'public'
|
|
54
|
+
AND table_type = 'BASE TABLE'
|
|
55
|
+
`;
|
|
56
|
+
|
|
57
|
+
const result = await pool.query(query);
|
|
58
|
+
return result.rows.map(row => row.table_name);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function generateSyncScript(
|
|
62
|
+
sourcePool: any,
|
|
63
|
+
targetPool: any,
|
|
64
|
+
sourceTables: string[],
|
|
65
|
+
targetTables: string[]
|
|
66
|
+
): Promise<string> {
|
|
67
|
+
let script = "-- Schema synchronization script\n\n";
|
|
68
|
+
|
|
69
|
+
// Procesar cada tabla de la fuente
|
|
70
|
+
for (const table of sourceTables) {
|
|
71
|
+
// Obtener columnas de la tabla fuente
|
|
72
|
+
const sourceColumns = await getTableColumns(sourcePool, table);
|
|
73
|
+
|
|
74
|
+
if (targetTables.includes(table)) {
|
|
75
|
+
// La tabla existe en ambas bases de datos, comparar columnas
|
|
76
|
+
const targetColumns = await getTableColumns(targetPool, table);
|
|
77
|
+
|
|
78
|
+
// Encontrar columnas que faltan en el destino
|
|
79
|
+
const missingColumns = sourceColumns.filter(
|
|
80
|
+
sourceCol => !targetColumns.some(targetCol => targetCol.column_name === sourceCol.column_name)
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
// Generar SQL para agregar columnas faltantes
|
|
84
|
+
for (const column of missingColumns) {
|
|
85
|
+
script += `-- Adding missing column ${column.column_name} to table ${table}\n`;
|
|
86
|
+
script += `ALTER TABLE "${table}" ADD COLUMN "${column.column_name}" ${column.data_type}`;
|
|
87
|
+
|
|
88
|
+
// Agregar restricciones si es necesario
|
|
89
|
+
if (column.is_nullable === 'NO') {
|
|
90
|
+
script += " NOT NULL";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
script += ";\n\n";
|
|
94
|
+
}
|
|
95
|
+
} else {
|
|
96
|
+
// La tabla no existe en el destino, generar SQL para crearla
|
|
97
|
+
script += `-- Table ${table} does not exist in target database\n`;
|
|
98
|
+
script += `-- Consider running a full schema migration instead\n\n`;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return script;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function getTableColumns(pool: any, table: string): Promise<any[]> {
|
|
106
|
+
const query = `
|
|
107
|
+
SELECT
|
|
108
|
+
column_name,
|
|
109
|
+
data_type,
|
|
110
|
+
is_nullable,
|
|
111
|
+
column_default
|
|
112
|
+
FROM
|
|
113
|
+
information_schema.columns
|
|
114
|
+
WHERE
|
|
115
|
+
table_schema = 'public'
|
|
116
|
+
AND table_name = $1
|
|
117
|
+
ORDER BY
|
|
118
|
+
ordinal_position
|
|
119
|
+
`;
|
|
120
|
+
|
|
121
|
+
const result = await pool.query(query, [table]);
|
|
122
|
+
return result.rows;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Ejecutar el script
|
|
126
|
+
main().catch(e => {
|
|
127
|
+
console.error(e);
|
|
128
|
+
process.exit(1);
|
|
129
129
|
});
|