From 032356b9a92854647e6ad53d2a7b6e4c8d9b08b5 Mon Sep 17 00:00:00 2001 From: Abhijeet Date: Thu, 14 Nov 2024 18:35:20 +0530 Subject: [PATCH] chore: Minor refactors --- .../server/configurations/CommonDBConfig.java | 2 +- .../docker/fs/opt/appsmith/utils/bin/backup.js | 4 +++- .../fs/opt/appsmith/utils/bin/backup.test.js | 14 ++++++++++++++ .../fs/opt/appsmith/utils/bin/export_db.js | 4 ++-- .../fs/opt/appsmith/utils/bin/import_db.js | 17 +++++++++++------ .../docker/fs/opt/appsmith/utils/bin/restore.js | 6 +++++- .../docker/fs/opt/appsmith/utils/bin/utils.js | 6 ++++++ 7 files changed, 42 insertions(+), 11 deletions(-) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/CommonDBConfig.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/CommonDBConfig.java index 3121170140f..417cb3de2bd 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/CommonDBConfig.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/CommonDBConfig.java @@ -33,7 +33,7 @@ public MongoProperties configureMongoDB() { if (!appsmithDbUrl.startsWith("mongodb")) { return null; } - log.info("Found MongoDB uri configuring now."); + log.info("Found MongoDB uri configuring now"); MongoProperties mongoProperties = new MongoProperties(); mongoProperties.setUri(appsmithDbUrl); return mongoProperties; diff --git a/deploy/docker/fs/opt/appsmith/utils/bin/backup.js b/deploy/docker/fs/opt/appsmith/utils/bin/backup.js index b9738cbf7ed..8689c43d154 100644 --- a/deploy/docker/fs/opt/appsmith/utils/bin/backup.js +++ b/deploy/docker/fs/opt/appsmith/utils/bin/backup.js @@ -131,6 +131,8 @@ async function exportDatabase(destFolder) { await executeMongoDumpCMD(destFolder, dbUrl); } else if (dbUrl.startsWith('postgresql')) { await executePostgresDumpCMD(destFolder, dbUrl); + } else { + throw new Error(`Unsupported database type in URL: ${dbUrl}`); } console.log('Exporting database done.'); } @@ -168,7 +170,7 @@ async function executeMongoDumpCMD(destFolder, dbUrl) { } async function executePostgresDumpCMD(destFolder, dbUrl) { - return await utils.execCommand(['pg_dump', dbUrl, '--verbose', '-n', 'appsmith','-Fc', '-f', destFolder + '/pg-data.archive']); + return await utils.execCommand(['pg_dump', dbUrl, '-n', 'appsmith','-Fc', '-f', destFolder + '/pg-data.archive']); } async function createFinalArchive(destFolder, timestamp) { diff --git a/deploy/docker/fs/opt/appsmith/utils/bin/backup.test.js b/deploy/docker/fs/opt/appsmith/utils/bin/backup.test.js index adb6700de35..a7f72ee8396 100644 --- a/deploy/docker/fs/opt/appsmith/utils/bin/backup.test.js +++ b/deploy/docker/fs/opt/appsmith/utils/bin/backup.test.js @@ -264,3 +264,17 @@ test('Get DB name from Mongo URI 4', async () => { expect(dbName).toEqual(expectedDBName) }) +test('Get DB name from PostgreSQL URI', async () => { + var pg_uri = "postgresql://user:password@host:5432/postgres_db" + var expectedDBName = 'postgres_db' + const dbName = utils.getDatabaseNameFromDBURI(pg_uri) + expect(dbName).toEqual(expectedDBName) +}) + +test('Get DB name from PostgreSQL URI with query params', async () => { + var pg_uri = "postgresql://user:password@host:5432/postgres_db?sslmode=disable" + var expectedDBName = 'postgres_db' + const dbName = utils.getDatabaseNameFromDBURI(pg_uri) + expect(dbName).toEqual(expectedDBName) +}) + diff --git a/deploy/docker/fs/opt/appsmith/utils/bin/export_db.js b/deploy/docker/fs/opt/appsmith/utils/bin/export_db.js index d561abf2258..602f6b958b3 100644 --- a/deploy/docker/fs/opt/appsmith/utils/bin/export_db.js +++ b/deploy/docker/fs/opt/appsmith/utils/bin/export_db.js @@ -12,9 +12,9 @@ function export_database() { cmd = `mongodump --uri='${dbUrl}' --archive='${Constants.BACKUP_PATH}/${Constants.DUMP_FILE_NAME}' --gzip`; } else if (dbUrl.startsWith('postgresql')) { // Dump only the appsmith schema with custom format - cmd = `pg_dump ${dbUrl} --verbose -n appsmith -Fc -f '${Constants.BACKUP_PATH}/${Constants.DUMP_FILE_NAME}'`; + cmd = `pg_dump ${dbUrl} -n appsmith -Fc -f '${Constants.BACKUP_PATH}/${Constants.DUMP_FILE_NAME}'`; } else { - throw new Error('Unsupported database URL'); + throw new Error('Unsupported database type, only MongoDB and PostgreSQL are supported'); } shell.exec(cmd); console.log('export_database done'); diff --git a/deploy/docker/fs/opt/appsmith/utils/bin/import_db.js b/deploy/docker/fs/opt/appsmith/utils/bin/import_db.js index 78a248a80b7..f4c5f015707 100644 --- a/deploy/docker/fs/opt/appsmith/utils/bin/import_db.js +++ b/deploy/docker/fs/opt/appsmith/utils/bin/import_db.js @@ -9,10 +9,12 @@ const utils = require('./utils'); function import_database() { console.log('import_database ....') dbUrl = utils.getDburl(); - if (utils.getDburl().startsWith('mongodb')) { + if (dbUrl.startsWith('mongodb')) { restore_mongo_db(dbUrl); - } else if (utils.getDburl().startsWith('postgresql')) { + } else if (dbUrl.startsWith('postgresql')) { restore_postgres_db(dbUrl); + } else { + throw new Error('Unsupported database type, only MongoDB and PostgreSQL are supported'); } console.log('import_database done') } @@ -43,10 +45,13 @@ function start_application() { function get_table_or_collection_len() { let count; - if (utils.getDburl().startsWith('mongodb')) { - count = shell.exec(`mongo ${utils.getDburl()} --quiet --eval "db.getCollectionNames().length"`) - } else if (utils.getDburl().startsWith('postgresql')) { - count = shell.exec(`psql -d ${utils.getDburl()} -t -c "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'appsmith';"`) + const dbUrl = utils.getDburl(); + if (dbUrl.startsWith('mongodb')) { + count = shell.exec(`mongo ${dbUrl} --quiet --eval "db.getCollectionNames().length"`) + } else if (dbUrl.startsWith('postgresql')) { + count = shell.exec(`psql -d ${dbUrl} -t -c "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'appsmith';"`) + } else { + throw new Error('Unsupported database type, only MongoDB and PostgreSQL are supported'); } return parseInt(count.stdout.toString().trimEnd()); } diff --git a/deploy/docker/fs/opt/appsmith/utils/bin/restore.js b/deploy/docker/fs/opt/appsmith/utils/bin/restore.js index 12f1c2821bd..59ccb234da7 100644 --- a/deploy/docker/fs/opt/appsmith/utils/bin/restore.js +++ b/deploy/docker/fs/opt/appsmith/utils/bin/restore.js @@ -63,6 +63,8 @@ async function restoreDatabase(restoreContentsPath, dbUrl) { await restore_mongo_db(restoreContentsPath, dbUrl); } else if (dbUrl.includes('postgresql')) { await restore_postgres_db(restoreContentsPath, dbUrl); + } else { + throw new Error('Unsupported database type, only MongoDB and PostgreSQL are supported'); } console.log('Restoring database completed'); } @@ -82,7 +84,9 @@ async function restore_mongo_db(restoreContentsPath, dbUrl) { async function restore_postgres_db(restoreContentsPath, dbUrl) { const cmd = ['pg_restore', '--verbose', '--clean', `${restoreContentsPath}/pg-data.archive`]; - if (dbUrl.includes('localhost') || dbUrl.includes('127.0.0.1')) { + const url = new URL(dbUrl); + const isLocalhost = ['localhost', '127.0.0.1'].includes(url.hostname); + if (isLocalhost) { let dbName; try { dbName = utils.getDatabaseNameFromDBURI(dbUrl); diff --git a/deploy/docker/fs/opt/appsmith/utils/bin/utils.js b/deploy/docker/fs/opt/appsmith/utils/bin/utils.js index 183d0800e65..98522cac9bf 100644 --- a/deploy/docker/fs/opt/appsmith/utils/bin/utils.js +++ b/deploy/docker/fs/opt/appsmith/utils/bin/utils.js @@ -179,6 +179,12 @@ function execCommandSilent(cmd, options) { }); } +/** + * Extracts database name from MongoDB or PostgreSQL connection URI + * @param {string} uri - Database connection URI + * @returns {string} Database name + * @returns + */ function getDatabaseNameFromDBURI(uri) { const uriParts = uri.split("/"); return uriParts[uriParts.length - 1].split("?")[0];