drop table optimalization - dont drop references in mysql and slite

This commit is contained in:
Jan Prochazka
2021-09-13 21:00:49 +02:00
parent 02ce6b0204
commit 2fa48b1138
9 changed files with 67 additions and 12 deletions

View File

@@ -0,0 +1,44 @@
const stableStringify = require('json-stable-stringify');
const _ = require('lodash');
const fp = require('lodash/fp');
const uuidv1 = require('uuid/v1');
const { testWrapper } = require('../tools');
const engines = require('../engines');
const { getAlterDatabaseScript, extendDatabaseInfo, generateDbPairingId } = require('dbgate-tools');
async function testDatabaseDiff(conn, driver, mangle) {
await driver.query(conn, `create table t0 (id int not null primary key)`);
await driver.query(
conn,
`create table t1 (
col_pk int not null primary key,
col_fk int null references t0(id)
)`
);
const structure1 = generateDbPairingId(extendDatabaseInfo(await driver.analyseFull(conn)));
let structure2 = _.cloneDeep(structure1);
mangle(structure2);
structure2 = extendDatabaseInfo(structure2);
const sql = getAlterDatabaseScript(structure1, structure2, {}, structure2, driver);
console.log('RUNNING ALTER SQL', driver.engine, ':', sql);
await driver.script(conn, sql);
const structure2Real = extendDatabaseInfo(await driver.analyseFull(conn));
expect(structure2Real.tables.length).toEqual(structure2.tables.length);
}
describe('Alter database', () => {
test.each(engines.map(engine => [engine.label, engine]))(
'Drop referenced table - %s',
testWrapper(async (conn, driver, engine) => {
await testDatabaseDiff(conn, driver, db => {
_.remove(db.tables, x => x.pureName == 't0');
});
})
);
});

View File

@@ -68,7 +68,7 @@ function engines_columns_source() {
return _.flatten(engines.map(engine => TESTED_COLUMNS.map(column => [engine.label, column, engine])));
}
describe('Alter processor', () => {
describe('Alter table', () => {
test.each(engines.map(engine => [engine.label, engine]))(
'Add column - %s',
testWrapper(async (conn, driver, engine) => {

View File

@@ -116,11 +116,11 @@ const engines = [
const filterLocal = [
// filter local testing
'-MySQL',
'-PostgreSQL',
'MySQL',
'PostgreSQL',
'SQL Server',
'-SQLite',
'-CockroachDB',
'SQLite',
'CockroachDB',
];
module.exports = process.env.CITEST

View File

@@ -248,13 +248,15 @@ export class AlterPlan {
if (op.operationType == 'dropTable') {
return [
...(op.oldObject.dependencies || []).map(oldObject => {
const opRes: AlterOperation = {
operationType: 'dropConstraint',
oldObject,
};
return opRes;
}),
...(this.dialect.dropReferencesWhenDropTable
? (op.oldObject.dependencies || []).map(oldObject => {
const opRes: AlterOperation = {
operationType: 'dropConstraint',
oldObject,
};
return opRes;
})
: []),
op,
];
}

View File

@@ -30,4 +30,6 @@ export interface SqlDialect {
dropUnique?: boolean;
createCheck?: boolean;
dropCheck?: boolean;
dropReferencesWhenDropTable?: boolean;
}

View File

@@ -33,6 +33,8 @@ const dialect = {
dropUnique: true,
createCheck: true,
dropCheck: true,
dropReferencesWhenDropTable: true,
};
/** @type {import('dbgate-types').EngineDriver} */

View File

@@ -28,6 +28,8 @@ const dialect = {
dropUnique: true,
createCheck: true,
dropCheck: true,
dropReferencesWhenDropTable: false,
};
const mysqlDriverBase = {

View File

@@ -30,6 +30,8 @@ const dialect = {
dropUnique: true,
createCheck: true,
dropCheck: true,
dropReferencesWhenDropTable: true,
};
const postgresDriverBase = {

View File

@@ -30,6 +30,7 @@ const dialect = {
dropForeignKey: false,
createPrimaryKey: false,
dropPrimaryKey: false,
dropReferencesWhenDropTable: false,
};
/** @type {import('dbgate-types').EngineDriver} */