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) { 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'); throw new Error('Recreate is not possible: oldTable.paringId != newTable.paringId');
} }
const tmpTable = `temp_${uuidv1()}`; const tmpTable = `temp_${uuidv1()}`;
// console.log('oldTable', oldTable);
// console.log('newTable', newTable);
const columnPairs = oldTable.columns const columnPairs = oldTable.columns
.map(oldcol => ({ .map(oldcol => ({
oldcol, oldcol,

View File

@ -1,4 +1,5 @@
import _ from 'lodash'; import _ from 'lodash';
import { generateTablePairingId } from '.';
import { import {
AlterProcessor, AlterProcessor,
ColumnInfo, ColumnInfo,
@ -371,12 +372,17 @@ export class AlterPlan {
const recreates = {}; const recreates = {};
for (const op of this.operations) { for (const op of this.operations) {
if (op.operationType == 'recreateTable') { if (op.operationType == 'recreateTable') {
const recreate = { const existingRecreate = recreates[`${op.table.schemaName}||${op.table.pureName}`];
...op, if (existingRecreate) {
operations: [...op.operations], existingRecreate.operations.push(...op.operations);
}; } else {
res.push(recreate); const recreate = {
recreates[`${op.table.schemaName}||${op.table.pureName}`] = recreate; ...op,
operations: [...op.operations],
};
res.push(recreate);
recreates[`${op.table.schemaName}||${op.table.pureName}`] = recreate;
}
} else { } else {
// @ts-ignore // @ts-ignore
const oldObject: TableInfo = op.oldObject; const oldObject: TableInfo = op.oldObject;
@ -453,7 +459,8 @@ export function runAlterOperation(op: AlterOperation, processor: AlterProcessor)
break; break;
case 'recreateTable': case 'recreateTable':
{ {
const newTable = _.cloneDeep(op.table); const oldTable = generateTablePairingId(op.table);
const newTable = _.cloneDeep(oldTable);
const newDb = DatabaseAnalyser.createEmptyStructure(); const newDb = DatabaseAnalyser.createEmptyStructure();
newDb.tables.push(newTable); newDb.tables.push(newTable);
// console.log('////////////////////////////newTable1', 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.operations', op.operations);
// console.log('////////////////////////////op.table', op.table); // console.log('////////////////////////////op.table', op.table);
// console.log('////////////////////////////newTable2', newTable); // console.log('////////////////////////////newTable2', newTable);
processor.recreateTable(op.table, newTable); processor.recreateTable(oldTable, newTable);
} }
break; 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 _ from 'lodash';
import uuidv1 from 'uuid/v1'; import uuidv1 from 'uuid/v1';
import { AlterPlan } from './alterPlan'; import { AlterPlan } from './alterPlan';
@ -287,7 +295,7 @@ function planAlterTable(plan: AlterPlan, oldTable: TableInfo, newTable: TableInf
.filter(x => x[0] && x[1]) .filter(x => x[0] && x[1])
.forEach(x => { .forEach(x => {
if (!testEqualConstraints(x[0], x[1], opts)) { 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]); plan.changeConstraint(x[0], x[1]);
} }
}); });
@ -357,7 +365,9 @@ export function getAlterTableScript(
): string { ): string {
const plan = createAlterTablePlan(oldTable, newTable, opts, db, driver); const plan = createAlterTablePlan(oldTable, newTable, opts, db, driver);
const dmp = driver.createDumper(); const dmp = driver.createDumper();
if (!driver.dialect.disableExplicitTransaction) dmp.beginTransaction();
plan.run(dmp); plan.run(dmp);
if (!driver.dialect.disableExplicitTransaction) dmp.commitTransaction();
return dmp.s; return dmp.s;
} }
@ -370,6 +380,8 @@ export function getAlterDatabaseScript(
): string { ): string {
const plan = createAlterDatabasePlan(oldDb, newDb, opts, db, driver); const plan = createAlterDatabasePlan(oldDb, newDb, opts, db, driver);
const dmp = driver.createDumper(); const dmp = driver.createDumper();
if (!driver.dialect.disableExplicitTransaction) dmp.beginTransaction();
plan.run(dmp); plan.run(dmp);
if (!driver.dialect.disableExplicitTransaction) dmp.commitTransaction();
return dmp.s; return dmp.s;
} }

View File

@ -1,5 +1,5 @@
import _ from 'lodash'; 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) { export function fullNameFromString(name) {
const m = name.match(/\[([^\]]+)\]\.\[([^\]]+)\]/); 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('_')}`; 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; if (!table) return table;
const res = _.cloneDeep(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}`; res.primaryKey.constraintName = `PK_${res.pureName}`;
} }
for (const fk of res.foreignKeys) { for (const fk of res.foreignKeys) {

View File

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

View File

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

View File

@ -102,7 +102,13 @@
function doSave(createTableName) { function doSave(createTableName) {
const driver = findEngineDriver($connection, $extensions); 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, { showModal(ConfirmSqlModal, {
sql, sql,

View File

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