mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-30 04:33:57 +00:00
constraint check fixed
This commit is contained in:
@@ -1,12 +1,18 @@
|
|||||||
import type {
|
import type {
|
||||||
|
CheckInfo,
|
||||||
ColumnInfo,
|
ColumnInfo,
|
||||||
|
ColumnReference,
|
||||||
ConstraintInfo,
|
ConstraintInfo,
|
||||||
DatabaseInfo,
|
DatabaseInfo,
|
||||||
EngineDriver,
|
EngineDriver,
|
||||||
|
ForeignKeyInfo,
|
||||||
|
IndexInfo,
|
||||||
NamedObjectInfo,
|
NamedObjectInfo,
|
||||||
|
PrimaryKeyInfo,
|
||||||
SqlDialect,
|
SqlDialect,
|
||||||
SqlObjectInfo,
|
SqlObjectInfo,
|
||||||
TableInfo,
|
TableInfo,
|
||||||
|
UniqueInfo,
|
||||||
ViewInfo,
|
ViewInfo,
|
||||||
} from 'dbgate-types';
|
} from 'dbgate-types';
|
||||||
import uuidv1 from 'uuid/v1';
|
import uuidv1 from 'uuid/v1';
|
||||||
@@ -346,20 +352,83 @@ export function testEqualColumns(
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testEqualColumnRefs(a: ColumnReference[], b: ColumnReference[], opts: DbDiffOptions) {
|
||||||
|
if (a.length != b.length) return false;
|
||||||
|
for (let i = 0; i < a.length; i++) {
|
||||||
|
if (!testEqualNames(a[i].columnName, b[i].columnName, opts)) return false;
|
||||||
|
if (!testEqualNames(a[i].refColumnName, b[i].refColumnName, opts)) return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function testEqualPrimaryKeys(a: PrimaryKeyInfo, b: PrimaryKeyInfo, opts: DbDiffOptions) {
|
||||||
|
if (!testEqualColumnRefs(a.columns, b.columns, opts)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function testEqualForeignKeys(a: ForeignKeyInfo, b: ForeignKeyInfo, opts: DbDiffOptions) {
|
||||||
|
if (!testEqualColumnRefs(a.columns, b.columns, opts)) return false;
|
||||||
|
if (!opts.ignoreConstraintNames && !testEqualNames(a.constraintName, b.constraintName, opts)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function testEqualIndex(a: IndexInfo, b: IndexInfo, opts: DbDiffOptions) {
|
||||||
|
if (!testEqualColumnRefs(a.columns, b.columns, opts)) return false;
|
||||||
|
if (!!a.isUnique != !!b.isUnique) return false;
|
||||||
|
|
||||||
|
if (!opts.ignoreConstraintNames && !testEqualNames(a.constraintName, b.constraintName, opts)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function testEqualUnique(a: UniqueInfo, b: UniqueInfo, opts: DbDiffOptions) {
|
||||||
|
if (!testEqualColumnRefs(a.columns, b.columns, opts)) return false;
|
||||||
|
|
||||||
|
if (!opts.ignoreConstraintNames && !testEqualNames(a.constraintName, b.constraintName, opts)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function testEqualCheck(a: CheckInfo, b: CheckInfo, opts: DbDiffOptions) {
|
||||||
|
if (a.definition != b.definition) return false;
|
||||||
|
if (!opts.ignoreConstraintNames && !testEqualNames(a.constraintName, b.constraintName, opts)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
function testEqualConstraints(a: ConstraintInfo, b: ConstraintInfo, opts: DbDiffOptions = {}) {
|
function testEqualConstraints(a: ConstraintInfo, b: ConstraintInfo, opts: DbDiffOptions = {}) {
|
||||||
const omitList = ['pairingId'];
|
if (a.constraintType != b.constraintType) {
|
||||||
if (opts.ignoreForeignKeyActions) {
|
console.debug(`Constraint ${a.pureName}: different constraint type: ${a.constraintType}, ${b.constraintType}`);
|
||||||
omitList.push('updateAction');
|
return false;
|
||||||
omitList.push('deleteAction');
|
|
||||||
}
|
}
|
||||||
if (opts.ignoreConstraintNames) {
|
|
||||||
omitList.push('constraintName');
|
switch (a.constraintType) {
|
||||||
}
|
case 'primaryKey':
|
||||||
if (opts.schemaMode == 'ignore') {
|
case 'sortingKey':
|
||||||
omitList.push('schemaName');
|
return testEqualPrimaryKeys(a as PrimaryKeyInfo, b as PrimaryKeyInfo, opts);
|
||||||
omitList.push('refSchemaName');
|
case 'foreignKey':
|
||||||
|
return testEqualForeignKeys(a as ForeignKeyInfo, b as ForeignKeyInfo, opts);
|
||||||
|
case 'index':
|
||||||
|
return testEqualIndex(a as IndexInfo, b as IndexInfo, opts);
|
||||||
|
case 'unique':
|
||||||
|
return testEqualUnique(a as UniqueInfo, b as UniqueInfo, opts);
|
||||||
|
case 'check':
|
||||||
|
return testEqualCheck(a as CheckInfo, b as CheckInfo, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.debug(`Unknown constraint type: ${a.pureName}`);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// const omitList = ['pairingId'];
|
||||||
|
// if (opts.ignoreForeignKeyActions) {
|
||||||
|
// omitList.push('updateAction');
|
||||||
|
// omitList.push('deleteAction');
|
||||||
|
// }
|
||||||
|
// if (opts.ignoreConstraintNames) {
|
||||||
|
// omitList.push('constraintName');
|
||||||
|
// }
|
||||||
|
// if (opts.schemaMode == 'ignore') {
|
||||||
|
// omitList.push('schemaName');
|
||||||
|
// omitList.push('refSchemaName');
|
||||||
|
// }
|
||||||
|
|
||||||
// if (a.constraintType == 'primaryKey' && b.constraintType == 'primaryKey') {
|
// if (a.constraintType == 'primaryKey' && b.constraintType == 'primaryKey') {
|
||||||
// console.log('PK1', stableStringify(_.omit(a, omitList)));
|
// console.log('PK1', stableStringify(_.omit(a, omitList)));
|
||||||
// console.log('PK2', stableStringify(_.omit(b, omitList)));
|
// console.log('PK2', stableStringify(_.omit(b, omitList)));
|
||||||
@@ -375,10 +444,10 @@ function testEqualConstraints(a: ConstraintInfo, b: ConstraintInfo, opts: DbDiff
|
|||||||
// console.log('IX2', stableStringify(_omit(b, omitList)));
|
// console.log('IX2', stableStringify(_omit(b, omitList)));
|
||||||
// }
|
// }
|
||||||
|
|
||||||
const aStringified = stableStringify(_omit(a, omitList));
|
// const aStringified = stableStringify(_omit(a, omitList));
|
||||||
const bStringified = stableStringify(_omit(b, omitList));
|
// const bStringified = stableStringify(_omit(b, omitList));
|
||||||
|
|
||||||
return aStringified == bStringified;
|
// return aStringified == bStringified;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function testEqualTypes(a: ColumnInfo, b: ColumnInfo, opts: DbDiffOptions = {}) {
|
export function testEqualTypes(a: ColumnInfo, b: ColumnInfo, opts: DbDiffOptions = {}) {
|
||||||
|
|||||||
Reference in New Issue
Block a user