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

173 lines
6.7 KiB
JavaScript
Raw Permalink Normal View History

2021-05-27 08:28:52 +00:00
const engines = require('../engines');
2021-05-27 11:52:51 +00:00
const { testWrapper } = require('../tools');
2021-05-27 07:12:21 +00:00
2024-09-13 14:11:38 +00:00
const t1Sql = 'CREATE TABLE t1 (id int not null primary key, val1 varchar(50))';
2021-08-19 14:08:27 +00:00
const ix1Sql = 'CREATE index ix1 ON t1(val1, id)';
2024-09-16 07:47:44 +00:00
const t2Sql = engine =>
`CREATE TABLE t2 (id int not null primary key, val2 varchar(50) ${engine.skipUnique ? '' : 'unique'})`;
2021-09-04 19:54:12 +00:00
const t3Sql = 'CREATE TABLE t3 (id int not null primary key, valfk int, foreign key (valfk) references t2(id))';
// const fkSql = 'ALTER TABLE t3 ADD FOREIGN KEY (valfk) REFERENCES t2(id)'
2021-05-27 16:30:49 +00:00
2024-09-13 14:11:38 +00:00
const txMatch = (engine, tname, vcolname, nextcol) =>
2021-05-27 16:30:49 +00:00
expect.objectContaining({
pureName: tname,
2021-05-27 08:16:05 +00:00
columns: [
expect.objectContaining({
columnName: 'id',
2024-09-13 14:30:48 +00:00
dataType: expect.stringMatching(/int.*/i),
2024-09-16 07:47:44 +00:00
...(engine.skipNullability ? {} : { notNull: true }),
2021-05-27 08:16:05 +00:00
}),
2021-05-27 16:30:49 +00:00
expect.objectContaining({
columnName: vcolname,
2024-09-16 07:47:44 +00:00
...(engine.skipNullability ? {} : { notNull: false }),
2024-09-13 14:30:48 +00:00
dataType: engine.skipStringLength
? expect.stringMatching(/.*string|char.*/i)
2024-09-16 07:47:44 +00:00
: expect.stringMatching(/.*char.*\(50\)/i),
2021-05-27 16:30:49 +00:00
}),
...(nextcol
? [
expect.objectContaining({
columnName: 'nextcol',
2024-09-16 07:47:44 +00:00
...(engine.skipNullability ? {} : { notNull: false }),
2024-09-13 14:30:48 +00:00
dataType: engine.skipStringLength
? expect.stringMatching(/.*string.*|char.*/i)
2024-09-16 07:47:44 +00:00
: expect.stringMatching(/.*char.*\(50\).*/i),
2021-05-27 16:30:49 +00:00
}),
]
: []),
2021-05-27 08:16:05 +00:00
],
2021-05-27 16:30:49 +00:00
primaryKey: expect.objectContaining({
columns: [
expect.objectContaining({
columnName: 'id',
}),
],
}),
});
2024-09-13 14:11:38 +00:00
const t1Match = engine => txMatch(engine, 't1', 'val1');
const t2Match = engine => txMatch(engine, 't2', 'val2');
const t2NextColMatch = engine => txMatch(engine, 't2', 'val2', true);
2021-05-27 07:43:08 +00:00
2021-05-27 10:26:20 +00:00
describe('Table analyse', () => {
2021-05-27 07:43:08 +00:00
test.each(engines.map(engine => [engine.label, engine]))(
2021-05-27 13:46:21 +00:00
'Table structure - full analysis - %s',
2021-05-27 11:17:25 +00:00
testWrapper(async (conn, driver, engine) => {
await driver.query(conn, t1Sql);
2021-05-27 07:12:21 +00:00
2021-05-27 11:17:25 +00:00
const structure = await driver.analyseFull(conn);
2021-05-27 07:12:21 +00:00
2021-05-27 11:17:25 +00:00
expect(structure.tables.length).toEqual(1);
2024-09-13 14:11:38 +00:00
expect(structure.tables[0]).toEqual(t1Match(engine));
2021-05-27 11:17:25 +00:00
})
2021-05-27 07:12:21 +00:00
);
2021-05-27 07:26:37 +00:00
2021-05-27 07:43:08 +00:00
test.each(engines.map(engine => [engine.label, engine]))(
2021-05-27 13:46:21 +00:00
'Table add - incremental analysis - %s',
2021-05-27 11:17:25 +00:00
testWrapper(async (conn, driver, engine) => {
2024-09-16 07:47:44 +00:00
await driver.query(conn, t2Sql(engine));
2021-05-27 16:30:49 +00:00
2021-05-27 07:26:37 +00:00
const structure1 = await driver.analyseFull(conn);
2021-05-27 16:30:49 +00:00
expect(structure1.tables.length).toEqual(1);
2024-09-13 14:11:38 +00:00
expect(structure1.tables[0]).toEqual(t2Match(engine));
2021-05-27 16:30:49 +00:00
2021-05-27 08:16:05 +00:00
await driver.query(conn, t1Sql);
2021-05-27 07:26:37 +00:00
const structure2 = await driver.analyseIncremental(conn, structure1);
expect(structure2.tables.length).toEqual(2);
2024-09-13 14:11:38 +00:00
expect(structure2.tables.find(x => x.pureName == 't1')).toEqual(t1Match(engine));
expect(structure2.tables.find(x => x.pureName == 't2')).toEqual(t2Match(engine));
2021-05-27 11:17:25 +00:00
})
2021-05-27 07:26:37 +00:00
);
2021-05-27 08:16:05 +00:00
test.each(engines.map(engine => [engine.label, engine]))(
2021-05-27 13:46:21 +00:00
'Table remove - incremental analysis - %s',
2021-05-27 11:17:25 +00:00
testWrapper(async (conn, driver, engine) => {
2021-05-27 08:16:05 +00:00
await driver.query(conn, t1Sql);
2024-09-16 07:47:44 +00:00
await driver.query(conn, t2Sql(engine));
2021-05-27 08:16:05 +00:00
const structure1 = await driver.analyseFull(conn);
2021-05-27 16:30:49 +00:00
expect(structure1.tables.length).toEqual(2);
2024-09-13 14:11:38 +00:00
expect(structure1.tables.find(x => x.pureName == 't1')).toEqual(t1Match(engine));
expect(structure1.tables.find(x => x.pureName == 't2')).toEqual(t2Match(engine));
2021-05-27 16:30:49 +00:00
2021-05-27 08:16:05 +00:00
await driver.query(conn, 'DROP TABLE t2');
const structure2 = await driver.analyseIncremental(conn, structure1);
expect(structure2.tables.length).toEqual(1);
2024-09-13 14:11:38 +00:00
expect(structure2.tables[0]).toEqual(t1Match(engine));
2021-05-27 11:17:25 +00:00
})
2021-05-27 08:16:05 +00:00
);
2021-05-27 16:30:49 +00:00
test.each(engines.map(engine => [engine.label, engine]))(
'Table change - incremental analysis - %s',
testWrapper(async (conn, driver, engine) => {
await driver.query(conn, t1Sql);
2024-09-16 07:47:44 +00:00
await driver.query(conn, t2Sql(engine));
2021-05-27 16:30:49 +00:00
const structure1 = await driver.analyseFull(conn);
if (engine.dbSnapshotBySeconds) await new Promise(resolve => setTimeout(resolve, 1100));
2021-08-14 07:36:22 +00:00
2024-09-16 07:47:44 +00:00
await driver.query(
conn,
`ALTER TABLE t2 ADD ${engine.alterTableAddColumnSyntax ? 'COLUMN' : ''} nextcol varchar(50)`
);
2021-05-27 16:30:49 +00:00
const structure2 = await driver.analyseIncremental(conn, structure1);
expect(structure2).toBeTruthy(); // if falsy, no modification is detected
expect(structure2.tables.length).toEqual(2);
2024-09-13 14:11:38 +00:00
expect(structure2.tables.find(x => x.pureName == 't1')).toEqual(t1Match(engine));
expect(structure2.tables.find(x => x.pureName == 't2')).toEqual(t2NextColMatch(engine));
2021-05-27 16:30:49 +00:00
})
);
2021-08-14 07:36:22 +00:00
2024-09-13 14:30:48 +00:00
test.each(engines.filter(x => !x.skipIndexes).map(engine => [engine.label, engine]))(
2021-08-14 07:36:22 +00:00
'Index - full analysis - %s',
testWrapper(async (conn, driver, engine) => {
await driver.query(conn, t1Sql);
await driver.query(conn, ix1Sql);
const structure = await driver.analyseFull(conn);
const t1 = structure.tables.find(x => x.pureName == 't1');
expect(t1.indexes.length).toEqual(1);
2021-08-19 14:08:27 +00:00
expect(t1.indexes[0].columns.length).toEqual(2);
2021-08-19 13:14:27 +00:00
expect(t1.indexes[0].columns[0]).toEqual(expect.objectContaining({ columnName: 'val1' }));
2021-08-19 14:08:27 +00:00
expect(t1.indexes[0].columns[1]).toEqual(expect.objectContaining({ columnName: 'id' }));
2021-08-14 07:36:22 +00:00
})
);
2021-08-26 11:00:38 +00:00
2024-09-13 14:30:48 +00:00
test.each(engines.filter(x => !x.skipUnique).map(engine => [engine.label, engine]))(
2021-08-26 11:00:38 +00:00
'Unique - full analysis - %s',
testWrapper(async (conn, driver, engine) => {
2024-09-16 07:47:44 +00:00
await driver.query(conn, t2Sql(engine));
2021-08-26 11:00:38 +00:00
const structure = await driver.analyseFull(conn);
const t2 = structure.tables.find(x => x.pureName == 't2');
// const indexesAndUniques = [...t2.uniques, ...t2.indexes];
expect(t2.uniques.length).toEqual(1);
expect(t2.uniques[0].columns.length).toEqual(1);
expect(t2.uniques[0].columns[0]).toEqual(expect.objectContaining({ columnName: 'val2' }));
2021-08-26 11:00:38 +00:00
})
);
2021-09-04 19:54:12 +00:00
2024-09-13 14:30:48 +00:00
test.each(engines.filter(x => !x.skipReferences).map(engine => [engine.label, engine]))(
2021-09-04 19:54:12 +00:00
'Foreign key - full analysis - %s',
testWrapper(async (conn, driver, engine) => {
2024-09-16 07:47:44 +00:00
await driver.query(conn, t2Sql(engine));
2021-09-04 19:54:12 +00:00
await driver.query(conn, t3Sql);
// await driver.query(conn, fkSql);
const structure = await driver.analyseFull(conn);
const t3 = structure.tables.find(x => x.pureName == 't3');
console.log('T3', t3.foreignKeys[0].columns);
expect(t3.foreignKeys.length).toEqual(1);
expect(t3.foreignKeys[0].columns.length).toEqual(1);
expect(t3.foreignKeys[0]).toEqual(expect.objectContaining({ refTableName: 't2' }));
expect(t3.foreignKeys[0].columns[0]).toEqual(
expect.objectContaining({ columnName: 'valfk', refColumnName: 'id' })
);
})
);
2021-05-27 07:12:21 +00:00
});