From a81f5c10c6ab08968a5784742cbfb88a170f20ba Mon Sep 17 00:00:00 2001 From: Irmo van den Berge Date: Thu, 7 Sep 2023 14:11:05 +0200 Subject: [PATCH 1/2] Fix postgres db custom schema option not working --- server/core/db.js | 11 ++++++++++- server/db/beta/index.js | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/server/core/db.js b/server/core/db.js index 25f21ad3..c4da0e26 100644 --- a/server/core/db.js +++ b/server/core/db.js @@ -187,11 +187,19 @@ module.exports = { } } }, + // -> Create DB Schema if different than the default 'public' + async createDefaultSchema () { + if (WIKI.config.db.schema && WIKI.config.db.schema !== 'public') { + await self.knex.raw(`CREATE SCHEMA IF NOT EXISTS ${WIKI.config.db.schema};`) + } + }, // -> Migrate DB Schemas async syncSchemas () { return self.knex.migrate.latest({ tableName: 'migrations', - migrationSource + migrationSource, + schemaName: (WIKI.config.db.schema && WIKI.config.db.schema !== 'public') + ? WIKI.config.db.schema : undefined }) }, // -> Migrate DB Schemas from beta @@ -202,6 +210,7 @@ module.exports = { let initTasksQueue = (WIKI.IS_MASTER) ? [ initTasks.connect, + initTasks.createDefaultSchema, initTasks.migrateFromBeta, initTasks.syncSchemas ] : [ diff --git a/server/db/beta/index.js b/server/db/beta/index.js index aa3157c3..e2e14253 100644 --- a/server/db/beta/index.js +++ b/server/db/beta/index.js @@ -84,6 +84,8 @@ module.exports = { const baseMigrationPath = path.join(WIKI.SERVERPATH, (WIKI.config.db.type !== 'sqlite') ? 'db/beta/migrations' : 'db/beta/migrations-sqlite') await knex.migrate.latest({ tableName: 'migrations', + schemaName: (WIKI.config.db.schema && WIKI.config.db.schema !== 'public') + ? WIKI.config.db.schema : undefined, migrationSource: { async getMigrations() { const migrationFiles = await fs.readdir(baseMigrationPath) From 8f56a515cb5dc4166bd738dccc20ade30caf834f Mon Sep 17 00:00:00 2001 From: Irmo van den Berge Date: Thu, 19 Oct 2023 13:57:27 +0200 Subject: [PATCH 2/2] Only try to create the schema if it doesn't already exist Signed-off-by: Irmo van den Berge --- server/core/db.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/server/core/db.js b/server/core/db.js index c4da0e26..f554b9fd 100644 --- a/server/core/db.js +++ b/server/core/db.js @@ -190,7 +190,15 @@ module.exports = { // -> Create DB Schema if different than the default 'public' async createDefaultSchema () { if (WIKI.config.db.schema && WIKI.config.db.schema !== 'public') { - await self.knex.raw(`CREATE SCHEMA IF NOT EXISTS ${WIKI.config.db.schema};`) + // Don't create the schema if it already exists. This avoids errors + // when the user lacks permission to create new schemas while the schema + // already exists. + let matched_schemas = await self.knex.select('schema_name') + .from('information_schema.schemata') + .where('schema_name', WIKI.config.db.schema); + if (matched_schemas.length == 0) { + await self.knex.raw(`CREATE SCHEMA IF NOT EXISTS ${WIKI.config.db.schema};`) + } } }, // -> Migrate DB Schemas