diff --git a/docs/developer-guide.md b/docs/developer-guide.md index a5ff88d2ba..505adf55cd 100644 --- a/docs/developer-guide.md +++ b/docs/developer-guide.md @@ -59,11 +59,14 @@ npm test We use database migrations to track database changes. ### Making a schema change +In order to run migrations you will set the environment variable for DATABASE_URL + +`export DATABASE_URL=postgres://unleash_user:passord@localhost:5432/unleash` Use db-migrate to create new migrations file. ```bash -> ./node_modules/.bin/db-migrate create your-migration-name +> npm run db-migrate -- create YOUR-MIGRATION-NAME ``` All migrations requires on `up` and one `down` method. @@ -86,6 +89,12 @@ exports.down = function (db, cb) { }; ``` +Test your migrations: + +```bash +> npm run db-migrate -- up +> npm run db-migrate -- down +``` ## Publishing / Releasing new packages diff --git a/migrations/20161212101749-better-strategy-parameter-definitions.js b/migrations/20161212101749-better-strategy-parameter-definitions.js new file mode 100644 index 0000000000..fdb98656a8 --- /dev/null +++ b/migrations/20161212101749-better-strategy-parameter-definitions.js @@ -0,0 +1,61 @@ +/* eslint camelcase: "off" */ +'use strict'; + +const async = require('async'); + +exports.up = function (db, callback) { + const populateNewData = (cb) => { + db.all('select name, parameters_template from strategies', (err, results) => { + const updateSQL = results + .map(({ name, parameters_template }) => { + const parameters = []; + Object.keys(parameters_template || {}).forEach(p => { + parameters.push({ name: p, type: parameters_template[p], description: '' }); + }); + return { name, parameters }; + }) + .map(strategy => ` + UPDATE strategies + SET parameters='${JSON.stringify(strategy.parameters)}' + WHERE name='${strategy.name}';`) + .join('\n'); + + db.runSql(updateSQL, cb); + }); + }; + + async.series([ + db.addColumn.bind(db, 'strategies', 'parameters', { type: 'json' }), + populateNewData.bind(db), + db.removeColumn.bind(db, 'strategies', 'parameters_template'), + ], callback); +}; + +exports.down = function (db, callback) { + const populateOldData = (cb) => { + db.all('select name, parameters from strategies', (err, results) => { + const updateSQL = results + .map(({ name, parameters }) => { + const parameters_template = {}; + parameters.forEach(p => { + parameters_template[p.name] = p.type; + }); + + return { name, parameters_template }; + }) + .map(strategy => ` + UPDATE strategies + SET parameters_template='${JSON.stringify(strategy.parameters_template)}' + WHERE name='${strategy.name}';`) + .join('\n'); + + db.runSql(updateSQL, cb); + }); + }; + + async.series([ + db.addColumn.bind(db, 'strategies', 'parameters_template', { type: 'json' }), + populateOldData.bind(db), + db.removeColumn.bind(db, 'strategies', 'parameters'), + ], callback); +}; diff --git a/package.json b/package.json index a0e1aaf943..6caf357099 100644 --- a/package.json +++ b/package.json @@ -37,8 +37,7 @@ "start:dev": "NODE_ENV=development supervisor --ignore ./node_modules/ server.js", "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", + "db-migrate": "db-migrate", "lint": "eslint lib", "pretest": "npm run lint", "test": "PORT=4243 ava test lib/*.test.js lib/**/*.test.js",