mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 14:16:01 +00:00
alter table WIP
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user