Merge pull request #76 from PostgreSQL-For-Wordpress/mb-fix-auto-increment

Improve table creation
This commit is contained in:
Matthew Bucci
2024-01-16 22:01:32 -08:00
committed by GitHub
7 changed files with 501 additions and 63 deletions

View File

@ -1039,6 +1039,49 @@ function wpsqli_affected_rows(&$connection)
return pg_affected_rows($result);
}
// Gets the list of sequences from postgres
function wpsqli_get_list_of_sequences(&$connection)
{
$sql = "SELECT sequencename FROM pg_sequences";
$result = pg_query($connection, $sql);
if(!$result) {
if (PG4WP_DEBUG || PG4WP_LOG) {
$log = "Unable to get list of sequences\n";
error_log($log, 3, PG4WP_LOG . 'pg4wp_errors.log');
}
return [];
}
$data = pg_fetch_all($result);
return array_column($data, 'sequencename');
}
// Get the primary sequence for a table
function wpsqli_get_primary_sequence_for_table(&$connection, $table)
{
// TODO: it should be possible to use a WP transient here for object caching
global $sequence_lookup;
if (empty($sequence_lookup)) {
$sequence_lookup = [];
}
if (isset($sequence_lookup[$table])) {
return $sequence_lookup[$table];
}
$sequences = wpsqli_get_list_of_sequences($connection);
foreach($sequences as $sequence) {
if (strncmp($sequence, $table, strlen($table)) === 0) {
$sequence_lookup[$table] = $sequence;
return $sequence;
}
}
// Fallback to default if we don't find a sequence
// Note: this will probably fail
return $table . '_seq';
}
/**
* Fetches the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
*
@ -1057,19 +1100,13 @@ function wpsqli_insert_id(&$connection = null)
$ins_field = $GLOBALS['pg4wp_ins_field'];
$table = $GLOBALS['pg4wp_ins_table'];
$lastq = $GLOBALS['pg4wp_last_insert'];
$seq = $table . '_seq';
$seq = wpsqli_get_primary_sequence_for_table($connection, $table);
// Special case for 'term_relationships' table, which does not have a sequence in PostgreSQL.
if ($table == $wpdb->term_relationships) {
// PostgreSQL: Using CURRVAL() to get the current value of the sequence.
$sql = "SELECT CURRVAL('$seq')";
$res = pg_query($connection, $sql);
if (false !== $res) {
$data = pg_fetch_result($res, 0, 0);
}
}
// Special case when using WP_Import plugin where ID is defined in the query itself.
elseif ('post_author' == $ins_field && false !== strpos($lastq, 'ID')) {
if($table == $wpdb->term_relationships) {
$sql = 'NO QUERY';
$data = 0;
} elseif ('post_author' == $ins_field && false !== strpos($lastq, 'ID')) {
// No PostgreSQL specific operation here.
$sql = 'ID was in query ';
$pattern = '/.+\'(\d+).+$/';
@ -1080,7 +1117,8 @@ function wpsqli_insert_id(&$connection = null)
$GLOBALS['pg4wp_queued_query'] = "SELECT SETVAL('$seq',(SELECT MAX(\"ID\") FROM $table)+1);";
} else {
// PostgreSQL: Using CURRVAL() to get the current value of the sequence.
$sql = "SELECT CURRVAL('$seq')";
// Double quoting is needed to prevent seq from being lowercased automatically
$sql = "SELECT CURRVAL('\"$seq\"')";
$res = pg_query($connection, $sql);
if (false !== $res) {
$data = pg_fetch_result($res, 0, 0);

View File

@ -53,7 +53,7 @@ function pg4wp_rewrite($sql)
// When installing, the sequence for table terms has to be updated
if(defined('WP_INSTALLING') && WP_INSTALLING && false !== strpos($sql, 'INSERT INTO `' . $wpdb->terms . '`')) {
$end .= ';SELECT setval(\'' . $wpdb->terms . '_seq\', (SELECT MAX(term_id) FROM ' . $wpdb->terms . ')+1);';
$end .= ';SELECT setval(\'' . $wpdb->terms . '_term_id_seq\', (SELECT MAX(term_id) FROM ' . $wpdb->terms . ')+1);';
}
break;
case 'Insert':

View File

@ -3,30 +3,32 @@
class AlterTableSQLRewriter extends AbstractSQLRewriter
{
private $stringReplacements = [
'bigint(20)' => 'bigint',
'bigint(10)' => 'int',
'int(11)' => 'int',
'tinytext' => 'text',
'mediumtext' => 'text',
'longtext' => 'text',
'unsigned' => '',
' bigint(40)' => ' bigint',
' bigint(20)' => ' bigint',
' bigint(10)' => ' int',
' int(11)' => ' int',
' int(10)' => ' int',
' tinytext' => ' text',
' mediumtext' => ' text',
' longtext' => ' text',
' unsigned' => ' ',
'gmt datetime NOT NULL default \'0000-00-00 00:00:00\'' => 'gmt timestamp NOT NULL DEFAULT timezone(\'gmt\'::text, now())',
'default \'0000-00-00 00:00:00\'' => 'DEFAULT now()',
'\'0000-00-00 00:00:00\'' => 'now()',
'datetime' => 'timestamp',
'DEFAULT CHARACTER SET utf8' => '',
' datetime' => ' timestamp',
' DEFAULT CHARACTER SET utf8' => '',
// WP 2.7.1 compatibility
'int(4)' => 'smallint',
' int(4)' => ' smallint',
// For WPMU (starting with WP 3.2)
'tinyint(2)' => 'smallint',
'tinyint(1)' => 'smallint',
"enum('0','1')" => 'smallint',
'COLLATE utf8_general_ci' => '',
' tinyint(2)' => ' smallint',
' tinyint(1)' => ' smallint',
" enum('0','1')" => ' smallint',
' COLLATE utf8_general_ci' => '',
// For flash-album-gallery plugin
'tinyint' => 'smallint'
' tinyint' => ' smallint'
];
public function rewrite(): string

View File

@ -3,32 +3,37 @@
class CreateTableSQLRewriter extends AbstractSQLRewriter
{
private $stringReplacements = [
'bigint(20)' => 'bigint',
'bigint(10)' => 'int',
'int(11)' => 'int',
'tinytext' => 'text',
'mediumtext' => 'text',
'longtext' => 'text',
'unsigned' => '',
' bigint(40)' => ' bigint',
' bigint(20)' => ' bigint',
' bigint(10)' => ' int',
' int(11)' => ' int',
' int(10)' => ' int',
' int(1)' => ' smallint',
' tinytext' => ' text',
' mediumtext' => ' text',
' longtext' => ' text',
' unsigned' => ' ',
'gmt datetime NOT NULL default \'0000-00-00 00:00:00\'' => 'gmt timestamp NOT NULL DEFAULT timezone(\'gmt\'::text, now())',
'default \'0000-00-00 00:00:00\'' => 'DEFAULT now()',
'\'0000-00-00 00:00:00\'' => 'now()',
'datetime' => 'timestamp',
'DEFAULT CHARACTER SET utf8mb4' => '',
'DEFAULT CHARACTER SET utf8' => '',
' DEFAULT CHARACTER SET utf8mb4' => '',
' DEFAULT CHARACTER SET utf8' => '',
// WP 2.7.1 compatibility
'int(4)' => 'smallint',
' int(4)' => ' smallint',
// For WPMU (starting with WP 3.2)
'tinyint(2)' => 'smallint',
'tinyint(1)' => 'smallint',
"enum('0','1')" => 'smallint',
'COLLATE utf8mb4_unicode_520_ci' => '',
'COLLATE utf8_general_ci' => '',
' tinyint(2)' => ' smallint',
' tinyint(1)' => ' smallint',
" enum('0','1')" => ' smallint',
' COLLATE utf8mb4_unicode_520_ci' => '',
' COLLATE utf8_general_ci' => '',
' CHARACTER SET utf8' => '',
' DEFAULT CHARSET=utf8' => '',
// For flash-album-gallery plugin
'tinyint' => 'smallint'
' tinyint' => ' smallint'
];
public function rewrite(): string
@ -36,46 +41,90 @@ class CreateTableSQLRewriter extends AbstractSQLRewriter
$sql = $this->original();
$sql = str_replace('CREATE TABLE IF NOT EXISTS ', 'CREATE TABLE ', $sql);
$tableSQL = str_replace('CREATE TABLE IF NOT EXISTS ', 'CREATE TABLE ', $sql);
$pattern = '/CREATE TABLE [`]?(\w+)[`]?/';
preg_match($pattern, $sql, $matches);
preg_match($pattern, $tableSQL, $matches);
$table = $matches[1];
// change all creates into create if not exists
$pattern = "/CREATE TABLE (IF NOT EXISTS )?(\w+)\s*\(/i";
$replacement = 'CREATE TABLE IF NOT EXISTS $2 (';
$sql = preg_replace($pattern, $replacement, $sql);
// Remove trailing spaces
$sql = trim($sql) . ';';
$sql = trim($sql);
// Add a slash if needed
if (substr($sql, strlen($sql) - 1, 1) != ";") {
$sql = $sql . ";";
}
// Translate types and some other replacements
$sql = str_replace(
$sql = str_ireplace(
array_keys($this->stringReplacements),
array_values($this->stringReplacements),
$sql
);
// Fix auto_increment by adding a sequence
$pattern = '/int[ ]+NOT NULL auto_increment/';
// bigint
$pattern = '/bigint(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
preg_match($pattern, $sql, $matches);
if($matches) {
$seq = $table . '_seq';
$sql = str_replace('NOT NULL auto_increment', "NOT NULL DEFAULT nextval('$seq'::text)", $sql);
$sql .= "\nCREATE SEQUENCE $seq;";
$sql = preg_replace($pattern, 'bigserial', $sql);
}
// Support for INDEX creation
$pattern = '/,\s+(UNIQUE |)KEY\s+([^\s]+)\s+\(((?:[\w]+(?:\([\d]+\))?[,]?)*)\)/';
// int
$pattern = '/int(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
preg_match($pattern, $sql, $matches);
if($matches) {
$sql = preg_replace($pattern, 'serial', $sql);
}
// smallint
$pattern = '/smallint(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i';
preg_match($pattern, $sql, $matches);
if($matches) {
$sql = preg_replace($pattern, 'smallserial', $sql);
}
// Handle for numeric and decimal -- being replaced with serial
$numeric_patterns = ['/numeric(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i', '/decimal(\(\d+\))?([ ]*NOT NULL)?[ ]*auto_increment/i'];
foreach($numeric_patterns as $pattern) {
preg_match($pattern, $sql, $matches);
if($matches) {
$sql = preg_replace($pattern, 'serial', $sql);
}
}
// Support for UNIQUE INDEX creation
$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];
$columns = preg_replace('/\(\d+\)/', '', $columns);
// Workaround for index name duplicate
$index = $table . '_' . $index;
$sql .= "\nCREATE {$unique}INDEX $index ON $table ($columns);";
// Removing backticks from the index names
$index = str_replace('`', '', $index);
// Removing backticks and key length constraints from the columns
$columns = preg_replace(["/`/", "/\(\d+\)/"], '', $columns);
// Creating a unique index name
$indexName = $table . '_' . $index;
// Appending the CREATE INDEX statement to SQL
$sql .= "\nCREATE {$unique}INDEX IF NOT EXISTS $indexName ON $table ($columns);";
}
}
// Now remove handled indexes
$sql = preg_replace($pattern, '', $sql);
$pattern = "/(,\s*)?UNIQUE KEY\s+[a-zA-Z0-9_]+\s+(\([a-zA-Z0-9_,\s]+\))/";
$replacement = "$1UNIQUE $2";
$sql = preg_replace($pattern, $replacement, $sql);
return $sql;
}
}

View File

@ -33,7 +33,6 @@ final class parseTest extends TestCase
$this->assertSame($GLOBALS['pg4wp_ins_field'], "post_author");
}
protected function setUp(): void
{
global $wpdb;

View File

@ -21,7 +21,7 @@ final class rewriteTest extends TestCase
$sql = 'SELECT COUNT(NULLIF(`meta_value` LIKE \'%"administrator"%\', false)), COUNT(NULLIF(`meta_value` = \'a:0:{}\', false)), COUNT(*) FROM wp_usermeta INNER JOIN wp_users ON user_id = ID WHERE meta_key = \'wp_capabilities\'';
$expected = 'SELECT COUNT(NULLIF(meta_value ILIKE \'%"administrator"%\', false)) AS count0, COUNT(NULLIF(meta_value = \'a:0:{}\', false)) AS count1, COUNT(*) FROM wp_usermeta INNER JOIN wp_users ON user_id = "ID" WHERE meta_key = \'wp_capabilities\'';
$postgresql = pg4wp_rewrite($sql);
$this->assertSame($postgresql, $expected);
$this->assertSame(trim($expected), trim($postgresql));
}
@ -31,9 +31,359 @@ final class rewriteTest extends TestCase
$sql = 'SELECT COUNT(id), username FROM users';
$expected = 'SELECT COUNT(id) AS count0, username FROM users GROUP BY username';
$postgresql = pg4wp_rewrite($sql);
$this->assertSame($postgresql, $expected);
$this->assertSame(trim($expected), trim($postgresql));
}
public function test_it_handles_auto_increment()
{
$sql = <<<SQL
CREATE TABLE wp_itsec_lockouts (
lockout_id bigint UNSIGNED NOT NULL AUTO_INCREMENT,
lockout_type varchar(25) NOT NULL,
lockout_start timestamp NOT NULL,
lockout_start_gmt timestamp NOT NULL,
lockout_expire timestamp NOT NULL,
lockout_expire_gmt timestamp NOT NULL,
lockout_host varchar(40),
lockout_user bigint UNSIGNED,
lockout_username varchar(60),
lockout_active int(1) NOT NULL DEFAULT 1,
lockout_context TEXT,
PRIMARY KEY (lockout_id)
)
SQL;
$expected = <<<SQL
CREATE TABLE IF NOT EXISTS wp_itsec_lockouts (
lockout_id bigserial,
lockout_type varchar(25) NOT NULL,
lockout_start timestamp NOT NULL,
lockout_start_gmt timestamp NOT NULL,
lockout_expire timestamp NOT NULL,
lockout_expire_gmt timestamp NOT NULL,
lockout_host varchar(40),
lockout_user bigint ,
lockout_username varchar(60),
lockout_active smallint NOT NULL DEFAULT 1,
lockout_context TEXT,
PRIMARY KEY (lockout_id)
);
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}
public function test_it_handles_auto_increment_without_null()
{
$sql = <<<SQL
CREATE TABLE wp_e_events (
id bigint auto_increment primary key,
event_data text null,
created_at timestamp not null
)
SQL;
$expected = <<<SQL
CREATE TABLE IF NOT EXISTS wp_e_events (
id bigserial primary key,
event_data text null,
created_at timestamp not null
);
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}
public function test_it_handles_keys()
{
$sql = <<<SQL
CREATE TABLE wp_itsec_dashboard_lockouts (
id int NOT NULL AUTO_INCREMENT,
ip varchar(40),
time timestamp NOT NULL,
count int NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY ip__time (ip, time)
)
SQL;
$expected = <<<SQL
CREATE TABLE IF NOT EXISTS wp_itsec_dashboard_lockouts (
id serial,
ip varchar(40),
time timestamp NOT NULL,
count int NOT NULL,
PRIMARY KEY (id)
);
CREATE UNIQUE INDEX IF NOT EXISTS wp_itsec_dashboard_lockouts_ip__time ON wp_itsec_dashboard_lockouts (ip, time);
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}
public function test_it_handles_keys_without_unique()
{
$sql = <<<SQL
CREATE TABLE wp_itsec_vulnerabilities (
id varchar(128) NOT NULL,
software_type varchar(20) NOT NULL,
software_slug varchar(255) NOT NULL,
first_seen timestamp NOT NULL,
last_seen timestamp NOT NULL,
resolved_at timestamp default NULL,
resolved_by bigint NOT NULL default 0,
resolution varchar(20) NOT NULL default '',
details text NOT NULL,
PRIMARY KEY (id),
KEY resolution (resolution),
KEY software_type (software_type),
KEY last_seen (last_seen)
)
SQL;
$expected = <<<SQL
CREATE TABLE IF NOT EXISTS wp_itsec_vulnerabilities (
id varchar(128) NOT NULL,
software_type varchar(20) NOT NULL,
software_slug varchar(255) NOT NULL,
first_seen timestamp NOT NULL,
last_seen timestamp NOT NULL,
resolved_at timestamp default NULL,
resolved_by bigint NOT NULL default 0,
resolution varchar(20) NOT NULL default '',
details text NOT NULL,
PRIMARY KEY (id)
);
CREATE INDEX IF NOT EXISTS wp_itsec_vulnerabilities_resolution ON wp_itsec_vulnerabilities (resolution);
CREATE INDEX IF NOT EXISTS wp_itsec_vulnerabilities_software_type ON wp_itsec_vulnerabilities (software_type);
CREATE INDEX IF NOT EXISTS wp_itsec_vulnerabilities_last_seen ON wp_itsec_vulnerabilities (last_seen);
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}
public function test_it_does_not_remove_if_not_exists()
{
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS wp_itsec_dashboard_lockouts (
id int NOT NULL AUTO_INCREMENT,
ip varchar(40),
time timestamp NOT NULL,
count int NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY ip__time (ip, time)
)
SQL;
$expected = <<<SQL
CREATE TABLE IF NOT EXISTS wp_itsec_dashboard_lockouts (
id serial,
ip varchar(40),
time timestamp NOT NULL,
count int NOT NULL,
PRIMARY KEY (id)
);
CREATE UNIQUE INDEX IF NOT EXISTS wp_itsec_dashboard_lockouts_ip__time ON wp_itsec_dashboard_lockouts (ip, time);
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}
public function test_it_removes_character_sets()
{
$sql = <<<SQL
CREATE TABLE wp_statistics_useronline (
ID bigint(20) NOT NULL AUTO_INCREMENT,
ip varchar(60) NOT NULL,
created int(11),
timestamp int(10) NOT NULL,
date datetime NOT NULL,
referred text CHARACTER SET utf8 NOT NULL,
agent varchar(255) NOT NULL,
platform varchar(255),
version varchar(255),
location varchar(10),
`user_id` BIGINT(48) NOT NULL,
`page_id` BIGINT(48) NOT NULL,
`type` VARCHAR(100) NOT NULL,
PRIMARY KEY (ID)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci
SQL;
$expected = <<<SQL
CREATE TABLE IF NOT EXISTS wp_statistics_useronline (
"ID" bigserial,
ip varchar(60) NOT NULL,
created int,
timestamp int NOT NULL,
date timestamp NOT NULL,
referred text NOT NULL,
agent varchar(255) NOT NULL,
platform varchar(255),
version varchar(255),
location varchar(10),
user_id BIGINT(48) NOT NULL,
page_id BIGINT(48) NOT NULL,
type VARCHAR(100) NOT NULL,
PRIMARY KEY ( "ID" )
);
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}
public function test_it_handles_multiple_keys()
{
$sql = <<<SQL
CREATE TABLE wp_statistics_pages (
page_id BIGINT(20) NOT NULL AUTO_INCREMENT,
uri varchar(190) NOT NULL,
type varchar(180) NOT NULL,
date date NOT NULL,
count int(11) NOT NULL,
id int(11) NOT NULL,
UNIQUE KEY date_2 (date,uri),
KEY url (uri),
KEY date (date),
KEY id (id),
KEY `uri` (`uri`,`count`,`id`),
PRIMARY KEY (`page_id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci
SQL;
$expected = <<<SQL
CREATE TABLE IF NOT EXISTS wp_statistics_pages (
page_id bigserial,
uri varchar(190) NOT NULL,
type varchar(180) NOT NULL,
date date NOT NULL,
count int NOT NULL,
id int NOT NULL,
PRIMARY KEY (page_id)
);
CREATE UNIQUE INDEX IF NOT EXISTS wp_statistics_pages_date_2 ON wp_statistics_pages (date,uri);
CREATE INDEX IF NOT EXISTS wp_statistics_pages_url ON wp_statistics_pages (uri);
CREATE INDEX IF NOT EXISTS wp_statistics_pages_date ON wp_statistics_pages (date);
CREATE INDEX IF NOT EXISTS wp_statistics_pages_id ON wp_statistics_pages (id);
CREATE INDEX IF NOT EXISTS wp_statistics_pages_uri ON wp_statistics_pages (uri,count,id);
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}
public function test_it_removes_table_charsets()
{
$sql = <<<SQL
CREATE TABLE `wp_yoast_migrations` (
`id` int(11) UNSIGNED auto_increment NOT NULL,
`version` varchar(191),
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
SQL;
$expected = <<<SQL
CREATE TABLE wp_yoast_migrations (
id serial NOT NULL,
version varchar(191),
PRIMARY KEY (id)
);
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}
public function test_it_can_create_keys_with_length()
{
$sql = <<<SQL
CREATE TABLE wp_usermeta (
umeta_id bigint(20) unsigned NOT NULL auto_increment,
user_id bigint(20) unsigned NOT NULL default '0',
meta_key varchar(255) default NULL,
meta_value longtext,
PRIMARY KEY (umeta_id),
KEY user_id (user_id),
KEY meta_key (meta_key(191))
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci
SQL;
$expected = <<<SQL
CREATE TABLE IF NOT EXISTS wp_usermeta (
umeta_id bigserial,
user_id bigint NOT NULL default '0',
meta_key varchar(255) default NULL,
meta_value text,
PRIMARY KEY (umeta_id)
);
CREATE INDEX IF NOT EXISTS wp_usermeta_user_id ON wp_usermeta (user_id);
CREATE INDEX IF NOT EXISTS wp_usermeta_meta_key ON wp_usermeta (meta_key);
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}
public function test_it_can_create_double_keys_with_length()
{
$sql = <<<SQL
CREATE TABLE wp_blogs (
blog_id bigint(20) NOT NULL auto_increment,
site_id bigint(20) NOT NULL default '0',
domain varchar(200) NOT NULL default '',
path varchar(100) NOT NULL default '',
registered datetime NOT NULL default '0000-00-00 00:00:00',
last_updated datetime NOT NULL default '0000-00-00 00:00:00',
public tinyint(2) NOT NULL default '1',
archived tinyint(2) NOT NULL default '0',
mature tinyint(2) NOT NULL default '0',
spam tinyint(2) NOT NULL default '0',
deleted tinyint(2) NOT NULL default '0',
lang_id int(11) NOT NULL default '0',
PRIMARY KEY (blog_id),
KEY domain (domain(50),path(5)),
KEY lang_id (lang_id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci
SQL;
$expected = <<<SQL
CREATE TABLE IF NOT EXISTS wp_blogs (
blog_id bigserial,
site_id bigint NOT NULL default '0',
domain varchar(200) NOT NULL default '',
path varchar(100) NOT NULL default '',
registered timestamp NOT NULL DEFAULT now(),
last_updated timestamp NOT NULL DEFAULT now(),
public smallint NOT NULL default '1',
archived smallint NOT NULL default '0',
mature smallint NOT NULL default '0',
spam smallint NOT NULL default '0',
deleted smallint NOT NULL default '0',
lang_id int NOT NULL default '0',
PRIMARY KEY (blog_id)
);
CREATE INDEX IF NOT EXISTS wp_blogs_domain ON wp_blogs (domain,path);
CREATE INDEX IF NOT EXISTS wp_blogs_lang_id ON wp_blogs (lang_id);
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}
protected function setUp(): void
{
global $wpdb;

View File

@ -22,7 +22,7 @@ final class verifyAgainstStubsTest extends TestCase
$files = array_diff(scandir(self::STUBS_DIRECTORY), array('.', '..'));
foreach($files as $file) {
$data = json_decode(file_get_contents(self::STUBS_DIRECTORY . "/" . $file), true);
$this->assertSame(pg4wp_rewrite($data['mysql']), $data['postgresql']);
$this->assertSame($data['postgresql'], pg4wp_rewrite($data['mysql']));
}
}