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

64 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-06-28 06:00:28 +00:00
const stableStringify = require('json-stable-stringify');
const _ = require('lodash');
2021-06-30 14:15:13 +00:00
const fp = require('lodash/fp');
2021-06-27 18:44:02 +00:00
const uuidv1 = require('uuid/v1');
const { testWrapper } = require('../tools');
2021-06-28 06:00:28 +00:00
const engines = require('../engines');
const { getAlterTableScript, extendDatabaseInfo } = require('dbgate-tools');
2021-06-27 18:44:02 +00:00
2021-06-30 14:15:13 +00:00
function pickImportantTableInfo(table) {
return {
pureName: table.pureName,
columns: table.columns.map(fp.pick(['columnName', 'notNull', 'autoIncrement'])),
};
}
function checkTableStructure(t1, t2) {
// expect(t1.pureName).toEqual(t2.pureName)
expect(pickImportantTableInfo(t1)).toEqual(pickImportantTableInfo(t2));
}
2021-06-27 18:44:02 +00:00
async function testTableDiff(conn, driver, mangle) {
await driver.query(conn, 'create table t1 (col1 int not null)');
2021-06-28 06:00:28 +00:00
const structure1 = extendDatabaseInfo(await driver.analyseFull(conn));
let structure2 = _.cloneDeep(structure1);
mangle(structure2.tables[0]);
structure2 = extendDatabaseInfo(structure2);
const sql = getAlterTableScript(structure1.tables[0], structure2.tables[0], {}, structure2, driver);
console.log('RUNNING ALTER SQL:', sql);
await driver.query(conn, sql);
const structure2Real = extendDatabaseInfo(await driver.analyseFull(conn));
2021-06-30 14:15:13 +00:00
checkTableStructure(structure2Real.tables[0], structure2.tables[0]);
// expect(stableStringify(structure2)).toEqual(stableStringify(structure2Real));
2021-06-27 18:44:02 +00:00
}
describe('Alter processor', () => {
test.each(engines.map(engine => [engine.label, engine]))(
'Add column - %s',
testWrapper(async (conn, driver, engine) => {
2021-06-28 06:00:28 +00:00
await testTableDiff(conn, driver, tbl =>
2021-06-27 18:44:02 +00:00
tbl.columns.push({
columnName: 'added',
dataType: 'int',
pairingId: uuidv1(),
2021-06-30 14:15:13 +00:00
notNull: false,
autoIncrement: false,
2021-06-27 18:44:02 +00:00
})
);
// console.log('ENGINE', engine);
// for (const sql of initSql) await driver.query(conn, sql);
// await driver.query(conn, object.create1);
// const structure = await driver.analyseFull(conn);
// expect(structure[type].length).toEqual(1);
// expect(structure[type][0]).toEqual(type.includes('views') ? view1Match : obj1Match);
})
);
});