mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-11 18:24:43 +02:00
Server: Allow setting the path to the SQLite database using SQLITE_DATABASE env variable
This commit is contained in:
parent
ed8ee67048
commit
68e79f1573
@ -7,7 +7,7 @@ import { argv } from 'yargs';
|
|||||||
import Logger, { LoggerWrapper, TargetType } from '@joplin/lib/Logger';
|
import Logger, { LoggerWrapper, TargetType } from '@joplin/lib/Logger';
|
||||||
import config, { initConfig, runningInDocker, EnvVariables } from './config';
|
import config, { initConfig, runningInDocker, EnvVariables } from './config';
|
||||||
import { createDb, dropDb } from './tools/dbTools';
|
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 { AppContext, Env } from './utils/types';
|
||||||
import FsDriverNode from '@joplin/lib/fs-driver-node';
|
import FsDriverNode from '@joplin/lib/fs-driver-node';
|
||||||
import routeHandler from './middleware/routeHandler';
|
import routeHandler from './middleware/routeHandler';
|
||||||
@ -26,12 +26,14 @@ const env: Env = argv.env as Env || Env.Prod;
|
|||||||
|
|
||||||
const envVariables: Record<Env, EnvVariables> = {
|
const envVariables: Record<Env, EnvVariables> = {
|
||||||
dev: {
|
dev: {
|
||||||
SQLITE_DATABASE: 'dev',
|
SQLITE_DATABASE: `${sqliteDefaultDir}/db-dev.sqlite`,
|
||||||
},
|
},
|
||||||
buildTypes: {
|
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;
|
let appLogger_: LoggerWrapper = null;
|
||||||
@ -130,7 +132,6 @@ async function main() {
|
|||||||
appLogger().info('Public base URL:', config().baseUrl);
|
appLogger().info('Public base URL:', config().baseUrl);
|
||||||
appLogger().info('Log dir:', config().logDir);
|
appLogger().info('Log dir:', config().logDir);
|
||||||
appLogger().info('DB Config:', markPasswords(config().database));
|
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...');
|
appLogger().info('Trying to connect to database...');
|
||||||
const connectionCheck = await waitForConnection(config().database);
|
const connectionCheck = await waitForConnection(config().database);
|
||||||
|
@ -23,6 +23,7 @@ export interface EnvVariables {
|
|||||||
MAILER_NOREPLY_NAME?: string;
|
MAILER_NOREPLY_NAME?: string;
|
||||||
MAILER_NOREPLY_EMAIL?: string;
|
MAILER_NOREPLY_EMAIL?: string;
|
||||||
|
|
||||||
|
// This must be the full path to the database file
|
||||||
SQLITE_DATABASE?: string;
|
SQLITE_DATABASE?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,7 +62,7 @@ function databaseConfigFromEnv(runningInDocker: boolean, env: EnvVariables): Dat
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
client: DatabaseConfigClient.SQLite,
|
client: DatabaseConfigClient.SQLite,
|
||||||
name: env.SQLITE_DATABASE || 'prod',
|
name: env.SQLITE_DATABASE,
|
||||||
asyncStackTraces: true,
|
asyncStackTraces: true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ require('pg').types.setTypeParser(20, function(val: any) {
|
|||||||
const logger = Logger.create('db');
|
const logger = Logger.create('db');
|
||||||
|
|
||||||
const migrationDir = `${__dirname}/migrations`;
|
const migrationDir = `${__dirname}/migrations`;
|
||||||
const sqliteDbDir = pathUtils.dirname(__dirname);
|
export const sqliteDefaultDir = pathUtils.dirname(__dirname);
|
||||||
|
|
||||||
export const defaultAdminEmail = 'admin@localhost';
|
export const defaultAdminEmail = 'admin@localhost';
|
||||||
export const defaultAdminPassword = 'admin';
|
export const defaultAdminPassword = 'admin';
|
||||||
@ -47,15 +47,11 @@ export interface ConnectionCheckResult {
|
|||||||
connection: DbConnection;
|
connection: DbConnection;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function sqliteFilePath(name: string): string {
|
|
||||||
return `${sqliteDbDir}/db-${name}.sqlite`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function makeKnexConfig(dbConfig: DatabaseConfig): KnexDatabaseConfig {
|
export function makeKnexConfig(dbConfig: DatabaseConfig): KnexDatabaseConfig {
|
||||||
const connection: DbConfigConnection = {};
|
const connection: DbConfigConnection = {};
|
||||||
|
|
||||||
if (dbConfig.client === 'sqlite3') {
|
if (dbConfig.client === 'sqlite3') {
|
||||||
connection.filename = sqliteFilePath(dbConfig.name);
|
connection.filename = dbConfig.name;
|
||||||
} else {
|
} else {
|
||||||
connection.database = dbConfig.name;
|
connection.database = dbConfig.name;
|
||||||
connection.host = dbConfig.host;
|
connection.host = dbConfig.host;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { connectDb, disconnectDb, migrateDb, sqliteFilePath } from '../db';
|
import { connectDb, disconnectDb, migrateDb } from '../db';
|
||||||
import * as fs from 'fs-extra';
|
import * as fs from 'fs-extra';
|
||||||
import { DatabaseConfig } from '../utils/types';
|
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 } });
|
await execCommand(cmd.join(' '), { env: { PGPASSWORD: config.password } });
|
||||||
} else if (config.client === 'sqlite3') {
|
} else if (config.client === 'sqlite3') {
|
||||||
const filePath = sqliteFilePath(config.name);
|
const filePath = config.name;
|
||||||
|
|
||||||
if (await fs.pathExists(filePath)) {
|
if (await fs.pathExists(filePath)) {
|
||||||
if (options.dropIfExists) {
|
if (options.dropIfExists) {
|
||||||
@ -71,6 +71,6 @@ export async function dropDb(config: DatabaseConfig, options: DropDbOptions = nu
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
} else if (config.client === 'sqlite3') {
|
} else if (config.client === 'sqlite3') {
|
||||||
await fs.remove(sqliteFilePath(config.name));
|
await fs.remove(config.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 { createDb } from '../../tools/dbTools';
|
||||||
import modelFactory from '../../models/factory';
|
import modelFactory from '../../models/factory';
|
||||||
import { AppContext, Env } from '../types';
|
import { AppContext, Env } from '../types';
|
||||||
@ -9,6 +9,7 @@ import FakeRequest from './koa/FakeRequest';
|
|||||||
import FakeResponse from './koa/FakeResponse';
|
import FakeResponse from './koa/FakeResponse';
|
||||||
import * as httpMocks from 'node-mocks-http';
|
import * as httpMocks from 'node-mocks-http';
|
||||||
import * as crypto from 'crypto';
|
import * as crypto from 'crypto';
|
||||||
|
import * as path from 'path';
|
||||||
import * as fs from 'fs-extra';
|
import * as fs from 'fs-extra';
|
||||||
import * as jsdom from 'jsdom';
|
import * as jsdom from 'jsdom';
|
||||||
import setupAppContext from '../setupAppContext';
|
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
|
// Takes into account the fact that this file will be inside the /dist directory
|
||||||
// when it runs.
|
// when it runs.
|
||||||
const packageRootDir = `${__dirname}/../../..`;
|
const packageRootDir = path.dirname(path.dirname(path.dirname(__dirname)));
|
||||||
|
|
||||||
let db_: DbConnection = null;
|
let db_: DbConnection = null;
|
||||||
|
|
||||||
@ -55,9 +56,11 @@ function initGlobalLogger() {
|
|||||||
Logger.initializeGlobalLogger(globalLogger);
|
Logger.initializeGlobalLogger(globalLogger);
|
||||||
}
|
}
|
||||||
|
|
||||||
let createdDbName_: string = null;
|
let createdDbPath_: string = null;
|
||||||
export async function beforeAllDb(unitName: string) {
|
export async function beforeAllDb(unitName: string) {
|
||||||
createdDbName_ = unitName;
|
createdDbPath_ = `${packageRootDir}/db-test-${unitName}.sqlite`;
|
||||||
|
|
||||||
|
console.info('PPPPPPPPPPPP', createdDbPath_);
|
||||||
|
|
||||||
const tempDir = `${packageRootDir}/temp/test-${unitName}`;
|
const tempDir = `${packageRootDir}/temp/test-${unitName}`;
|
||||||
await fs.mkdirp(tempDir);
|
await fs.mkdirp(tempDir);
|
||||||
@ -67,7 +70,7 @@ export async function beforeAllDb(unitName: string) {
|
|||||||
|
|
||||||
// initConfig({
|
// initConfig({
|
||||||
// DB_CLIENT: 'pg',
|
// DB_CLIENT: 'pg',
|
||||||
// POSTGRES_DATABASE: createdDbName_,
|
// POSTGRES_DATABASE: createdDbPath_,
|
||||||
// POSTGRES_USER: 'joplin',
|
// POSTGRES_USER: 'joplin',
|
||||||
// POSTGRES_PASSWORD: 'joplin',
|
// POSTGRES_PASSWORD: 'joplin',
|
||||||
// }, {
|
// }, {
|
||||||
@ -75,7 +78,7 @@ export async function beforeAllDb(unitName: string) {
|
|||||||
// });
|
// });
|
||||||
|
|
||||||
initConfig({
|
initConfig({
|
||||||
SQLITE_DATABASE: createdDbName_,
|
SQLITE_DATABASE: createdDbPath_,
|
||||||
}, {
|
}, {
|
||||||
tempDir: tempDir,
|
tempDir: tempDir,
|
||||||
});
|
});
|
||||||
@ -102,10 +105,9 @@ export async function afterAllTests() {
|
|||||||
tempDir_ = null;
|
tempDir_ = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (createdDbName_) {
|
if (createdDbPath_) {
|
||||||
const filePath = sqliteFilePath(createdDbName_);
|
await fs.remove(createdDbPath_);
|
||||||
await fs.remove(filePath);
|
createdDbPath_ = null;
|
||||||
createdDbName_ = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,6 +36,8 @@ export enum DatabaseConfigClient {
|
|||||||
|
|
||||||
export interface DatabaseConfig {
|
export interface DatabaseConfig {
|
||||||
client: DatabaseConfigClient;
|
client: DatabaseConfigClient;
|
||||||
|
// For Postgres, this is the actual database name. For SQLite, this is the
|
||||||
|
// path to the SQLite file.
|
||||||
name: string;
|
name: string;
|
||||||
host?: string;
|
host?: string;
|
||||||
port?: number;
|
port?: number;
|
||||||
|
Loading…
Reference in New Issue
Block a user