mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			71 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			71 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 'use strict';
 | |
| 
 | |
| const migrator = require('../../../migrator');
 | |
| const { createStores } = require('../../../lib/db');
 | |
| const { createDb } = require('../../../lib/db/db-pool');
 | |
| 
 | |
| const dbState = require('./database.json');
 | |
| 
 | |
| require('db-migrate-shared').log.silence(true);
 | |
| 
 | |
| // because of migrator bug
 | |
| delete process.env.DATABASE_URL;
 | |
| 
 | |
| // because of db-migrate bug (https://github.com/Unleash/unleash/issues/171)
 | |
| process.setMaxListeners(0);
 | |
| 
 | |
| 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(),
 | |
|     ]);
 | |
| }
 | |
| 
 | |
| async function setupDatabase(stores) {
 | |
|     const updates = [];
 | |
|     updates.push(...createStrategies(stores.strategyStore));
 | |
|     updates.push(...createFeatures(stores.featureToggleStore));
 | |
|     updates.push(...createClientInstance(stores.clientInstanceStore));
 | |
|     updates.push(...createApplications(stores.clientApplicationsStore));
 | |
| 
 | |
|     await Promise.all(updates);
 | |
| }
 | |
| 
 | |
| function createStrategies(store) {
 | |
|     return dbState.strategies.map(s => store._createStrategy(s));
 | |
| }
 | |
| 
 | |
| function createApplications(store) {
 | |
|     return dbState.applications.map(a => store.upsert(a));
 | |
| }
 | |
| 
 | |
| function createClientInstance(store) {
 | |
|     return dbState.clientInstances.map(i => store.insert(i));
 | |
| }
 | |
| 
 | |
| function createFeatures(store) {
 | |
|     return dbState.features.map(f => store._createFeature(f));
 | |
| }
 | |
| 
 | |
| module.exports = async function init(databaseSchema = 'test') {
 | |
|     const options = {
 | |
|         databaseUrl: require('./database-config').getDatabaseUrl(),
 | |
|         databaseSchema,
 | |
|         minPool: 0,
 | |
|         maxPool: 0,
 | |
|     };
 | |
| 
 | |
|     const db = createDb(options);
 | |
| 
 | |
|     await db.raw(`CREATE SCHEMA IF NOT EXISTS ${options.databaseSchema}`);
 | |
|     await migrator(options);
 | |
|     await db.destroy();
 | |
|     const stores = await createStores(options);
 | |
|     await resetDatabase(stores);
 | |
|     await setupDatabase(stores);
 | |
| 
 | |
|     return stores;
 | |
| };
 |