From 68e79f15735fc05578c128a0fe0c1c6e1c3bc72f Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 25 May 2021 12:13:35 +0200 Subject: [PATCH] Server: Allow setting the path to the SQLite database using SQLITE_DATABASE env variable --- packages/server/src/app.ts | 11 +++++----- packages/server/src/config.ts | 3 ++- packages/server/src/db.ts | 8 ++----- packages/server/src/tools/dbTools.ts | 6 ++--- .../server/src/utils/testing/testUtils.ts | 22 ++++++++++--------- packages/server/src/utils/types.ts | 2 ++ 6 files changed, 27 insertions(+), 25 deletions(-) diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index 7fdd3cd8e..df301c853 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -7,7 +7,7 @@ import { argv } from 'yargs'; import Logger, { LoggerWrapper, TargetType } from '@joplin/lib/Logger'; import config, { initConfig, runningInDocker, EnvVariables } from './config'; import { createDb, dropDb } from './tools/dbTools'; -import { dropTables, connectDb, disconnectDb, migrateDb, waitForConnection, sqliteFilePath } from './db'; +import { dropTables, connectDb, disconnectDb, migrateDb, waitForConnection, sqliteDefaultDir } from './db'; import { AppContext, Env } from './utils/types'; import FsDriverNode from '@joplin/lib/fs-driver-node'; import routeHandler from './middleware/routeHandler'; @@ -26,12 +26,14 @@ const env: Env = argv.env as Env || Env.Prod; const envVariables: Record = { dev: { - SQLITE_DATABASE: 'dev', + SQLITE_DATABASE: `${sqliteDefaultDir}/db-dev.sqlite`, }, buildTypes: { - SQLITE_DATABASE: 'buildTypes', + SQLITE_DATABASE: `${sqliteDefaultDir}/db-buildTypes.sqlite`, + }, + prod: { + SQLITE_DATABASE: `${sqliteDefaultDir}/db-prod.sqlite`, }, - prod: {}, // Actually get the env variables from the environment }; let appLogger_: LoggerWrapper = null; @@ -130,7 +132,6 @@ async function main() { appLogger().info('Public base URL:', config().baseUrl); appLogger().info('Log dir:', config().logDir); appLogger().info('DB Config:', markPasswords(config().database)); - if (config().database.client === 'sqlite3') appLogger().info('DB file:', sqliteFilePath(config().database.name)); appLogger().info('Trying to connect to database...'); const connectionCheck = await waitForConnection(config().database); diff --git a/packages/server/src/config.ts b/packages/server/src/config.ts index 8ef12a17c..9ddaff1d5 100644 --- a/packages/server/src/config.ts +++ b/packages/server/src/config.ts @@ -23,6 +23,7 @@ export interface EnvVariables { MAILER_NOREPLY_NAME?: string; MAILER_NOREPLY_EMAIL?: string; + // This must be the full path to the database file SQLITE_DATABASE?: string; } @@ -61,7 +62,7 @@ function databaseConfigFromEnv(runningInDocker: boolean, env: EnvVariables): Dat return { client: DatabaseConfigClient.SQLite, - name: env.SQLITE_DATABASE || 'prod', + name: env.SQLITE_DATABASE, asyncStackTraces: true, }; } diff --git a/packages/server/src/db.ts b/packages/server/src/db.ts index b2e29dcc1..5cec67548 100644 --- a/packages/server/src/db.ts +++ b/packages/server/src/db.ts @@ -17,7 +17,7 @@ require('pg').types.setTypeParser(20, function(val: any) { const logger = Logger.create('db'); const migrationDir = `${__dirname}/migrations`; -const sqliteDbDir = pathUtils.dirname(__dirname); +export const sqliteDefaultDir = pathUtils.dirname(__dirname); export const defaultAdminEmail = 'admin@localhost'; export const defaultAdminPassword = 'admin'; @@ -47,15 +47,11 @@ export interface ConnectionCheckResult { connection: DbConnection; } -export function sqliteFilePath(name: string): string { - return `${sqliteDbDir}/db-${name}.sqlite`; -} - export function makeKnexConfig(dbConfig: DatabaseConfig): KnexDatabaseConfig { const connection: DbConfigConnection = {}; if (dbConfig.client === 'sqlite3') { - connection.filename = sqliteFilePath(dbConfig.name); + connection.filename = dbConfig.name; } else { connection.database = dbConfig.name; connection.host = dbConfig.host; diff --git a/packages/server/src/tools/dbTools.ts b/packages/server/src/tools/dbTools.ts index 6865c76e2..bc1a0f119 100644 --- a/packages/server/src/tools/dbTools.ts +++ b/packages/server/src/tools/dbTools.ts @@ -1,4 +1,4 @@ -import { connectDb, disconnectDb, migrateDb, sqliteFilePath } from '../db'; +import { connectDb, disconnectDb, migrateDb } from '../db'; import * as fs from 'fs-extra'; import { DatabaseConfig } from '../utils/types'; @@ -33,7 +33,7 @@ export async function createDb(config: DatabaseConfig, options: CreateDbOptions await execCommand(cmd.join(' '), { env: { PGPASSWORD: config.password } }); } else if (config.client === 'sqlite3') { - const filePath = sqliteFilePath(config.name); + const filePath = config.name; if (await fs.pathExists(filePath)) { if (options.dropIfExists) { @@ -71,6 +71,6 @@ export async function dropDb(config: DatabaseConfig, options: DropDbOptions = nu throw error; } } else if (config.client === 'sqlite3') { - await fs.remove(sqliteFilePath(config.name)); + await fs.remove(config.name); } } diff --git a/packages/server/src/utils/testing/testUtils.ts b/packages/server/src/utils/testing/testUtils.ts index 7925eb14a..c8db654b7 100644 --- a/packages/server/src/utils/testing/testUtils.ts +++ b/packages/server/src/utils/testing/testUtils.ts @@ -1,4 +1,4 @@ -import { User, Session, DbConnection, connectDb, disconnectDb, truncateTables, sqliteFilePath, Item, Uuid } from '../../db'; +import { User, Session, DbConnection, connectDb, disconnectDb, truncateTables, Item, Uuid } from '../../db'; import { createDb } from '../../tools/dbTools'; import modelFactory from '../../models/factory'; import { AppContext, Env } from '../types'; @@ -9,6 +9,7 @@ import FakeRequest from './koa/FakeRequest'; import FakeResponse from './koa/FakeResponse'; import * as httpMocks from 'node-mocks-http'; import * as crypto from 'crypto'; +import * as path from 'path'; import * as fs from 'fs-extra'; import * as jsdom from 'jsdom'; import setupAppContext from '../setupAppContext'; @@ -21,7 +22,7 @@ import MustacheService from '../../services/MustacheService'; // Takes into account the fact that this file will be inside the /dist directory // when it runs. -const packageRootDir = `${__dirname}/../../..`; +const packageRootDir = path.dirname(path.dirname(path.dirname(__dirname))); let db_: DbConnection = null; @@ -55,9 +56,11 @@ function initGlobalLogger() { Logger.initializeGlobalLogger(globalLogger); } -let createdDbName_: string = null; +let createdDbPath_: string = null; export async function beforeAllDb(unitName: string) { - createdDbName_ = unitName; + createdDbPath_ = `${packageRootDir}/db-test-${unitName}.sqlite`; + + console.info('PPPPPPPPPPPP', createdDbPath_); const tempDir = `${packageRootDir}/temp/test-${unitName}`; await fs.mkdirp(tempDir); @@ -67,7 +70,7 @@ export async function beforeAllDb(unitName: string) { // initConfig({ // DB_CLIENT: 'pg', - // POSTGRES_DATABASE: createdDbName_, + // POSTGRES_DATABASE: createdDbPath_, // POSTGRES_USER: 'joplin', // POSTGRES_PASSWORD: 'joplin', // }, { @@ -75,7 +78,7 @@ export async function beforeAllDb(unitName: string) { // }); initConfig({ - SQLITE_DATABASE: createdDbName_, + SQLITE_DATABASE: createdDbPath_, }, { tempDir: tempDir, }); @@ -102,10 +105,9 @@ export async function afterAllTests() { tempDir_ = null; } - if (createdDbName_) { - const filePath = sqliteFilePath(createdDbName_); - await fs.remove(filePath); - createdDbName_ = null; + if (createdDbPath_) { + await fs.remove(createdDbPath_); + createdDbPath_ = null; } } diff --git a/packages/server/src/utils/types.ts b/packages/server/src/utils/types.ts index 451ced808..0333c8a35 100644 --- a/packages/server/src/utils/types.ts +++ b/packages/server/src/utils/types.ts @@ -36,6 +36,8 @@ export enum DatabaseConfigClient { export interface DatabaseConfig { client: DatabaseConfigClient; + // For Postgres, this is the actual database name. For SQLite, this is the + // path to the SQLite file. name: string; host?: string; port?: number;