mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	upgrade db-migrate to support programmatic API #150
This commit is contained in:
		
							parent
							
								
									7fa12095f2
								
							
						
					
					
						commit
						e2d5153edb
					
				
							
								
								
									
										25
									
								
								packages/unleash-api/migrator.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								packages/unleash-api/migrator.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,25 @@ | ||||
| 'use strict'; | ||||
| 
 | ||||
| const DBMigrate = require('db-migrate'); | ||||
| const path = require('path'); | ||||
| 
 | ||||
| function findUnleashApiRoot () { | ||||
|     try { | ||||
|         return path.dirname(require.resolve('unleash-api/package.json')); | ||||
|     } catch (e) {} | ||||
|     try { | ||||
|         return path.dirname(require.resolve('../unleash-api')); | ||||
|     } catch (e) {} | ||||
|     return process.cwd(); | ||||
| } | ||||
| 
 | ||||
| function migrateDb (dbUri) { | ||||
|     const dbmigrate = DBMigrate.getInstance(true, { | ||||
|         cwd: findUnleashApiRoot(), | ||||
|         config: { custom: dbUri }, | ||||
|         env: 'custom' } | ||||
|     ); | ||||
|     return dbmigrate.up(); | ||||
| } | ||||
| 
 | ||||
| module.exports = migrateDb; | ||||
| @ -36,6 +36,7 @@ | ||||
|     "start:dev:pg": "pg_virtualenv npm run start:dev:pg-chain", | ||||
|     "start:dev:pg-chain": "export DATABASE_URL=postgres://$PGUSER:$PGPASSWORD@localhost:$PGPORT/postgres ; db-migrate up && npm run start:dev", | ||||
|     "db-migrate": "db-migrate up", | ||||
|     "db-migrate:down": "db-migrate down", | ||||
|     "test": "export PORT=4243 ; mocha test/**/*.js && npm run test:coverage", | ||||
|     "test:unit": "mocha test/unit/**/*.js ", | ||||
|     "test:ci": "npm run db-migrate && npm run test", | ||||
| @ -49,7 +50,8 @@ | ||||
|     "bluebird": "^3.4.6", | ||||
|     "body-parser": "1.15.2", | ||||
|     "cookie-parser": "^1.4.1", | ||||
|     "db-migrate": "0.9.25", | ||||
|     "db-migrate": "0.10.0-beta.17", | ||||
|     "db-migrate-pg": "0.1.10", | ||||
|     "deep-diff": "^0.3.3", | ||||
|     "errorhandler": "^1.4.3", | ||||
|     "express": "4.14.0", | ||||
|  | ||||
| @ -1,11 +1,16 @@ | ||||
| 'use strict'; | ||||
| const logger = require('./lib/logger'); | ||||
| const defaultDatabaseUri = process.env.DATABASE_URL; | ||||
| const migrator = require('./migrator'); | ||||
| 
 | ||||
| function start (options) { | ||||
|     options = options || {}; | ||||
| const DEFAULT_OPTIONS = { | ||||
|     databaseUri: process.env.DATABASE_URL, | ||||
|     port: process.env.HTTP_PORT || process.env.PORT || 4242, | ||||
|     baseUriPath: process.env.BASE_URI_PATH || '', | ||||
| }; | ||||
| 
 | ||||
| function createApp (options) { | ||||
|     const db = require('./lib/db/dbPool')(options.databaseUri); | ||||
| 
 | ||||
|     const db = require('./lib/db/dbPool')(options.databaseUri || defaultDatabaseUri); | ||||
|     // Database dependecies (statefull)
 | ||||
|     const eventDb = require('./lib/db/event')(db); | ||||
|     const EventStore = require('./lib/eventStore'); | ||||
| @ -14,27 +19,33 @@ function start (options) { | ||||
|     const strategyDb = require('./lib/db/strategy')(db, eventStore); | ||||
| 
 | ||||
|     const config = { | ||||
|         baseUriPath: process.env.BASE_URI_PATH || '', | ||||
|         port: process.env.HTTP_PORT || process.env.PORT || 4242, | ||||
|         baseUriPath: options.baseUriPath, | ||||
|         port: options.port, | ||||
|         publicFolder: options.publicFolder, | ||||
|         db, | ||||
|         eventDb, | ||||
|         eventStore, | ||||
|         featureDb, | ||||
|         strategyDb, | ||||
|         publicFolder: options.publicFolder, | ||||
|     }; | ||||
| 
 | ||||
|     const app = require('./app')(config); | ||||
| 
 | ||||
|     const server = app.listen(app.get('port'), () => { | ||||
|         logger.info(`unleash started on ${app.get('port')}`); | ||||
|         logger.info(`Unleash started on ${app.get('port')}`); | ||||
|     }); | ||||
|     return { app, server }; | ||||
| } | ||||
| 
 | ||||
|     return { | ||||
|         app, | ||||
|         server, | ||||
|         config, | ||||
|     }; | ||||
| function start (opts) { | ||||
|     const options = Object.assign({}, DEFAULT_OPTIONS, opts); | ||||
| 
 | ||||
|     if (!options.databaseUri) { | ||||
|         throw new Error('You must either pass databaseUri option or set environemnt variable DATABASE_URL'); | ||||
|     } | ||||
| 
 | ||||
|     return migrator(options.databaseUri) | ||||
|         .then(() => createApp(options)) | ||||
|         .catch(err => logger.error('failed to migrate db', err)); | ||||
| } | ||||
| 
 | ||||
| process.on('uncaughtException', err => { | ||||
|  | ||||
| @ -25,7 +25,7 @@ | ||||
|   }, | ||||
|   "main": "./server.js", | ||||
|   "bin": { | ||||
|     "unleash": "NODE_ENV=production ./unleash.js" | ||||
|     "unleash": "./unleash.js" | ||||
|   }, | ||||
|   "scripts": { | ||||
|     "db-migrate-and-start": "npm run db-migrate && npm run start", | ||||
|  | ||||
| @ -2,14 +2,21 @@ | ||||
| 
 | ||||
| 'use strict'; | ||||
| 
 | ||||
| process.env.NODE_ENV = 'production'; | ||||
| 
 | ||||
| const program = require('commander'); | ||||
| const unleash = require('./server.js'); | ||||
| 
 | ||||
| 
 | ||||
| program | ||||
|     .command('start', 'start unleash server') | ||||
|     .command('migrate', 'migrate the unleash db') | ||||
|     .option('-p, --port <port>', 'The full port you want to start unleash on.') | ||||
|     .option('-d, --databaseUri <databaseUri>', 'The full databaseUri to connect to, including username and password') | ||||
|     .parse(process.argv); | ||||
| 
 | ||||
| unleash.start({ databaseUri: program.databaseUri }); | ||||
| unleash.start({ | ||||
|     databaseUri: program.databaseUri || process.env.DATABASE_URL, | ||||
|     port: program.port || process.env.PORT || 4242, | ||||
| }).then(conf => { | ||||
|     console.log(`Unleash started on port:${conf.app.get('port')}`); | ||||
| }); | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user