dbgate/integration-tests/__tests__/deploy-database.spec.js

330 lines
8.6 KiB
JavaScript
Raw Permalink Normal View History

2021-09-30 14:01:43 +00:00
/// TODO
const { testWrapper } = require('../tools');
2021-10-02 14:49:10 +00:00
const _ = require('lodash');
2021-09-30 14:01:43 +00:00
const engines = require('../engines');
2021-10-02 12:32:56 +00:00
const deployDb = require('dbgate-api/src/shell/deployDb');
2021-10-02 14:49:10 +00:00
const { databaseInfoFromYamlModel } = require('dbgate-tools');
2021-10-03 07:53:32 +00:00
const generateDeploySql = require('dbgate-api/src/shell/generateDeploySql');
2021-09-30 14:01:43 +00:00
2021-10-02 14:49:10 +00:00
function checkStructure(structure, model) {
const expected = databaseInfoFromYamlModel(model);
expect(structure.tables.length).toEqual(expected.tables.length);
2021-10-14 13:49:07 +00:00
for (const [realTable, expectedTable] of _.zip(
_.sortBy(structure.tables, 'pureName'),
_.sortBy(expected.tables, 'pureName')
)) {
2021-10-14 11:34:27 +00:00
expect(realTable.columns.length).toBeGreaterThanOrEqual(expectedTable.columns.length);
2021-10-02 14:49:10 +00:00
}
}
2021-10-14 13:49:07 +00:00
async function testDatabaseDeploy(conn, driver, dbModelsYaml, testEmptyLastScript) {
let index = 0;
2021-10-02 14:49:10 +00:00
for (const loadedDbModel of dbModelsYaml) {
2021-10-14 13:49:07 +00:00
const { sql, isEmpty } = await generateDeploySql({
2021-10-03 07:53:32 +00:00
systemConnection: conn,
driver,
loadedDbModel,
});
2021-10-05 19:52:04 +00:00
console.debug('Generated deploy script:', sql);
2021-10-03 07:53:32 +00:00
expect(sql.toUpperCase().includes('DROP ')).toBeFalsy();
2021-10-14 13:49:07 +00:00
console.log('dbModelsYaml.length', dbModelsYaml.length, index);
if (testEmptyLastScript && index == dbModelsYaml.length - 1) {
expect(isEmpty).toBeTruthy();
}
2021-10-02 14:49:10 +00:00
await deployDb({
systemConnection: conn,
driver,
loadedDbModel,
});
2021-10-14 13:49:07 +00:00
index++;
2021-10-02 14:49:10 +00:00
}
2021-09-30 14:01:43 +00:00
2021-10-02 12:32:56 +00:00
const structure = await driver.analyseFull(conn);
2021-10-02 14:49:10 +00:00
checkStructure(structure, dbModelsYaml[dbModelsYaml.length - 1]);
2021-09-30 14:01:43 +00:00
}
describe('Deploy database', () => {
test.each(engines.map(engine => [engine.label, engine]))(
2021-10-02 14:49:10 +00:00
'Deploy database simple - %s',
testWrapper(async (conn, driver, engine) => {
await testDatabaseDeploy(conn, driver, [
[
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [{ name: 'id', type: 'int' }],
primaryKey: ['id'],
},
},
],
]);
})
);
test.each(engines.map(engine => [engine.label, engine]))(
'Deploy database simple twice - %s',
2021-09-30 14:01:43 +00:00
testWrapper(async (conn, driver, engine) => {
2021-10-14 13:49:07 +00:00
await testDatabaseDeploy(
conn,
driver,
2021-10-02 14:49:10 +00:00
[
2021-10-14 13:49:07 +00:00
[
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [{ name: 'id', type: 'int' }],
primaryKey: ['id'],
},
2021-10-02 14:49:10 +00:00
},
2021-10-14 13:49:07 +00:00
],
[
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [{ name: 'id', type: 'int' }],
primaryKey: ['id'],
},
2021-10-02 12:32:56 +00:00
},
2021-10-14 13:49:07 +00:00
],
2021-10-02 12:32:56 +00:00
],
2021-10-14 13:49:07 +00:00
true
);
2021-09-30 14:01:43 +00:00
})
);
2021-10-14 11:34:27 +00:00
test.each(engines.map(engine => [engine.label, engine]))(
'Add column - %s',
testWrapper(async (conn, driver, engine) => {
await testDatabaseDeploy(conn, driver, [
[
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [{ name: 'id', type: 'int' }],
primaryKey: ['id'],
},
},
],
[
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [
{ name: 'id', type: 'int' },
{ name: 'val', type: 'int' },
],
primaryKey: ['id'],
},
},
],
]);
})
);
test.each(engines.map(engine => [engine.label, engine]))(
'Dont drop column - %s',
testWrapper(async (conn, driver, engine) => {
2021-10-14 13:49:07 +00:00
await testDatabaseDeploy(
conn,
driver,
2021-10-14 11:34:27 +00:00
[
2021-10-14 13:49:07 +00:00
[
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [
{ name: 'id', type: 'int' },
{ name: 'val', type: 'int' },
],
primaryKey: ['id'],
},
2021-10-14 11:34:27 +00:00
},
2021-10-14 13:49:07 +00:00
],
[
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [{ name: 'id', type: 'int' }],
primaryKey: ['id'],
},
},
],
2021-10-14 11:34:27 +00:00
],
2021-10-14 13:49:07 +00:00
true
);
})
);
test.each(engines.filter(x => !x.skipReferences).map(engine => [engine.label, engine]))(
2021-10-14 13:49:07 +00:00
'Foreign keys - %s',
testWrapper(async (conn, driver, engine) => {
await testDatabaseDeploy(
conn,
driver,
2021-10-14 11:34:27 +00:00
[
2021-10-14 13:49:07 +00:00
[
{
name: 't2.table.yaml',
json: {
name: 't2',
columns: [
{ name: 't2id', type: 'int' },
{ name: 't1id', type: 'int', references: 't1' },
],
primaryKey: ['t2id'],
},
2021-10-14 11:34:27 +00:00
},
2021-10-14 13:49:07 +00:00
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [{ name: 't1id', type: 'int' }],
primaryKey: ['t1id'],
},
},
],
[
{
name: 't2.table.yaml',
json: {
name: 't2',
columns: [
{ name: 't2id', type: 'int' },
{ name: 't1id', type: 'int', references: 't1' },
],
primaryKey: ['t2id'],
},
},
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [{ name: 't1id', type: 'int' }],
primaryKey: ['t1id'],
},
},
],
2021-10-14 11:34:27 +00:00
],
2021-10-14 13:49:07 +00:00
true
);
2021-10-14 11:34:27 +00:00
})
);
2021-11-27 16:49:02 +00:00
test.each(engines.filter(x => !x.skipDataModifications).map(engine => [engine.label, engine]))(
2021-11-27 16:49:02 +00:00
'Deploy preloaded data - %s',
testWrapper(async (conn, driver, engine) => {
await testDatabaseDeploy(conn, driver, [
[
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [
{ name: 'id', type: 'int' },
{ name: 'value', type: 'int' },
],
primaryKey: ['id'],
data: [
{ id: 1, value: 1 },
{ id: 2, value: 2 },
{ id: 3, value: 3 },
],
},
},
],
]);
const res = await driver.query(conn, `select count(*) as cnt from t1`);
expect(res.rows[0].cnt.toString()).toEqual('3');
})
);
test.each(engines.filter(x => !x.skipDataModifications).map(engine => [engine.label, engine]))(
'Deploy preloaded data - update - %s',
testWrapper(async (conn, driver, engine) => {
await testDatabaseDeploy(conn, driver, [
[
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [
{ name: 'id', type: 'int' },
{ name: 'val', type: 'int' },
],
primaryKey: ['id'],
data: [
{ id: 1, val: 1 },
{ id: 2, val: 2 },
{ id: 3, val: 3 },
],
},
},
],
[
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [
{ name: 'id', type: 'int' },
{ name: 'val', type: 'int' },
],
primaryKey: ['id'],
data: [
{ id: 1, val: 1 },
{ id: 2, val: 5 },
{ id: 3, val: 3 },
],
},
},
],
]);
const res = await driver.query(conn, `select val from t1 where id = 2`);
expect(res.rows[0].val.toString()).toEqual('5');
})
);
test.each(engines.enginesPostgre.map(engine => [engine.label, engine]))(
'Current timestamp default value - %s',
testWrapper(async (conn, driver, engine) => {
await testDatabaseDeploy(conn, driver, [
[
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [
{ name: 'id', type: 'int' },
{
name: 'val',
type: 'timestamp',
default: 'current_timestamp',
},
],
primaryKey: ['id'],
},
},
],
]);
await driver.query(conn, `insert into t1 (id) values (1)`);
const res = await driver.query(conn, ` select val from t1 where id = 1`);
expect(res.rows[0].val.toString().substring(0, 2)).toEqual('20');
})
);
2021-09-30 14:01:43 +00:00
});