-
-
Notifications
You must be signed in to change notification settings - Fork 704
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for aggregate_strategies in the API.
This commit changes the features-tables: - drop columns 'strategy' and 'parameters' - add column 'strategies' of type json. - migrates existing strategy-mappings in to the new format. The idea is that the 'strategies' column should contain a json-array of strategy-configuration for the toggle: ``` [{ "name" : "strategy1", "parameters": { "name": "vale" } }] ``` To make sure to not break exiting clients the api is extended with a mapping layer (adding old fields to the json-respons, and mapping to the new format on create/update a feature toggle. this commit is first step in solving #102
- Loading branch information
Showing
10 changed files
with
257 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
function addOldFields (feature) { | ||
let modifiedFeature = Object.assign({}, feature); | ||
modifiedFeature.strategy = feature.strategies[0].name; | ||
modifiedFeature.parameters = Object.assign({}, feature.strategies[0].parameters); | ||
return modifiedFeature; | ||
} | ||
|
||
function isOldFomrat (feature) { | ||
return feature.strategy && !feature.strategies; | ||
} | ||
|
||
function toNewFormat (feature) { | ||
if (isOldFomrat(feature)) { | ||
return { | ||
name: feature.name, | ||
description: feature.description, | ||
enabled: feature.enabled, | ||
strategies: [ | ||
{ | ||
name: feature.strategy, | ||
parameters: Object.assign({}, feature.parameters), | ||
}, | ||
], | ||
}; | ||
} | ||
return feature; | ||
} | ||
|
||
module.exports = { addOldFields, toNewFormat }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/unleash-api/migrations/20160618193924-add-strategies-to-features.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
'use strict'; | ||
module.exports = require('../scripts/migration-runner').create('007-add-strategies-to-features'); |
13 changes: 13 additions & 0 deletions
13
packages/unleash-api/migrations/sql/007-add-strategies-to-features.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--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"; |
12 changes: 12 additions & 0 deletions
12
packages/unleash-api/migrations/sql/007-add-strategies-to-features.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Schema | ||
|
||
## Table: _migrations_ | ||
|
||
Used by db-migrate module to keep track of migrations. | ||
|
||
| NAME | TYPE | SIZE | NULLABLE | COLUMN_DEF | | ||
| ----------- | --------- | ----------- | -------- | -------------------------------------- | | ||
| id | serial | 10 | 0 | nextval('migrations_id_seq'::regclass) | | ||
| name | varchar | 255 | 0 | (null) | | ||
| run_on | timestamp | 29 | 0 | (null) | | ||
|
||
|
||
|
||
## Table: _events_ | ||
| NAME | TYPE | SIZE | NULLABLE | COLUMN_DEF | | ||
| ----------- | --------- | ----------- | -------- | ---------------------------------- | | ||
| id | serial | 10 | 0 | nextval('events_id_seq'::regclass) | | ||
| created_at | timestamp | 29 | 1 | now() | | ||
| type | varchar | 255 | 0 | (null) | | ||
| created_by | varchar | 255 | 0 | (null) | | ||
| data | json | 2147483647 | 1 | (null) | | ||
|
||
|
||
## Table: _strategies_loc | ||
| NAME | TYPE | SIZE | NULLABLE | COLUMN_DEF | | ||
| ------------------- | --------- | ----------- | -------- | ---------- | | ||
| created_at | timestamp | 29 | 1 | now() | | ||
| name | varchar | 255 | 0 | (null) | | ||
| description | text | 2147483647 | 1 | (null) | | ||
| parameters_template | json | 2147483647 | 1 | (null) | | ||
|
||
|
||
## Table: _features_ | ||
|
||
| **NAME** | **TYPE** | **SIZE** | **NULLABLE** | **COLUMN_DEF** | **COMMENT** | | ||
| ------------- | --------- | ----------- | ------------ | -------------- | ----------- | | ||
| created_at | timestamp | 29 | 1 | now() | | | ||
| name | varchar | 255 | 0 | (null) | | | ||
| enabled | int4 | 10 | 1 | 0 | | | ||
| description | text | 2147483647 | 1 | (null) | | | ||
| archived | int4 | 10 | 1 | 0 | | | ||
| parameters | json | 2147483647 | 1 | (null) | deprecated (*) | | ||
| strategy_name | varchar | 255 | 1 | (null) | deprecated (*) | | ||
| strategies | json | 2147483647 | 1 | (null) | | | ||
|
||
(*) we migrated from `parmaters` and `strategy_name` to `strategies` which should contain an array of these. | ||
|
||
For [aggregate strategies](https:/finn-no/unleash/issues/102) we had the following sql to migrate to the strategies column: | ||
|
||
```sql | ||
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; | ||
``` | ||
|
||
In order to migrate back, one can use the following sql (it will loose all, but the first activation strategy): | ||
|
||
```sql | ||
UPDATE features | ||
SET strategy_name = f.strategies->0->>'name', | ||
parameters = f.strategies->0->'parameters' | ||
FROM features as f | ||
WHERE f.name = features.name; | ||
|
||
ALTER TABLE features DROP COLUMN "strategies"; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.