ToolJet/server/scripts/create-database.ts

120 lines
3.4 KiB
TypeScript
Raw Normal View History

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';
async function createDatabaseFromFile(envPath: string): Promise<void> {
const result = dotenv.config({ path: envPath });
Merge main to develop (#4049) * Fix: User group permissions error on Openshift platform (#4041) * update dockerfile for file permissions on root group * add permissions from the user group on dockerfile * bump to v1.24.4 * bump to v1.25.0 * [feature] Added pagination and filtering features to users page (#3921) * added pagination and filtering in backend * added pagination - created a seperate component for users table - added pagination * Added filter UI * temporary css fix for pagination footer * fixed pagination width issue * now result will also clear when user clicks on clear icon * Added seperate api for comment mentions * Now we can search mentions by email, first and last names * Fixed a bug - email didn't send for comment mentions * refactoring the code * resolved PR changes * Added isAdmin guard * adding some checks * fixed lint errors * added wild card search * Added no result found text * fixed failing test case * Working on PR changes * Now users table avatars will load image too * replaced skeleton classes with skeleton library component * Completed PR changes * added orderby * Fixed some issues * fixed failed test case * have fixed some css issues * replaced query with quersrting package * fixed minor width issue * Fixed some css issues * fixed darkMode issue * implemented on enter press search * Refactored the code * fixed white space issue * refactored the code * fixed overlapping issue * refactored the code * fixing some issues * fixes * removed guard * code cleanup * comments notification fix * fixed conflict issues * fixed css height issue Co-authored-by: gsmithun4 <gsmithun4@gmail.com> * Remove signup guard from set-password-from-token API (#4050) * Remove sign up guard set-password-from-token API * test cases fix * Bump to v1.25.1 * Feature: Add PG_DB_OWNER env var to disable db and extension creation (#4055) * add PG_DB_OWNER env var to disable db and extension creation * update docs * bump to v1.25.2 Co-authored-by: Akshay <akshaysasidharan93@gmail.com> Co-authored-by: Muhsin Shah C P <muhsinshah21@gmail.com>
2022-09-16 15:38:45 +00:00
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);
if (process.env.ENABLE_TOOLJET_DB === 'true') {
await createTooljetDb(envVars, envVars.TOOLJET_DB);
}
}
}
function checkCommandAvailable(command: string) {
try {
const options = { env: process.env } as ExecFileSyncOptions;
execFileSync('which', [command], options);
} catch (error) {
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`)) {
2023-01-09 18:08:39 +00:00
console.log(`Using Application database\nPG_DB: ${dbName}\nPG_HOST: ${envVars.PG_HOST}\n`);
} else {
throw error;
2023-01-09 18:08:39 +00:00
}
}
2023-01-09 18:08:39 +00:00
}
async function createTooljetDb(envVars, dbName): Promise<void> {
if (isEmpty(dbName)) {
throw new Error('Database name cannot be empty');
}
2023-01-09 18:08:39 +00:00
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`)) {
2023-01-09 18:08:39 +00:00
console.log(`Using Tooljet database\nTOOLJET_DB: ${dbName}\nTOOLJET_DB_HOST: ${envVars.TOOLJET_DB_HOST}\n`);
} 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);
}