mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-21 12:56:00 +00:00
fk deploy fix
This commit is contained in:
@@ -244,6 +244,10 @@ export class AlterPlan {
|
||||
if (op.operationType == 'dropColumn') {
|
||||
const constraints = this._getDependendColumnConstraints(op.oldObject, this.dialect.dropColumnDependencies);
|
||||
|
||||
if (constraints.length > 0 && this.opts.noDropConstraint) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const res: AlterOperation[] = [
|
||||
...constraints.map(oldObject => {
|
||||
const opRes: AlterOperation = {
|
||||
@@ -260,6 +264,10 @@ export class AlterPlan {
|
||||
if (op.operationType == 'changeColumn') {
|
||||
const constraints = this._getDependendColumnConstraints(op.oldObject, this.dialect.changeColumnDependencies);
|
||||
|
||||
if (constraints.length > 0 && this.opts.noDropConstraint) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const res: AlterOperation[] = [
|
||||
...constraints.map(oldObject => {
|
||||
const opRes: AlterOperation = {
|
||||
@@ -300,6 +308,11 @@ export class AlterPlan {
|
||||
}
|
||||
|
||||
if (op.operationType == 'changeConstraint') {
|
||||
if (this.opts.noDropConstraint) {
|
||||
// skip constraint recreate
|
||||
return [];
|
||||
}
|
||||
|
||||
this.recreates.constraints += 1;
|
||||
const opDrop: AlterOperation = {
|
||||
operationType: 'dropConstraint',
|
||||
@@ -418,6 +431,37 @@ export class AlterPlan {
|
||||
return res;
|
||||
}
|
||||
|
||||
_moveForeignKeysToLast(): AlterOperation[] {
|
||||
if (!this.dialect.createForeignKey) {
|
||||
return this.operations;
|
||||
}
|
||||
const fks = [];
|
||||
const res = this.operations.map(op => {
|
||||
if (op.operationType == 'createTable') {
|
||||
fks.push(...(op.newObject.foreignKeys || []));
|
||||
return {
|
||||
...op,
|
||||
newObject: {
|
||||
...op.newObject,
|
||||
foreignKeys: [],
|
||||
},
|
||||
};
|
||||
}
|
||||
return op;
|
||||
});
|
||||
|
||||
return [
|
||||
...res,
|
||||
...fks.map(
|
||||
fk =>
|
||||
({
|
||||
operationType: 'createConstraint',
|
||||
newObject: fk,
|
||||
} as AlterOperation_CreateConstraint)
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
transformPlan() {
|
||||
// console.log('*****************OPERATIONS0', this.operations);
|
||||
|
||||
@@ -432,6 +476,10 @@ export class AlterPlan {
|
||||
this.operations = this._groupTableRecreations();
|
||||
|
||||
// console.log('*****************OPERATIONS3', this.operations);
|
||||
|
||||
this.operations = this._moveForeignKeysToLast();
|
||||
|
||||
// console.log('*****************OPERATIONS4', this.operations);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,10 +7,13 @@ import {
|
||||
SqlDialect,
|
||||
TableInfo,
|
||||
} from 'dbgate-types';
|
||||
import _ from 'lodash';
|
||||
import uuidv1 from 'uuid/v1';
|
||||
import { AlterPlan } from './alterPlan';
|
||||
import stableStringify from 'json-stable-stringify';
|
||||
import _omit from 'lodash/omit';
|
||||
import _cloneDeep from 'lodash/cloneDeep';
|
||||
import _isEqual from 'lodash/isEqual';
|
||||
import _pick from 'lodash/pick';
|
||||
|
||||
type DbDiffSchemaMode = 'strict' | 'ignore' | 'ignoreImplicit';
|
||||
|
||||
@@ -32,6 +35,7 @@ export interface DbDiffOptions {
|
||||
noDropSqlObject?: boolean;
|
||||
noRenameTable?: boolean;
|
||||
noRenameColumn?: boolean;
|
||||
ignoreForeignKeyActions?: boolean;
|
||||
}
|
||||
|
||||
export function generateTablePairingId(table: TableInfo): TableInfo {
|
||||
@@ -245,15 +249,29 @@ export function testEqualColumns(
|
||||
|
||||
function testEqualConstraints(a: ConstraintInfo, b: ConstraintInfo, opts: DbDiffOptions = {}) {
|
||||
const omitList = [];
|
||||
if (opts.ignoreConstraintNames) omitList.push('constraintName');
|
||||
if (opts.schemaMode == 'ignore') omitList.push('schemaName');
|
||||
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') {
|
||||
// console.log('PK1', stableStringify(_.omit(a, omitList)));
|
||||
// console.log('PK2', stableStringify(_.omit(b, omitList)));
|
||||
// }
|
||||
|
||||
return stableStringify(_.omit(a, omitList)) == stableStringify(_.omit(b, omitList));
|
||||
|
||||
// if (a.constraintType == 'foreignKey' && b.constraintType == 'foreignKey') {
|
||||
// console.log('FK1', stableStringify(_omit(a, omitList)));
|
||||
// console.log('FK2', stableStringify(_omit(b, omitList)));
|
||||
// }
|
||||
|
||||
return stableStringify(_omit(a, omitList)) == stableStringify(_omit(b, omitList));
|
||||
}
|
||||
|
||||
export function testEqualTypes(a: ColumnInfo, b: ColumnInfo, opts: DbDiffOptions = {}) {
|
||||
@@ -441,11 +459,12 @@ export function getAlterDatabaseScript(
|
||||
return {
|
||||
sql: dmp.s,
|
||||
recreates: plan.recreates,
|
||||
isEmpty: plan.operations.length == 0,
|
||||
};
|
||||
}
|
||||
|
||||
export function matchPairedObjects(db1: DatabaseInfo, db2: DatabaseInfo, opts: DbDiffOptions) {
|
||||
const res = _.cloneDeep(db2);
|
||||
const res = _cloneDeep(db2);
|
||||
|
||||
for (const objectTypeField of ['tables', 'views', 'procedures', 'matviews', 'functions']) {
|
||||
for (const obj2 of res[objectTypeField] || []) {
|
||||
@@ -458,6 +477,18 @@ export function matchPairedObjects(db1: DatabaseInfo, db2: DatabaseInfo, opts: D
|
||||
const col1 = obj1.columns.find(x => testEqualNames(x.columnName, col2.columnName, opts));
|
||||
if (col1) col2.pairingId = col1.pairingId;
|
||||
}
|
||||
|
||||
for (const fk2 of obj2.foreignKeys) {
|
||||
const fk1 = obj1.foreignKeys.find(
|
||||
x =>
|
||||
testEqualNames(x.refTableName, fk2.refTableName, opts) &&
|
||||
_isEqual(
|
||||
x.columns.map(y => _pick(y, ['columnName', 'refColumnName'])),
|
||||
fk2.columns.map(y => _pick(y, ['columnName', 'refColumnName']))
|
||||
)
|
||||
);
|
||||
if (fk1) fk2.pairingId = fk1.pairingId;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user