Merge pull request #135 from PostgreSQL-For-Wordpress/hotfix/handle-utc

rewrite utc timestamp requests
This commit is contained in:
Matthew Bucci
2024-10-17 05:55:50 -04:00
committed by GitHub
3 changed files with 31 additions and 1 deletions

View File

@ -11,6 +11,8 @@ class InsertSQLRewriter extends AbstractSQLRewriter
// Those are used when we need to set the date to now() in gmt time
$sql = str_replace("'0000-00-00 00:00:00'", 'now() AT TIME ZONE \'gmt\'', $sql);
$sql = str_replace("utc_timestamp()", "CURRENT_TIMESTAMP AT TIME ZONE 'UTC'", $sql);
// Multiple values group when calling INSERT INTO don't always work
if(false !== strpos($sql, $wpdb->options) && false !== strpos($sql, '), (')) {
$pattern = '/INSERT INTO.+VALUES/';

View File

@ -68,6 +68,8 @@ class SelectSQLRewriter extends AbstractSQLRewriter
// HANDLE REGEXP
$sql = preg_replace('/REGEXP/', '~', $sql);
$sql = str_replace("utc_timestamp()", "CURRENT_TIMESTAMP AT TIME ZONE 'UTC'", $sql);
// In order for users counting to work...
$matches = array();
if(preg_match_all('/COUNT[^C]+\),/', $sql, $matches)) {

View File

@ -757,7 +757,33 @@ final class rewriteTest extends TestCase
$this->assertSame(trim($expected), trim($postgresql));
}
public function test_it_rewrites_utc_timestamp_inserts()
{
$sql = <<<SQL
INSERT INTO wp_gf_form(title, date_created) VALUES('Test', utc_timestamp())
SQL;
$expected = <<<SQL
INSERT INTO wp_gf_form(title, date_created) VALUES('Test', CURRENT_TIMESTAMP AT TIME ZONE 'UTC') RETURNING *
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}
public function test_it_rewrites_utc_timestamp_selects()
{
$sql = <<<SQL
SELECT utc_timestamp()
SQL;
$expected = <<<SQL
SELECT CURRENT_TIMESTAMP AT TIME ZONE 'UTC'
SQL;
$postgresql = pg4wp_rewrite($sql);
$this->assertSame(trim($expected), trim($postgresql));
}
protected function setUp(): void
{