2021-06-28 06:00:28 +00:00
|
|
|
const stableStringify = require('json-stable-stringify');
|
|
|
|
const _ = require('lodash');
|
2021-09-05 07:38:38 +00:00
|
|
|
const fp = require('lodash/fp');
|
|
|
|
const { testWrapper } = require('../tools');
|
2021-06-28 06:00:28 +00:00
|
|
|
const engines = require('../engines');
|
2024-05-20 17:48:21 +00:00
|
|
|
const crypto = require('crypto');
|
2021-07-01 07:12:15 +00:00
|
|
|
const { getAlterTableScript, extendDatabaseInfo, generateDbPairingId } = require('dbgate-tools');
|
2021-06-27 18:44:02 +00:00
|
|
|
|
2021-09-05 07:38:38 +00:00
|
|
|
function pickImportantTableInfo(table) {
|
|
|
|
return {
|
|
|
|
pureName: table.pureName,
|
2021-09-05 08:55:53 +00:00
|
|
|
columns: table.columns
|
|
|
|
.filter(x => x.columnName != 'rowid')
|
|
|
|
.map(fp.pick(['columnName', 'notNull', 'autoIncrement'])),
|
2021-09-05 07:38:38 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (
|
2021-08-26 09:45:44 +00:00
|
|
|
col_pk int not null primary key,
|
2021-07-01 07:41:28 +00:00
|
|
|
col_std int null,
|
|
|
|
col_def int null default 12,
|
|
|
|
col_fk int null references t0(id),
|
2021-08-26 09:45:44 +00:00
|
|
|
col_idx int null,
|
|
|
|
col_uq int null unique,
|
|
|
|
col_ref int null unique
|
2021-07-01 07:41:28 +00:00
|
|
|
)`
|
|
|
|
);
|
|
|
|
|
|
|
|
await driver.query(conn, `create index idx1 on t1(col_idx)`);
|
|
|
|
|
2021-08-26 09:45:44 +00:00
|
|
|
await driver.query(conn, `create table t2 (id int not null primary key, fkval int null references t1(col_ref))`);
|
|
|
|
|
2021-07-01 07:41:28 +00:00
|
|
|
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-11-25 14:14:23 +00:00
|
|
|
const { sql } = getAlterTableScript(tget(structure1), tget(structure2), {}, structure1, 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
|
|
|
|
2021-09-04 19:06:42 +00:00
|
|
|
await driver.script(conn, sql);
|
2021-06-28 06:00:28 +00:00
|
|
|
|
|
|
|
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-09-06 16:26:12 +00:00
|
|
|
const TESTED_COLUMNS = ['col_pk', 'col_std', 'col_def', 'col_fk', 'col_ref', 'col_idx', 'col_uq'];
|
2021-09-05 08:55:53 +00:00
|
|
|
// const TESTED_COLUMNS = ['col_pk'];
|
2021-09-04 19:06:42 +00:00
|
|
|
// const TESTED_COLUMNS = ['col_idx'];
|
2021-09-06 16:26:12 +00:00
|
|
|
// const TESTED_COLUMNS = ['col_def'];
|
2021-08-26 09:45:44 +00:00
|
|
|
// const TESTED_COLUMNS = ['col_std'];
|
2021-09-09 07:09:06 +00:00
|
|
|
// const TESTED_COLUMNS = ['col_ref'];
|
2021-07-01 08:50:41 +00:00
|
|
|
|
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-09-13 19:00:49 +00:00
|
|
|
describe('Alter table', () => {
|
2021-06-27 18:44:02 +00:00
|
|
|
test.each(engines.map(engine => [engine.label, engine]))(
|
|
|
|
'Add column - %s',
|
|
|
|
testWrapper(async (conn, driver, engine) => {
|
2021-09-04 16:43:59 +00:00
|
|
|
await testTableDiff(conn, driver, tbl => {
|
2021-06-27 18:44:02 +00:00
|
|
|
tbl.columns.push({
|
|
|
|
columnName: 'added',
|
|
|
|
dataType: 'int',
|
2024-05-20 17:15:03 +00:00
|
|
|
pairingId: crypto.randomUUID(),
|
2021-06-30 14:15:13 +00:00
|
|
|
notNull: false,
|
|
|
|
autoIncrement: false,
|
2021-09-04 16:43:59 +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
|
|
|
})
|
|
|
|
);
|
2021-09-04 16:43:59 +00:00
|
|
|
|
|
|
|
test.each(engines.map(engine => [engine.label, engine]))(
|
|
|
|
'Drop index - %s',
|
|
|
|
testWrapper(async (conn, driver, engine) => {
|
|
|
|
await testTableDiff(conn, driver, tbl => {
|
|
|
|
tbl.indexes = [];
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2021-06-27 18:44:02 +00:00
|
|
|
});
|