From c47e5d34df840aa4c29241f5eb896a04b0cee225 Mon Sep 17 00:00:00 2001 From: rxu Date: Tue, 10 Jun 2025 13:55:48 +0700 Subject: [PATCH] [ticket/17524] Add index migrator tests PHPBB-17524 --- tests/dbal/migration/schema.php | 20 ++++++++++---- tests/dbal/migrator_test.php | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/tests/dbal/migration/schema.php b/tests/dbal/migration/schema.php index 38663bcc16..d91ed6a316 100644 --- a/tests/dbal/migration/schema.php +++ b/tests/dbal/migration/schema.php @@ -22,12 +22,22 @@ class phpbb_dbal_migration_schema extends \phpbb\db\migration\migration ), ), 'add_tables' => array( - $this->table_prefix . 'foobar' => array( - 'COLUMNS' => array( - 'module_id' => array('UINT:3', NULL, 'auto_increment'), - ), + $this->table_prefix . 'foobar' => [ + 'COLUMNS' => [ + 'module_id' => ['UINT:3', NULL, 'auto_increment'], + 'user_id' => ['ULINT', 0], + 'endpoint' => ['TEXT', ''], + 'expiration_time' => ['TIMESTAMP', 0], + 'p256dh' => ['VCHAR', ''], + 'auth' => ['VCHAR:100', ''], + ], 'PRIMARY_KEY' => 'module_id', - ), + 'KEYS' => [ + 'i_simple' => ['INDEX', ['user_id', 'endpoint:191']], + 'i_uniq' => ['UNIQUE', ['expiration_time', 'p256dh(100)']], + 'i_auth' => ['INDEX', 'auth'], + ], + ], ), ); } diff --git a/tests/dbal/migrator_test.php b/tests/dbal/migrator_test.php index f4b80fc5e9..2ef16282d3 100644 --- a/tests/dbal/migrator_test.php +++ b/tests/dbal/migrator_test.php @@ -412,6 +412,54 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case $this->assertTrue($this->db_tools->sql_column_exists('phpbb_config', 'test_column1')); $this->assertTrue($this->db_tools->sql_table_exists('phpbb_foobar')); + $index_data_row = $this->db_tools->sql_get_table_index_data('phpbb_foobar'); + $is_mysql = $this->db->get_sql_layer() === 'mysqli'; // Index length only applies to MySQL indexes + $is_mssql = in_array($this->db->get_sql_layer(), ['mssqlnative', 'mssql_odbc']); // MSSQL primary index key has 'clustered' flag + foreach ($index_data_row as $index_name => $index_data) + { + switch ($index_name) + { + case 'i_simple': + $this->assertEquals(['user_id', 'endpoint'], $index_data['columns']); + $this->assertEmpty($index_data['flags']); + $this->assertFalse($index_data['is_primary']); + $this->assertFalse($index_data['is_unique']); + $this->assertTrue($index_data['is_simple']); + $this->assertEquals(2, count($index_data['options']['lengths'])); + $this->assertEmpty($index_data['options']['lengths'][0]); + $this->assertEquals($is_mysql ? 191 : null, $index_data['options']['lengths'][1]); + break; + case 'i_uniq': + $this->assertEquals(['expiration_time', 'p256dh'], $index_data['columns']); + $this->assertEmpty($index_data['flags']); + $this->assertFalse($index_data['is_primary']); + $this->assertTrue($index_data['is_unique']); + $this->assertFalse($index_data['is_simple']); + $this->assertEquals(2, count($index_data['options']['lengths'])); + $this->assertEmpty($index_data['options']['lengths'][0]); + $this->assertEquals($is_mysql ? 100 : null, $index_data['options']['lengths'][1]); + break; + case 'i_auth': + $this->assertEquals(['auth'], $index_data['columns']); + $this->assertEmpty($index_data['flags']); + $this->assertFalse($index_data['is_primary']); + $this->assertFalse($index_data['is_unique']); + $this->assertTrue($index_data['is_simple']); + $this->assertEquals(1, count($index_data['options']['lengths'])); + $this->assertEmpty($index_data['options']['lengths'][0]); + break; + default: // Primary key + $this->assertEquals(['module_id'], $index_data['columns']); + $this->assertEquals($is_mssql ? ['clustered'] : [], $index_data['flags']); + $this->assertTrue($index_data['is_primary']); + $this->assertTrue($index_data['is_unique']); + $this->assertFalse($index_data['is_simple']); + $this->assertEquals(1, count($index_data['options']['lengths'])); + $this->assertEmpty($index_data['options']['lengths'][0]); + break; + } + } + while ($this->migrator->migration_state('phpbb_dbal_migration_schema')) { $this->migrator->revert('phpbb_dbal_migration_schema');