From 84d195ac218bfc09c76f588131cb921864a614ca Mon Sep 17 00:00:00 2001 From: rxu Date: Tue, 17 Jun 2025 12:14:01 +0700 Subject: [PATCH] [ticket/17525] Add migration renaming index test PHPBB-17525 --- tests/dbal/migration/schema_index.php | 65 +++++++++++++++++++++++++++ tests/dbal/migrator_test.php | 24 ++++++++++ 2 files changed, 89 insertions(+) create mode 100644 tests/dbal/migration/schema_index.php diff --git a/tests/dbal/migration/schema_index.php b/tests/dbal/migration/schema_index.php new file mode 100644 index 0000000000..1ea8eb7c78 --- /dev/null +++ b/tests/dbal/migration/schema_index.php @@ -0,0 +1,65 @@ + +* @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_migration_schema_index extends \phpbb\db\migration\migration +{ + function update_schema() + { + return [ + 'add_tables' => [ + $this->table_prefix . 'foobar1' => [ + 'COLUMNS' => [ + 'user_id' => ['UINT', 0], + 'username' => ['VCHAR:50', 0], + ], + 'KEYS' => [ + 'tstidx_user_id' => ['UNIQUE', 'user_id'], + 'tstidx_username' => ['INDEX', 'username'], + ], + ], + $this->table_prefix . 'foobar2' => [ + 'COLUMNS' => [ + 'ban_userid' => ['ULINT', 0], + 'ban_ip' => ['VCHAR:40', ''], + 'ban_reason' => ['VCHAR:100', ''], + ], + 'KEYS' => [ + 'tstidx_ban_userid' => ['UNIQUE', 'ban_userid'], + 'tstidxban_data' => ['INDEX', ['ban_ip', 'ban_reason']], + ], + ], + ], + + 'rename_index' => [ + $this->table_prefix . 'foobar1' => [ + 'tstidx_user_id' => 'fbr1_user_id', + 'tstidx_username' => 'fbr1_username', + ], + $this->table_prefix . 'foobar2' => [ + 'tstidx_ban_userid' => 'fbr2_ban_userid', + 'tstidxban_data' => 'fbr2_ban_data', + ], + ], + ]; + } + + function revert_schema() + { + return [ + 'drop_tables' => [ + $this->table_prefix . 'foobar1', + $this->table_prefix . 'foobar2', + ], + ]; + } +} diff --git a/tests/dbal/migrator_test.php b/tests/dbal/migrator_test.php index 5d16fa63ee..f48ad35275 100644 --- a/tests/dbal/migrator_test.php +++ b/tests/dbal/migrator_test.php @@ -24,6 +24,7 @@ require_once __DIR__ . '/migration/revert_table_with_dependency.php'; require_once __DIR__ . '/migration/fail.php'; require_once __DIR__ . '/migration/installed.php'; require_once __DIR__ . '/migration/schema.php'; +require_once __DIR__ . '/migration/schema_index.php'; class phpbb_dbal_migrator_test extends phpbb_database_test_case { @@ -416,4 +417,27 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case $this->assertFalse($this->db_tools->sql_column_exists('phpbb_config', 'test_column1')); $this->assertFalse($this->db_tools->sql_table_exists('phpbb_foobar')); } + + public function test_rename_index() + { + $this->migrator->set_migrations(array('phpbb_dbal_migration_schema_index')); + + while (!$this->migrator->finished()) + { + $this->migrator->update(); + } + + $this->assertTrue($this->db_tools->sql_unique_index_exists('phpbb_foobar1', 'fbr1_user_id')); + $this->assertTrue($this->db_tools->sql_index_exists('phpbb_foobar1', 'fbr1_username')); + $this->assertTrue($this->db_tools->sql_unique_index_exists('phpbb_foobar2', 'fbr2_ban_userid')); + $this->assertTrue($this->db_tools->sql_index_exists('phpbb_foobar2', 'fbr2_ban_data')); + + while ($this->migrator->migration_state('phpbb_dbal_migration_schema_index')) + { + $this->migrator->revert('phpbb_dbal_migration_schema_index'); + } + + $this->assertFalse($this->db_tools->sql_table_exists('phpbb_foobar1')); + $this->assertFalse($this->db_tools->sql_table_exists('phpbb_foobar2')); + } }