alter table WIP

This commit is contained in:
Jan Prochazka 2021-09-16 09:31:10 +02:00
parent ce85f8f94d
commit 185699cb51
8 changed files with 48 additions and 15 deletions

View File

@ -538,11 +538,14 @@ export class SqlDumper implements AlterProcessor {
}
recreateTable(oldTable: TableInfo, newTable: TableInfo) {
if (oldTable.pairingId != newTable.pairingId) {
if (!oldTable.pairingId || !newTable.pairingId || oldTable.pairingId != newTable.pairingId) {
throw new Error('Recreate is not possible: oldTable.paringId != newTable.paringId');
}
const tmpTable = `temp_${uuidv1()}`;
// console.log('oldTable', oldTable);
// console.log('newTable', newTable);
const columnPairs = oldTable.columns
.map(oldcol => ({
oldcol,

View File

@ -1,4 +1,5 @@
import _ from 'lodash';
import { generateTablePairingId } from '.';
import {
AlterProcessor,
ColumnInfo,
@ -371,12 +372,17 @@ export class AlterPlan {
const recreates = {};
for (const op of this.operations) {
if (op.operationType == 'recreateTable') {
const recreate = {
...op,
operations: [...op.operations],
};
res.push(recreate);
recreates[`${op.table.schemaName}||${op.table.pureName}`] = recreate;
const existingRecreate = recreates[`${op.table.schemaName}||${op.table.pureName}`];
if (existingRecreate) {
existingRecreate.operations.push(...op.operations);
} else {
const recreate = {
...op,
operations: [...op.operations],
};
res.push(recreate);
recreates[`${op.table.schemaName}||${op.table.pureName}`] = recreate;
}
} else {
// @ts-ignore
const oldObject: TableInfo = op.oldObject;
@ -453,7 +459,8 @@ export function runAlterOperation(op: AlterOperation, processor: AlterProcessor)
break;
case 'recreateTable':
{
const newTable = _.cloneDeep(op.table);
const oldTable = generateTablePairingId(op.table);
const newTable = _.cloneDeep(oldTable);
const newDb = DatabaseAnalyser.createEmptyStructure();
newDb.tables.push(newTable);
// console.log('////////////////////////////newTable1', newTable);
@ -461,7 +468,7 @@ export function runAlterOperation(op: AlterOperation, processor: AlterProcessor)
// console.log('////////////////////////////op.operations', op.operations);
// console.log('////////////////////////////op.table', op.table);
// console.log('////////////////////////////newTable2', newTable);
processor.recreateTable(op.table, newTable);
processor.recreateTable(oldTable, newTable);
}
break;
}

View File

@ -1,4 +1,12 @@
import { ColumnInfo, ConstraintInfo, DatabaseInfo, EngineDriver, NamedObjectInfo, TableInfo } from 'dbgate-types';
import {
ColumnInfo,
ConstraintInfo,
DatabaseInfo,
EngineDriver,
NamedObjectInfo,
SqlDialect,
TableInfo,
} from 'dbgate-types';
import _ from 'lodash';
import uuidv1 from 'uuid/v1';
import { AlterPlan } from './alterPlan';
@ -287,7 +295,7 @@ function planAlterTable(plan: AlterPlan, oldTable: TableInfo, newTable: TableInf
.filter(x => x[0] && x[1])
.forEach(x => {
if (!testEqualConstraints(x[0], x[1], opts)) {
// console.log('PLAN CHANGE COLUMN')
// console.log('PLAN CHANGE CONSTRAINT', x[0], x[1]);
plan.changeConstraint(x[0], x[1]);
}
});
@ -357,7 +365,9 @@ export function getAlterTableScript(
): string {
const plan = createAlterTablePlan(oldTable, newTable, opts, db, driver);
const dmp = driver.createDumper();
if (!driver.dialect.disableExplicitTransaction) dmp.beginTransaction();
plan.run(dmp);
if (!driver.dialect.disableExplicitTransaction) dmp.commitTransaction();
return dmp.s;
}
@ -370,6 +380,8 @@ export function getAlterDatabaseScript(
): string {
const plan = createAlterDatabasePlan(oldDb, newDb, opts, db, driver);
const dmp = driver.createDumper();
if (!driver.dialect.disableExplicitTransaction) dmp.beginTransaction();
plan.run(dmp);
if (!driver.dialect.disableExplicitTransaction) dmp.commitTransaction();
return dmp.s;
}

View File

@ -1,5 +1,5 @@
import _ from 'lodash';
import { ColumnInfo, ColumnReference, DatabaseInfo, DatabaseInfoObjects, TableInfo } from 'dbgate-types';
import { ColumnInfo, ColumnReference, DatabaseInfo, DatabaseInfoObjects, SqlDialect, TableInfo } from 'dbgate-types';
export function fullNameFromString(name) {
const m = name.match(/\[([^\]]+)\]\.\[([^\]]+)\]/);
@ -74,10 +74,10 @@ function columnsConstraintName(prefix: string, table: TableInfo, columns: Column
return `${prefix}_${table.pureName}_${columns.map(x => x.columnName.replace(' ', '_')).join('_')}`;
}
export function fillConstraintNames(table: TableInfo) {
export function fillConstraintNames(table: TableInfo, dialect: SqlDialect) {
if (!table) return table;
const res = _.cloneDeep(table);
if (res.primaryKey && !res.primaryKey.constraintName) {
if (res.primaryKey && !res.primaryKey.constraintName && !dialect.anonymousPrimaryKey) {
res.primaryKey.constraintName = `PK_${res.pureName}`;
}
for (const fk of res.foreignKeys) {

View File

@ -32,4 +32,5 @@ export interface SqlDialect {
dropCheck?: boolean;
dropReferencesWhenDropTable?: boolean;
disableExplicitTransaction?: boolean;
}

View File

@ -17,4 +17,6 @@ export interface SqlDumper extends AlterProcessor {
endCommand();
allowIdentityInsert(table: NamedObjectInfo, allow: boolean);
beginTransaction();
commitTransaction();
}

View File

@ -102,7 +102,13 @@
function doSave(createTableName) {
const driver = findEngineDriver($connection, $extensions);
const sql = getAlterTableScript($editorValue.base, fillConstraintNames($editorValue.current), {}, $dbInfo, driver);
const sql = getAlterTableScript(
$editorValue.base,
fillConstraintNames($editorValue.current, driver.dialect),
{},
$dbInfo,
driver
);
showModal(ConfirmSqlModal, {
sql,

View File

@ -21,6 +21,8 @@ const dialect = {
quoteIdentifier(s) {
return `[${s}]`;
},
anonymousPrimaryKey: true,
disableExplicitTransaction: true,
createColumn: true,
dropColumn: true,