alter table WIP

This commit is contained in:
Jan Prochazka
2021-08-26 11:45:44 +02:00
parent a5b5f36298
commit 3fe13f0443
9 changed files with 98 additions and 17 deletions

View File

@@ -350,14 +350,33 @@ export class SqlDumper implements AlterProcessor {
changeTriggerSchema(obj: TriggerInfo, newSchema: string) {}
renameTrigger(obj: TriggerInfo, newSchema: string) {}
dropConstraint(cnt: ConstraintInfo) {
dropConstraintCore(cnt: ConstraintInfo) {
this.putCmd('^alter ^table %f ^drop ^constraint %i', cnt, cnt.constraintName);
}
dropConstraint(cnt: ConstraintInfo) {
switch (cnt.constraintType) {
case 'primaryKey':
this.dropPrimaryKey(cnt as PrimaryKeyInfo);
break;
case 'foreignKey':
this.dropForeignKey(cnt as ForeignKeyInfo);
break;
case 'unique':
this.dropUnique(cnt as UniqueInfo);
break;
case 'check':
this.dropCheck(cnt as CheckInfo);
break;
case 'index':
this.dropIndex(cnt as IndexInfo);
break;
}
}
dropForeignKey(fk: ForeignKeyInfo) {
if (this.dialect.explicitDropConstraint) {
this.putCmd('^alter ^table %f ^drop ^foreign ^key %i', fk, fk.constraintName);
} else {
this.dropConstraint(fk);
this.dropConstraintCore(fk);
}
}
createForeignKey(fk: ForeignKeyInfo) {
@@ -369,7 +388,7 @@ export class SqlDumper implements AlterProcessor {
if (this.dialect.explicitDropConstraint) {
this.putCmd('^alter ^table %f ^drop ^primary ^key', pk);
} else {
this.dropConstraint(pk);
this.dropConstraintCore(pk);
}
}
createPrimaryKey(pk: PrimaryKeyInfo) {
@@ -385,7 +404,7 @@ export class SqlDumper implements AlterProcessor {
createIndex(ix: IndexInfo) {}
dropUnique(uq: UniqueInfo) {
this.dropConstraint(uq);
this.dropConstraintCore(uq);
}
createUniqueCore(uq: UniqueInfo) {
this.put(
@@ -402,7 +421,7 @@ export class SqlDumper implements AlterProcessor {
}
dropCheck(ch: CheckInfo) {
this.dropConstraint(ch);
this.dropConstraintCore(ch);
}
createCheckCore(ch: CheckInfo) {

View File

@@ -1,4 +1,13 @@
import { AlterProcessor, ColumnInfo, ConstraintInfo, DatabaseInfo, NamedObjectInfo, TableInfo } from '../../types';
import _ from 'lodash';
import {
AlterProcessor,
ColumnInfo,
ConstraintInfo,
DatabaseInfo,
NamedObjectInfo,
SqlDialect,
TableInfo,
} from '../../types';
interface AlterOperation_CreateTable {
operationType: 'createTable';
@@ -74,8 +83,8 @@ type AlterOperation =
| AlterOperation_RenameConstraint;
export class AlterPlan {
operations: AlterOperation[] = [];
constructor(public db: DatabaseInfo) {}
public operations: AlterOperation[] = [];
constructor(public db: DatabaseInfo, public dialect: SqlDialect) {}
createTable(table: TableInfo) {
this.operations.push({
@@ -164,6 +173,45 @@ export class AlterPlan {
runAlterOperation(op, processor);
}
}
_addLogicalDependencies(): AlterOperation[] {
const lists = this.operations.map(op => {
if (op.operationType == 'dropColumn') {
const table = this.db.tables.find(
x => x.pureName == op.oldObject.pureName && x.schemaName == op.oldObject.schemaName
);
const deletedFks = this.dialect.dropColumnDependencies?.includes('foreignKey')
? table.dependencies.filter(fk => fk.columns.find(col => col.refColumnName == op.oldObject.columnName))
: [];
const deletedConstraints = _.compact([
table.primaryKey,
...table.foreignKeys,
...table.indexes,
...table.uniques,
]).filter(cnt => cnt.columns.find(col => col.columnName == op.oldObject.columnName));
const res: AlterOperation[] = [
...[...deletedFks, ...deletedConstraints].map(oldObject => {
const opRes: AlterOperation = {
operationType: 'dropConstraint',
oldObject,
};
return opRes;
}),
op,
];
return res;
}
return [op];
});
return _.flatten(lists);
}
transformPlan() {
this.operations = this._addLogicalDependencies();
}
}
export function runAlterOperation(op: AlterOperation, processor: AlterProcessor) {

View File

@@ -272,14 +272,16 @@ export function createAlterTablePlan(
oldTable: TableInfo,
newTable: TableInfo,
opts: DbDiffOptions,
db: DatabaseInfo
db: DatabaseInfo,
driver: EngineDriver
): AlterPlan {
const plan = new AlterPlan(db);
const plan = new AlterPlan(db, driver.dialect);
if (oldTable == null) {
plan.createTable(newTable);
} else {
planAlterTable(plan, oldTable, newTable, opts);
}
plan.transformPlan();
return plan;
}
@@ -290,7 +292,7 @@ export function getAlterTableScript(
db: DatabaseInfo,
driver: EngineDriver
): string {
const plan = createAlterTablePlan(oldTable, newTable, opts, db);
const plan = createAlterTablePlan(oldTable, newTable, opts, db, driver);
const dmp = driver.createDumper();
plan.run(dmp);
return dmp.s;