Skip to content
This repository was archived by the owner on Sep 11, 2026. It is now read-only.

Commit dfd509c

Browse files
batonacclaude
andcommitted
Avoid a row-value UPDATE when syncing column key info
sync_column_key_info() assigned COLUMN_KEY and IS_NULLABLE together as a row value fed by a correlated aggregate subquery. On SQLite that is safe: an aggregate with no GROUP BY always yields exactly one row, so the CASE ELSE branches always supply a value. Turso does not always yield that row. When the row being updated sorts before a row that does have a matching statistics entry, the subquery produces no row, both columns are set to NULL, and the NOT NULL constraint on IS_NULLABLE fails: NOT NULL constraint failed: _wp_sqlite_mysql_information_schema_columns.IS_NULLABLE Reproduced against Turso 0.8.0-pre.7 with no PHP involved: two rows in a STRICT table, only the later one indexed, and the same UPDATE. Whether it fails depends purely on the sort order of the stored COLUMN_NAME values. WordPress hits it on 9 of the 12 core tables, because wp_users.comment_id-style columns sort before the indexed primary key column -- so CREATE TABLE fails and wp_install() cannot complete. Assign the two columns separately and wrap each subquery in COALESCE(), falling back to the values the ELSE branches would have produced for a column with no index entries. Verified to produce identical results to the row-value form on real SQLite, and to fix the failure on Turso. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 22ad5d8 commit dfd509c

2 files changed

Lines changed: 50 additions & 27 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php‎

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,29 +1954,52 @@ private function sync_column_key_info( bool $table_is_temporary, string $table_n
19541954
// @TODO: Consider listing only affected columns.
19551955
$columns_table_name = $this->get_table_name( $table_is_temporary, 'columns' );
19561956
$statistics_table_name = $this->get_table_name( $table_is_temporary, 'statistics' );
1957-
$this->connection->query(
1958-
'
1959-
UPDATE ' . $this->connection->quote_identifier( $columns_table_name ) . " AS c
1960-
SET (column_key, is_nullable) = (
1961-
SELECT
1962-
CASE
1963-
WHEN MAX(s.index_name = 'PRIMARY') THEN 'PRI'
1964-
WHEN MAX(s.non_unique = 0 AND s.seq_in_index = 1) THEN 'UNI'
1965-
WHEN MAX(s.seq_in_index = 1) THEN 'MUL'
1966-
ELSE ''
1967-
END,
1968-
CASE
1969-
WHEN MAX(s.index_name = 'PRIMARY') THEN 'NO'
1970-
ELSE c.is_nullable
1971-
END
1972-
FROM " . $this->connection->quote_identifier( $statistics_table_name ) . ' AS s
1957+
/*
1958+
* Assign the two columns separately rather than as a row value, and guard
1959+
* each subquery with COALESCE().
1960+
*
1961+
* An aggregate subquery with no GROUP BY always yields exactly one row, so
1962+
* on SQLite the COALESCE() calls never fire and the row-value form would be
1963+
* equivalent. Turso, however, can yield no row at all from the row-value
1964+
* correlated subquery when the row being updated sorts before a row that
1965+
* does have a matching statistics entry, which sets both columns to NULL
1966+
* and trips the NOT NULL constraint on IS_NULLABLE.
1967+
*
1968+
* The fallbacks are the values the ELSE branches would have produced for a
1969+
* column with no index entries, so behavior is unchanged on either engine.
1970+
*/
1971+
$columns_table = $this->connection->quote_identifier( $columns_table_name );
1972+
$statistics_table = $this->connection->quote_identifier( $statistics_table_name );
1973+
$correlation = "FROM $statistics_table AS s
19731974
WHERE s.table_schema = c.table_schema
19741975
AND s.table_name = c.table_name
1975-
AND s.column_name = c.column_name
1976-
)
1976+
AND s.column_name = c.column_name";
1977+
1978+
$this->connection->query(
1979+
"
1980+
UPDATE $columns_table AS c
1981+
SET
1982+
column_key = COALESCE((
1983+
SELECT
1984+
CASE
1985+
WHEN MAX(s.index_name = 'PRIMARY') THEN 'PRI'
1986+
WHEN MAX(s.non_unique = 0 AND s.seq_in_index = 1) THEN 'UNI'
1987+
WHEN MAX(s.seq_in_index = 1) THEN 'MUL'
1988+
ELSE ''
1989+
END
1990+
$correlation
1991+
), ''),
1992+
is_nullable = COALESCE((
1993+
SELECT
1994+
CASE
1995+
WHEN MAX(s.index_name = 'PRIMARY') THEN 'NO'
1996+
ELSE c.is_nullable
1997+
END
1998+
$correlation
1999+
), c.is_nullable)
19772000
WHERE c.table_schema = ?
19782001
AND c.table_name = ?
1979-
',
2002+
",
19802003
array( self::SAVED_DATABASE_NAME, $table_name )
19812004
);
19822005
}

0 commit comments

Comments
 (0)