ToolJet/server/scripts/drop-database.ts
Midhun G S 0c5ab3484c
Platform LTS Final fixes (#13221)
* Cloud Blocker bugfixes (#13160)

* fix

* minor email fixes

* settings menu fix

* fixes

* Bugfixes/whitelabelling apis (#13180)

* white-labelling apis

* removed consoles logs

* reverts

* fixes for white-labelling

* fixes

* reverted breadcrumb changes (#13194)

* fixes for getting public sso configurations

* fix for enable signup on cloud

* Cloud Trial and Banners (#13182)

* Cloud Blocker bugfixes (#13160)

* fix

* minor email fixes

* settings menu fix

* fixes

* Cloud Trial and Banners

* revert

* initial commit

* Added website onboarding APIs

* moved ai onboarding controller to auth module

* ee banners

* fix

---------

Co-authored-by: Rohan Lahori <64496391+rohanlahori@users.noreply.github.com>
Co-authored-by: gsmithun4 <gsmithun4@gmail.com>

* Bugfixes/minor UI fixes-CLoud (#13203)

* Bugfixes/UI bugs platform 1 (#13205)

* cleanup

* Audit logs fix

* gitignore changes

* postgrest configs removed

* removed unused import

* improvements

* fix

* improved startup logs

* Platform cypress fix (#13192)

* Cloud Blocker bugfixes (#13160)

* fix

* minor email fixes

* settings menu fix

* fixes

* Bugfixes/whitelabelling apis (#13180)

* white-labelling apis

* removed consoles logs

* reverts

* fixes for white-labelling

* fixes

* Cypress fix

* reverted breadcrumb changes (#13194)

* cypress fix

* title fix

* fixes for getting public sso configurations

---------

Co-authored-by: Rohan Lahori <64496391+rohanlahori@users.noreply.github.com>
Co-authored-by: gsmithun4 <gsmithun4@gmail.com>

* deployment fix

* added interfaces and permissions

* Bugfixes/lts 3.6 branch 1 platform (#13238)

* fix

* Licensing Banners Fixes Cloud and EE (#13241)

* design: Adds license buttons to header

* Refactor header actions

* Cloud Blocker bugfixes (#13160)

* fix

* minor email fixes

* settings menu fix

* fixes

* subscription page

* fix banners

---------

Co-authored-by: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com>
Co-authored-by: Rohan Lahori <64496391+rohanlahori@users.noreply.github.com>

* fix for public apps

* fix

* CE Instance Signup bug (#13254)

* CE Instance Signup bug

* improvement

* fix

* Add WEBSITE_SIGNUP_URL to deployment environment variables

* Add WEBSITE_SIGNUP_URL to environment variables for deployment

* Super admin banner fix (#13262)

* Git Sync Fixes  (#13249)

* git-sync module changes

* git sync fixes

* added app resource guard

* git-sync fixes

* removed require feature

* fix

* review comment changes

* ypress fix

* App logo fix inside app builder

* fix for subpath cache

* fix (#13274)

* platform-cypress-fix (#13271)

* git sync fixes (#13277)

* fix

* Add data-cy for new components (#13289)

---------

Co-authored-by: Rohan Lahori <64496391+rohanlahori@users.noreply.github.com>
Co-authored-by: Rudhra Deep Biswas <98055396+rudeUltra@users.noreply.github.com>
Co-authored-by: Ajith KV <ajith.jaban@gmail.com>
Co-authored-by: Nithin David Thomas <1277421+nithindavid@users.noreply.github.com>
Co-authored-by: rohanlahori <rohanlahori99@gmail.com>
Co-authored-by: Adish M <adish.madhu@gmail.com>
Co-authored-by: Rudra deep Biswas <rudra21ultra@gmail.com>
2025-07-09 22:36:41 +05:30

85 lines
2.5 KiB
TypeScript

import * as path from 'path';
import * as dotenv from 'dotenv';
import * as fs from 'fs';
import { ExecFileSyncOptions, exec, execFileSync } from 'child_process';
import { buildAndValidateDatabaseConfig } from './database-config-utils';
function dropDatabaseFromFile(envPath: string): void {
const result = dotenv.config({ path: envPath });
if (result.error) {
throw result.error;
}
dropDatabase();
}
function dropDatabase(): void {
const { value: envVars, error } = buildAndValidateDatabaseConfig();
if (error) {
throw new Error(`Config validation error: ${error.message}`);
}
const connectivityCheck = exec('command -v createdb');
connectivityCheck.on('exit', function (signal) {
if (signal === 1) {
console.error('Unable to connect to database');
process.exit(1);
}
});
// Allow dropping db based on cmd line arg
const dbNameFromArg = process.argv[2];
if (dbNameFromArg) return dropDb(envVars, dbNameFromArg);
dropDb(envVars, envVars.PG_DB);
}
function checkCommandAvailable(command: string) {
try {
const options = { env: Object.assign({}, process.env) } as ExecFileSyncOptions;
execFileSync('which', [command], options);
} catch {
throw `Error: ${command} not found. Make sure it's installed and available in the system's PATH.`;
}
}
function dropDb(envVars, dbName) {
const env = Object.assign({}, process.env, { PGPASSWORD: envVars.PG_PASS });
const dropDbArgs = ['-h', envVars.PG_HOST, '-p', envVars.PG_PORT, '-U', envVars.PG_USER, dbName];
const options = { env, stdio: 'pipe' } as ExecFileSyncOptions;
try {
execFileSync('dropdb', dropDbArgs, options);
console.log(`Dropped database ${dbName}`);
} catch (error) {
const errorMessage = `database "${dbName}" does not exist`;
if (error.message.includes(errorMessage)) {
console.log(errorMessage);
} else {
console.error(errorMessage);
process.exit(1);
}
}
}
try {
checkCommandAvailable('dropdb');
const nodeEnvPath = path.resolve(process.cwd(), process.env.NODE_ENV === 'test' ? '../.env.test' : '../.env');
const fallbackPath = path.resolve(process.cwd(), '../.env');
if (fs.existsSync(nodeEnvPath)) {
dropDatabaseFromFile(nodeEnvPath);
} else if (fs.existsSync(fallbackPath)) {
dropDatabaseFromFile(fallbackPath);
} else {
console.log(`${nodeEnvPath} file not found to drop database\n` + 'Picking up config from the environment');
dropDatabase();
}
} catch (error) {
console.error(error);
process.exit(1);
}