From b67f9a64874f8be556a7c0d7aabd4616a1ef486b Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Tue, 6 Dec 2016 11:44:58 +0100 Subject: [PATCH 1/2] Cleanup client_instances related to #176 --- .../20161104074441-create-client-instances.js | 16 +++++++++++++++- .../sql/010-create-client-instances.down.sql | 1 - .../sql/010-create-client-instances.up.sql | 7 ------- 3 files changed, 15 insertions(+), 9 deletions(-) delete mode 100644 migrations/sql/010-create-client-instances.down.sql delete mode 100644 migrations/sql/010-create-client-instances.up.sql diff --git a/migrations/20161104074441-create-client-instances.js b/migrations/20161104074441-create-client-instances.js index 039bb7f310..5346ab6fe3 100644 --- a/migrations/20161104074441-create-client-instances.js +++ b/migrations/20161104074441-create-client-instances.js @@ -1,3 +1,17 @@ 'use strict'; -module.exports = require('../scripts/migration-runner').create('010-create-client-instances'); +exports.up = function (db, callback) { + db.runSql(` + CREATE TABLE client_instances ( + app_name varchar(255), + instance_id varchar(255), + client_ip varchar(255), + last_seen timestamp default now(), + created_at timestamp default now() + );`, callback); +}; + + +exports.down = function (db, callback) { + db.runSql('DROP TABLE client_instances;', callback); +}; diff --git a/migrations/sql/010-create-client-instances.down.sql b/migrations/sql/010-create-client-instances.down.sql deleted file mode 100644 index c113b5c4ce..0000000000 --- a/migrations/sql/010-create-client-instances.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE client_instances; diff --git a/migrations/sql/010-create-client-instances.up.sql b/migrations/sql/010-create-client-instances.up.sql deleted file mode 100644 index f2c96e54a7..0000000000 --- a/migrations/sql/010-create-client-instances.up.sql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE client_instances ( - app_name varchar(255), - instance_id varchar(255), - client_ip varchar(255), - last_seen timestamp default now(), - created_at timestamp default now() -); From 2bd7b24dea9919aaf14f32568e4eb85e9b4325d4 Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Sat, 10 Dec 2016 11:35:13 +0100 Subject: [PATCH 2/2] Cleanup legacy migrations --- docs/developer-guide.md | 19 ++++++++++ migrations/20141020151056-initial-schema.js | 35 +++++++++++++++++- ...41110144153-add-description-to-features.js | 9 ++++- ...5-add-parameters-template-to-strategies.js | 8 ++++- .../20141117202209-insert-default-strategy.js | 13 ++++++- .../20141118071458-default-strategy-event.js | 13 ++++++- ...215210141-005-archived-flag-to-features.js | 9 ++++- .../20150210152531-006-rename-eventtype.js | 15 +++++++- ...160618193924-add-strategies-to-features.js | 36 ++++++++++++++++++- migrations/20161027134128-create-metrics.js | 14 +++++++- ...161205203516-create-client-applications.js | 10 +++--- migrations/sql/001-initial-schema.down.sql | 3 -- migrations/sql/001-initial-schema.up.sql | 21 ----------- .../002-add-description-to-features.down.sql | 1 - .../002-add-description-to-features.up.sql | 1 - ...parameters-template-to-strategies.down.sql | 1 - ...d-parameters-template-to-strategies.up.sql | 1 - ...004-insert-default-strategy-event.down.sql | 1 - .../004-insert-default-strategy-event.up.sql | 1 - .../sql/004-insert-default-strategy.down.sql | 1 - .../sql/004-insert-default-strategy.up.sql | 1 - .../005-archived-flag-to-features.down.sql | 1 - .../sql/005-archived-flag-to-features.up.sql | 1 - migrations/sql/006-rename-eventtype.down.sql | 2 -- migrations/sql/006-rename-eventtype.up.sql | 2 -- .../007-add-strategies-to-features.down.sql | 13 ------- .../sql/007-add-strategies-to-features.up.sql | 12 ------- migrations/sql/008-create-metrics.down.sql | 2 -- migrations/sql/008-create-metrics.up.sql | 6 ---- scripts/migration-runner.js | 20 ----------- 30 files changed, 167 insertions(+), 105 deletions(-) delete mode 100644 migrations/sql/001-initial-schema.down.sql delete mode 100644 migrations/sql/001-initial-schema.up.sql delete mode 100644 migrations/sql/002-add-description-to-features.down.sql delete mode 100644 migrations/sql/002-add-description-to-features.up.sql delete mode 100644 migrations/sql/003-add-parameters-template-to-strategies.down.sql delete mode 100644 migrations/sql/003-add-parameters-template-to-strategies.up.sql delete mode 100644 migrations/sql/004-insert-default-strategy-event.down.sql delete mode 100644 migrations/sql/004-insert-default-strategy-event.up.sql delete mode 100644 migrations/sql/004-insert-default-strategy.down.sql delete mode 100644 migrations/sql/004-insert-default-strategy.up.sql delete mode 100644 migrations/sql/005-archived-flag-to-features.down.sql delete mode 100644 migrations/sql/005-archived-flag-to-features.up.sql delete mode 100644 migrations/sql/006-rename-eventtype.down.sql delete mode 100644 migrations/sql/006-rename-eventtype.up.sql delete mode 100644 migrations/sql/007-add-strategies-to-features.down.sql delete mode 100644 migrations/sql/007-add-strategies-to-features.up.sql delete mode 100644 migrations/sql/008-create-metrics.down.sql delete mode 100644 migrations/sql/008-create-metrics.up.sql delete mode 100644 scripts/migration-runner.js diff --git a/docs/developer-guide.md b/docs/developer-guide.md index 67903fb22a..a5ff88d2ba 100644 --- a/docs/developer-guide.md +++ b/docs/developer-guide.md @@ -66,6 +66,25 @@ Use db-migrate to create new migrations file. > ./node_modules/.bin/db-migrate create your-migration-name ``` +All migrations requires on `up` and one `down` method. + +Example of a typical migration: + +```js +/* eslint camelcase: "off" */ +'use strict'; + +exports.up = function (db, cb) { + db.createTable('examples', { + id: { type: 'int', primaryKey: true, notNull: true }, + created_at: { type: 'timestamp', defaultValue: 'now()' }, + }, cb); +}; + +exports.down = function (db, cb) { + return db.dropTable('examples', cb); +}; +``` diff --git a/migrations/20141020151056-initial-schema.js b/migrations/20141020151056-initial-schema.js index 1b332b4629..ad08f9ea34 100644 --- a/migrations/20141020151056-initial-schema.js +++ b/migrations/20141020151056-initial-schema.js @@ -1,3 +1,36 @@ 'use strict'; -module.exports = require('../scripts/migration-runner').create('001-initial-schema'); +exports.up = function (db, callback) { + db.runSql(` +CREATE TABLE strategies ( + created_at timestamp default now(), + name varchar(255) PRIMARY KEY NOT NULL, + description text +); + +CREATE TABLE features ( + created_at timestamp default now(), + name varchar(255) PRIMARY KEY NOT NULL, + enabled integer default 0, + strategy_name varchar(255), + parameters json +); + +CREATE TABLE events ( + id serial primary key, + created_at timestamp default now(), + type varchar(255) NOT NULL, + created_by varchar(255) NOT NULL, + data json +); + `, callback); +}; + + +exports.down = function (db, callback) { + db.runSql(` +DROP TABLE events; +DROP TABLE features; +DROP TABLE strategies; + `, callback); +}; diff --git a/migrations/20141110144153-add-description-to-features.js b/migrations/20141110144153-add-description-to-features.js index 88078c57f6..a20ab6a3a4 100644 --- a/migrations/20141110144153-add-description-to-features.js +++ b/migrations/20141110144153-add-description-to-features.js @@ -1,3 +1,10 @@ 'use strict'; -module.exports = require('../scripts/migration-runner').create('002-add-description-to-features'); +exports.up = function (db, callback) { + db.runSql('ALTER TABLE features ADD "description" text;', callback); +}; + + +exports.down = function (db, callback) { + db.runSql('ALTER TABLE features DROP COLUMN "description";', callback); +}; diff --git a/migrations/20141117200435-add-parameters-template-to-strategies.js b/migrations/20141117200435-add-parameters-template-to-strategies.js index f1ca91168e..2a0bcfb98b 100644 --- a/migrations/20141117200435-add-parameters-template-to-strategies.js +++ b/migrations/20141117200435-add-parameters-template-to-strategies.js @@ -1,4 +1,10 @@ 'use strict'; -module.exports = require('../scripts/migration-runner').create('003-add-parameters-template-to-strategies'); +exports.up = function (db, callback) { + db.runSql('ALTER TABLE strategies ADD "parameters_template" json;', callback); +}; + +exports.down = function (db, callback) { + db.runSql('ALTER TABLE strategies DROP COLUMN "parameters_template";', callback); +}; diff --git a/migrations/20141117202209-insert-default-strategy.js b/migrations/20141117202209-insert-default-strategy.js index 4c40ad9d43..af0fe8db61 100644 --- a/migrations/20141117202209-insert-default-strategy.js +++ b/migrations/20141117202209-insert-default-strategy.js @@ -1,3 +1,14 @@ 'use strict'; -module.exports = require('../scripts/migration-runner').create('004-insert-default-strategy'); +exports.up = function (db, callback) { + db.runSql(` +INSERT INTO strategies(name, description) +VALUES ('default', 'Default on/off strategy.'); + `, callback); +}; + + +exports.down = function (db, callback) { + db.runSql(` +DELETE FROM strategies where name='default';`, callback); +}; diff --git a/migrations/20141118071458-default-strategy-event.js b/migrations/20141118071458-default-strategy-event.js index 560b293fe0..d7be785cff 100644 --- a/migrations/20141118071458-default-strategy-event.js +++ b/migrations/20141118071458-default-strategy-event.js @@ -1,3 +1,14 @@ 'use strict'; -module.exports = require('../scripts/migration-runner').create('004-insert-default-strategy-event'); +exports.up = function (db, callback) { + db.runSql(` +INSERT INTO events(type, created_by, data) +VALUES ('strategy-created', 'migration', '{"name":"default","description":"Default on or off Strategy."}'); + `, callback); +}; + + +exports.down = function (db, callback) { + db.runSql(` +delete from events where type='strategy-created' and data->>'name' = 'default';`, callback); +}; diff --git a/migrations/20141215210141-005-archived-flag-to-features.js b/migrations/20141215210141-005-archived-flag-to-features.js index 5f11f0df55..6bb2d440d9 100644 --- a/migrations/20141215210141-005-archived-flag-to-features.js +++ b/migrations/20141215210141-005-archived-flag-to-features.js @@ -1,3 +1,10 @@ 'use strict'; -module.exports = require('../scripts/migration-runner').create('005-archived-flag-to-features'); +exports.up = function (db, callback) { + db.runSql('ALTER TABLE features ADD archived integer DEFAULT 0;', callback); +}; + + +exports.down = function (db, callback) { + db.runSql('ALTER TABLE features DROP COLUMN "archived";', callback); +}; diff --git a/migrations/20150210152531-006-rename-eventtype.js b/migrations/20150210152531-006-rename-eventtype.js index d03c832d3a..24a597850f 100644 --- a/migrations/20150210152531-006-rename-eventtype.js +++ b/migrations/20150210152531-006-rename-eventtype.js @@ -1,3 +1,16 @@ 'use strict'; -module.exports = require('../scripts/migration-runner').create('006-rename-eventtype'); +exports.up = function (db, callback) { + db.runSql(` +UPDATE events SET type='feature-revived' WHERE type='feature-revive'; +UPDATE events SET type='feature-archived' WHERE type='feature-archive'; + `, callback); +}; + + +exports.down = function (db, callback) { + db.runSql(` +UPDATE events SET type='feature-revive' WHERE type='feature-revived'; +UPDATE events SET type='feature-archive' WHERE type='feature-archived'; + `, callback); +}; diff --git a/migrations/20160618193924-add-strategies-to-features.js b/migrations/20160618193924-add-strategies-to-features.js index 9f970332b2..ce9aae1932 100644 --- a/migrations/20160618193924-add-strategies-to-features.js +++ b/migrations/20160618193924-add-strategies-to-features.js @@ -1,3 +1,37 @@ 'use strict'; -module.exports = require('../scripts/migration-runner').create('007-add-strategies-to-features'); +exports.up = function (db, callback) { + db.runSql(` +--create new strategies-column +ALTER TABLE features ADD "strategies" json; + +--populate the strategies column +UPDATE features +SET strategies = ('[{"name":"'||f.strategy_name||'","parameters":'||f.parameters||'}]')::json +FROM features as f +WHERE f.name = features.name; + +--delete old strategy-columns +ALTER TABLE features DROP COLUMN "strategy_name"; +ALTER TABLE features DROP COLUMN "parameters"; + `, callback); +}; + + +exports.down = function (db, callback) { + db.runSql(` +--create old columns +ALTER TABLE features ADD "parameters" json; +ALTER TABLE features ADD "strategy_name" varchar(255); + +--populate old columns +UPDATE features +SET strategy_name = f.strategies->0->>'name', + parameters = f.strategies->0->'parameters' +FROM features as f +WHERE f.name = features.name; + +--drop new column +ALTER TABLE features DROP COLUMN "strategies"; + `, callback); +}; diff --git a/migrations/20161027134128-create-metrics.js b/migrations/20161027134128-create-metrics.js index cc43e6f405..27c728cb68 100644 --- a/migrations/20161027134128-create-metrics.js +++ b/migrations/20161027134128-create-metrics.js @@ -1,3 +1,15 @@ 'use strict'; -module.exports = require('../scripts/migration-runner').create('008-create-metrics'); +exports.up = function (db, callback) { + db.runSql(` +CREATE TABLE client_metrics ( + id serial primary key, + created_at timestamp default now(), + metrics json +);`, callback); +}; + + +exports.down = function (db, callback) { + db.runSql('DROP TABLE client_metrics;', callback); +}; diff --git a/migrations/20161205203516-create-client-applications.js b/migrations/20161205203516-create-client-applications.js index e665fda6c3..22ce469ef6 100644 --- a/migrations/20161205203516-create-client-applications.js +++ b/migrations/20161205203516-create-client-applications.js @@ -3,15 +3,15 @@ exports.up = function (db, cb) { db.createTable('client_applications', { - app_name: { type: 'varchar', length: 255, primaryKey: true, notNull: true }, + app_name: { type: 'string', length: 255, primaryKey: true, notNull: true }, created_at: { type: 'timestamp', defaultValue: 'now()' }, updated_at: { type: 'timestamp', defaultValue: 'now()' }, seen_at: { type: 'timestamp' }, strategies: { type: 'json' }, - description: { type: 'varchar', length: 255 }, - icon: { type: 'varchar', length: 255 }, - url: { type: 'varchar', length: 255 }, - color: { type: 'varchar', length: 255 }, + description: { type: 'string', length: 255 }, + icon: { type: 'string', length: 255 }, + url: { type: 'string', length: 255 }, + color: { type: 'string', length: 255 }, }, cb); }; diff --git a/migrations/sql/001-initial-schema.down.sql b/migrations/sql/001-initial-schema.down.sql deleted file mode 100644 index 3c0cf359a8..0000000000 --- a/migrations/sql/001-initial-schema.down.sql +++ /dev/null @@ -1,3 +0,0 @@ -DROP TABLE events; -DROP TABLE features; -DROP TABLE strategies; diff --git a/migrations/sql/001-initial-schema.up.sql b/migrations/sql/001-initial-schema.up.sql deleted file mode 100644 index 3712917b9c..0000000000 --- a/migrations/sql/001-initial-schema.up.sql +++ /dev/null @@ -1,21 +0,0 @@ -CREATE TABLE strategies ( - created_at timestamp default now(), - name varchar(255) PRIMARY KEY NOT NULL, - description text -); - -CREATE TABLE features ( - created_at timestamp default now(), - name varchar(255) PRIMARY KEY NOT NULL, - enabled integer default 0, - strategy_name varchar(255), - parameters json -); - -CREATE TABLE events ( - id serial primary key, - created_at timestamp default now(), - type varchar(255) NOT NULL, - created_by varchar(255) NOT NULL, - data json -); diff --git a/migrations/sql/002-add-description-to-features.down.sql b/migrations/sql/002-add-description-to-features.down.sql deleted file mode 100644 index 95bc6040d0..0000000000 --- a/migrations/sql/002-add-description-to-features.down.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE features DROP COLUMN "description"; diff --git a/migrations/sql/002-add-description-to-features.up.sql b/migrations/sql/002-add-description-to-features.up.sql deleted file mode 100644 index d4057d6ff7..0000000000 --- a/migrations/sql/002-add-description-to-features.up.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE features ADD "description" text; diff --git a/migrations/sql/003-add-parameters-template-to-strategies.down.sql b/migrations/sql/003-add-parameters-template-to-strategies.down.sql deleted file mode 100644 index 103a1caecd..0000000000 --- a/migrations/sql/003-add-parameters-template-to-strategies.down.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE strategies DROP COLUMN "parameters_template"; \ No newline at end of file diff --git a/migrations/sql/003-add-parameters-template-to-strategies.up.sql b/migrations/sql/003-add-parameters-template-to-strategies.up.sql deleted file mode 100644 index 6c5055b4d6..0000000000 --- a/migrations/sql/003-add-parameters-template-to-strategies.up.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE strategies ADD "parameters_template" json; diff --git a/migrations/sql/004-insert-default-strategy-event.down.sql b/migrations/sql/004-insert-default-strategy-event.down.sql deleted file mode 100644 index 118d29e4ae..0000000000 --- a/migrations/sql/004-insert-default-strategy-event.down.sql +++ /dev/null @@ -1 +0,0 @@ -delete from events where type='strategy-created' and data->>'name' = 'default'; \ No newline at end of file diff --git a/migrations/sql/004-insert-default-strategy-event.up.sql b/migrations/sql/004-insert-default-strategy-event.up.sql deleted file mode 100644 index 867787fa5d..0000000000 --- a/migrations/sql/004-insert-default-strategy-event.up.sql +++ /dev/null @@ -1 +0,0 @@ -INSERT INTO events(type, created_by, data) values ('strategy-created', 'migration', '{"name":"default","description":"Default on or off Strategy."}'); \ No newline at end of file diff --git a/migrations/sql/004-insert-default-strategy.down.sql b/migrations/sql/004-insert-default-strategy.down.sql deleted file mode 100644 index 170908dda3..0000000000 --- a/migrations/sql/004-insert-default-strategy.down.sql +++ /dev/null @@ -1 +0,0 @@ -DELETE FROM strategies where name='default'; \ No newline at end of file diff --git a/migrations/sql/004-insert-default-strategy.up.sql b/migrations/sql/004-insert-default-strategy.up.sql deleted file mode 100644 index bbb36cdad5..0000000000 --- a/migrations/sql/004-insert-default-strategy.up.sql +++ /dev/null @@ -1 +0,0 @@ -INSERT INTO strategies(name, description) values ('default', 'Default on/off strategy.'); \ No newline at end of file diff --git a/migrations/sql/005-archived-flag-to-features.down.sql b/migrations/sql/005-archived-flag-to-features.down.sql deleted file mode 100644 index 4330a640ed..0000000000 --- a/migrations/sql/005-archived-flag-to-features.down.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE features DROP COLUMN "archived"; \ No newline at end of file diff --git a/migrations/sql/005-archived-flag-to-features.up.sql b/migrations/sql/005-archived-flag-to-features.up.sql deleted file mode 100644 index 6676bed910..0000000000 --- a/migrations/sql/005-archived-flag-to-features.up.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE features ADD archived integer DEFAULT 0; \ No newline at end of file diff --git a/migrations/sql/006-rename-eventtype.down.sql b/migrations/sql/006-rename-eventtype.down.sql deleted file mode 100644 index 787edface4..0000000000 --- a/migrations/sql/006-rename-eventtype.down.sql +++ /dev/null @@ -1,2 +0,0 @@ -UPDATE events SET type='feature-revive' WHERE type='feature-revived'; -UPDATE events SET type='feature-archive' WHERE type='feature-archived'; \ No newline at end of file diff --git a/migrations/sql/006-rename-eventtype.up.sql b/migrations/sql/006-rename-eventtype.up.sql deleted file mode 100644 index cfb23b849d..0000000000 --- a/migrations/sql/006-rename-eventtype.up.sql +++ /dev/null @@ -1,2 +0,0 @@ -UPDATE events SET type='feature-revived' WHERE type='feature-revive'; -UPDATE events SET type='feature-archived' WHERE type='feature-archive'; \ No newline at end of file diff --git a/migrations/sql/007-add-strategies-to-features.down.sql b/migrations/sql/007-add-strategies-to-features.down.sql deleted file mode 100644 index 45adc8d9d4..0000000000 --- a/migrations/sql/007-add-strategies-to-features.down.sql +++ /dev/null @@ -1,13 +0,0 @@ ---create old columns -ALTER TABLE features ADD "parameters" json; -ALTER TABLE features ADD "strategy_name" varchar(255); - ---populate old columns -UPDATE features -SET strategy_name = f.strategies->0->>'name', - parameters = f.strategies->0->'parameters' -FROM features as f -WHERE f.name = features.name; - ---drop new column -ALTER TABLE features DROP COLUMN "strategies"; diff --git a/migrations/sql/007-add-strategies-to-features.up.sql b/migrations/sql/007-add-strategies-to-features.up.sql deleted file mode 100644 index ea01049bd3..0000000000 --- a/migrations/sql/007-add-strategies-to-features.up.sql +++ /dev/null @@ -1,12 +0,0 @@ ---create new strategies-column -ALTER TABLE features ADD "strategies" json; - ---populate the strategies column -UPDATE features -SET strategies = ('[{"name":"'||f.strategy_name||'","parameters":'||f.parameters||'}]')::json -FROM features as f -WHERE f.name = features.name; - ---delete old strategy-columns -ALTER TABLE features DROP COLUMN "strategy_name"; -ALTER TABLE features DROP COLUMN "parameters"; diff --git a/migrations/sql/008-create-metrics.down.sql b/migrations/sql/008-create-metrics.down.sql deleted file mode 100644 index 88a3e5a53e..0000000000 --- a/migrations/sql/008-create-metrics.down.sql +++ /dev/null @@ -1,2 +0,0 @@ ---drop new metrics table -DROP TABLE client_metrics; \ No newline at end of file diff --git a/migrations/sql/008-create-metrics.up.sql b/migrations/sql/008-create-metrics.up.sql deleted file mode 100644 index 7b0d95f3f3..0000000000 --- a/migrations/sql/008-create-metrics.up.sql +++ /dev/null @@ -1,6 +0,0 @@ ---create new metrics table -CREATE TABLE client_metrics ( - id serial primary key, - created_at timestamp default now(), - metrics json -); \ No newline at end of file diff --git a/scripts/migration-runner.js b/scripts/migration-runner.js deleted file mode 100644 index 8624c5dcf3..0000000000 --- a/scripts/migration-runner.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict'; - -const fs = require('fs'); -const util = require('util'); -const path = require('path'); - -const runMigration = function (migrationPath, db, callback) { - db.runSql(fs.readFileSync(migrationPath, { encoding: 'utf8' }), callback); -}; - -module.exports = { - create (name) { - const format = path.resolve(__dirname, '../migrations/sql/%s.%s.sql'); - - return { - up: runMigration.bind(null, util.format(format, name, 'up')), - down: runMigration.bind(null, util.format(format, name, 'down')), - }; - }, -};