diff --git a/packages/api/src/shell/dropAllDbObjects.js b/packages/api/src/shell/dropAllDbObjects.js new file mode 100644 index 00000000..fdc3001c --- /dev/null +++ b/packages/api/src/shell/dropAllDbObjects.js @@ -0,0 +1,42 @@ +const executeQuery = require('./executeQuery'); +const requireEngineDriver = require('../utility/requireEngineDriver'); +const connectUtility = require('../utility/connectUtility'); +const { getLogger, extendDatabaseInfo } = require('dbgate-tools'); + +const logger = getLogger('dropAllDbObjects'); + +async function dropAllDbObjects({ connection, systemConnection, driver, analysedStructure }) { + if (!driver) driver = requireEngineDriver(connection); + + const dbhan = systemConnection || (await connectUtility(driver, connection, 'write')); + + logger.info(`Connected.`); + + if (!analysedStructure) { + analysedStructure = await driver.analyseFull(dbhan); + } + + analysedStructure = extendDatabaseInfo(analysedStructure); + + const dmp = driver.createDumper(); + + for (const table of analysedStructure.tables) { + for (const fk of table.foreignKeys) { + dmp.dropForeignKey(fk); + } + } + for (const table of analysedStructure.tables) { + dmp.dropTable(table); + } + for (const field of Object.keys(analysedStructure)) { + if (dmp.getSqlObjectSqlName(field)) { + for (const obj of analysedStructure[field]) { + dmp.dropSqlObject(obj); + } + } + } + + await executeQuery({ connection, systemConnection, driver, sql: dmp.s, logScriptItems: true }); +} + +module.exports = dropAllDbObjects; diff --git a/packages/api/src/shell/index.js b/packages/api/src/shell/index.js index dfbe508b..54b9da1a 100644 --- a/packages/api/src/shell/index.js +++ b/packages/api/src/shell/index.js @@ -34,6 +34,7 @@ const dataTypeMapperTransform = require('./dataTypeMapperTransform'); const sqlTextReplacementTransform = require('./sqlTextReplacementTransform'); const autoIndexForeignKeysTransform = require('./autoIndexForeignKeysTransform'); const generateDeploySql = require('./generateDeploySql'); +const dropAllDbObjects = require('./dropAllDbObjects'); const dbgateApi = { queryReader, @@ -71,6 +72,7 @@ const dbgateApi = { sqlTextReplacementTransform, autoIndexForeignKeysTransform, generateDeploySql, + dropAllDbObjects, }; requirePlugin.initializeDbgateApi(dbgateApi);