diff --git a/packages/server/src/tools/dbTools.ts b/packages/server/src/tools/dbTools.ts index 6b14b1222..6865c76e2 100644 --- a/packages/server/src/tools/dbTools.ts +++ b/packages/server/src/tools/dbTools.ts @@ -31,7 +31,7 @@ export async function createDb(config: DatabaseConfig, options: CreateDbOptions await dropDb(config, { ignoreIfNotExists: true }); } - await execCommand(cmd.join(' ')); + await execCommand(cmd.join(' '), { env: { PGPASSWORD: config.password } }); } else if (config.client === 'sqlite3') { const filePath = sqliteFilePath(config.name); @@ -65,7 +65,7 @@ export async function dropDb(config: DatabaseConfig, options: DropDbOptions = nu ]; try { - await execCommand(cmd.join(' ')); + await execCommand(cmd.join(' '), { env: { PGPASSWORD: config.password } }); } catch (error) { if (options.ignoreIfNotExists && error.message.includes('does not exist')) return; throw error; diff --git a/packages/server/src/utils/testing/testUtils.ts b/packages/server/src/utils/testing/testUtils.ts index 76c9837e8..b6814daeb 100644 --- a/packages/server/src/utils/testing/testUtils.ts +++ b/packages/server/src/utils/testing/testUtils.ts @@ -61,6 +61,18 @@ export async function beforeAllDb(unitName: string) { const tempDir = `${packageRootDir}/temp/test-${unitName}`; await fs.mkdirp(tempDir); + // Uncomment the code below to run the test units with Postgres. Run first + // `docker-compose -f docker-compose.db-dev.yml` to get a dev db. + + // initConfig({ + // DB_CLIENT: 'pg', + // POSTGRES_DATABASE: createdDbName_, + // POSTGRES_USER: 'joplin', + // POSTGRES_PASSWORD: 'joplin', + // }, { + // tempDir: tempDir, + // }); + initConfig({ SQLITE_DATABASE: createdDbName_, }, { diff --git a/packages/tools/tool-utils.ts b/packages/tools/tool-utils.ts index 4fb54a805..89301a610 100644 --- a/packages/tools/tool-utils.ts +++ b/packages/tools/tool-utils.ts @@ -108,11 +108,13 @@ async function saveGitHubUsernameCache(cache: any) { // Returns the project root dir export const rootDir = require('path').dirname(require('path').dirname(__dirname)); -export function execCommand(command: string) { +export function execCommand(command: string, options: any = null) { + options = options || {}; + const exec = require('child_process').exec; return new Promise((resolve, reject) => { - exec(command, (error: any, stdout: any, stderr: any) => { + exec(command, options, (error: any, stdout: any, stderr: any) => { if (error) { if (error.signal == 'SIGTERM') { resolve('Process was killed');