From 9e541bd7e6511595e1edbd22293335d8d8c0ec7d Mon Sep 17 00:00:00 2001 From: Ivar Date: Sat, 11 Feb 2017 11:17:11 +0100 Subject: [PATCH] Add default built-in strategies. Uses migration to define these activation strategies: - applicationHostname - gradualRolloutRandom - gradualRolloutSessionId - gradualRolloutUserId - remoteAddress - userWithId closes #207 --- .../20170211085502-built-in-strategies.js | 15 ++++ .../20170211090541-add-default-strategies.js | 37 +++++++++ migrations/default-strategies.json | 77 +++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 migrations/20170211085502-built-in-strategies.js create mode 100644 migrations/20170211090541-add-default-strategies.js create mode 100644 migrations/default-strategies.json diff --git a/migrations/20170211085502-built-in-strategies.js b/migrations/20170211085502-built-in-strategies.js new file mode 100644 index 0000000000..e28a36a7c7 --- /dev/null +++ b/migrations/20170211085502-built-in-strategies.js @@ -0,0 +1,15 @@ +/* eslint camelcase: "off" */ +'use strict'; + +const async = require('async'); + +exports.up = function (db, cb) { + async.series([ + db.addColumn.bind(db, 'strategies', 'built_in', { type: 'int', defaultValue: 0 }), + db.runSql.bind(db, 'UPDATE strategies SET built_in=1 where name=\'default\''), + ], cb); +}; + +exports.down = function (db, cb) { + return db.removeColumn('strategies', 'built_in', cb); +}; diff --git a/migrations/20170211090541-add-default-strategies.js b/migrations/20170211090541-add-default-strategies.js new file mode 100644 index 0000000000..b0d91ad0df --- /dev/null +++ b/migrations/20170211090541-add-default-strategies.js @@ -0,0 +1,37 @@ +/* eslint camelcase: "off" */ +'use strict'; + +const strategies = require('./default-strategies.json'); +const async = require('async'); + +function insertStrategySQL (strategy) { + return ` + INSERT INTO strategies (name, description, parameters, built_in) + SELECT '${strategy.name}', '${strategy.description}', '${JSON.stringify(strategy.parameters)}', 1 + WHERE + NOT EXISTS ( + SELECT name FROM strategies WHERE name = '${strategy.name}' + );`; +} + +function insertEventsSQL (strategy) { + return ` + INSERT INTO events (type, created_by, data) + SELECT 'strategy-created', 'migration', '${JSON.stringify(strategy)}' + WHERE + NOT EXISTS ( + SELECT name FROM strategies WHERE name = '${strategy.name}' + );`; +} + +exports.up = function (db, callback) { + const insertStrategies = strategies.map((s) => (cb) => { + db.runSql(insertEventsSQL(s), cb); + db.runSql(insertStrategySQL(s), cb); + }); + async.series(insertStrategies, callback); +}; + +exports.down = function (db, cb) { + return cb(); +}; diff --git a/migrations/default-strategies.json b/migrations/default-strategies.json new file mode 100644 index 0000000000..e2af38b2d1 --- /dev/null +++ b/migrations/default-strategies.json @@ -0,0 +1,77 @@ +[ + { + "name": "default", + "description": "Default on/off strategy.", + "parameters": [] + }, + { + "name": "userWithId", + "description": "Active for users with a userId defined in the userIds-list", + "parameters": [{ + "name": "userIds", + "type": "list", + "description": "", + "required": false + }] + }, + { + "name": "applicationHostname", + "description": "Active for client instances with a hostName in the hostNames-list.", + "parameters": [{ + "name": "hostNames", + "type": "list", + "description": "List of hostnames to enable the feature toggle for.", + "required": false + }] + }, + { + "name": "gradualRolloutRandom", + "description": "Randomly activate the feature toggle. No stickiness.", + "parameters": [{ + "name": "percentage", + "type": "percentage", + "description": "", + "required": false + }] + }, + { + "name": "gradualRolloutSessionId", + "description": "Gradually activate feature toggle. Stickiness based on session id.", + "parameters": [{ + "name": "percentage", + "type": "percentage", + "description": "", + "required": false + },{ + "name": "groupId", + "type": "string", + "description": "Used to define a activation groups, which allows you to correlate across feature toggles.", + "required": true + }] + }, + { + "name": "gradualRolloutUserId", + "description": "Gradually activate feature toggle for logged in users. Stickiness based on user id.", + "parameters": [{ + "name": "percentage", + "type": "percentage", + "description": "", + "required": false + },{ + "name": "groupId", + "type": "string", + "description": "Used to define a activation groups, which allows you to correlate across feature toggles.", + "required": true + }] + }, + { + "name": "remoteAddress", + "description": "Active for remote addresses defined in the IPs list.", + "parameters": [{ + "name": "IPs", + "type": "list", + "description": "List of IPs to enable the feature toggle for.", + "required": true + }] + } +]