1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Server: Allow setting the path to the SQLite database using SQLITE_DATABASE env variable

This commit is contained in:
Laurent Cozic 2021-05-25 12:13:35 +02:00
parent ed8ee67048
commit 68e79f1573
6 changed files with 27 additions and 25 deletions

View File

@ -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<Env, EnvVariables> = {
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);

View File

@ -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,
};
}

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;