Skip to content

Commit 0873fda

Browse files
authored
Merge pull request #131 from PostgreSQL-For-Wordpress/hotfix/numeric-rewriting
Numeric Rewriting should be case insensitive.
2 parents d8b83c8 + b463ead commit 0873fda

File tree

2 files changed

+85
-5
lines changed

2 files changed

+85
-5
lines changed

pg4wp/rewriters/CreateTableSQLRewriter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
2121
' CHARACTER SET utf8' => '',
2222
' DEFAULT CHARSET=utf8' => '',
2323

24-
// For flash-album-gallery plugin
25-
' tinyint' => ' smallint'
24+
' tinyint' => ' smallint',
25+
' mediumint' => ' integer'
2626
];
2727

2828
public function rewrite(): string
@@ -96,7 +96,7 @@ private function rewrite_numeric_type($sql)
9696
$numeric_types_imploded = implode('|', $numeric_types);
9797

9898
// Prepare regex pattern to match 'type(x)'
99-
$pattern = "/(" . $numeric_types_imploded . ")\(\d+\)/";
99+
$pattern = "/(" . $numeric_types_imploded . ")\(\d+\)/i";
100100

101101
// Execute type find & replace
102102
$sql = preg_replace_callback($pattern, function ($matches) {

tests/rewriteTest.php

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,29 @@ public function test_it_handles_auto_increment_without_null()
9696
$this->assertSame(trim($expected), trim($postgresql));
9797
}
9898

99+
public function test_it_handles_numerics_without_auto_incrment_case_insensitively()
100+
{
101+
$sql = <<<SQL
102+
CREATE TABLE IF NOT EXISTS stars_votes (
103+
voter_ip VARCHAR(150) NOT NULL,
104+
post_id BIGINT(20) UNSIGNED NOT NULL,
105+
rating INT(1) UNSIGNED NOT NULL,
106+
PRIMARY KEY (voter_ip, post_id)
107+
)
108+
SQL;
109+
110+
$expected = <<<SQL
111+
CREATE TABLE IF NOT EXISTS stars_votes (
112+
voter_ip VARCHAR(150) NOT NULL,
113+
post_id BIGINT NOT NULL,
114+
rating INT NOT NULL,
115+
PRIMARY KEY (voter_ip, post_id)
116+
);
117+
SQL;
118+
119+
$postgresql = pg4wp_rewrite($sql);
120+
$this->assertSame(trim($expected), trim($postgresql));
121+
}
99122

100123
public function test_it_handles_keys()
101124
{
@@ -229,8 +252,8 @@ public function test_it_removes_character_sets()
229252
platform varchar(255),
230253
version varchar(255),
231254
location varchar(10),
232-
user_id BIGINT(48) NOT NULL,
233-
page_id BIGINT(48) NOT NULL,
255+
user_id BIGINT NOT NULL,
256+
page_id BIGINT NOT NULL,
234257
type VARCHAR(100) NOT NULL,
235258
PRIMARY KEY ( "ID" )
236259
);
@@ -631,6 +654,63 @@ public function test_it_can_handle_insert_sql_containing_nested_parathesis_with_
631654
$this->assertSame(trim($expected), trim($postgresql));
632655
}
633656

657+
public function test_it_rewrites_mediumints()
658+
{
659+
$sql = <<<SQL
660+
CREATE TABLE wp_relevanssi (
661+
doc bigint(20) NOT NULL DEFAULT '0',
662+
term varchar(50) NOT NULL DEFAULT '0',
663+
term_reverse varchar(50) NOT NULL DEFAULT '0',
664+
content mediumint(9) NOT NULL DEFAULT '0',
665+
title mediumint(9) NOT NULL DEFAULT '0',
666+
comment mediumint(9) NOT NULL DEFAULT '0',
667+
tag mediumint(9) NOT NULL DEFAULT '0',
668+
link mediumint(9) NOT NULL DEFAULT '0',
669+
author mediumint(9) NOT NULL DEFAULT '0',
670+
category mediumint(9) NOT NULL DEFAULT '0',
671+
excerpt mediumint(9) NOT NULL DEFAULT '0',
672+
taxonomy mediumint(9) NOT NULL DEFAULT '0',
673+
customfield mediumint(9) NOT NULL DEFAULT '0',
674+
mysqlcolumn MEDIUMINT(9) NOT NULL DEFAULT '0',
675+
taxonomy_detail longtext NOT NULL,
676+
customfield_detail longtext NOT NULL DEFAULT '',
677+
mysqlcolumn_detail longtext NOT NULL DEFAULT '',
678+
type varchar(210) NOT NULL DEFAULT 'post',
679+
item bigint(20) NOT NULL DEFAULT '0',
680+
PRIMARY KEY doctermitem (doc, term, item)
681+
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci
682+
SQL;
683+
684+
$expected = <<<SQL
685+
CREATE TABLE IF NOT EXISTS wp_relevanssi (
686+
doc bigint NOT NULL DEFAULT '0',
687+
term varchar(50) NOT NULL DEFAULT '0',
688+
term_reverse varchar(50) NOT NULL DEFAULT '0',
689+
content integer NOT NULL DEFAULT '0',
690+
title integer NOT NULL DEFAULT '0',
691+
comment integer NOT NULL DEFAULT '0',
692+
tag integer NOT NULL DEFAULT '0',
693+
link integer NOT NULL DEFAULT '0',
694+
author integer NOT NULL DEFAULT '0',
695+
category integer NOT NULL DEFAULT '0',
696+
excerpt integer NOT NULL DEFAULT '0',
697+
taxonomy integer NOT NULL DEFAULT '0',
698+
customfield integer NOT NULL DEFAULT '0',
699+
mysqlcolumn integer NOT NULL DEFAULT '0',
700+
taxonomy_detail text NOT NULL,
701+
customfield_detail text NOT NULL DEFAULT '',
702+
mysqlcolumn_detail text NOT NULL DEFAULT '',
703+
type varchar(210) NOT NULL DEFAULT 'post',
704+
item bigint NOT NULL DEFAULT '0',
705+
PRIMARY KEY doctermitem (doc, term, item)
706+
);
707+
SQL;
708+
709+
$postgresql = pg4wp_rewrite($sql);
710+
$this->assertSame(trim($expected), trim($postgresql));
711+
}
712+
713+
634714

635715

636716

0 commit comments

Comments
 (0)