Skip to content

Commit

Permalink
Add ability to remove a feature from rollout
Browse files Browse the repository at this point in the history
  • Loading branch information
richardfullmer committed Nov 19, 2015
1 parent 41a2afe commit 9c76ecc
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 2 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
2.0.0
-----

(BC Break) The `StorageInterface` interface had a `remove` function added to its definition. If you have your own implementation of this interface, you'll need to update your implementation to include this functionality.

- Added support for removing a feature [PR #10](https://github.com/opensoft/rollout/pull/10)

1.0.4
-----

- Added redis support [PR #9](https://github.com/opensoft/rollout/pull/9)
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ $rollout->deactivate('chat');

You may wish to disable features programmatically if monitoring tools detect unusually high error rates for example.

Remove a Feature (added in 2.0.0)
---------------------------------

After a feature becomes mainstream or a failed experiment, you may want to remove the feature definition from rollout.

```php
$rollout->remove('chat');
```

Note: If there is still code referencing the feature, it will be recreated with default settings.

Symfony2 Bundle
---------------

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"php": ">=5.3.3"
},
"suggest": {
"doctrine/cache": "For use with the DoctrineCacheStorageAdapter"
"doctrine/cache": "For use with the DoctrineCacheStorageAdapter",
"predis/predis": "For use with the RedisStorageAdapter"
},
"require-dev": {
"phpunit/phpunit": "*",
Expand Down
17 changes: 16 additions & 1 deletion src/Rollout.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public function activate($feature)
}
}


/**
* @param string $feature
*/
Expand Down Expand Up @@ -191,6 +190,22 @@ public function get($feature)
return $f;
}

/**
* Remove a feature definition from rollout
*
* @param string $feature
*/
public function remove($feature)
{
$this->storage->remove($this->key($feature));

$features = $this->features();
if (in_array($feature, $features)) {
$features = array_diff($features, [$feature]);
}
$this->storage->set($this->featuresKey(), implode(',', $features));
}

/**
* @return array
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Storage/ArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@ public function set($key, $value)
$this->storage[$key] = $value;
}

/**
* @param string $key
*/
public function remove($key)
{
if (isset($this->storage[$key])) {
unset($this->storage[$key]);
}
}
}
8 changes: 8 additions & 0 deletions src/Storage/DoctrineCacheStorageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ public function set($key, $value)
{
$this->cache->save($key, $value);
}

/**
* @param string $key
*/
public function remove($key)
{
$this->cache->delete($key);
}
}
12 changes: 12 additions & 0 deletions src/Storage/PDOStorageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class PDOStorageAdapter implements StorageInterface
const STMT_SELECT = 'SELECT settings FROM :table WHERE name = :key';
const STMT_INSERT = 'INSERT INTO :table (name, settings) VALUES (:key, :value)';
const STMT_UPDATE = 'UPDATE :table SET settings = :value WHERE name = :key';
const STMT_DELETE = 'DELETE FROM :table WHERE name = :key';

/**
* @var \PDO
Expand Down Expand Up @@ -69,6 +70,17 @@ public function set($key, $value)
$statement->execute();
}

/**
* @param string $key
*/
public function remove($key)
{
$statement = $this->pdoConnection->prepare($this->getSQLStatement(self::STMT_DELETE));

$statement->bindParam('key', $key);
$statement->execute();
}

/**
* @param string $sql
*
Expand Down
8 changes: 8 additions & 0 deletions src/Storage/RedisStorageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ public function set($key, $value)
{
$this->redis->hset($this->group, $key, json_encode($value));
}

/**
* @inheritdoc
*/
public function remove($key)
{
$this->redis->hdel($this->group, $key);
}
}
6 changes: 6 additions & 0 deletions src/Storage/StorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ public function get($key);
* @return void
*/
public function set($key, $value);

/**
* @param string $key
* @return void
*/
public function remove($key);
}
10 changes: 10 additions & 0 deletions tests/RolloutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,16 @@ public function testGet()
$this->assertEmpty($feature->getUsers());
$this->assertEquals(100, $feature->getPercentage());
}

public function testRemove()
{
$this->rollout->activate('signup');
$feature = $this->rollout->get('signup');
$this->assertEquals('signup', $feature->getName());

$this->rollout->remove('signup');
$this->assertNotContains('signup', $this->rollout->features());
}
}


Expand Down
17 changes: 17 additions & 0 deletions tests/Storage/PDOStorageAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ public function testGet()
$this->assertEquals('success', $result);
}

public function testRemove()
{
$statement = $this->mockPDOStatement();

$statement->expects($this->once())
->method('bindParam')
->with('key', 'test');

$statement->expects($this->once())
->method('execute');

$pdo = $this->mockPDO($this->prepareSQL(PDOStorageAdapter::STMT_DELETE), $statement);

$adapter = new PDOStorageAdapter($pdo);
$adapter->remove('test');
}

public function testTableName()
{
$statement = $this->mockPDOStatement();
Expand Down
12 changes: 12 additions & 0 deletions tests/Storage/RedisStorageAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ public function testSet()
$adapter->set('key', 'value');
}

public function testRemove()
{
$this->redis->expects($this->once())
->method('hdel')
->with(RedisStorageAdapter::DEFAULT_GROUP, 'key');

$adapter = new RedisStorageAdapter($this->redis);

$adapter->remove('key');
}

public function testSetWithCustomGroup()
{
$this->redis->expects($this->once())
Expand All @@ -92,4 +103,5 @@ interface mockRedis
{
public function hget($key, $field);
public function hset($key, $field, $value);
public function hdel($key, $field);
}

0 comments on commit 9c76ecc

Please sign in to comment.