Skip to content

Commit

Permalink
Merge pull request #132 from PostgreSQL-For-Wordpress/hotfix/primary_…
Browse files Browse the repository at this point in the history
…key_rewriting

remove key names which are not supported during key creation in postgres
  • Loading branch information
mattbucci authored Oct 17, 2024
2 parents 0873fda + 07b49db commit 3de1eaa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pg4wp/rewriters/CreateTableSQLRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,16 @@ public function rewrite(): string
$sql = preg_replace($pattern, '', $sql);


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

// Rewrite Primary keys to not have a key name
$pattern = '/PRIMARY KEY\s+\w+\s*\((.*?)\)/';
$replacement = 'PRIMARY KEY ($1)';
$sql = preg_replace($pattern, $replacement, $sql);

return $sql;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/rewriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ public function test_it_rewrites_mediumints()
mysqlcolumn_detail text NOT NULL DEFAULT '',
type varchar(210) NOT NULL DEFAULT 'post',
item bigint NOT NULL DEFAULT '0',
PRIMARY KEY doctermitem (doc, term, item)
PRIMARY KEY (doc, term, item)
);
SQL;

Expand Down

0 comments on commit 3de1eaa

Please sign in to comment.