alter processor fixes

This commit is contained in:
Jan Prochazka 2021-09-04 21:06:42 +02:00
parent 04a6540890
commit e2ce349a30
7 changed files with 63 additions and 8 deletions

View File

@ -47,7 +47,7 @@ async function testTableDiff(conn, driver, mangle) {
const sql = getAlterTableScript(tget(structure1), tget(structure2), {}, structure2, driver);
console.log('RUNNING ALTER SQL', driver.engine, ':', sql);
await driver.query(conn, sql);
await driver.script(conn, sql);
const structure2Real = extendDatabaseInfo(await driver.analyseFull(conn));
@ -56,8 +56,8 @@ async function testTableDiff(conn, driver, mangle) {
}
// const TESTED_COLUMNS = ['col_pk', 'col_std', 'col_def', 'col_fk', 'col_ref', 'col_idx', 'col_uq'];
// const TESTED_COLUMNS = ['col_pk'];
const TESTED_COLUMNS = ['col_idx'];
const TESTED_COLUMNS = ['col_pk'];
// const TESTED_COLUMNS = ['col_idx'];
// const TESTED_COLUMNS = ['col_fk'];
// const TESTED_COLUMNS = ['col_std'];

View File

@ -16,6 +16,7 @@ const matviews = {
const engines = [
{
label: 'MySQL',
skipLocal: true,
connection: {
engine: 'mysql@dbgate-plugin-mysql',
password: 'Pwd2020Db',
@ -33,6 +34,7 @@ const engines = [
},
{
label: 'PostgreSQL',
skipLocal: true,
connection: {
engine: 'postgres@dbgate-plugin-postgres',
password: 'Pwd2020Db',
@ -56,8 +58,10 @@ const engines = [
},
{
type: 'functions',
create1: 'CREATE FUNCTION obj1() returns int LANGUAGE plpgsql AS $$ declare res integer; begin select count(*) into res from t1; return res; end; $$',
create2: 'CREATE FUNCTION obj2() returns int LANGUAGE plpgsql AS $$ declare res integer; begin select count(*) into res from t2; return res; end; $$',
create1:
'CREATE FUNCTION obj1() returns int LANGUAGE plpgsql AS $$ declare res integer; begin select count(*) into res from t1; return res; end; $$',
create2:
'CREATE FUNCTION obj2() returns int LANGUAGE plpgsql AS $$ declare res integer; begin select count(*) into res from t2; return res; end; $$',
drop1: 'DROP FUNCTION obj1',
drop2: 'DROP FUNCTION obj2',
},
@ -65,6 +69,7 @@ const engines = [
},
{
label: 'SQL Server',
skipLocal: true,
connection: {
engine: 'mssql@dbgate-plugin-mssql',
password: 'Pwd2020Db',
@ -89,6 +94,7 @@ const engines = [
},
{
label: 'SQLite',
skipLocal: true,
generateDbFile: true,
connection: {
engine: 'sqlite@dbgate-plugin-sqlite',
@ -97,6 +103,7 @@ const engines = [
},
{
label: 'CockroachDB',
// skipLocal: true,
connection: {
engine: 'cockroach@dbgate-plugin-postgres',
user: 'root',
@ -112,4 +119,4 @@ const engines = [
},
];
module.exports = process.env.CITEST ? engines.filter(x => !x.skipOnCI) : engines;
module.exports = process.env.CITEST ? engines.filter(x => !x.skipOnCI) : engines.filter(x => !x.skipLocal);

View File

@ -17,6 +17,9 @@
"run:local": "docker-compose down && docker-compose up -d && yarn wait:local && yarn test:local"
},
"jest": {
"testTimeout": 5000
},
"devDependencies": {
"cross-env": "^7.0.3",
"jest": "^27.0.1"

View File

@ -207,7 +207,7 @@ export class AlterPlan {
...(this.dialect.dropColumnDependencies?.includes('uniques') ? table.uniques : []),
]).filter(cnt => cnt.columns.find(col => col.columnName == op.oldObject.columnName));
console.log('deletedConstraints', deletedConstraints);
// console.log('deletedConstraints', deletedConstraints);
const res: AlterOperation[] = [
...[...deletedFks, ...deletedConstraints].map(oldObject => {
@ -283,9 +283,47 @@ export class AlterPlan {
return null;
}
_groupTableRecreations(): AlterOperation[] {
const res = [];
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;
} else {
// @ts-ignore
const oldObject: TableInfo = op.oldObject;
if (oldObject) {
const recreated = recreates[`${oldObject.schemaName}||${oldObject.pureName}`];
if (recreated) {
recreated.operations.push(op);
continue;
}
}
res.push(op);
}
}
return res;
}
transformPlan() {
// console.log('*****************OPERATIONS0', this.operations);
this.operations = this._addLogicalDependencies();
// console.log('*****************OPERATIONS1', this.operations);
this.operations = this._transformToImplementedOps();
// console.log('*****************OPERATIONS2', this.operations);
this.operations = this._groupTableRecreations();
// console.log('*****************OPERATIONS3', this.operations);
}
}
@ -327,6 +365,8 @@ export function runAlterOperation(op: AlterOperation, processor: AlterProcessor)
const newDb = DatabaseAnalyser.createEmptyStructure();
newDb.tables.push(newTable);
op.operations.forEach(child => runAlterOperation(child, new DatabaseInfoAlterProcessor(newDb)));
// console.log('////////////////////////////op.table', op.table);
// console.log('////////////////////////////newTable', newTable);
processor.recreateTable(op.table, newTable);
}
break;

View File

@ -41,7 +41,7 @@ class Dumper extends SqlDumper {
dropTable(obj, options = {}) {
this.put('^drop ^table');
if (options.testIfExists) this.put(' ^if ^exists');
this.put(' %f', obj.FullName);
this.put(' %f', obj);
this.endCommand();
}

View File

@ -57,6 +57,7 @@ const cockroachDriver = {
...dialect,
materializedViews: true,
dropColumnDependencies: ['primaryKey'],
dropPrimaryKey: false,
},
};

View File

@ -4,6 +4,10 @@ class Dumper extends SqlDumper {
renameColumn(column, newcol) {
this.putCmd('^alter ^table %f ^rename ^column %i ^to %i', column, column.columnName, newcol);
}
renameTable(obj, newname) {
this.putCmd('^alter ^table %f ^rename ^to %i', obj, newname);
}
}
module.exports = Dumper;