db deploy - next tests

This commit is contained in:
Jan Prochazka
2021-10-02 16:49:10 +02:00
parent d953d1b342
commit b5f22516b6
3 changed files with 57 additions and 17 deletions

View File

@@ -1,30 +1,41 @@
/// TODO /// TODO
const { testWrapper } = require('../tools'); const { testWrapper } = require('../tools');
const _ = require('lodash');
const engines = require('../engines'); const engines = require('../engines');
const deployDb = require('dbgate-api/src/shell/deployDb'); const deployDb = require('dbgate-api/src/shell/deployDb');
const { databaseInfoFromYamlModel } = require('dbgate-tools');
async function testDatabaseDeploy(conn, driver, dbModelYaml, checkDb) { function checkStructure(structure, model) {
await deployDb({ const expected = databaseInfoFromYamlModel(model);
systemConnection: conn, expect(structure.tables.length).toEqual(expected.tables.length);
driver,
loadedDbModel: dbModelYaml, for (const [realTable, expectedTable] of _.zip(structure.tables, expected.tables)) {
}); expect(realTable.columns.length).toEqual(expectedTable.columns.length);
}
}
async function testDatabaseDeploy(conn, driver, dbModelsYaml) {
for (const loadedDbModel of dbModelsYaml) {
await deployDb({
systemConnection: conn,
driver,
loadedDbModel,
});
}
const structure = await driver.analyseFull(conn); const structure = await driver.analyseFull(conn);
checkDb(structure); checkStructure(structure, dbModelsYaml[dbModelsYaml.length - 1]);
} }
describe('Deploy database', () => { describe('Deploy database', () => {
test.each(engines.map(engine => [engine.label, engine]))( test.each(engines.map(engine => [engine.label, engine]))(
'Drop referenced table - %s', 'Deploy database simple - %s',
testWrapper(async (conn, driver, engine) => { testWrapper(async (conn, driver, engine) => {
await testDatabaseDeploy( await testDatabaseDeploy(conn, driver, [
conn,
driver,
[ [
{ {
name: 'tables.yaml', name: 't1.table.yaml',
json: { json: {
name: 't1', name: 't1',
columns: [{ name: 'id', type: 'int' }], columns: [{ name: 'id', type: 'int' }],
@@ -32,10 +43,35 @@ describe('Deploy database', () => {
}, },
}, },
], ],
db => { ]);
expect(db.tables.length).toEqual(1); })
} );
);
test.each(engines.map(engine => [engine.label, engine]))(
'Deploy database simple twice - %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' }],
primaryKey: ['id'],
},
},
],
]);
}) })
); );
}); });

View File

@@ -10,6 +10,7 @@ async function deployDb({ connection, systemConnection, driver, analysedStructur
modelFolder, modelFolder,
loadedDbModel, loadedDbModel,
}); });
console.log('RUNNING DEPLOY SCRIPT:', sql);
await executeQuery({ connection, systemConnection, driver, sql }); await executeQuery({ connection, systemConnection, driver, sql });
} }

View File

@@ -65,7 +65,7 @@ function columnInfoFromYaml(column: ColumnInfoYaml, table: TableInfoYaml): Colum
columnName: column.name, columnName: column.name,
dataType: column.type, dataType: column.type,
autoIncrement: column.autoIncrement, autoIncrement: column.autoIncrement,
notNull: column.notNull, notNull: column.notNull || (table.primaryKey && table.primaryKey.includes(column.name)),
}; };
return res; return res;
} }
@@ -170,5 +170,8 @@ export function databaseInfoFromYamlModel(files: DatabaseModelFile[]): DatabaseI
} }
} }
} }
model.tables = tablesYaml.map(table => tableInfoFromYaml(table, tablesYaml));
return model; return model;
} }