1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00

Merge pull request #167 from Unleash/db_schemas

options to specify different db-schema
This commit is contained in:
Sveinung Røsaker 2016-11-12 11:27:05 +01:00 committed by GitHub
commit a1837d90d4
6 changed files with 24 additions and 13 deletions

View File

@ -2,10 +2,13 @@
const knex = require('knex'); const knex = require('knex');
module.exports.createDb = function (databaseConnection) { module.exports.createDb = function (databaseConnection, schema = 'public') {
return knex({ const db = knex({
client: 'pg', client: 'pg',
connection: databaseConnection, connection: databaseConnection,
pool: { min: 2, max: 20 }, pool: { min: 2, max: 20 },
searchPath: schema,
}); });
return db;
}; };

View File

@ -9,7 +9,7 @@ const ClientMetricsStore = require('./client-metrics-store');
const ClientStrategyStore = require('./client-strategy-store'); const ClientStrategyStore = require('./client-strategy-store');
module.exports.createStores = (config) => { module.exports.createStores = (config) => {
const db = createDb(config.databaseUri); const db = createDb(config.databaseUri, config.databaseSchema);
const eventStore = new EventStore(db); const eventStore = new EventStore(db);
return { return {

View File

@ -2,12 +2,14 @@
const DBMigrate = require('db-migrate'); const DBMigrate = require('db-migrate');
const path = require('path'); const path = require('path');
const parseDbUrl = require('parse-database-url');
function migrateDb (dbUri) { function migrateDb (dbUrl, schema = "public") {
console.log(dbUri); const custom = parseDbUrl(dbUrl);
custom.schema = schema;
const dbmigrate = DBMigrate.getInstance(true, { const dbmigrate = DBMigrate.getInstance(true, {
cwd: __dirname, cwd: __dirname,
config: { "custom": dbUri}, config: { custom },
env: 'custom' } env: 'custom' }
); );
return dbmigrate.up(); return dbmigrate.up();

View File

@ -63,6 +63,7 @@
"knex": "^0.12.6", "knex": "^0.12.6",
"log4js": "^1.0.1", "log4js": "^1.0.1",
"moment": "^2.15.2", "moment": "^2.15.2",
"parse-database-url": "^0.3.0",
"pg": "^6.1.0", "pg": "^6.1.0",
"serve-favicon": "^2.3.0", "serve-favicon": "^2.3.0",
"unleash-frontend": "github:unleash/unleash-frontend", "unleash-frontend": "github:unleash/unleash-frontend",

View File

@ -4,7 +4,6 @@ function getDatabaseUri () {
if (process.env.TEST_DATABASE_URL) { if (process.env.TEST_DATABASE_URL) {
return process.env.TEST_DATABASE_URL; return process.env.TEST_DATABASE_URL;
} else { } else {
console.log('Using default unleash_test database');
return 'postgres://unleash_user:passord@localhost:5432/unleash_test'; return 'postgres://unleash_user:passord@localhost:5432/unleash_test';
} }
} }

View File

@ -6,14 +6,20 @@ let supertest = require('supertest');
const options = { const options = {
databaseUri: require('./database-config').getDatabaseUri(), databaseUri: require('./database-config').getDatabaseUri(),
databaseSchema: 'test'
}; };
const migrator = require('../../../migrator'); const migrator = require('../../../migrator');
const { createStores } = require('../../../lib/db'); 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 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 stores = createStores(options);
const app = require('../../../app')({stores}); const app = require('../../../app')({stores});
return { stores, request: supertest(app) }; return { stores, request: supertest(app) };