Skip to content

Commit 3de1eaa

Browse files
authored
Merge pull request #132 from PostgreSQL-For-Wordpress/hotfix/primary_key_rewriting
remove key names which are not supported during key creation in postgres
2 parents 0873fda + 07b49db commit 3de1eaa

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

pg4wp/rewriters/CreateTableSQLRewriter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,16 @@ public function rewrite(): string
8282
$sql = preg_replace($pattern, '', $sql);
8383

8484

85+
// Rewrite unique keys to just be UNIQUE without a key name
8586
$pattern = "/(,\s*)?UNIQUE KEY\s+[a-zA-Z0-9_]+\s+(\([a-zA-Z0-9_,\s]+\))/";
8687
$replacement = "$1UNIQUE $2";
8788
$sql = preg_replace($pattern, $replacement, $sql);
8889

90+
// Rewrite Primary keys to not have a key name
91+
$pattern = '/PRIMARY KEY\s+\w+\s*\((.*?)\)/';
92+
$replacement = 'PRIMARY KEY ($1)';
93+
$sql = preg_replace($pattern, $replacement, $sql);
94+
8995
return $sql;
9096
}
9197

tests/rewriteTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ public function test_it_rewrites_mediumints()
702702
mysqlcolumn_detail text NOT NULL DEFAULT '',
703703
type varchar(210) NOT NULL DEFAULT 'post',
704704
item bigint NOT NULL DEFAULT '0',
705-
PRIMARY KEY doctermitem (doc, term, item)
705+
PRIMARY KEY (doc, term, item)
706706
);
707707
SQL;
708708

0 commit comments

Comments
 (0)