Skip to content

Commit

Permalink
Merge pull request #131 from PostgreSQL-For-Wordpress/hotfix/numeric-…
Browse files Browse the repository at this point in the history
…rewriting

Numeric Rewriting should be case insensitive.
  • Loading branch information
mattbucci authored Oct 17, 2024
2 parents d8b83c8 + b463ead commit 0873fda
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pg4wp/rewriters/CreateTableSQLRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
' CHARACTER SET utf8' => '',
' DEFAULT CHARSET=utf8' => '',

// For flash-album-gallery plugin
' tinyint' => ' smallint'
' tinyint' => ' smallint',
' mediumint' => ' integer'
];

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

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

// Execute type find & replace
$sql = preg_replace_callback($pattern, function ($matches) {
Expand Down
84 changes: 82 additions & 2 deletions tests/rewriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,29 @@ public function test_it_handles_auto_increment_without_null()
$this->assertSame(trim($expected), trim($postgresql));
}

public function test_it_handles_numerics_without_auto_incrment_case_insensitively()
{
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS stars_votes (
voter_ip VARCHAR(150) NOT NULL,
post_id BIGINT(20) UNSIGNED NOT NULL,
rating INT(1) UNSIGNED NOT NULL,
PRIMARY KEY (voter_ip, post_id)
)
SQL;

$expected = <<<SQL
CREATE TABLE IF NOT EXISTS stars_votes (
voter_ip VARCHAR(150) NOT NULL,
post_id BIGINT NOT NULL,
rating INT NOT NULL,
PRIMARY KEY (voter_ip, post_id)
);
SQL;

$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}

public function test_it_handles_keys()
{
Expand Down Expand Up @@ -229,8 +252,8 @@ public function test_it_removes_character_sets()
platform varchar(255),
version varchar(255),
location varchar(10),
user_id BIGINT(48) NOT NULL,
page_id BIGINT(48) NOT NULL,
user_id BIGINT NOT NULL,
page_id BIGINT NOT NULL,
type VARCHAR(100) NOT NULL,
PRIMARY KEY ( "ID" )
);
Expand Down Expand Up @@ -631,6 +654,63 @@ public function test_it_can_handle_insert_sql_containing_nested_parathesis_with_
$this->assertSame(trim($expected), trim($postgresql));
}

public function test_it_rewrites_mediumints()
{
$sql = <<<SQL
CREATE TABLE wp_relevanssi (
doc bigint(20) NOT NULL DEFAULT '0',
term varchar(50) NOT NULL DEFAULT '0',
term_reverse varchar(50) NOT NULL DEFAULT '0',
content mediumint(9) NOT NULL DEFAULT '0',
title mediumint(9) NOT NULL DEFAULT '0',
comment mediumint(9) NOT NULL DEFAULT '0',
tag mediumint(9) NOT NULL DEFAULT '0',
link mediumint(9) NOT NULL DEFAULT '0',
author mediumint(9) NOT NULL DEFAULT '0',
category mediumint(9) NOT NULL DEFAULT '0',
excerpt mediumint(9) NOT NULL DEFAULT '0',
taxonomy mediumint(9) NOT NULL DEFAULT '0',
customfield mediumint(9) NOT NULL DEFAULT '0',
mysqlcolumn MEDIUMINT(9) NOT NULL DEFAULT '0',
taxonomy_detail longtext NOT NULL,
customfield_detail longtext NOT NULL DEFAULT '',
mysqlcolumn_detail longtext NOT NULL DEFAULT '',
type varchar(210) NOT NULL DEFAULT 'post',
item bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY doctermitem (doc, term, item)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci
SQL;

$expected = <<<SQL
CREATE TABLE IF NOT EXISTS wp_relevanssi (
doc bigint NOT NULL DEFAULT '0',
term varchar(50) NOT NULL DEFAULT '0',
term_reverse varchar(50) NOT NULL DEFAULT '0',
content integer NOT NULL DEFAULT '0',
title integer NOT NULL DEFAULT '0',
comment integer NOT NULL DEFAULT '0',
tag integer NOT NULL DEFAULT '0',
link integer NOT NULL DEFAULT '0',
author integer NOT NULL DEFAULT '0',
category integer NOT NULL DEFAULT '0',
excerpt integer NOT NULL DEFAULT '0',
taxonomy integer NOT NULL DEFAULT '0',
customfield integer NOT NULL DEFAULT '0',
mysqlcolumn integer NOT NULL DEFAULT '0',
taxonomy_detail text NOT NULL,
customfield_detail text NOT NULL DEFAULT '',
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)
);
SQL;

$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}





Expand Down

0 comments on commit 0873fda

Please sign in to comment.