not connected deploy test

This commit is contained in:
Jan Prochazka 2024-10-01 10:37:05 +02:00
parent 87aa60bc3e
commit 3ec7f651c1
5 changed files with 74 additions and 10 deletions

View File

@ -1,11 +1,12 @@
/// TODO
const { testWrapper } = require('../tools');
const { testWrapper, testWrapperPrepareOnly } = require('../tools');
const _ = require('lodash');
const engines = require('../engines');
const deployDb = require('dbgate-api/src/shell/deployDb');
const { databaseInfoFromYamlModel } = require('dbgate-tools');
const generateDeploySql = require('dbgate-api/src/shell/generateDeploySql');
const connectUtility = require('dbgate-api/src/utility/connectUtility');
function checkStructure(structure, model) {
const expected = databaseInfoFromYamlModel(model);
@ -23,7 +24,8 @@ async function testDatabaseDeploy(conn, driver, dbModelsYaml, testEmptyLastScrip
let index = 0;
for (const loadedDbModel of dbModelsYaml) {
const { sql, isEmpty } = await generateDeploySql({
systemConnection: conn,
systemConnection: conn.isPreparedOnly ? undefined : conn,
connection: conn.isPreparedOnly ? conn : undefined,
driver,
loadedDbModel,
});
@ -36,7 +38,8 @@ async function testDatabaseDeploy(conn, driver, dbModelsYaml, testEmptyLastScrip
}
await deployDb({
systemConnection: conn,
systemConnection: conn.isPreparedOnly ? undefined : conn,
connection: conn.isPreparedOnly ? conn : undefined,
driver,
loadedDbModel,
});
@ -44,7 +47,9 @@ async function testDatabaseDeploy(conn, driver, dbModelsYaml, testEmptyLastScrip
index++;
}
const structure = await driver.analyseFull(conn);
const dbhan = conn.isPreparedOnly ? await connectUtility(driver, conn, 'read') : conn;
const structure = await driver.analyseFull(dbhan);
if (conn.isPreparedOnly) await driver.close(dbhan);
checkStructure(structure, dbModelsYaml[dbModelsYaml.length - 1]);
}
@ -67,6 +72,24 @@ describe('Deploy database', () => {
})
);
test.each(engines.map(engine => [engine.label, engine]))(
'Deploy database simple - %s - not connected',
testWrapperPrepareOnly(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',
testWrapper(async (conn, driver, engine) => {

View File

@ -185,7 +185,7 @@ const engines = [
const filterLocal = [
// filter local testing
'-MySQL',
'MariaDB',
'-MariaDB',
'PostgreSQL',
'-SQL Server',
'-SQLite',

View File

@ -43,6 +43,29 @@ async function connect(engine, database) {
}
}
async function prepareConnection(engine, database) {
const connection = extractConnection(engine);
const driver = requireEngineDriver(connection);
if (engine.generateDbFile) {
return {
...connection,
databaseFile: `dbtemp/${database}`,
isPreparedOnly: true,
};
} else {
const conn = await driver.connect(connection);
await driver.query(conn, `CREATE DATABASE ${database}`);
await driver.close(conn);
return {
...connection,
database,
isPreparedOnly: true,
};
}
}
const testWrapper =
body =>
async (label, ...other) => {
@ -56,9 +79,19 @@ const testWrapper =
}
};
const testWrapperPrepareOnly =
body =>
async (label, ...other) => {
const engine = other[other.length - 1];
const driver = requireEngineDriver(engine.connection);
const conn = await prepareConnection(engine, randomDbName());
await body(conn, driver, ...other);
};
module.exports = {
randomDbName,
connect,
extractConnection,
testWrapper,
testWrapperPrepareOnly,
};

View File

@ -8,10 +8,14 @@ async function executeQuery({ connection = undefined, systemConnection = undefin
logger.info({ sql }, `Execute query`);
if (!driver) driver = requireEngineDriver(connection);
const pool = systemConnection || (await connectUtility(driver, connection, 'script'));
const dbhan = systemConnection || (await connectUtility(driver, connection, 'script'));
logger.info(`Connected.`);
await driver.script(pool, sql);
await driver.script(dbhan, sql);
if (!systemConnection) {
await driver.close(dbhan);
}
}
module.exports = executeQuery;

View File

@ -21,9 +21,9 @@ async function generateDeploySql({
}) {
if (!driver) driver = requireEngineDriver(connection);
const pool = systemConnection || (await connectUtility(driver, connection, 'read'));
const dbhan = systemConnection || (await connectUtility(driver, connection, 'read'));
if (!analysedStructure) {
analysedStructure = await driver.analyseFull(pool);
analysedStructure = await driver.analyseFull(dbhan);
}
const deployedModel = generateDbPairingId(
@ -41,7 +41,7 @@ async function generateDeploySql({
noRenameColumn: true,
};
const currentModelPaired = matchPairedObjects(deployedModel, currentModel, opts);
const currentModelPairedPreloaded = await enrichWithPreloadedRows(deployedModel, currentModelPaired, pool, driver);
const currentModelPairedPreloaded = await enrichWithPreloadedRows(deployedModel, currentModelPaired, dbhan, driver);
// console.log('currentModelPairedPreloaded', currentModelPairedPreloaded.tables[0]);
// console.log('deployedModel', deployedModel.tables[0]);
@ -55,6 +55,10 @@ async function generateDeploySql({
deployedModel,
driver
);
if (!systemConnection) {
await driver.close(dbhan);
}
return res;
}