diff --git a/pg4wp/rewriters/CreateTableSQLRewriter.php b/pg4wp/rewriters/CreateTableSQLRewriter.php index 6120ca7..bc88bdd 100644 --- a/pg4wp/rewriters/CreateTableSQLRewriter.php +++ b/pg4wp/rewriters/CreateTableSQLRewriter.php @@ -50,7 +50,7 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter $sql = trim($sql); // Add a slash if needed - if (substr($sql,strlen($sql)-1, 1) != ";") { + if (substr($sql, strlen($sql) - 1, 1) != ";") { $sql = $sql . ";"; } @@ -92,18 +92,18 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter } // Support for UNIQUE INDEX creation - $pattern = '/,\s*(UNIQUE |)KEY\s+(`[^`]+`|\w+)\s+\(([^)]+)\)/'; + $pattern = '/,\s*(UNIQUE |)KEY\s+(`[^`]+`|\w+)\s+\((.*?)\)(?!\))/'; if(preg_match_all($pattern, $sql, $matches, PREG_SET_ORDER)) { foreach($matches as $match) { $unique = $match[1]; $index = $match[2]; $columns = $match[3]; - // Removing backticks from the index names + // Removing backticks from the index names $index = str_replace('`', '', $index); - // Removing backticks from the columns - $columns = str_replace('`', '', $columns); + // Removing backticks and key length constraints from the columns + $columns = preg_replace(["/`/", "/\(\d+\)/"], '', $columns); // Creating a unique index name $indexName = $table . '_' . $index; diff --git a/tests/rewriteTest.php b/tests/rewriteTest.php index 18bd6e4..3ccc2e3 100644 --- a/tests/rewriteTest.php +++ b/tests/rewriteTest.php @@ -304,6 +304,36 @@ final class rewriteTest extends TestCase } + public function test_it_can_create_keys_with_length() + { + $sql = <<assertSame(trim($expected), trim($postgresql)); + } + protected function setUp(): void