added test - table is never dropped in deploy db

This commit is contained in:
Jan Prochazka 2024-10-29 18:02:27 +01:00
parent 460f511bf6
commit 10d79dca4d

View File

@ -8,19 +8,50 @@ const { databaseInfoFromYamlModel } = require('dbgate-tools');
const generateDeploySql = require('dbgate-api/src/shell/generateDeploySql'); const generateDeploySql = require('dbgate-api/src/shell/generateDeploySql');
const connectUtility = require('dbgate-api/src/utility/connectUtility'); const connectUtility = require('dbgate-api/src/utility/connectUtility');
function checkStructure(structure, model) { function checkStructure(structure, model, { checkRenameDeletedObjects = false, disallowExtraObjects = false }) {
const expected = databaseInfoFromYamlModel(model); const expected = databaseInfoFromYamlModel(model);
expect(structure.tables.length).toEqual(expected.tables.length); expect(structure.tables.length).toEqual(expected.tables.length);
for (const [realTable, expectedTable] of _.zip( for (const expectedTable of expected.tables) {
_.sortBy(structure.tables, 'pureName'), const realTable = structure.tables.find(x => x.pureName == expectedTable.pureName);
_.sortBy(expected.tables, 'pureName')
)) { for (const column of expectedTable.columns) {
expect(realTable.columns.length).toBeGreaterThanOrEqual(expectedTable.columns.length); const realColumn = realTable.columns.find(x => x.columnName == column.columnName);
expect(realColumn).toBeTruthy();
expect(realColumn.notNull).toEqual(column.notNull);
}
for (const realColumn of realTable.columns) {
const column = expectedTable.columns.find(x => x.columnName == realColumn.columnName);
if (!column) {
if (checkRenameDeletedObjects) {
expect(realColumn.columnName).toMatch(/^_deleted_/);
}
if (disallowExtraObjects) {
expect(realColumn).toBeFalsy();
}
}
} }
} }
async function testDatabaseDeploy(conn, driver, dbModelsYaml, testEmptyLastScript) { for (const realTable of structure.tables) {
const expectedTable = expected.tables.find(x => x.pureName == realTable.pureName);
if (!expectedTable) {
if (checkRenameDeletedObjects) {
expect(realTable.pureName).toMatch(/^_deleted_/);
}
if (disallowExtraObjects) {
expect(realTable).toBeFalsy();
}
}
}
}
async function testDatabaseDeploy(conn, driver, dbModelsYaml, options) {
const { testEmptyLastScript, checkDeletedObjects, finalCheckAgainstModel, finalCheckAgainstFirstModel } =
options || {};
let index = 0; let index = 0;
for (const loadedDbModel of dbModelsYaml) { for (const loadedDbModel of dbModelsYaml) {
const { sql, isEmpty } = await generateDeploySql({ const { sql, isEmpty } = await generateDeploySql({
@ -50,7 +81,11 @@ async function testDatabaseDeploy(conn, driver, dbModelsYaml, testEmptyLastScrip
const dbhan = conn.isPreparedOnly ? await connectUtility(driver, conn, 'read') : conn; const dbhan = conn.isPreparedOnly ? await connectUtility(driver, conn, 'read') : conn;
const structure = await driver.analyseFull(dbhan); const structure = await driver.analyseFull(dbhan);
if (conn.isPreparedOnly) await driver.close(dbhan); if (conn.isPreparedOnly) await driver.close(dbhan);
checkStructure(structure, dbModelsYaml[dbModelsYaml.length - 1]); checkStructure(
structure,
finalCheckAgainstFirstModel ? dbModelsYaml[0] : finalCheckAgainstModel ?? dbModelsYaml[dbModelsYaml.length - 1],
options
);
} }
describe('Deploy database', () => { describe('Deploy database', () => {
@ -118,7 +153,7 @@ describe('Deploy database', () => {
}, },
], ],
], ],
true { testEmptyLastScript: true }
); );
}) })
); );
@ -185,7 +220,7 @@ describe('Deploy database', () => {
}, },
], ],
], ],
true { testEmptyLastScript: true }
); );
}) })
); );
@ -240,7 +275,7 @@ describe('Deploy database', () => {
}, },
], ],
], ],
true { testEmptyLastScript: true }
); );
}) })
); );
@ -349,4 +384,67 @@ describe('Deploy database', () => {
expect(res.rows[0].val.toString().substring(0, 2)).toEqual('20'); expect(res.rows[0].val.toString().substring(0, 2)).toEqual('20');
}) })
); );
test.each(engines.map(engine => [engine.label, engine]))(
'Dont remove column - %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'],
},
},
],
[
{
name: 't1.table.yaml',
json: {
name: 't1',
columns: [{ name: 'id', type: 'int' }],
primaryKey: ['id'],
},
},
],
],
{ finalCheckAgainstFirstModel: true, disallowExtraObjects: true }
);
})
);
test.each(engines.map(engine => [engine.label, engine]))(
'Dont remove table - %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'],
},
},
],
[],
],
{ finalCheckAgainstFirstModel: true, disallowExtraObjects: true }
);
})
);
}); });