diff --git a/integration-tests/__tests__/deploy-database.spec.js b/integration-tests/__tests__/deploy-database.spec.js index bcbe4e74..139fb7f7 100644 --- a/integration-tests/__tests__/deploy-database.spec.js +++ b/integration-tests/__tests__/deploy-database.spec.js @@ -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) => { diff --git a/integration-tests/engines.js b/integration-tests/engines.js index 96625c7b..7e08fb6a 100644 --- a/integration-tests/engines.js +++ b/integration-tests/engines.js @@ -185,7 +185,7 @@ const engines = [ const filterLocal = [ // filter local testing '-MySQL', - 'MariaDB', + '-MariaDB', 'PostgreSQL', '-SQL Server', '-SQLite', diff --git a/integration-tests/tools.js b/integration-tests/tools.js index 4ab8c20d..2f912902 100644 --- a/integration-tests/tools.js +++ b/integration-tests/tools.js @@ -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, }; diff --git a/packages/api/src/shell/executeQuery.js b/packages/api/src/shell/executeQuery.js index a13361bd..9f594c38 100644 --- a/packages/api/src/shell/executeQuery.js +++ b/packages/api/src/shell/executeQuery.js @@ -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; diff --git a/packages/api/src/shell/generateDeploySql.js b/packages/api/src/shell/generateDeploySql.js index 7cdd1b5a..af780fce 100644 --- a/packages/api/src/shell/generateDeploySql.js +++ b/packages/api/src/shell/generateDeploySql.js @@ -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; }