Skip to content

Commit

Permalink
chore: Minor refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
abhvsn committed Nov 14, 2024
1 parent a6d9be8 commit 032356b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion deploy/docker/fs/opt/appsmith/utils/bin/backup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions deploy/docker/fs/opt/appsmith/utils/bin/backup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

4 changes: 2 additions & 2 deletions deploy/docker/fs/opt/appsmith/utils/bin/export_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
17 changes: 11 additions & 6 deletions deploy/docker/fs/opt/appsmith/utils/bin/import_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down Expand Up @@ -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());
}
Expand Down
6 changes: 5 additions & 1 deletion deploy/docker/fs/opt/appsmith/utils/bin/restore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions deploy/docker/fs/opt/appsmith/utils/bin/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 032356b

Please sign in to comment.