ToolJet/server/scripts/create-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

147 lines
4.3 KiB
TypeScript

import * as path from 'path';
import * as dotenv from 'dotenv';
import * as fs from 'fs';
import { ExecFileSyncOptions, execFileSync } from 'child_process';
import { buildAndValidateDatabaseConfig } from './database-config-utils';
import { isEmpty } from 'lodash';
import { populateSampleData } from './populate-sample-db';
async function createDatabaseFromFile(envPath: string): Promise<void> {
const result = dotenv.config({ path: envPath });
if (process.env.PG_DB_OWNER === 'false') {
console.log('Skipping database creation');
return;
}
if (result.error) {
throw result.error;
}
await createDatabase();
}
async function createDatabase(): Promise<void> {
const { value: envVars, error } = buildAndValidateDatabaseConfig();
if (error) {
throw new Error(`Config validation error: ${error.message}`);
}
if (envVars.PG_DB_OWNER === 'false') {
console.log('Skipping database creation');
return;
}
const dbNameFromArg = process.argv[2];
if (dbNameFromArg) {
await createDb(envVars, dbNameFromArg);
} else {
await createDb(envVars, envVars.PG_DB);
await createTooljetDb(envVars, envVars.TOOLJET_DB);
createSampleDb(envVars, envVars.SAMPLE_DB);
populateSampleData(envVars);
}
}
function checkCommandAvailable(command: string) {
try {
const options = { env: 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 executeCreateDb(host: string, port: string, user: string, password: string, dbName: string) {
const env = Object.assign({}, process.env, { PGPASSWORD: password });
const createDbArgs = ['-h', host, '-p', port, '-U', user, dbName];
const options = { env, stdio: 'pipe' } as ExecFileSyncOptions;
execFileSync('createdb', createDbArgs, options);
}
async function createDb(envVars, dbName): Promise<void> {
if (isEmpty(dbName)) {
throw new Error('Database name cannot be empty');
}
try {
executeCreateDb(envVars.PG_HOST, envVars.PG_PORT, envVars.PG_USER, envVars.PG_PASS, dbName);
console.log(`Created database ${dbName}\n`);
} catch (error) {
if (error.message.includes(`database "${dbName}" already exists`)) {
console.log(`Using Application database\nPG_DB: ${dbName}\nPG_HOST: ${envVars.PG_HOST}\n`);
} else {
throw error;
}
}
}
async function createTooljetDb(envVars, dbName): Promise<void> {
if (isEmpty(dbName)) {
throw new Error('Database name cannot be empty');
}
if (envVars.PG_DB === dbName)
throw new Error(`The name of the App database and the ToolJet database must not be identical.`);
try {
executeCreateDb(
envVars.TOOLJET_DB_HOST,
envVars.TOOLJET_DB_PORT,
envVars.TOOLJET_DB_USER,
envVars.TOOLJET_DB_PASS,
dbName
);
} catch (error) {
if (error.message.includes(`database "${dbName}" already exists`)) {
console.log(`Using Tooljet database\nTOOLJET_DB: ${dbName}\nTOOLJET_DB_HOST: ${envVars.TOOLJET_DB_HOST}\n`);
} else {
throw error;
}
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function createSampleDb(envVars, dbName): Promise<void> {
if (isEmpty(dbName)) {
throw new Error('Database name cannot be empty');
}
try {
executeCreateDb(
envVars.SAMPLE_PG_DB_HOST,
envVars.SAMPLE_PG_DB_PORT,
envVars.SAMPLE_PG_DB_USER,
envVars.SAMPLE_PG_DB_PASS,
dbName
);
} catch (error) {
if (error.message.includes(`database "${dbName}" already exists`)) {
console.log(
`Already present Sample database\n${dbName}\n HOST: ${envVars.SAMPLE_PG_DB_HOST}\n PORT: ${envVars.SAMPLE_PG_DB_PORT}`
);
} else {
throw error;
}
}
}
try {
checkCommandAvailable('createdb');
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)) {
createDatabaseFromFile(nodeEnvPath);
} else if (fs.existsSync(fallbackPath)) {
createDatabaseFromFile(fallbackPath);
} else {
console.log('Picking up config from the environment');
createDatabase();
}
} catch (error) {
console.error(error);
process.exit(1);
}