2017-11-16 16:45:01 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-08-04 11:10:51 +02:00
|
|
|
const { EventEmitter } = require('events');
|
2017-11-16 16:45:01 +01:00
|
|
|
const migrator = require('../../../migrator');
|
|
|
|
const { createStores } = require('../../../lib/db');
|
|
|
|
const { createDb } = require('../../../lib/db/db-pool');
|
2020-04-14 22:29:11 +02:00
|
|
|
const dbConfig = require('./database-config');
|
2017-11-16 16:45:01 +01:00
|
|
|
|
|
|
|
const dbState = require('./database.json');
|
|
|
|
|
2019-10-02 22:48:12 +02:00
|
|
|
// require('db-migrate-shared').log.silence(false);
|
|
|
|
|
2017-11-16 16:45:01 +01:00
|
|
|
// because of migrator bug
|
|
|
|
delete process.env.DATABASE_URL;
|
|
|
|
|
2019-10-02 22:48:12 +02:00
|
|
|
// because of db-migrate bug (https://github.com/Unleash/unleash/issues/171)
|
|
|
|
process.setMaxListeners(0);
|
|
|
|
|
2017-11-16 16:45:01 +01:00
|
|
|
async function resetDatabase(stores) {
|
|
|
|
return Promise.all([
|
|
|
|
stores.db('strategies').del(),
|
|
|
|
stores.db('features').del(),
|
|
|
|
stores.db('client_applications').del(),
|
|
|
|
stores.db('client_instances').del(),
|
2020-02-27 21:26:18 +01:00
|
|
|
stores.db('context_fields').del(),
|
2020-05-12 23:05:26 +02:00
|
|
|
stores.db('users').del(),
|
2020-09-28 21:54:44 +02:00
|
|
|
stores.db('projects').del(),
|
2021-01-04 10:29:33 +01:00
|
|
|
stores.db('tags').del(),
|
|
|
|
stores.db('tag_types').del(),
|
2021-01-19 10:42:45 +01:00
|
|
|
stores.db('addons').del(),
|
2017-11-16 16:45:01 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createStrategies(store) {
|
2021-01-18 12:32:19 +01:00
|
|
|
return dbState.strategies.map(s => store.createStrategy(s));
|
2017-11-16 16:45:01 +01:00
|
|
|
}
|
|
|
|
|
2020-02-27 21:26:18 +01:00
|
|
|
function createContextFields(store) {
|
|
|
|
return dbState.contextFields.map(c => store._createContextField(c));
|
|
|
|
}
|
|
|
|
|
2017-11-16 16:45:01 +01:00
|
|
|
function createApplications(store) {
|
|
|
|
return dbState.applications.map(a => store.upsert(a));
|
|
|
|
}
|
|
|
|
|
|
|
|
function createClientInstance(store) {
|
|
|
|
return dbState.clientInstances.map(i => store.insert(i));
|
|
|
|
}
|
|
|
|
|
2020-09-28 21:54:44 +02:00
|
|
|
function createProjects(store) {
|
|
|
|
return dbState.projects.map(i => store.create(i));
|
|
|
|
}
|
|
|
|
|
2017-11-16 16:45:01 +01:00
|
|
|
function createFeatures(store) {
|
2021-01-18 12:32:19 +01:00
|
|
|
return dbState.features.map(f => store.createFeature(f));
|
2017-11-16 16:45:01 +01:00
|
|
|
}
|
|
|
|
|
2021-01-18 12:32:19 +01:00
|
|
|
async function tagFeatures(tagStore, store) {
|
|
|
|
await tagStore.createTag({ value: 'Tester', type: 'simple' });
|
2021-01-04 10:29:33 +01:00
|
|
|
return dbState.features.map(f =>
|
2021-01-18 12:32:19 +01:00
|
|
|
store.tagFeature(f.name, {
|
2021-01-04 10:29:33 +01:00
|
|
|
value: 'Tester',
|
|
|
|
type: 'simple',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createTagTypes(store) {
|
2021-01-18 12:32:19 +01:00
|
|
|
return dbState.tag_types.map(t => store.createTagType(t));
|
2021-01-04 10:29:33 +01:00
|
|
|
}
|
|
|
|
|
2020-04-14 22:29:11 +02:00
|
|
|
async function setupDatabase(stores) {
|
2021-01-04 10:29:33 +01:00
|
|
|
await Promise.all(createStrategies(stores.strategyStore));
|
|
|
|
await Promise.all(createContextFields(stores.contextFieldStore));
|
|
|
|
await Promise.all(createFeatures(stores.featureToggleStore));
|
|
|
|
await Promise.all(createClientInstance(stores.clientInstanceStore));
|
|
|
|
await Promise.all(createApplications(stores.clientApplicationsStore));
|
|
|
|
await Promise.all(createProjects(stores.projectStore));
|
|
|
|
await Promise.all(createTagTypes(stores.tagTypeStore));
|
2021-01-22 13:39:42 +01:00
|
|
|
await tagFeatures(stores.tagStore, stores.featureToggleStore);
|
2020-04-14 22:29:11 +02:00
|
|
|
}
|
|
|
|
|
2019-04-30 21:14:23 +02:00
|
|
|
module.exports = async function init(databaseSchema = 'test', getLogger) {
|
2017-11-16 16:45:01 +01:00
|
|
|
const options = {
|
2020-04-14 22:29:11 +02:00
|
|
|
db: dbConfig.getDb(),
|
2017-11-16 16:45:01 +01:00
|
|
|
databaseSchema,
|
2019-10-02 23:02:29 +02:00
|
|
|
minPool: 1,
|
2019-10-02 22:23:02 +02:00
|
|
|
maxPool: 1,
|
2019-04-30 21:14:23 +02:00
|
|
|
getLogger,
|
2017-11-16 16:45:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const db = createDb(options);
|
2019-08-04 11:10:51 +02:00
|
|
|
const eventBus = new EventEmitter();
|
2017-11-16 16:45:01 +01:00
|
|
|
|
2020-12-22 10:39:50 +01:00
|
|
|
await db.raw(`DROP SCHEMA IF EXISTS ${options.databaseSchema} CASCADE`);
|
2017-11-16 16:45:01 +01:00
|
|
|
await db.raw(`CREATE SCHEMA IF NOT EXISTS ${options.databaseSchema}`);
|
|
|
|
await migrator(options);
|
2019-10-02 23:14:53 +02:00
|
|
|
await db.destroy();
|
|
|
|
const stores = await createStores(options, eventBus);
|
2021-01-06 13:25:25 +01:00
|
|
|
stores.clientMetricsStore.setMaxListeners(0);
|
2021-01-19 10:42:45 +01:00
|
|
|
stores.eventStore.setMaxListeners(0);
|
2017-11-16 16:45:01 +01:00
|
|
|
await resetDatabase(stores);
|
|
|
|
await setupDatabase(stores);
|
|
|
|
|
2019-10-03 15:01:33 +02:00
|
|
|
return {
|
|
|
|
stores,
|
|
|
|
reset: async () => {
|
|
|
|
await resetDatabase(stores);
|
|
|
|
await setupDatabase(stores);
|
|
|
|
},
|
|
|
|
};
|
2017-11-16 16:45:01 +01:00
|
|
|
};
|