@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,416 +1,416 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import { Command } from "commander";
|
|
4
|
-
import { config } from "dotenv";
|
|
5
|
-
import { Logger } from "@nestjs/common";
|
|
6
|
-
import { Pool } from "pg";
|
|
7
|
-
import * as path from "path";
|
|
8
|
-
import * as fs from "fs";
|
|
9
|
-
|
|
10
|
-
// Load environment variables
|
|
11
|
-
config();
|
|
12
|
-
|
|
13
|
-
const program = new Command();
|
|
14
|
-
const logger = new Logger("SequenceSyncCLI");
|
|
15
|
-
|
|
16
|
-
interface MigrationConfig {
|
|
17
|
-
commonSchema: string;
|
|
18
|
-
tables: {
|
|
19
|
-
[key: string]: {
|
|
20
|
-
type: string;
|
|
21
|
-
idField: string;
|
|
22
|
-
dependencies?: string[];
|
|
23
|
-
filterColumn?: string;
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
migrationPriorities: {
|
|
27
|
-
high: string[];
|
|
28
|
-
medium: string[];
|
|
29
|
-
low: string[];
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
program
|
|
34
|
-
.name("sequence-sync")
|
|
35
|
-
.description("Synchronize ID sequences in PostgreSQL database")
|
|
36
|
-
.option(
|
|
37
|
-
"-d, --database-url <url>",
|
|
38
|
-
"Database connection URL",
|
|
39
|
-
process.env.DATABASE_URL
|
|
40
|
-
)
|
|
41
|
-
.option(
|
|
42
|
-
"--dry-run",
|
|
43
|
-
"Show changes that would be made without applying them",
|
|
44
|
-
false
|
|
45
|
-
)
|
|
46
|
-
.option("-v, --verbose", "Show detailed information during execution", false)
|
|
47
|
-
.option("-t, --table <table>", "Synchronize a specific table", "")
|
|
48
|
-
.option(
|
|
49
|
-
"--priority <level>",
|
|
50
|
-
"Synchronize tables of a specific priority (high, medium, low)",
|
|
51
|
-
""
|
|
52
|
-
)
|
|
53
|
-
.option("--fix", "Attempt to fix missing sequences", false);
|
|
54
|
-
|
|
55
|
-
program.parse(process.argv);
|
|
56
|
-
const options = program.opts();
|
|
57
|
-
|
|
58
|
-
if (!options.databaseUrl) {
|
|
59
|
-
logger.error(
|
|
60
|
-
"Database URL must be provided via --database-url or DATABASE_URL environment variable"
|
|
61
|
-
);
|
|
62
|
-
process.exit(1);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Load configuration
|
|
66
|
-
const configPath = path.join(
|
|
67
|
-
__dirname,
|
|
68
|
-
"data-migration",
|
|
69
|
-
"migration-config.json"
|
|
70
|
-
);
|
|
71
|
-
let migrationConfig: MigrationConfig;
|
|
72
|
-
|
|
73
|
-
try {
|
|
74
|
-
migrationConfig = JSON.parse(fs.readFileSync(configPath, "utf8"));
|
|
75
|
-
} catch (error) {
|
|
76
|
-
logger.error("Error loading configuration file:", error.message);
|
|
77
|
-
process.exit(1);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
interface TableInfo {
|
|
81
|
-
tableName: string;
|
|
82
|
-
primaryKeyColumn: string;
|
|
83
|
-
sequenceName: string | null;
|
|
84
|
-
hasAutoIncrement: boolean;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async function findExistingSequence(
|
|
88
|
-
pool: Pool,
|
|
89
|
-
tableName: string,
|
|
90
|
-
columnName: string
|
|
91
|
-
): Promise<string | null> {
|
|
92
|
-
try {
|
|
93
|
-
// Query to find all sequences that might be associated with this column
|
|
94
|
-
const query = `
|
|
95
|
-
SELECT
|
|
96
|
-
s.relname as sequence_name
|
|
97
|
-
FROM pg_class s
|
|
98
|
-
JOIN pg_depend d ON d.objid = s.oid
|
|
99
|
-
JOIN pg_class t ON d.refobjid = t.oid
|
|
100
|
-
JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
|
|
101
|
-
JOIN pg_namespace n ON n.oid = s.relnamespace
|
|
102
|
-
WHERE s.relkind = 'S'
|
|
103
|
-
AND n.nspname = 'public'
|
|
104
|
-
AND t.relname = $1
|
|
105
|
-
AND a.attname = $2;
|
|
106
|
-
`;
|
|
107
|
-
|
|
108
|
-
const result = await pool.query(query, [tableName, columnName]);
|
|
109
|
-
|
|
110
|
-
if (result.rows.length > 0) {
|
|
111
|
-
if (options.verbose) {
|
|
112
|
-
logger.log(
|
|
113
|
-
`Found sequence for ${tableName}.${columnName}: ${result.rows[0].sequence_name}`
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
return result.rows[0].sequence_name;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return null;
|
|
120
|
-
} catch (error) {
|
|
121
|
-
logger.error(
|
|
122
|
-
`Error finding existing sequence for ${tableName}.${columnName}:`,
|
|
123
|
-
error.message
|
|
124
|
-
);
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
async function findOrCreateSequence(
|
|
130
|
-
pool: Pool,
|
|
131
|
-
tableName: string,
|
|
132
|
-
columnName: string
|
|
133
|
-
): Promise<string | null> {
|
|
134
|
-
try {
|
|
135
|
-
// First try to find an existing sequence
|
|
136
|
-
let sequenceName = await findExistingSequence(pool, tableName, columnName);
|
|
137
|
-
|
|
138
|
-
if (sequenceName) {
|
|
139
|
-
return sequenceName;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (!options.fix) {
|
|
143
|
-
if (options.verbose) {
|
|
144
|
-
logger.log(
|
|
145
|
-
`No sequence found for ${tableName}.${columnName} and --fix is not enabled`
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
return null;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Try common sequence name patterns
|
|
152
|
-
const possibleNames = [
|
|
153
|
-
`${tableName}_${columnName}_seq`,
|
|
154
|
-
`${tableName}_id_seq`,
|
|
155
|
-
`${tableName}_seq`,
|
|
156
|
-
];
|
|
157
|
-
|
|
158
|
-
for (const name of possibleNames) {
|
|
159
|
-
const exists = await pool.query(
|
|
160
|
-
`
|
|
161
|
-
SELECT EXISTS (
|
|
162
|
-
SELECT 1 FROM pg_class c
|
|
163
|
-
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
164
|
-
WHERE n.nspname = 'public'
|
|
165
|
-
AND c.relname = $1
|
|
166
|
-
AND c.relkind = 'S'
|
|
167
|
-
);
|
|
168
|
-
`,
|
|
169
|
-
[name]
|
|
170
|
-
);
|
|
171
|
-
|
|
172
|
-
if (exists.rows[0].exists) {
|
|
173
|
-
sequenceName = name;
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if (!sequenceName) {
|
|
179
|
-
// If no sequence exists, create a new one
|
|
180
|
-
sequenceName = `${tableName}_${columnName}_seq`;
|
|
181
|
-
|
|
182
|
-
if (options.verbose) {
|
|
183
|
-
logger.log(`Creating new sequence: ${sequenceName}`);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// Get current maximum value
|
|
187
|
-
const maxValQuery = `
|
|
188
|
-
SELECT COALESCE(MAX("${columnName}"), 0) as max_val
|
|
189
|
-
FROM "${tableName}";
|
|
190
|
-
`;
|
|
191
|
-
const maxValResult = await pool.query(maxValQuery);
|
|
192
|
-
const maxVal = parseInt(maxValResult.rows[0].max_val);
|
|
193
|
-
|
|
194
|
-
if (!options.dryRun) {
|
|
195
|
-
// Create sequence
|
|
196
|
-
await pool.query(`
|
|
197
|
-
CREATE SEQUENCE IF NOT EXISTS "${sequenceName}"
|
|
198
|
-
START WITH ${maxVal + 1}
|
|
199
|
-
INCREMENT BY 1
|
|
200
|
-
NO MINVALUE
|
|
201
|
-
NO MAXVALUE
|
|
202
|
-
CACHE 1;
|
|
203
|
-
`);
|
|
204
|
-
|
|
205
|
-
// Associate sequence with column
|
|
206
|
-
await pool.query(`
|
|
207
|
-
ALTER TABLE "${tableName}"
|
|
208
|
-
ALTER COLUMN "${columnName}"
|
|
209
|
-
SET DEFAULT nextval('${sequenceName}'::regclass);
|
|
210
|
-
`);
|
|
211
|
-
|
|
212
|
-
logger.log(`✅ Sequence created and associated: ${sequenceName}`);
|
|
213
|
-
} else {
|
|
214
|
-
logger.log(`[DRY-RUN] Would create sequence: ${sequenceName}`);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
return sequenceName;
|
|
219
|
-
} catch (error) {
|
|
220
|
-
logger.error(
|
|
221
|
-
`Error handling sequence for ${tableName}.${columnName}:`,
|
|
222
|
-
error.message
|
|
223
|
-
);
|
|
224
|
-
return null;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
async function getTableInfo(
|
|
229
|
-
pool: Pool,
|
|
230
|
-
tableName: string,
|
|
231
|
-
idField: string
|
|
232
|
-
): Promise<TableInfo | null> {
|
|
233
|
-
try {
|
|
234
|
-
// Check if table exists
|
|
235
|
-
const tableExistsQuery = `
|
|
236
|
-
SELECT EXISTS (
|
|
237
|
-
SELECT 1
|
|
238
|
-
FROM information_schema.tables
|
|
239
|
-
WHERE table_schema = $1
|
|
240
|
-
AND table_name = $2
|
|
241
|
-
);
|
|
242
|
-
`;
|
|
243
|
-
|
|
244
|
-
const tableExists = await pool.query(tableExistsQuery, [
|
|
245
|
-
migrationConfig.commonSchema,
|
|
246
|
-
tableName,
|
|
247
|
-
]);
|
|
248
|
-
|
|
249
|
-
if (!tableExists.rows[0].exists) {
|
|
250
|
-
logger.warn(`Table ${tableName} does not exist in database`);
|
|
251
|
-
return null;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// Try to find or create sequence
|
|
255
|
-
const sequenceName = await findOrCreateSequence(pool, tableName, idField);
|
|
256
|
-
|
|
257
|
-
// Check if column is auto-incrementing
|
|
258
|
-
const columnQuery = `
|
|
259
|
-
SELECT EXISTS (
|
|
260
|
-
SELECT 1 FROM pg_attrdef ad
|
|
261
|
-
JOIN pg_attribute a ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
|
|
262
|
-
WHERE a.attrelid = $1::regclass
|
|
263
|
-
AND a.attname = $2
|
|
264
|
-
AND (pg_get_expr(ad.adbin, ad.adrelid) LIKE 'nextval%' OR $3::text IS NOT NULL)
|
|
265
|
-
) as is_auto_increment;
|
|
266
|
-
`;
|
|
267
|
-
|
|
268
|
-
const columnResult = await pool.query(columnQuery, [
|
|
269
|
-
tableName,
|
|
270
|
-
idField,
|
|
271
|
-
sequenceName,
|
|
272
|
-
]);
|
|
273
|
-
|
|
274
|
-
return {
|
|
275
|
-
tableName,
|
|
276
|
-
primaryKeyColumn: idField,
|
|
277
|
-
sequenceName,
|
|
278
|
-
hasAutoIncrement: columnResult.rows[0].is_auto_increment,
|
|
279
|
-
};
|
|
280
|
-
} catch (error) {
|
|
281
|
-
logger.error(
|
|
282
|
-
`Error getting table information for ${tableName}:`,
|
|
283
|
-
error.message
|
|
284
|
-
);
|
|
285
|
-
return null;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
async function checkTableSequence(
|
|
290
|
-
pool: Pool,
|
|
291
|
-
tableName: string
|
|
292
|
-
): Promise<void> {
|
|
293
|
-
try {
|
|
294
|
-
const tableConfig = migrationConfig.tables[tableName];
|
|
295
|
-
if (!tableConfig) {
|
|
296
|
-
logger.warn(`No configuration found for table ${tableName}`);
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
const tableInfo = await getTableInfo(pool, tableName, tableConfig.idField);
|
|
301
|
-
if (!tableInfo) return;
|
|
302
|
-
|
|
303
|
-
if (!tableInfo.hasAutoIncrement || !tableInfo.sequenceName) {
|
|
304
|
-
if (options.verbose) {
|
|
305
|
-
logger.log(`\nTable ${tableInfo.tableName}:`);
|
|
306
|
-
logger.log(`- Primary key column: ${tableInfo.primaryKeyColumn}`);
|
|
307
|
-
logger.log(`- Does not use automatic sequence`);
|
|
308
|
-
}
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
if (options.verbose) {
|
|
313
|
-
logger.log(`\nTable information:`);
|
|
314
|
-
logger.log(`- Table name: ${tableInfo.tableName}`);
|
|
315
|
-
logger.log(`- Primary key column: ${tableInfo.primaryKeyColumn}`);
|
|
316
|
-
logger.log(`- Sequence: ${tableInfo.sequenceName}`);
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
// Get maximum ID
|
|
320
|
-
const maxIdResult = await pool.query(`
|
|
321
|
-
SELECT COALESCE(MAX("${tableInfo.primaryKeyColumn}"), 0) as max_id
|
|
322
|
-
FROM "${tableInfo.tableName}";
|
|
323
|
-
`);
|
|
324
|
-
const maxId = parseInt(maxIdResult.rows[0].max_id);
|
|
325
|
-
|
|
326
|
-
// Get current sequence value
|
|
327
|
-
const seqResult = await pool.query(`
|
|
328
|
-
SELECT last_value, is_called
|
|
329
|
-
FROM "${tableInfo.sequenceName}";
|
|
330
|
-
`);
|
|
331
|
-
const currentSeqValue = parseInt(seqResult.rows[0].last_value);
|
|
332
|
-
const isCalled = seqResult.rows[0].is_called;
|
|
333
|
-
|
|
334
|
-
logger.log(`\nDiagnostics for table ${tableInfo.tableName}:`);
|
|
335
|
-
logger.log(
|
|
336
|
-
`- Maximum ID in table (${tableInfo.primaryKeyColumn}): ${maxId}`
|
|
337
|
-
);
|
|
338
|
-
logger.log(`- Current sequence value: ${currentSeqValue}`);
|
|
339
|
-
logger.log(`- Sequence has been called: ${isCalled}`);
|
|
340
|
-
|
|
341
|
-
if (currentSeqValue <= maxId) {
|
|
342
|
-
logger.log(`\n⚠️ Problem detected! Sequence is behind maximum ID`);
|
|
343
|
-
|
|
344
|
-
if (!options.dryRun) {
|
|
345
|
-
await pool.query(`SELECT setval($1, $2, true);`, [
|
|
346
|
-
tableInfo.sequenceName,
|
|
347
|
-
maxId,
|
|
348
|
-
]);
|
|
349
|
-
logger.log(`✅ Sequence updated to value ${maxId}`);
|
|
350
|
-
} else {
|
|
351
|
-
logger.log(`[DRY-RUN] Would update sequence to value ${maxId}`);
|
|
352
|
-
}
|
|
353
|
-
} else {
|
|
354
|
-
logger.log(`✅ Sequence is properly configured`);
|
|
355
|
-
}
|
|
356
|
-
} catch (error) {
|
|
357
|
-
logger.error(`Error processing table ${tableName}:`, error.message);
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
async function main() {
|
|
362
|
-
try {
|
|
363
|
-
logger.log("Starting sequence synchronization...");
|
|
364
|
-
logger.log(`Database URL: ${options.databaseUrl.split("@")[1]}`);
|
|
365
|
-
|
|
366
|
-
if (options.dryRun) {
|
|
367
|
-
logger.log("Dry-run mode: No changes will be made");
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
const pool = new Pool({ connectionString: options.databaseUrl });
|
|
371
|
-
|
|
372
|
-
if (options.table) {
|
|
373
|
-
await checkTableSequence(pool, options.table);
|
|
374
|
-
} else if (options.priority) {
|
|
375
|
-
const tables =
|
|
376
|
-
migrationConfig.migrationPriorities[
|
|
377
|
-
options.priority as keyof typeof migrationConfig.migrationPriorities
|
|
378
|
-
];
|
|
379
|
-
if (!tables) {
|
|
380
|
-
logger.error(`Invalid priority: ${options.priority}`);
|
|
381
|
-
process.exit(1);
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
for (const table of tables) {
|
|
385
|
-
if (options.verbose) {
|
|
386
|
-
logger.log(
|
|
387
|
-
`\nProcessing ${options.priority} priority table: ${table}`
|
|
388
|
-
);
|
|
389
|
-
}
|
|
390
|
-
await checkTableSequence(pool, table);
|
|
391
|
-
}
|
|
392
|
-
} else {
|
|
393
|
-
// Process all tables in priority order
|
|
394
|
-
const allTables = [
|
|
395
|
-
...migrationConfig.migrationPriorities.high,
|
|
396
|
-
...migrationConfig.migrationPriorities.medium,
|
|
397
|
-
...migrationConfig.migrationPriorities.low,
|
|
398
|
-
];
|
|
399
|
-
|
|
400
|
-
for (const table of allTables) {
|
|
401
|
-
if (options.verbose) {
|
|
402
|
-
logger.log(`\nProcessing table: ${table}`);
|
|
403
|
-
}
|
|
404
|
-
await checkTableSequence(pool, table);
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
await pool.end();
|
|
409
|
-
logger.log("\nSequence synchronization completed successfully");
|
|
410
|
-
} catch (error) {
|
|
411
|
-
logger.error("Error during synchronization:", error.stack);
|
|
412
|
-
process.exit(1);
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
main();
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { Command } from "commander";
|
|
4
|
+
import { config } from "dotenv";
|
|
5
|
+
import { Logger } from "@nestjs/common";
|
|
6
|
+
import { Pool } from "pg";
|
|
7
|
+
import * as path from "path";
|
|
8
|
+
import * as fs from "fs";
|
|
9
|
+
|
|
10
|
+
// Load environment variables
|
|
11
|
+
config();
|
|
12
|
+
|
|
13
|
+
const program = new Command();
|
|
14
|
+
const logger = new Logger("SequenceSyncCLI");
|
|
15
|
+
|
|
16
|
+
interface MigrationConfig {
|
|
17
|
+
commonSchema: string;
|
|
18
|
+
tables: {
|
|
19
|
+
[key: string]: {
|
|
20
|
+
type: string;
|
|
21
|
+
idField: string;
|
|
22
|
+
dependencies?: string[];
|
|
23
|
+
filterColumn?: string;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
migrationPriorities: {
|
|
27
|
+
high: string[];
|
|
28
|
+
medium: string[];
|
|
29
|
+
low: string[];
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
program
|
|
34
|
+
.name("sequence-sync")
|
|
35
|
+
.description("Synchronize ID sequences in PostgreSQL database")
|
|
36
|
+
.option(
|
|
37
|
+
"-d, --database-url <url>",
|
|
38
|
+
"Database connection URL",
|
|
39
|
+
process.env.DATABASE_URL
|
|
40
|
+
)
|
|
41
|
+
.option(
|
|
42
|
+
"--dry-run",
|
|
43
|
+
"Show changes that would be made without applying them",
|
|
44
|
+
false
|
|
45
|
+
)
|
|
46
|
+
.option("-v, --verbose", "Show detailed information during execution", false)
|
|
47
|
+
.option("-t, --table <table>", "Synchronize a specific table", "")
|
|
48
|
+
.option(
|
|
49
|
+
"--priority <level>",
|
|
50
|
+
"Synchronize tables of a specific priority (high, medium, low)",
|
|
51
|
+
""
|
|
52
|
+
)
|
|
53
|
+
.option("--fix", "Attempt to fix missing sequences", false);
|
|
54
|
+
|
|
55
|
+
program.parse(process.argv);
|
|
56
|
+
const options = program.opts();
|
|
57
|
+
|
|
58
|
+
if (!options.databaseUrl) {
|
|
59
|
+
logger.error(
|
|
60
|
+
"Database URL must be provided via --database-url or DATABASE_URL environment variable"
|
|
61
|
+
);
|
|
62
|
+
process.exit(1);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Load configuration
|
|
66
|
+
const configPath = path.join(
|
|
67
|
+
__dirname,
|
|
68
|
+
"data-migration",
|
|
69
|
+
"migration-config.json"
|
|
70
|
+
);
|
|
71
|
+
let migrationConfig: MigrationConfig;
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
migrationConfig = JSON.parse(fs.readFileSync(configPath, "utf8"));
|
|
75
|
+
} catch (error) {
|
|
76
|
+
logger.error("Error loading configuration file:", error.message);
|
|
77
|
+
process.exit(1);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface TableInfo {
|
|
81
|
+
tableName: string;
|
|
82
|
+
primaryKeyColumn: string;
|
|
83
|
+
sequenceName: string | null;
|
|
84
|
+
hasAutoIncrement: boolean;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function findExistingSequence(
|
|
88
|
+
pool: Pool,
|
|
89
|
+
tableName: string,
|
|
90
|
+
columnName: string
|
|
91
|
+
): Promise<string | null> {
|
|
92
|
+
try {
|
|
93
|
+
// Query to find all sequences that might be associated with this column
|
|
94
|
+
const query = `
|
|
95
|
+
SELECT
|
|
96
|
+
s.relname as sequence_name
|
|
97
|
+
FROM pg_class s
|
|
98
|
+
JOIN pg_depend d ON d.objid = s.oid
|
|
99
|
+
JOIN pg_class t ON d.refobjid = t.oid
|
|
100
|
+
JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
|
|
101
|
+
JOIN pg_namespace n ON n.oid = s.relnamespace
|
|
102
|
+
WHERE s.relkind = 'S'
|
|
103
|
+
AND n.nspname = 'public'
|
|
104
|
+
AND t.relname = $1
|
|
105
|
+
AND a.attname = $2;
|
|
106
|
+
`;
|
|
107
|
+
|
|
108
|
+
const result = await pool.query(query, [tableName, columnName]);
|
|
109
|
+
|
|
110
|
+
if (result.rows.length > 0) {
|
|
111
|
+
if (options.verbose) {
|
|
112
|
+
logger.log(
|
|
113
|
+
`Found sequence for ${tableName}.${columnName}: ${result.rows[0].sequence_name}`
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
return result.rows[0].sequence_name;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return null;
|
|
120
|
+
} catch (error) {
|
|
121
|
+
logger.error(
|
|
122
|
+
`Error finding existing sequence for ${tableName}.${columnName}:`,
|
|
123
|
+
error.message
|
|
124
|
+
);
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function findOrCreateSequence(
|
|
130
|
+
pool: Pool,
|
|
131
|
+
tableName: string,
|
|
132
|
+
columnName: string
|
|
133
|
+
): Promise<string | null> {
|
|
134
|
+
try {
|
|
135
|
+
// First try to find an existing sequence
|
|
136
|
+
let sequenceName = await findExistingSequence(pool, tableName, columnName);
|
|
137
|
+
|
|
138
|
+
if (sequenceName) {
|
|
139
|
+
return sequenceName;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (!options.fix) {
|
|
143
|
+
if (options.verbose) {
|
|
144
|
+
logger.log(
|
|
145
|
+
`No sequence found for ${tableName}.${columnName} and --fix is not enabled`
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Try common sequence name patterns
|
|
152
|
+
const possibleNames = [
|
|
153
|
+
`${tableName}_${columnName}_seq`,
|
|
154
|
+
`${tableName}_id_seq`,
|
|
155
|
+
`${tableName}_seq`,
|
|
156
|
+
];
|
|
157
|
+
|
|
158
|
+
for (const name of possibleNames) {
|
|
159
|
+
const exists = await pool.query(
|
|
160
|
+
`
|
|
161
|
+
SELECT EXISTS (
|
|
162
|
+
SELECT 1 FROM pg_class c
|
|
163
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
164
|
+
WHERE n.nspname = 'public'
|
|
165
|
+
AND c.relname = $1
|
|
166
|
+
AND c.relkind = 'S'
|
|
167
|
+
);
|
|
168
|
+
`,
|
|
169
|
+
[name]
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
if (exists.rows[0].exists) {
|
|
173
|
+
sequenceName = name;
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (!sequenceName) {
|
|
179
|
+
// If no sequence exists, create a new one
|
|
180
|
+
sequenceName = `${tableName}_${columnName}_seq`;
|
|
181
|
+
|
|
182
|
+
if (options.verbose) {
|
|
183
|
+
logger.log(`Creating new sequence: ${sequenceName}`);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Get current maximum value
|
|
187
|
+
const maxValQuery = `
|
|
188
|
+
SELECT COALESCE(MAX("${columnName}"), 0) as max_val
|
|
189
|
+
FROM "${tableName}";
|
|
190
|
+
`;
|
|
191
|
+
const maxValResult = await pool.query(maxValQuery);
|
|
192
|
+
const maxVal = parseInt(maxValResult.rows[0].max_val);
|
|
193
|
+
|
|
194
|
+
if (!options.dryRun) {
|
|
195
|
+
// Create sequence
|
|
196
|
+
await pool.query(`
|
|
197
|
+
CREATE SEQUENCE IF NOT EXISTS "${sequenceName}"
|
|
198
|
+
START WITH ${maxVal + 1}
|
|
199
|
+
INCREMENT BY 1
|
|
200
|
+
NO MINVALUE
|
|
201
|
+
NO MAXVALUE
|
|
202
|
+
CACHE 1;
|
|
203
|
+
`);
|
|
204
|
+
|
|
205
|
+
// Associate sequence with column
|
|
206
|
+
await pool.query(`
|
|
207
|
+
ALTER TABLE "${tableName}"
|
|
208
|
+
ALTER COLUMN "${columnName}"
|
|
209
|
+
SET DEFAULT nextval('${sequenceName}'::regclass);
|
|
210
|
+
`);
|
|
211
|
+
|
|
212
|
+
logger.log(`✅ Sequence created and associated: ${sequenceName}`);
|
|
213
|
+
} else {
|
|
214
|
+
logger.log(`[DRY-RUN] Would create sequence: ${sequenceName}`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return sequenceName;
|
|
219
|
+
} catch (error) {
|
|
220
|
+
logger.error(
|
|
221
|
+
`Error handling sequence for ${tableName}.${columnName}:`,
|
|
222
|
+
error.message
|
|
223
|
+
);
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async function getTableInfo(
|
|
229
|
+
pool: Pool,
|
|
230
|
+
tableName: string,
|
|
231
|
+
idField: string
|
|
232
|
+
): Promise<TableInfo | null> {
|
|
233
|
+
try {
|
|
234
|
+
// Check if table exists
|
|
235
|
+
const tableExistsQuery = `
|
|
236
|
+
SELECT EXISTS (
|
|
237
|
+
SELECT 1
|
|
238
|
+
FROM information_schema.tables
|
|
239
|
+
WHERE table_schema = $1
|
|
240
|
+
AND table_name = $2
|
|
241
|
+
);
|
|
242
|
+
`;
|
|
243
|
+
|
|
244
|
+
const tableExists = await pool.query(tableExistsQuery, [
|
|
245
|
+
migrationConfig.commonSchema,
|
|
246
|
+
tableName,
|
|
247
|
+
]);
|
|
248
|
+
|
|
249
|
+
if (!tableExists.rows[0].exists) {
|
|
250
|
+
logger.warn(`Table ${tableName} does not exist in database`);
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Try to find or create sequence
|
|
255
|
+
const sequenceName = await findOrCreateSequence(pool, tableName, idField);
|
|
256
|
+
|
|
257
|
+
// Check if column is auto-incrementing
|
|
258
|
+
const columnQuery = `
|
|
259
|
+
SELECT EXISTS (
|
|
260
|
+
SELECT 1 FROM pg_attrdef ad
|
|
261
|
+
JOIN pg_attribute a ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
|
|
262
|
+
WHERE a.attrelid = $1::regclass
|
|
263
|
+
AND a.attname = $2
|
|
264
|
+
AND (pg_get_expr(ad.adbin, ad.adrelid) LIKE 'nextval%' OR $3::text IS NOT NULL)
|
|
265
|
+
) as is_auto_increment;
|
|
266
|
+
`;
|
|
267
|
+
|
|
268
|
+
const columnResult = await pool.query(columnQuery, [
|
|
269
|
+
tableName,
|
|
270
|
+
idField,
|
|
271
|
+
sequenceName,
|
|
272
|
+
]);
|
|
273
|
+
|
|
274
|
+
return {
|
|
275
|
+
tableName,
|
|
276
|
+
primaryKeyColumn: idField,
|
|
277
|
+
sequenceName,
|
|
278
|
+
hasAutoIncrement: columnResult.rows[0].is_auto_increment,
|
|
279
|
+
};
|
|
280
|
+
} catch (error) {
|
|
281
|
+
logger.error(
|
|
282
|
+
`Error getting table information for ${tableName}:`,
|
|
283
|
+
error.message
|
|
284
|
+
);
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
async function checkTableSequence(
|
|
290
|
+
pool: Pool,
|
|
291
|
+
tableName: string
|
|
292
|
+
): Promise<void> {
|
|
293
|
+
try {
|
|
294
|
+
const tableConfig = migrationConfig.tables[tableName];
|
|
295
|
+
if (!tableConfig) {
|
|
296
|
+
logger.warn(`No configuration found for table ${tableName}`);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const tableInfo = await getTableInfo(pool, tableName, tableConfig.idField);
|
|
301
|
+
if (!tableInfo) return;
|
|
302
|
+
|
|
303
|
+
if (!tableInfo.hasAutoIncrement || !tableInfo.sequenceName) {
|
|
304
|
+
if (options.verbose) {
|
|
305
|
+
logger.log(`\nTable ${tableInfo.tableName}:`);
|
|
306
|
+
logger.log(`- Primary key column: ${tableInfo.primaryKeyColumn}`);
|
|
307
|
+
logger.log(`- Does not use automatic sequence`);
|
|
308
|
+
}
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (options.verbose) {
|
|
313
|
+
logger.log(`\nTable information:`);
|
|
314
|
+
logger.log(`- Table name: ${tableInfo.tableName}`);
|
|
315
|
+
logger.log(`- Primary key column: ${tableInfo.primaryKeyColumn}`);
|
|
316
|
+
logger.log(`- Sequence: ${tableInfo.sequenceName}`);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Get maximum ID
|
|
320
|
+
const maxIdResult = await pool.query(`
|
|
321
|
+
SELECT COALESCE(MAX("${tableInfo.primaryKeyColumn}"), 0) as max_id
|
|
322
|
+
FROM "${tableInfo.tableName}";
|
|
323
|
+
`);
|
|
324
|
+
const maxId = parseInt(maxIdResult.rows[0].max_id);
|
|
325
|
+
|
|
326
|
+
// Get current sequence value
|
|
327
|
+
const seqResult = await pool.query(`
|
|
328
|
+
SELECT last_value, is_called
|
|
329
|
+
FROM "${tableInfo.sequenceName}";
|
|
330
|
+
`);
|
|
331
|
+
const currentSeqValue = parseInt(seqResult.rows[0].last_value);
|
|
332
|
+
const isCalled = seqResult.rows[0].is_called;
|
|
333
|
+
|
|
334
|
+
logger.log(`\nDiagnostics for table ${tableInfo.tableName}:`);
|
|
335
|
+
logger.log(
|
|
336
|
+
`- Maximum ID in table (${tableInfo.primaryKeyColumn}): ${maxId}`
|
|
337
|
+
);
|
|
338
|
+
logger.log(`- Current sequence value: ${currentSeqValue}`);
|
|
339
|
+
logger.log(`- Sequence has been called: ${isCalled}`);
|
|
340
|
+
|
|
341
|
+
if (currentSeqValue <= maxId) {
|
|
342
|
+
logger.log(`\n⚠️ Problem detected! Sequence is behind maximum ID`);
|
|
343
|
+
|
|
344
|
+
if (!options.dryRun) {
|
|
345
|
+
await pool.query(`SELECT setval($1, $2, true);`, [
|
|
346
|
+
tableInfo.sequenceName,
|
|
347
|
+
maxId,
|
|
348
|
+
]);
|
|
349
|
+
logger.log(`✅ Sequence updated to value ${maxId}`);
|
|
350
|
+
} else {
|
|
351
|
+
logger.log(`[DRY-RUN] Would update sequence to value ${maxId}`);
|
|
352
|
+
}
|
|
353
|
+
} else {
|
|
354
|
+
logger.log(`✅ Sequence is properly configured`);
|
|
355
|
+
}
|
|
356
|
+
} catch (error) {
|
|
357
|
+
logger.error(`Error processing table ${tableName}:`, error.message);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
async function main() {
|
|
362
|
+
try {
|
|
363
|
+
logger.log("Starting sequence synchronization...");
|
|
364
|
+
logger.log(`Database URL: ${options.databaseUrl.split("@")[1]}`);
|
|
365
|
+
|
|
366
|
+
if (options.dryRun) {
|
|
367
|
+
logger.log("Dry-run mode: No changes will be made");
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const pool = new Pool({ connectionString: options.databaseUrl });
|
|
371
|
+
|
|
372
|
+
if (options.table) {
|
|
373
|
+
await checkTableSequence(pool, options.table);
|
|
374
|
+
} else if (options.priority) {
|
|
375
|
+
const tables =
|
|
376
|
+
migrationConfig.migrationPriorities[
|
|
377
|
+
options.priority as keyof typeof migrationConfig.migrationPriorities
|
|
378
|
+
];
|
|
379
|
+
if (!tables) {
|
|
380
|
+
logger.error(`Invalid priority: ${options.priority}`);
|
|
381
|
+
process.exit(1);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
for (const table of tables) {
|
|
385
|
+
if (options.verbose) {
|
|
386
|
+
logger.log(
|
|
387
|
+
`\nProcessing ${options.priority} priority table: ${table}`
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
await checkTableSequence(pool, table);
|
|
391
|
+
}
|
|
392
|
+
} else {
|
|
393
|
+
// Process all tables in priority order
|
|
394
|
+
const allTables = [
|
|
395
|
+
...migrationConfig.migrationPriorities.high,
|
|
396
|
+
...migrationConfig.migrationPriorities.medium,
|
|
397
|
+
...migrationConfig.migrationPriorities.low,
|
|
398
|
+
];
|
|
399
|
+
|
|
400
|
+
for (const table of allTables) {
|
|
401
|
+
if (options.verbose) {
|
|
402
|
+
logger.log(`\nProcessing table: ${table}`);
|
|
403
|
+
}
|
|
404
|
+
await checkTableSequence(pool, table);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
await pool.end();
|
|
409
|
+
logger.log("\nSequence synchronization completed successfully");
|
|
410
|
+
} catch (error) {
|
|
411
|
+
logger.error("Error during synchronization:", error.stack);
|
|
412
|
+
process.exit(1);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
main();
|