From 14deebae1f8e7fbad28b6821c7b373c78cd34d22 Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Sat, 12 Nov 2016 11:21:40 +0100 Subject: [PATCH] options to specify different db-schema --- app.js | 4 ++-- lib/db/db-pool.js | 7 +++++-- lib/db/index.js | 2 +- migrator.js | 8 +++++--- package.json | 1 + test/e2e/util/database-config.js | 1 - test/e2e/util/test-helper.js | 18 ++++++++++++------ 7 files changed, 26 insertions(+), 15 deletions(-) diff --git a/app.js b/app.js index 4d0d30cd71..bf4a13c322 100644 --- a/app.js +++ b/app.js @@ -21,10 +21,10 @@ module.exports = function (config) { app.locals.baseUriPath = baseUriPath; app.use(cookieParser()); - if (publicFolder) { +/* if (publicFolder) { app.use(favicon(path.join(publicFolder, 'favicon.ico'))); } - +*/ app.use(validator([])); if (publicFolder) { app.use(baseUriPath, express.static(publicFolder)); diff --git a/lib/db/db-pool.js b/lib/db/db-pool.js index ab6b734ce6..086e44e6fd 100644 --- a/lib/db/db-pool.js +++ b/lib/db/db-pool.js @@ -2,10 +2,13 @@ const knex = require('knex'); -module.exports.createDb = function (databaseConnection) { - return knex({ +module.exports.createDb = function (databaseConnection, schema = 'public') { + const db = knex({ client: 'pg', connection: databaseConnection, pool: { min: 2, max: 20 }, + searchPath: schema, }); + + return db; }; diff --git a/lib/db/index.js b/lib/db/index.js index 8cbc185cef..58ca375b22 100644 --- a/lib/db/index.js +++ b/lib/db/index.js @@ -9,7 +9,7 @@ const ClientMetricsStore = require('./client-metrics-store'); const ClientStrategyStore = require('./client-strategy-store'); module.exports.createStores = (config) => { - const db = createDb(config.databaseUri); + const db = createDb(config.databaseUri, config.databaseSchema); const eventStore = new EventStore(db); return { diff --git a/migrator.js b/migrator.js index f443dbeaec..b22b517969 100644 --- a/migrator.js +++ b/migrator.js @@ -2,12 +2,14 @@ const DBMigrate = require('db-migrate'); const path = require('path'); +const parseDbUrl = require('parse-database-url'); -function migrateDb (dbUri) { - console.log(dbUri); +function migrateDb (dbUrl, schema = "public") { + const custom = parseDbUrl(dbUrl); + custom.schema = schema; const dbmigrate = DBMigrate.getInstance(true, { cwd: __dirname, - config: { "custom": dbUri}, + config: { custom }, env: 'custom' } ); return dbmigrate.up(); diff --git a/package.json b/package.json index 7f3fb3e27e..861fc5856b 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,7 @@ "knex": "^0.12.6", "log4js": "^1.0.1", "moment": "^2.15.2", + "parse-database-url": "^0.3.0", "pg": "^6.1.0", "serve-favicon": "^2.3.0", "unleash-frontend": "github:unleash/unleash-frontend", diff --git a/test/e2e/util/database-config.js b/test/e2e/util/database-config.js index 0dcc7fc69b..384c416b74 100644 --- a/test/e2e/util/database-config.js +++ b/test/e2e/util/database-config.js @@ -4,7 +4,6 @@ function getDatabaseUri () { if (process.env.TEST_DATABASE_URL) { return process.env.TEST_DATABASE_URL; } else { - console.log('Using default unleash_test database'); return 'postgres://unleash_user:passord@localhost:5432/unleash_test'; } } diff --git a/test/e2e/util/test-helper.js b/test/e2e/util/test-helper.js index 175d90c7c5..d140b3178e 100644 --- a/test/e2e/util/test-helper.js +++ b/test/e2e/util/test-helper.js @@ -6,18 +6,24 @@ let supertest = require('supertest'); const options = { databaseUri: require('./database-config').getDatabaseUri(), + databaseSchema: 'test' }; const migrator = require('../../../migrator'); const { createStores } = require('../../../lib/db'); -process.env.DATABASE_URL = options.databaseUri +// because of migrator bug +delete process.env.DATABASE_URL; -const createApp = migrator(options.databaseUri).then(() => { - const stores = createStores(options); - const app = require('../../../app')({stores}); - return { stores, request: supertest(app) }; -}); +const db = require('../../../lib/db/db-pool').createDb(options.databaseUri); + +const createApp = db.raw(`CREATE SCHEMA IF NOT EXISTS ${options.databaseSchema}`) + .then(() => migrator(options.databaseUri, options.databaseSchema)) + .then(() => { + const stores = createStores(options); + const app = require('../../../app')({stores}); + return { stores, request: supertest(app) }; + }); function createStrategies (stores) { return [