Skip to content

Commit 403888b

Browse files
committed
Extend DEFAULT now() handling + update UPDATE Translation_Tests expectations
1 parent aaa380c commit 403888b

1 file changed

Lines changed: 53 additions & 5 deletions

File tree

.github/workflows/phpunit-tests-turso.yml

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,9 +1321,11 @@ jobs:
13211321
"\t\t\t\t} elseif ( str_contains( $column['EXTRA'], 'DEFAULT_GENERATED' ) ) {\n"
13221322
"\t\t\t\t\t$ast = $this->create_parser( 'SELECT ' . $column['COLUMN_DEFAULT'] )->parse();\n"
13231323
"\t\t\t\t\t$expr = $ast->get_first_descendant_node( 'selectItem' )->get_first_child_node();\n"
1324-
"\t\t\t\t\t$default_clause = $this->translate( $expr );\n"
1325-
"\t\t\t\t\tif ( preg_match( '/^[A-Za-z_][A-Za-z_0-9]*$/', trim( $default_clause ) ) ) {\n"
1326-
"\t\t\t\t\t\t// Simple identifier (e.g. CURRENT_TIMESTAMP): omit parens\n"
1324+
"\t\t\t\t\t$default_clause = trim( $this->translate( $expr ) );\n"
1325+
"\t\t\t\t\t$dc_upper = strtoupper( $default_clause );\n"
1326+
"\t\t\t\t\t$is_keyword = in_array( $dc_upper, array( 'CURRENT_TIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIME' ), true );\n"
1327+
"\t\t\t\t\tif ( $is_keyword || preg_match( '/^[A-Za-z_][A-Za-z_0-9]*$/', $default_clause ) ) {\n"
1328+
"\t\t\t\t\t\t// Simple identifier or known keyword: emit without parens\n"
13271329
"\t\t\t\t\t\t// so Turso's PRAGMA round-trip matches SQLite.\n"
13281330
"\t\t\t\t\t\t$query .= ' DEFAULT ' . $default_clause;\n"
13291331
"\t\t\t\t\t} else {\n"
@@ -1332,8 +1334,31 @@ jobs:
13321334
"\t\t\t\t}"
13331335
)
13341336
assert old in src, 'DEFAULT_GENERATED block not found'
1335-
open(path, 'w').write(src.replace(old, new, 1))
1336-
print('patched DEFAULT_GENERATED simple-identifier unwrap')
1337+
src = src.replace(old, new, 1)
1338+
1339+
# Also extend the existing CURRENT_TIMESTAMP literal special-case to
1340+
# match "now()" (MySQL alias) so the DEFAULT_GENERATED branch isn't
1341+
# taken at all for that input. Real SQLite normalizes this; this
1342+
# makes our SQLite emission match.
1343+
old_ct = (
1344+
"\t\t\t\tif (\n"
1345+
"\t\t\t\t\t'CURRENT_TIMESTAMP' === $column['COLUMN_DEFAULT']\n"
1346+
"\t\t\t\t\t&& ( 'timestamp' === $column['DATA_TYPE'] || 'datetime' === $column['DATA_TYPE'] )\n"
1347+
"\t\t\t\t) {\n"
1348+
"\t\t\t\t\t$query .= ' DEFAULT CURRENT_TIMESTAMP';\n"
1349+
)
1350+
new_ct = (
1351+
"\t\t\t\tif (\n"
1352+
"\t\t\t\t\tin_array( strtolower( $column['COLUMN_DEFAULT'] ), array( 'current_timestamp', 'now()' ), true )\n"
1353+
"\t\t\t\t\t&& ( 'timestamp' === $column['DATA_TYPE'] || 'datetime' === $column['DATA_TYPE'] )\n"
1354+
"\t\t\t\t) {\n"
1355+
"\t\t\t\t\t$query .= ' DEFAULT CURRENT_TIMESTAMP';\n"
1356+
)
1357+
assert old_ct in src, 'CURRENT_TIMESTAMP literal special case not found'
1358+
src = src.replace(old_ct, new_ct, 1)
1359+
1360+
open(path, 'w').write(src)
1361+
print('patched DEFAULT_GENERATED simple-identifier unwrap + now() special case')
13371362
13381363
# 16. Turso doesn't implement UPDATE ... FROM. The driver uses it to
13391364
# translate MySQL multi-table UPDATE (UPDATE t1, t2 JOIN t3 SET
@@ -1407,6 +1432,29 @@ jobs:
14071432
open(path, 'w').write(src)
14081433
print('patched UPDATE multi-table to rowid-IN subquery')
14091434
1435+
# Update Translation_Tests testUpdate expectations to match the
1436+
# rowid-IN subquery form.
1437+
path_tt = 'tests/WP_SQLite_Driver_Translation_Tests.php'
1438+
src_tt = open(path_tt).read()
1439+
for old_q, new_q in [
1440+
(
1441+
"'UPDATE `t1` SET `id` = 1 FROM `t2` WHERE `t1`.`c` = `t2`.`c`'",
1442+
"'UPDATE `t1` SET `id` = 1 WHERE rowid IN ( SELECT `t1`.rowid FROM `t1` , `t2` WHERE `t1`.`c` = `t2`.`c` )'",
1443+
),
1444+
(
1445+
"'UPDATE `t1` SET `id` = 1 FROM `t2` WHERE `t1`.`c` = 2 AND `t1`.`c` = `t2`.`c`'",
1446+
"'UPDATE `t1` SET `id` = 1 WHERE rowid IN ( SELECT `t1`.rowid FROM `t1` JOIN `t2` ON `t1`.`c` = `t2`.`c` WHERE `t1`.`c` = 2 )'",
1447+
),
1448+
(
1449+
"'UPDATE `t1` SET `id` = 1 FROM ( SELECT * FROM `t2` ) AS `t2` WHERE `t1`.`c` = 2 AND `t1`.`c` = `t2`.`c`'",
1450+
"'UPDATE `t1` SET `id` = 1 WHERE rowid IN ( SELECT `t1`.rowid FROM `t1` JOIN ( SELECT * FROM `t2` ) AS `t2` ON `t1`.`c` = `t2`.`c` WHERE `t1`.`c` = 2 )'",
1451+
),
1452+
]:
1453+
if old_q in src_tt:
1454+
src_tt = src_tt.replace(old_q, new_q, 1)
1455+
open(path_tt, 'w').write(src_tt)
1456+
print('patched Translation_Tests testUpdate expectations to rowid-IN form')
1457+
14101458
# 14. Update Translation_Tests::testHexadecimalLiterals to match
14111459
# the hex-literal alias force patch (which needs to stay so
14121460
# Turso doesn't mangle x'417a' into 17a' at runtime).

0 commit comments

Comments
 (0)