neopg 2.2.7 → 2.2.8
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/lib/SchemaSync.js +87 -10
- package/package.json +1 -1
package/lib/SchemaSync.js
CHANGED
|
@@ -102,9 +102,9 @@ class SchemaSync {
|
|
|
102
102
|
for (const c of cols) inf[c.column_name] = c;
|
|
103
103
|
|
|
104
104
|
await this.syncColumn(sql, def, inf, curTableName, debug, force, dropNotExistCol);
|
|
105
|
+
await this.autoRemoveIndex(sql, def, schema, tableName, debug);
|
|
105
106
|
await this.syncIndex(sql, def, schema, curTableName, debug);
|
|
106
107
|
await this.syncUnique(sql, def, schema, curTableName, debug);
|
|
107
|
-
await this.autoRemoveIndex(sql, def, schema, tableName, debug);
|
|
108
108
|
await this.syncReferences(sql, def, schema, curTableName, schemaOid, debug, ctx, options);
|
|
109
109
|
|
|
110
110
|
if (debug) console.log(` - 表结构同步完成 (${tableName}) - `);
|
|
@@ -290,6 +290,33 @@ class SchemaSync {
|
|
|
290
290
|
const indices = def.index || [];
|
|
291
291
|
if (!Array.isArray(indices)) return;
|
|
292
292
|
|
|
293
|
+
let gen_real_index = indname => {
|
|
294
|
+
let real_indexname
|
|
295
|
+
if (Array.isArray(indname)) {
|
|
296
|
+
real_indexname = indname.sort((a,b) => a.localeCompare(b)).join('_');
|
|
297
|
+
} else if (typeof indname === 'string') {
|
|
298
|
+
if (indname.indexOf(',') >= 0) {
|
|
299
|
+
real_indexname = indname.split(',').map(s=>s.trim()).filter(s=>s)
|
|
300
|
+
.sort((a,b)=>a.localeCompare(b))
|
|
301
|
+
.join('_');
|
|
302
|
+
} else {
|
|
303
|
+
real_indexname = indname.trim();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
return real_indexname;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
let unique_indexs = (def.unique || []).map(a => gen_real_index(a));
|
|
311
|
+
|
|
312
|
+
let check_in_unique = (indname) => {
|
|
313
|
+
for (let a of unique_indexs) {
|
|
314
|
+
if (a === gen_real_index(indname)) return true;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
|
|
293
320
|
for (const indname of indices) {
|
|
294
321
|
const removeIndex = def.removeIndex || [];
|
|
295
322
|
if (removeIndex.includes(indname)) continue;
|
|
@@ -299,7 +326,21 @@ class SchemaSync {
|
|
|
299
326
|
continue;
|
|
300
327
|
}
|
|
301
328
|
|
|
302
|
-
|
|
329
|
+
if (!Array.isArray(indname) && typeof indname !== 'string') {
|
|
330
|
+
debug && console.error(`Index ${indname} 不是字符串或数组,跳过。`);
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (check_in_unique(indname)) {
|
|
335
|
+
debug && console.error(`Index ${indname} 是唯一索引,不会创建index索引,跳过。`);
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
let idxCols = Array.isArray(indname)
|
|
340
|
+
? indname.sort((a,b) => a.localeCompare(b))
|
|
341
|
+
: indname.split(',').map(s=>s.trim()).filter(s=>s)
|
|
342
|
+
.sort((a,b) => a.localeCompare(b));
|
|
343
|
+
|
|
303
344
|
const idxNamePart = idxCols.join('_');
|
|
304
345
|
const targetIdxName = `${def.tableName}_${idxNamePart}_idx`;
|
|
305
346
|
|
|
@@ -339,7 +380,14 @@ class SchemaSync {
|
|
|
339
380
|
for (const indname of uniques) {
|
|
340
381
|
if (!this._checkColumnsExist(indname, def)) continue;
|
|
341
382
|
|
|
342
|
-
|
|
383
|
+
if (!Array.isArray(indname) && typeof indname !== 'string') {
|
|
384
|
+
debug && console.error(`Index ${indname} 不是字符串或数组,跳过。`);
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
let idxCols = Array.isArray(indname)
|
|
389
|
+
? indname.sort((a,b)=>a.localeCompare(b))
|
|
390
|
+
: indname.split(',').map(s=>s.trim()).filter(s=>s).sort((a,b)=>a.localeCompare(b));
|
|
343
391
|
|
|
344
392
|
// 监测是否等于主键
|
|
345
393
|
if (pkSet.size > 0 && idxCols.length === pkSet.size) {
|
|
@@ -368,7 +416,7 @@ class SchemaSync {
|
|
|
368
416
|
|
|
369
417
|
static async autoRemoveIndex(sql, def, schema, tableName, debug) {
|
|
370
418
|
const allIdx = await sql`
|
|
371
|
-
SELECT
|
|
419
|
+
SELECT * FROM pg_indexes
|
|
372
420
|
WHERE tablename = ${tableName}
|
|
373
421
|
AND schemaname = ${schema}
|
|
374
422
|
AND indexname != ${tableName + '_pkey'}
|
|
@@ -377,22 +425,51 @@ class SchemaSync {
|
|
|
377
425
|
if (allIdx.length === 0) return;
|
|
378
426
|
|
|
379
427
|
const currentIdxNames = allIdx.map(i => i.indexname);
|
|
428
|
+
let indexTable = Object.create(null);
|
|
429
|
+
for (let a of allIdx) {
|
|
430
|
+
indexTable[a.indexname] = a;
|
|
431
|
+
}
|
|
380
432
|
|
|
381
433
|
const indices = def.index || [];
|
|
382
434
|
const uniques = def.unique || [];
|
|
383
|
-
|
|
384
|
-
const keepSet = new Set();
|
|
385
|
-
const makeName = (n) => `${tableName}_${Array.isArray(n) ? n.map(x=>x.trim()).join('_') : n.split(',').map(x=>x.trim()).filter(x=>x).join('_')}_idx`;
|
|
386
435
|
|
|
387
|
-
|
|
388
|
-
|
|
436
|
+
const makeName = (n) => `${tableName}_${Array.isArray(n)
|
|
437
|
+
? n.map(x=>x.trim()).sort((a,b) => a.localeCompare(b)).join('_')
|
|
438
|
+
: n.split(',').map(x=>x.trim()).filter(x=>x).sort((a,b) => a.localeCompare(b)).join('_')}_idx`;
|
|
439
|
+
|
|
440
|
+
let allIndex = Object.create(null);
|
|
441
|
+
for (let k of indices) {
|
|
442
|
+
allIndex[makeName(k)] = k;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
let allUnique = Object.create(null);
|
|
446
|
+
for (let k of uniques) {
|
|
447
|
+
allUnique[makeName(k)] = k;
|
|
448
|
+
}
|
|
389
449
|
|
|
390
450
|
for (const idxName of currentIdxNames) {
|
|
391
|
-
if (!
|
|
451
|
+
if (!allIndex[idxName] && !allUnique[idxName]) {
|
|
392
452
|
if (debug) console.log('Auto removing index:', idxName);
|
|
393
453
|
await sql.unsafe(`DROP INDEX ${schema}.${idxName}`);
|
|
394
454
|
}
|
|
395
455
|
}
|
|
456
|
+
|
|
457
|
+
for (let k in allIndex) {
|
|
458
|
+
if (!indexTable[k]) continue;
|
|
459
|
+
|
|
460
|
+
if (indexTable[k].indexdef.toLowerCase().indexOf('create index') !== 0) {
|
|
461
|
+
debug && console.log('Auto remove index with inconsistent type:', k);
|
|
462
|
+
await sql.unsafe(`DROP INDEX ${schema}.${k};`);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
for (let k in allUnique) {
|
|
467
|
+
if (!indexTable[k]) continue;
|
|
468
|
+
if (indexTable[k].indexdef.toLowerCase().indexOf('create unique') !== 0) {
|
|
469
|
+
debug && console.log('Auto remove index with inconsistent type:', k);
|
|
470
|
+
await sql.unsafe(`DROP INDEX ${schema}.${k};`);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
396
473
|
}
|
|
397
474
|
|
|
398
475
|
// --- 外键同步 ---
|