mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	Cleanup legacy migrations
This commit is contained in:
		
							parent
							
								
									b67f9a6487
								
							
						
					
					
						commit
						2bd7b24dea
					
				| @ -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); | ||||
| }; | ||||
| ```  | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
| @ -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);
 | ||||
| }; | ||||
|  | ||||
| @ -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); | ||||
| }; | ||||
|  | ||||
| @ -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); | ||||
| }; | ||||
|  | ||||
| @ -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);
 | ||||
| }; | ||||
|  | ||||
| @ -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);
 | ||||
| }; | ||||
|  | ||||
| @ -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); | ||||
| }; | ||||
|  | ||||
| @ -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);
 | ||||
| }; | ||||
|  | ||||
| @ -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);
 | ||||
| }; | ||||
|  | ||||
| @ -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); | ||||
| }; | ||||
|  | ||||
| @ -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); | ||||
| }; | ||||
| 
 | ||||
|  | ||||
| @ -1,3 +0,0 @@ | ||||
| DROP TABLE events; | ||||
| DROP TABLE features; | ||||
| DROP TABLE strategies; | ||||
| @ -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 | ||||
| ); | ||||
| @ -1 +0,0 @@ | ||||
| ALTER TABLE features DROP COLUMN "description"; | ||||
| @ -1 +0,0 @@ | ||||
| ALTER TABLE features ADD "description" text; | ||||
| @ -1 +0,0 @@ | ||||
| ALTER TABLE strategies DROP COLUMN "parameters_template"; | ||||
| @ -1 +0,0 @@ | ||||
| ALTER TABLE strategies ADD "parameters_template" json; | ||||
| @ -1 +0,0 @@ | ||||
| delete from events where type='strategy-created' and data->>'name' = 'default'; | ||||
| @ -1 +0,0 @@ | ||||
| INSERT INTO events(type, created_by, data) values ('strategy-created', 'migration', '{"name":"default","description":"Default on or off Strategy."}'); | ||||
| @ -1 +0,0 @@ | ||||
| DELETE FROM strategies where name='default'; | ||||
| @ -1 +0,0 @@ | ||||
| INSERT INTO strategies(name, description) values ('default', 'Default on/off strategy.'); | ||||
| @ -1 +0,0 @@ | ||||
| ALTER TABLE features DROP COLUMN "archived"; | ||||
| @ -1 +0,0 @@ | ||||
| ALTER TABLE features ADD archived integer DEFAULT 0; | ||||
| @ -1,2 +0,0 @@ | ||||
| UPDATE events SET type='feature-revive' WHERE type='feature-revived'; | ||||
| UPDATE events SET type='feature-archive' WHERE type='feature-archived'; | ||||
| @ -1,2 +0,0 @@ | ||||
| UPDATE events SET type='feature-revived' WHERE type='feature-revive'; | ||||
| UPDATE events SET type='feature-archived' WHERE type='feature-archive'; | ||||
| @ -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"; | ||||
| @ -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"; | ||||
| @ -1,2 +0,0 @@ | ||||
| --drop new metrics table | ||||
| DROP TABLE client_metrics; | ||||
| @ -1,6 +0,0 @@ | ||||
| --create new metrics table | ||||
| CREATE TABLE client_metrics ( | ||||
|   id serial primary key, | ||||
|   created_at timestamp default now(), | ||||
|   metrics json | ||||
| ); | ||||
| @ -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')), | ||||
|         }; | ||||
|     }, | ||||
| }; | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user