Skip to content

Commit fffa8fc

Browse files
committed
Emit DEFAULT without parens for simple identifiers
The DEFAULT_GENERATED path wraps the translated expression in parens unconditionally. Real SQLite's PRAGMA strips outer parens round-trip; Turso keeps them. For simple identifier defaults (CURRENT_TIMESTAMP, etc.) SQLite accepts the unwrapped form; emit without parens so Turso's PRAGMA matches SQLite's. Fixes testCreateTableWithDefaultNowFunction. testCreateTableWithDefaultExpressions still fails because 1 + 2 needs parens for SQLite syntax.
1 parent c0772d3 commit fffa8fc

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,41 @@ jobs:
12401240
open(path, 'w').write(src.replace(old, new, 1))
12411241
print('patched CHECK TABLE missing-table check')
12421242
1243+
# 15. DEFAULT_GENERATED path wraps the translated expression in parens
1244+
# unconditionally: `DEFAULT (<expr>)`. Real SQLite strips the outer
1245+
# parens on round-trip through PRAGMA; Turso keeps them. For simple
1246+
# identifiers (e.g. CURRENT_TIMESTAMP), SQLite accepts the unwrapped
1247+
# form too, so emit without parens there.
1248+
path = 'src/sqlite/class-wp-pdo-mysql-on-sqlite.php'
1249+
src = open(path).read()
1250+
old = (
1251+
"\t\t\t\t} elseif ( str_contains( $column['EXTRA'], 'DEFAULT_GENERATED' ) ) {\n"
1252+
"\t\t\t\t\t// Handle DEFAULT values with expressions (DEFAULT_GENERATED).\n"
1253+
"\t\t\t\t\t// Translate the default clause from MySQL to SQLite.\n"
1254+
"\t\t\t\t\t$ast = $this->create_parser( 'SELECT ' . $column['COLUMN_DEFAULT'] )->parse();\n"
1255+
"\t\t\t\t\t$expr = $ast->get_first_descendant_node( 'selectItem' )->get_first_child_node();\n"
1256+
"\t\t\t\t\t$default_clause = $this->translate( $expr );\n"
1257+
"\t\t\t\t\t$query .= sprintf( ' DEFAULT (%s)', $default_clause );\n"
1258+
"\t\t\t\t}"
1259+
)
1260+
new = (
1261+
"\t\t\t\t} elseif ( str_contains( $column['EXTRA'], 'DEFAULT_GENERATED' ) ) {\n"
1262+
"\t\t\t\t\t$ast = $this->create_parser( 'SELECT ' . $column['COLUMN_DEFAULT'] )->parse();\n"
1263+
"\t\t\t\t\t$expr = $ast->get_first_descendant_node( 'selectItem' )->get_first_child_node();\n"
1264+
"\t\t\t\t\t$default_clause = $this->translate( $expr );\n"
1265+
"\t\t\t\t\tif ( preg_match( '/^[A-Za-z_][A-Za-z_0-9]*$/', trim( $default_clause ) ) ) {\n"
1266+
"\t\t\t\t\t\t// Simple identifier (e.g. CURRENT_TIMESTAMP): omit parens\n"
1267+
"\t\t\t\t\t\t// so Turso's PRAGMA round-trip matches SQLite.\n"
1268+
"\t\t\t\t\t\t$query .= ' DEFAULT ' . $default_clause;\n"
1269+
"\t\t\t\t\t} else {\n"
1270+
"\t\t\t\t\t\t$query .= sprintf( ' DEFAULT (%s)', $default_clause );\n"
1271+
"\t\t\t\t\t}\n"
1272+
"\t\t\t\t}"
1273+
)
1274+
assert old in src, 'DEFAULT_GENERATED block not found'
1275+
open(path, 'w').write(src.replace(old, new, 1))
1276+
print('patched DEFAULT_GENERATED simple-identifier unwrap')
1277+
12431278
# 14. Update Translation_Tests::testHexadecimalLiterals to match
12441279
# the hex-literal alias force patch (which needs to stay so
12451280
# Turso doesn't mangle x'417a' into 17a' at runtime).

0 commit comments

Comments
 (0)