diff options
author | Nathan Guse <nathaniel.guse@gmail.com> | 2013-01-10 15:09:51 -0600 |
---|---|---|
committer | Nathan Guse <nathaniel.guse@gmail.com> | 2013-01-10 15:09:51 -0600 |
commit | d50500860fe44a78c8f29e0f2382b96da17c0b62 (patch) | |
tree | 77d9b2691d02fc77fd786ae28cae5cba4ef1084b /tests/dbal | |
parent | dbe71bb170dc684311174bb025696c81f1d50883 (diff) | |
download | forums-d50500860fe44a78c8f29e0f2382b96da17c0b62.tar forums-d50500860fe44a78c8f29e0f2382b96da17c0b62.tar.gz forums-d50500860fe44a78c8f29e0f2382b96da17c0b62.tar.bz2 forums-d50500860fe44a78c8f29e0f2382b96da17c0b62.tar.xz forums-d50500860fe44a78c8f29e0f2382b96da17c0b62.zip |
[feature/migrations] Store depends on in the database (serialized)
This is required so that when migrations are reverted we can check through
all installed migrations and make sure that all dependencies are handled
properly and so that we are only required to load the migrations files
that could be dependent on the ones installed.
I believe in normal proper use the old way might have worked, but in case
something happens and an unrelated migration file is installed, but cannot
be loaded, this makes sure we do not stop everything unless we absolutely
must (one of those files is dependent on something we want to revert).
PHPBB3-9737
Diffstat (limited to 'tests/dbal')
-rw-r--r-- | tests/dbal/fixtures/migrator.xml | 2 | ||||
-rw-r--r-- | tests/dbal/migration/fail.php | 46 | ||||
-rw-r--r-- | tests/dbal/migrator_test.php | 57 |
3 files changed, 91 insertions, 14 deletions
diff --git a/tests/dbal/fixtures/migrator.xml b/tests/dbal/fixtures/migrator.xml index 1f9079c811..25be4d4129 100644 --- a/tests/dbal/fixtures/migrator.xml +++ b/tests/dbal/fixtures/migrator.xml @@ -2,6 +2,7 @@ <dataset> <table name="phpbb_migrations"> <column>migration_name</column> + <column>migration_depends_on</column> <column>migration_schema_done</column> <column>migration_data_done</column> <column>migration_data_state</column> @@ -9,6 +10,7 @@ <column>migration_end_time</column> <row> <value>installed_migration</value> + <value></value> <value>1</value> <value>1</value> <value></value> diff --git a/tests/dbal/migration/fail.php b/tests/dbal/migration/fail.php new file mode 100644 index 0000000000..8b5c521e09 --- /dev/null +++ b/tests/dbal/migration/fail.php @@ -0,0 +1,46 @@ +<?php +/** +* +* @package migration +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License v2 +* +*/ + +class phpbb_dbal_migration_fail extends phpbb_db_migration +{ + function depends_on() + { + return array(); + } + + function update_schema() + { + return array( + 'add_columns' => array( + $this->table_prefix . 'config' => array( + 'test_column' => array('BOOL', 1), + ), + ), + ); + } + + function revert_schema() + { + return array( + 'drop_columns' => array( + $this->table_prefix . 'config' => array( + 'test_column', + ), + ), + ); + } + + function update_data() + { + return array( + array('config.add', array('foobar3', true)), + array('config.update', array('does_not_exist', true)), + ); + } +} diff --git a/tests/dbal/migrator_test.php b/tests/dbal/migrator_test.php index 84bcb109b2..69db7ca047 100644 --- a/tests/dbal/migrator_test.php +++ b/tests/dbal/migrator_test.php @@ -18,6 +18,7 @@ require_once dirname(__FILE__) . '/migration/if.php'; require_once dirname(__FILE__) . '/migration/recall.php'; require_once dirname(__FILE__) . '/migration/revert.php'; require_once dirname(__FILE__) . '/migration/revert_with_dependency.php'; +require_once dirname(__FILE__) . '/migration/fail.php'; class phpbb_dbal_migrator_test extends phpbb_database_test_case { @@ -163,8 +164,8 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case $this->migrator->set_migrations(array('phpbb_dbal_migration_revert', 'phpbb_dbal_migration_revert_with_dependency')); - $this->assertFalse($this->migrator->migration_installed('phpbb_dbal_migration_revert')); - $this->assertFalse($this->migrator->migration_installed('phpbb_dbal_migration_revert_with_dependency')); + $this->assertFalse($this->migrator->migration_state('phpbb_dbal_migration_revert')); + $this->assertFalse($this->migrator->migration_state('phpbb_dbal_migration_revert_with_dependency')); // Install the migration first while (!$this->migrator->finished()) @@ -172,8 +173,8 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case $this->migrator->update(); } - $this->assertTrue($this->migrator->migration_installed('phpbb_dbal_migration_revert')); - $this->assertTrue($this->migrator->migration_installed('phpbb_dbal_migration_revert_with_dependency')); + $this->assertTrue($this->migrator->migration_state('phpbb_dbal_migration_revert') !== false); + $this->assertTrue($this->migrator->migration_state('phpbb_dbal_migration_revert_with_dependency') !== false); $this->assertSqlResultEquals( array(array('bar_column' => '1')), @@ -183,25 +184,53 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case $this->assertTrue(isset($this->config['foobartest'])); - while ($this->migrator->migration_installed('phpbb_dbal_migration_revert')) + while ($this->migrator->migration_state('phpbb_dbal_migration_revert') !== false) { $this->migrator->revert('phpbb_dbal_migration_revert'); } - $this->assertFalse($this->migrator->migration_installed('phpbb_dbal_migration_revert')); - $this->assertFalse($this->migrator->migration_installed('phpbb_dbal_migration_revert_with_dependency')); + $this->assertFalse($this->migrator->migration_state('phpbb_dbal_migration_revert')); + $this->assertFalse($this->migrator->migration_state('phpbb_dbal_migration_revert_with_dependency')); $this->assertFalse(isset($this->config['foobartest'])); + $sql = 'SELECT * FROM phpbb_config'; + $result = $this->db->sql_query_limit($sql, 1); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + if (isset($row['bar_column'])) + { + $this->fail('Revert did not remove test_column.'); + } + } + + public function test_fail() + { + $this->migrator->set_migrations(array('phpbb_dbal_migration_fail')); + + $this->assertFalse(isset($this->config['foobar3'])); + try { - // Should cause an error - $this->assertSqlResultEquals( - false, - "SELECT bar_column FROM phpbb_config WHERE config_name = 'foo'", - 'Revert did not remove bar_column.' - ); + while (!$this->migrator->finished()) + { + $this->migrator->update(); + } + } + catch (phpbb_db_migration_exception $e) {} + + // Failure should have caused an automatic roll-back, so this should not exist. + $this->assertFalse(isset($this->config['foobar3'])); + + $sql = 'SELECT * FROM phpbb_config'; + $result = $this->db->sql_query_limit($sql, 1); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + if (isset($row['test_column'])) + { + $this->fail('Revert did not remove test_column.'); } - catch (Exception $e) {} } } |