From b623868756e99b65c0c9f03d09b0f45dad6ef4a4 Mon Sep 17 00:00:00 2001 From: Nick Liu Date: Wed, 2 Aug 2023 15:03:23 +0200 Subject: [PATCH] `db_verify::getIndex()`: Support `index_col_name` optional parts In the MariaDB `CREATE TABLE` [`index_definition`](https://mariadb.com/kb/en/create-table/#index-definitions), the `index_col_name` could have an optional length and a sort order: ``` index_definition: {INDEX|KEY} [index_name] [index_type] (index_col_name,...) [index_option] ... {{{|}}} {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...) [index_option] ... {{{|}}} [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...) [index_option] ... {{{|}}} [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...) [index_option] ... {{{|}}} [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,...) reference_definition index_col_name: col_name [(length)] [ASC | DESC] index_type: USING {BTREE | HASH | RTREE} index_option: [ KEY_BLOCK_SIZE [=] value {{{|}}} index_type {{{|}}} WITH PARSER parser_name {{{|}}} COMMENT 'string' {{{|}}} CLUSTERING={YES| NO} ] [ IGNORED | NOT IGNORED ] reference_definition: REFERENCES tbl_name (index_col_name,...) [MATCH FULL | MATCH PARTIAL | MATCH SIMPLE] [ON DELETE reference_option] [ON UPDATE reference_option] reference_option: RESTRICT | CASCADE | SET NULL | NO ACTION ``` `db_verify::getIndex()` didn't handle this possibility, leading to a database validity check failure despite the index actually existing. Fixes: https://github.com/e107inc/e107/issues/5054 --- e107_handlers/db_verify_class.php | 5 ++- e107_tests/tests/unit/db_verifyTest.php | 47 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/e107_handlers/db_verify_class.php b/e107_handlers/db_verify_class.php index 8a991a073..a572f021c 100755 --- a/e107_handlers/db_verify_class.php +++ b/e107_handlers/db_verify_class.php @@ -1065,9 +1065,8 @@ class db_verify */ function getIndex($data, $print = false) { - // $regex = "/(?:(PRIMARY|UNIQUE|FULLTEXT))?[\s]*?KEY (?: ?`?([\w]*)`?)[\s]* ?(?:\([\s]?`?([\w,]*[\s]?)`?\))?,?/i"; - // $regex = "/(?:(PRIMARY|UNIQUE|FULLTEXT|FOREIGN))?[\s]*?KEY (?: ?`?([\w]*)`?)[\s]* ?(?:\([\s]?([\w\s,`]*[\s]?)`?\))?,?/i"; - $regex = "/(?:(PRIMARY|UNIQUE|FULLTEXT|FOREIGN))?[\s]*?(INDEX|KEY) (?: ?`?([\w]*)`?)[\s]* ?(?:\([\s]?([\w\s,`]*[\s]?)`?\))?,?/i"; + $regex = "/(PRIMARY|UNIQUE|FULLTEXT|FOREIGN)?[\s]*?(INDEX|KEY)[\s]*(?:`?([\w]*)`?)?[\s]*?\(`?([\w]+)`?(?:\s*\(\d+\))?(?:\s*(ASC|DESC))?\)[\s]*,?/i"; + preg_match_all($regex,$data,$m); if (count($m) > 0) diff --git a/e107_tests/tests/unit/db_verifyTest.php b/e107_tests/tests/unit/db_verifyTest.php index ecce2a5db..b2b847b57 100644 --- a/e107_tests/tests/unit/db_verifyTest.php +++ b/e107_tests/tests/unit/db_verifyTest.php @@ -263,6 +263,53 @@ $this->assertEquals($expected,$result); } + /** + * @see https://github.com/e107inc/e107/issues/5054 + */ + public function testGetIndexOptionalLengthAndSortOrder() + { + $data = << + array( + 'type' => '', + 'keyname' => 'field1', + 'field' => 'field1', + ), + 'field2' => + array( + 'type' => '', + 'keyname' => 'field2', + 'field' => 'field2', + ), + 'field3' => + array( + 'type' => '', + 'keyname' => 'field3', + 'field' => 'field3', + ), + 'field4' => + array( + 'type' => '', + 'keyname' => 'field4', + 'field' => 'field4', + ), + ); + + $result = $this->dbv->getIndex($data); + $this->assertEquals($expected,$result); + } + /** * FIXME: This test has no assertions! */