1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-01 00:08:27 +01:00

Merge pull request #181 from Unleash/cleanup_db_migrations

Cleanup client_instances
This commit is contained in:
Ivar Conradi Østhus 2016-12-10 11:43:30 +01:00 committed by GitHub
commit b62ee42c9a
33 changed files with 182 additions and 114 deletions

View File

@ -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);
};
```

View File

@ -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);
};

View File

@ -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);
};

View File

@ -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);
};

View File

@ -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);
};

View File

@ -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);
};

View File

@ -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);
};

View File

@ -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);
};

View File

@ -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);
};

View File

@ -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);
};

View File

@ -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);
};

View File

@ -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);
};

View File

@ -1,3 +0,0 @@
DROP TABLE events;
DROP TABLE features;
DROP TABLE strategies;

View File

@ -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
);

View File

@ -1 +0,0 @@
ALTER TABLE features DROP COLUMN "description";

View File

@ -1 +0,0 @@
ALTER TABLE features ADD "description" text;

View File

@ -1 +0,0 @@
ALTER TABLE strategies DROP COLUMN "parameters_template";

View File

@ -1 +0,0 @@
ALTER TABLE strategies ADD "parameters_template" json;

View File

@ -1 +0,0 @@
delete from events where type='strategy-created' and data->>'name' = 'default';

View File

@ -1 +0,0 @@
INSERT INTO events(type, created_by, data) values ('strategy-created', 'migration', '{"name":"default","description":"Default on or off Strategy."}');

View File

@ -1 +0,0 @@
DELETE FROM strategies where name='default';

View File

@ -1 +0,0 @@
INSERT INTO strategies(name, description) values ('default', 'Default on/off strategy.');

View File

@ -1 +0,0 @@
ALTER TABLE features DROP COLUMN "archived";

View File

@ -1 +0,0 @@
ALTER TABLE features ADD archived integer DEFAULT 0;

View File

@ -1,2 +0,0 @@
UPDATE events SET type='feature-revive' WHERE type='feature-revived';
UPDATE events SET type='feature-archive' WHERE type='feature-archived';

View File

@ -1,2 +0,0 @@
UPDATE events SET type='feature-revived' WHERE type='feature-revive';
UPDATE events SET type='feature-archived' WHERE type='feature-archive';

View File

@ -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";

View File

@ -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";

View File

@ -1,2 +0,0 @@
--drop new metrics table
DROP TABLE client_metrics;

View File

@ -1,6 +0,0 @@
--create new metrics table
CREATE TABLE client_metrics (
id serial primary key,
created_at timestamp default now(),
metrics json
);

View File

@ -1 +0,0 @@
DROP TABLE client_instances;

View File

@ -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()
);

View File

@ -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')),
};
},
};