diff --git a/integration-tests/__tests__/db-import.spec.js b/integration-tests/__tests__/db-import.spec.js new file mode 100644 index 000000000..e972a15e8 --- /dev/null +++ b/integration-tests/__tests__/db-import.spec.js @@ -0,0 +1,43 @@ +const engines = require('../engines'); +const stream = require('stream'); +const { testWrapper } = require('../tools'); +const tableWriter = require('dbgate-api/src/shell/tableWriter'); +const copyStream = require('dbgate-api/src/shell/copyStream'); +const fakeObjectReader = require('dbgate-api/src/shell/fakeObjectReader'); + +function createImportStream() { + const pass = new stream.PassThrough({ + objectMode: true, + }); + pass.write({ columns: [{ columnName: 'id' }, { columnName: 'country' }], __isStreamHeader: true }); + pass.write({ id: 1, country: 'Czechia' }); + pass.write({ id: 2, country: 'Austria' }); + pass.write({ country: 'Germany', id: 3 }); + pass.write({ country: 'Romania', id: 4 }); + pass.write({ country: 'Great Britain', id: 5 }); + pass.write({ country: 'Bosna, Hecegovina', id: 6 }); + pass.end(); + + return pass; +} + +describe('DB Import', () => { + test.each(engines.map(engine => [engine.label, engine]))( + 'Import one table - %s', + testWrapper(async (conn, driver, engine) => { + // const reader = await fakeObjectReader({ delay: 10 }); + // const reader = await fakeObjectReader(); + const reader = createImportStream(); + const writer = await tableWriter({ + systemConnection: conn, + driver, + pureName: 't1', + createIfNotExists: true, + }); + await copyStream(reader, writer); + + const res = await driver.query(conn, `select count(*) as cnt from t1`); + expect(res.rows[0].cnt.toString()).toEqual('6'); + }) + ); +}); diff --git a/integration-tests/engines.js b/integration-tests/engines.js index cb0401c71..b11d40cb2 100644 --- a/integration-tests/engines.js +++ b/integration-tests/engines.js @@ -116,11 +116,11 @@ const engines = [ const filterLocal = [ // filter local testing - '-MySQL', + 'MySQL', 'PostgreSQL', - '-SQL Server', - '-SQLite', - '-CockroachDB', + 'SQL Server', + 'SQLite', + 'CockroachDB', ]; module.exports = process.env.CITEST diff --git a/packages/api/src/shell/fakeObjectReader.js b/packages/api/src/shell/fakeObjectReader.js index a5e19ca44..18e2d70a4 100644 --- a/packages/api/src/shell/fakeObjectReader.js +++ b/packages/api/src/shell/fakeObjectReader.js @@ -5,7 +5,7 @@ async function fakeObjectReader({ delay = 0 } = {}) { objectMode: true, }); function doWrite() { - pass.write({ columns: [{ columnName: 'id' }, { columnName: 'country' }] }); + pass.write({ columns: [{ columnName: 'id' }, { columnName: 'country' }], __isStreamHeader: true }); pass.write({ id: 1, country: 'Czechia' }); pass.write({ id: 2, country: 'Austria' }); pass.write({ country: 'Germany', id: 3 }); diff --git a/packages/api/src/shell/tableWriter.js b/packages/api/src/shell/tableWriter.js index f4eff0e40..aa9639da2 100644 --- a/packages/api/src/shell/tableWriter.js +++ b/packages/api/src/shell/tableWriter.js @@ -1,13 +1,15 @@ const { fullNameToString } = require('dbgate-tools'); const requireEngineDriver = require('../utility/requireEngineDriver'); -const { decryptConnection } = require('../utility/crypting'); const connectUtility = require('../utility/connectUtility'); -async function tableWriter({ connection, schemaName, pureName, ...options }) { +async function tableWriter({ connection, schemaName, pureName, driver, systemConnection, ...options }) { console.log(`Writing table ${fullNameToString({ schemaName, pureName })}`); - const driver = requireEngineDriver(connection); - const pool = await connectUtility(driver, connection); + if (!driver) { + driver = requireEngineDriver(connection); + } + const pool = systemConnection || (await connectUtility(driver, connection)); + console.log(`Connected.`); return await driver.writeTable(pool, { schemaName, pureName }, options); } diff --git a/plugins/dbgate-plugin-postgres/src/backend/Analyser.js b/plugins/dbgate-plugin-postgres/src/backend/Analyser.js index 0d503a02c..647b60ccb 100644 --- a/plugins/dbgate-plugin-postgres/src/backend/Analyser.js +++ b/plugins/dbgate-plugin-postgres/src/backend/Analyser.js @@ -41,9 +41,9 @@ class Analyser extends DatabaseAnalyser { } createQuery(resFileName, typeFields) { - return super - .createQuery(sql[resFileName], typeFields) - .replace('#REFTABLECOND#', this.driver.__analyserInternals.refTableCond); + const query = super.createQuery(sql[resFileName], typeFields); + if (query) return query.replace('#REFTABLECOND#', this.driver.__analyserInternals.refTableCond); + return null; } async _computeSingleObjectId() {