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');
|
2021-07-01 07:12:15 +00:00
|
|
|
const { getAlterTableScript, extendDatabaseInfo, generateDbPairingId } = 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) {
|
2021-07-01 07:41:28 +00:00
|
|
|
await driver.query(conn, `create table t0 (id int not null primary key)`);
|
2021-06-27 18:44:02 +00:00
|
|
|
|
2021-07-01 07:41:28 +00:00
|
|
|
await driver.query(
|
|
|
|
conn,
|
|
|
|
`create table t1 (
|
|
|
|
id int not null primary key,
|
|
|
|
col_std int null,
|
|
|
|
col_def int null default 12,
|
|
|
|
col_fk int null references t0(id),
|
|
|
|
col_idx int null
|
|
|
|
)`
|
|
|
|
);
|
|
|
|
|
|
|
|
await driver.query(conn, `create index idx1 on t1(col_idx)`);
|
|
|
|
|
|
|
|
const tget = x => x.tables.find(y => y.pureName == 't1');
|
2021-07-01 07:12:15 +00:00
|
|
|
const structure1 = generateDbPairingId(extendDatabaseInfo(await driver.analyseFull(conn)));
|
2021-06-28 06:00:28 +00:00
|
|
|
let structure2 = _.cloneDeep(structure1);
|
2021-07-01 07:41:28 +00:00
|
|
|
mangle(tget(structure2));
|
2021-06-28 06:00:28 +00:00
|
|
|
structure2 = extendDatabaseInfo(structure2);
|
|
|
|
|
2021-07-01 07:41:28 +00:00
|
|
|
const sql = getAlterTableScript(tget(structure1), tget(structure2), {}, structure2, driver);
|
2021-07-01 08:50:41 +00:00
|
|
|
console.log('RUNNING ALTER SQL', driver.engine, ':', sql);
|
2021-06-28 06:00:28 +00:00
|
|
|
|
|
|
|
await driver.query(conn, sql);
|
|
|
|
|
|
|
|
const structure2Real = extendDatabaseInfo(await driver.analyseFull(conn));
|
|
|
|
|
2021-07-01 07:41:28 +00:00
|
|
|
checkTableStructure(tget(structure2Real), tget(structure2));
|
2021-06-30 14:15:13 +00:00
|
|
|
// expect(stableStringify(structure2)).toEqual(stableStringify(structure2Real));
|
2021-06-27 18:44:02 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 08:50:41 +00:00
|
|
|
// const TESTED_COLUMNS = ['col_std', 'col_def', 'col_fk', 'col_idx'];
|
|
|
|
const TESTED_COLUMNS = ['col_std'];
|
|
|
|
|
2021-07-01 08:23:13 +00:00
|
|
|
function engines_columns_source() {
|
2021-07-01 08:50:41 +00:00
|
|
|
return _.flatten(engines.map(engine => TESTED_COLUMNS.map(column => [engine.label, column, engine])));
|
2021-07-01 08:23:13 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
})
|
|
|
|
);
|
2021-07-01 07:41:28 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2021-07-01 08:23:13 +00:00
|
|
|
test.each(engines_columns_source())(
|
|
|
|
'Drop column - %s - %s',
|
|
|
|
testWrapper(async (conn, driver, column, engine) => {
|
|
|
|
await testTableDiff(conn, driver, tbl => (tbl.columns = tbl.columns.filter(x => x.columnName != column)));
|
2021-07-01 07:41:28 +00:00
|
|
|
})
|
|
|
|
);
|
2021-06-27 18:44:02 +00:00
|
|
|
|
2021-07-01 08:23:13 +00:00
|
|
|
test.each(engines_columns_source())(
|
|
|
|
'Change nullability - %s - %s',
|
|
|
|
testWrapper(async (conn, driver, column, engine) => {
|
|
|
|
await testTableDiff(
|
|
|
|
conn,
|
|
|
|
driver,
|
|
|
|
tbl => (tbl.columns = tbl.columns.map(x => (x.columnName == column ? { ...x, notNull: true } : x)))
|
|
|
|
);
|
2021-07-01 07:41:28 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2021-07-01 08:23:13 +00:00
|
|
|
test.each(engines_columns_source())(
|
2021-07-01 08:50:41 +00:00
|
|
|
'Rename column - %s - %s',
|
2021-07-01 08:23:13 +00:00
|
|
|
testWrapper(async (conn, driver, column, engine) => {
|
2021-07-01 07:41:28 +00:00
|
|
|
await testTableDiff(
|
|
|
|
conn,
|
|
|
|
driver,
|
2021-07-01 08:23:13 +00:00
|
|
|
tbl => (tbl.columns = tbl.columns.map(x => (x.columnName == column ? { ...x, columnName: 'col_renamed' } : x)))
|
2021-07-01 07:41:28 +00:00
|
|
|
);
|
2021-06-27 18:44:02 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|