dbgate/integration-tests/__tests__/alter-database.spec.js

71 lines
2.1 KiB
JavaScript
Raw Normal View History

const stableStringify = require('json-stable-stringify');
const _ = require('lodash');
const fp = require('lodash/fp');
const { testWrapper } = require('../tools');
const engines = require('../engines');
const { getAlterDatabaseScript, extendDatabaseInfo, generateDbPairingId } = require('dbgate-tools');
2021-09-13 19:38:28 +00:00
function flatSource() {
return _.flatten(
2024-09-13 11:09:33 +00:00
engines
.filter(x => !x.skipReferences)
.map(engine => (engine.objects || []).map(object => [engine.label, object.type, object, engine]))
2021-09-13 19:38:28 +00:00
);
}
async function testDatabaseDiff(conn, driver, mangle, createObject = null) {
await driver.query(conn, `create table t1 (id int not null primary key)`);
await driver.query(
conn,
2021-09-13 19:38:28 +00:00
`create table t2 (
id int not null primary key,
t1_id int null references t1(id)
)`
);
2021-09-13 19:38:28 +00:00
if (createObject) await driver.query(conn, createObject);
const structure1 = generateDbPairingId(extendDatabaseInfo(await driver.analyseFull(conn)));
let structure2 = _.cloneDeep(structure1);
mangle(structure2);
structure2 = extendDatabaseInfo(structure2);
2021-11-25 14:14:23 +00:00
const { sql } = getAlterDatabaseScript(structure1, structure2, {}, structure1, 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);
2021-09-13 19:38:28 +00:00
return structure2Real;
}
describe('Alter database', () => {
2024-09-13 11:09:33 +00:00
test.each(engines.filter(x => !x.skipReferences).map(engine => [engine.label, engine]))(
'Drop referenced table - %s',
testWrapper(async (conn, driver, engine) => {
await testDatabaseDiff(conn, driver, db => {
2021-09-13 19:38:28 +00:00
_.remove(db.tables, x => x.pureName == 't1');
});
})
);
2021-09-13 19:38:28 +00:00
test.each(flatSource())(
'Drop object - %s - %s',
testWrapper(async (conn, driver, type, object, engine) => {
const db = await testDatabaseDiff(
conn,
driver,
db => {
_.remove(db[type], x => x.pureName == 'obj1');
},
object.create1
);
expect(db[type].length).toEqual(0);
})
);
});
2024-09-18 14:01:02 +00:00