1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-01-19 07:08:09 +01:00

[ticket/12812] Add a migrator tool for config_text database changes

PHPBB3-12812
This commit is contained in:
Matt Friedman 2014-07-05 00:57:34 -07:00
parent 53ff23671c
commit 606d82f7dd
4 changed files with 218 additions and 0 deletions

View File

@ -29,6 +29,13 @@ services:
tags:
- { name: migrator.tool }
migrator.tool.config_text:
class: phpbb\db\migration\tool\config_text
arguments:
- @config_text
tags:
- { name: migrator.tool }
migrator.tool.module:
class: phpbb\db\migration\tool\module
arguments:

View File

@ -0,0 +1,125 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\migration\tool;
/**
* Migration config_text tool
*/
class config_text implements \phpbb\db\migration\tool\tool_interface
{
/** @var \phpbb\config\db_text */
protected $config_text;
/**
* Constructor
*
* @param \phpbb\config\db_text $config_text
*/
public function __construct(\phpbb\config\db_text $config_text)
{
$this->config_text = $config_text;
}
/**
* {@inheritdoc}
*/
public function get_name()
{
return 'config_text';
}
/**
* Add a config_text setting.
*
* @param string $config_name The name of the config_text setting
* you would like to add
* @param mixed $config_value The value of the config_text setting
* @return null
*/
public function add($config_name, $config_value)
{
if (!is_null($this->config_text->get($config_name)))
{
return;
}
$this->config_text->set($config_name, $config_value);
}
/**
* Update an existing config_text setting.
*
* @param string $config_name The name of the config_text setting you would
* like to update
* @param mixed $config_value The value of the config_text setting
* @return null
* @throws \phpbb\db\migration\exception
*/
public function update($config_name, $config_value)
{
if (is_null($this->config_text->get($config_name)))
{
throw new \phpbb\db\migration\exception('CONFIG_NOT_EXIST', $config_name);
}
$this->config_text->set($config_name, $config_value);
}
/**
* Remove an existing config_text setting.
*
* @param string $config_name The name of the config_text setting you would
* like to remove
* @return null
*/
public function remove($config_name)
{
if (is_null($this->config_text->get($config_name)))
{
return;
}
$this->config_text->delete($config_name);
}
/**
* {@inheritdoc}
*/
public function reverse()
{
$arguments = func_get_args();
$original_call = array_shift($arguments);
$call = false;
switch ($original_call)
{
case 'add':
$call = 'remove';
break;
case 'remove':
$call = 'add';
if (sizeof($arguments) == 1)
{
$arguments[] = '';
}
break;
}
if ($call)
{
return call_user_func_array(array(&$this, $call), $arguments);
}
}
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
<table name="phpbb_config_text">
<column>config_name</column>
<column>config_value</column>
</table>
</dataset>

View File

@ -0,0 +1,79 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
class phpbb_dbal_migrator_tool_config_text_test extends phpbb_database_test_case
{
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/migrator_config_text.xml');
}
public function setup()
{
global $db;
parent::setup();
$db = $this->db = $this->new_dbal();
$this->config_text = new \phpbb\config\db_text($this->db, 'phpbb_config_text');
$this->tool = new \phpbb\db\migration\tool\config_text($this->config_text);
}
public function test_add()
{
$this->tool->add('foo', 'bar');
$this->assertEquals('bar', $this->config_text->get('foo'));
}
public function test_add_twice()
{
$this->tool->add('foo', 'bar');
$this->assertEquals('bar', $this->config_text->get('foo'));
$this->tool->add('foo', 'bar2');
$this->assertEquals('bar', $this->config_text->get('foo'));
}
public function test_update()
{
$this->config_text->set('foo', 'bar');
$this->tool->update('foo', 'bar2');
$this->assertEquals('bar2', $this->config_text->get('foo'));
}
public function test_remove()
{
$this->config_text->set('foo', 'bar');
$this->tool->remove('foo');
$this->assertTrue(is_null($this->config_text->get('foo')));
}
public function test_reverse_add()
{
$this->config_text->set('foo', 'bar');
$this->tool->reverse('add', 'foo');
$this->assertTrue(is_null($this->config_text->get('foo')));
}
public function test_reverse_remove()
{
$this->config_text->delete('foo');
$this->tool->reverse('remove', 'foo');
$this->assertEquals('', $this->config_text->get('foo'));
}
}