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

140 lines
4.4 KiB
JavaScript
Raw Normal View History

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');
const { getAlterTableScript, extendDatabaseInfo, generateDbPairingId } = require('dbgate-tools');
2021-06-27 18:44:02 +00:00
2024-09-13 11:09:33 +00:00
function pickImportantTableInfo(engine, table) {
const props = ['columnName'];
if (!engine.skipNullability) props.push('notNull');
if (!engine.skipAutoIncrement) props.push('autoIncrement');
2021-09-05 07:38:38 +00:00
return {
pureName: table.pureName,
2024-09-13 11:09:33 +00:00
columns: table.columns.filter(x => x.columnName != 'rowid').map(fp.pick(props)),
2021-09-05 07:38:38 +00:00
};
}
2024-09-13 11:09:33 +00:00
function checkTableStructure(engine, t1, t2) {
2021-09-05 07:38:38 +00:00
// expect(t1.pureName).toEqual(t2.pureName)
2024-09-13 11:09:33 +00:00
expect(pickImportantTableInfo(engine, t1)).toEqual(pickImportantTableInfo(engine, t2));
2021-09-05 07:38:38 +00:00
}
2024-09-13 11:09:33 +00:00
async function testTableDiff(engine, 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,
2024-09-13 11:09:33 +00:00
col_std int,
col_def int default 12,
${engine.skipReferences ? '' : 'col_fk int references t0(id),'}
col_idx int,
col_uq int ${engine.skipUnique ? '' : 'unique'} ,
col_ref int ${engine.skipUnique ? '' : 'unique'}
2021-07-01 07:41:28 +00:00
)`
);
2024-09-13 11:09:33 +00:00
if (!engine.skipIndexes) {
await driver.query(conn, `create index idx1 on t1(col_idx)`);
}
2021-07-01 07:41:28 +00:00
2024-09-13 11:09:33 +00:00
if (!engine.skipReferences) {
await driver.query(conn, `create table t2 (id int not null primary key, fkval int null references t1(col_ref))`);
}
2021-08-26 09:45:44 +00:00
2021-07-01 07:41:28 +00:00
const tget = x => x.tables.find(y => y.pureName == 't1');
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));
2024-09-13 11:09:33 +00:00
checkTableStructure(engine, 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'];
// 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() {
2024-09-13 13:10:58 +00:00
return _.flatten(
engines.map(engine =>
TESTED_COLUMNS.filter(col => !col.endsWith('_pk') || !engine.skipPkColumnTesting).map(column => [
engine.label,
column,
engine,
])
)
);
2021-07-01 08:23:13 +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) => {
2024-09-13 11:09:33 +00:00
await testTableDiff(engine, conn, driver, tbl => {
2021-06-27 18:44:02 +00:00
tbl.columns.push({
columnName: 'added',
dataType: 'int',
pairingId: crypto.randomUUID(),
2021-06-30 14:15:13 +00:00
notNull: false,
autoIncrement: false,
});
});
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) => {
2024-09-13 11:09:33 +00:00
await testTableDiff(engine, 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(
2024-09-13 11:09:33 +00:00
engine,
2021-07-01 08:23:13 +00:00
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(
2024-09-13 11:09:33 +00:00
engine,
2021-07-01 07:41:28 +00:00
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
})
);
test.each(engines.map(engine => [engine.label, engine]))(
'Drop index - %s',
testWrapper(async (conn, driver, engine) => {
2024-09-13 11:09:33 +00:00
await testTableDiff(engine, conn, driver, tbl => {
tbl.indexes = [];
});
})
);
2021-06-27 18:44:02 +00:00
});