mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 00:56:02 +00:00
not connected deploy test
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
/// TODO
|
/// TODO
|
||||||
|
|
||||||
const { testWrapper } = require('../tools');
|
const { testWrapper, testWrapperPrepareOnly } = require('../tools');
|
||||||
const _ = require('lodash');
|
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');
|
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');
|
||||||
|
|
||||||
function checkStructure(structure, model) {
|
function checkStructure(structure, model) {
|
||||||
const expected = databaseInfoFromYamlModel(model);
|
const expected = databaseInfoFromYamlModel(model);
|
||||||
@@ -23,7 +24,8 @@ async function testDatabaseDeploy(conn, driver, dbModelsYaml, testEmptyLastScrip
|
|||||||
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({
|
||||||
systemConnection: conn,
|
systemConnection: conn.isPreparedOnly ? undefined : conn,
|
||||||
|
connection: conn.isPreparedOnly ? conn : undefined,
|
||||||
driver,
|
driver,
|
||||||
loadedDbModel,
|
loadedDbModel,
|
||||||
});
|
});
|
||||||
@@ -36,7 +38,8 @@ async function testDatabaseDeploy(conn, driver, dbModelsYaml, testEmptyLastScrip
|
|||||||
}
|
}
|
||||||
|
|
||||||
await deployDb({
|
await deployDb({
|
||||||
systemConnection: conn,
|
systemConnection: conn.isPreparedOnly ? undefined : conn,
|
||||||
|
connection: conn.isPreparedOnly ? conn : undefined,
|
||||||
driver,
|
driver,
|
||||||
loadedDbModel,
|
loadedDbModel,
|
||||||
});
|
});
|
||||||
@@ -44,7 +47,9 @@ async function testDatabaseDeploy(conn, driver, dbModelsYaml, testEmptyLastScrip
|
|||||||
index++;
|
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]);
|
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]))(
|
test.each(engines.map(engine => [engine.label, engine]))(
|
||||||
'Deploy database simple twice - %s',
|
'Deploy database simple twice - %s',
|
||||||
testWrapper(async (conn, driver, engine) => {
|
testWrapper(async (conn, driver, engine) => {
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ const engines = [
|
|||||||
const filterLocal = [
|
const filterLocal = [
|
||||||
// filter local testing
|
// filter local testing
|
||||||
'-MySQL',
|
'-MySQL',
|
||||||
'MariaDB',
|
'-MariaDB',
|
||||||
'PostgreSQL',
|
'PostgreSQL',
|
||||||
'-SQL Server',
|
'-SQL Server',
|
||||||
'-SQLite',
|
'-SQLite',
|
||||||
|
|||||||
@@ -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 =
|
const testWrapper =
|
||||||
body =>
|
body =>
|
||||||
async (label, ...other) => {
|
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 = {
|
module.exports = {
|
||||||
randomDbName,
|
randomDbName,
|
||||||
connect,
|
connect,
|
||||||
extractConnection,
|
extractConnection,
|
||||||
testWrapper,
|
testWrapper,
|
||||||
|
testWrapperPrepareOnly,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -8,10 +8,14 @@ async function executeQuery({ connection = undefined, systemConnection = undefin
|
|||||||
logger.info({ sql }, `Execute query`);
|
logger.info({ sql }, `Execute query`);
|
||||||
|
|
||||||
if (!driver) driver = requireEngineDriver(connection);
|
if (!driver) driver = requireEngineDriver(connection);
|
||||||
const pool = systemConnection || (await connectUtility(driver, connection, 'script'));
|
const dbhan = systemConnection || (await connectUtility(driver, connection, 'script'));
|
||||||
logger.info(`Connected.`);
|
logger.info(`Connected.`);
|
||||||
|
|
||||||
await driver.script(pool, sql);
|
await driver.script(dbhan, sql);
|
||||||
|
|
||||||
|
if (!systemConnection) {
|
||||||
|
await driver.close(dbhan);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = executeQuery;
|
module.exports = executeQuery;
|
||||||
|
|||||||
@@ -21,9 +21,9 @@ async function generateDeploySql({
|
|||||||
}) {
|
}) {
|
||||||
if (!driver) driver = requireEngineDriver(connection);
|
if (!driver) driver = requireEngineDriver(connection);
|
||||||
|
|
||||||
const pool = systemConnection || (await connectUtility(driver, connection, 'read'));
|
const dbhan = systemConnection || (await connectUtility(driver, connection, 'read'));
|
||||||
if (!analysedStructure) {
|
if (!analysedStructure) {
|
||||||
analysedStructure = await driver.analyseFull(pool);
|
analysedStructure = await driver.analyseFull(dbhan);
|
||||||
}
|
}
|
||||||
|
|
||||||
const deployedModel = generateDbPairingId(
|
const deployedModel = generateDbPairingId(
|
||||||
@@ -41,7 +41,7 @@ async function generateDeploySql({
|
|||||||
noRenameColumn: true,
|
noRenameColumn: true,
|
||||||
};
|
};
|
||||||
const currentModelPaired = matchPairedObjects(deployedModel, currentModel, opts);
|
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('currentModelPairedPreloaded', currentModelPairedPreloaded.tables[0]);
|
||||||
// console.log('deployedModel', deployedModel.tables[0]);
|
// console.log('deployedModel', deployedModel.tables[0]);
|
||||||
@@ -55,6 +55,10 @@ async function generateDeploySql({
|
|||||||
deployedModel,
|
deployedModel,
|
||||||
driver
|
driver
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!systemConnection) {
|
||||||
|
await driver.close(dbhan);
|
||||||
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user