Repository navigation
Commit ba9d450
Fix unique keys and foreign key restore without stringified fetches (#532)
## Summary
Fixes #451 together with #522.
`ALTER TABLE`, `CREATE TABLE`, and `SHOW CREATE TABLE` compare fetched
metadata to string values. PDO SQLite returns native types unless
`PDO::ATTR_STRINGIFY_FETCHES` is set, so on PHP 8.1 and newer, these
comparisons fail for callers that don't set that attribute. The
WordPress plugin and the MySQL proxy set it, so they are unaffected, but
the PDO-compatible API is:
```php
$pdo = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:dbname=wp', ... );
$pdo->query( 'CREATE TABLE u (id INT, email VARCHAR(50), UNIQUE KEY ue (email))' );
$pdo->query( "INSERT INTO u VALUES (1, 'a@example.com')" );
$pdo->query( "INSERT INTO u VALUES (2, 'a@example.com')" );
// Accepted: the unique key was created as a plain index.
```
The affected comparisons are the remaining ones listed in #451:
- `NON_UNIQUE` is compared to `'0'` when generating the SQLite schema
and when generating `SHOW CREATE TABLE` output, so every `UNIQUE KEY`
becomes a plain index. That also breaks `INSERT ... ON DUPLICATE KEY
UPDATE`, which then inserts duplicates.
- `PRAGMA foreign_keys` is compared to `'1'` after `ALTER TABLE`
recreates a table, so the foreign key check is skipped.
#522 fixes the temporary table lookup, and #506 already fixed schema
reconstruction.
## Fix
The values are compared as integers.
## Tests
`testUniqueKeysWithoutStringifiedFetches` disables stringified fetches,
creates a table with a unique key, checks `SHOW CREATE TABLE`, recreates
the table with `ALTER TABLE`, and checks that a duplicate row is
rejected. It fails on `trunk`. Each of the two `NON_UNIQUE` comparisons
fails it separately.
The `PRAGMA foreign_keys` comparison has no test. `ALTER TABLE` runs
inside the wrapper transaction, where `PRAGMA foreign_keys` is a no-op,
so the skipped restore has no observable effect today. It is fixed for
consistency.
I also ran the whole suite with stringified fetches disabled in every
test class. With this PR and #522, the only remaining failures are type
differences in returned values (`1` vs `'1'`) and the two tests of the
attribute itself.
`packages/mysql-on-sqlite`: 1044 tests pass (PHP 8.3, SQLite 3.45.1);
PHPCS is clean.
## Use of AI
This change was prepared with Claude Code (Claude Opus 5.5). I reviewed
the code, the tests, and this description, and I take responsibility for
the change.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
- **Bug Fixes**
- Fixed unique database indexes being lost when tables are recreated
after a schema change. Duplicate values in uniquely constrained fields
are now correctly rejected.
- Improved consistency when adding columns to tables: unique constraints
are preserved whether database results are returned as strings or other
value types. Database table definitions also continue to report unique
indexes correctly in either case.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Jan Jakeš <jan@jakes.pro>1 parent 8dc5c27 commit ba9d450
3 files changed
Lines changed: 34 additions & 4 deletions
File tree
- packages/mysql-on-sqlite
- src/sqlite
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5646 | 5646 | | |
5647 | 5647 | | |
5648 | 5648 | | |
5649 | | - | |
| 5649 | + | |
5650 | 5650 | | |
5651 | 5651 | | |
5652 | 5652 | | |
| |||
7138 | 7138 | | |
7139 | 7139 | | |
7140 | 7140 | | |
7141 | | - | |
| 7141 | + | |
7142 | 7142 | | |
7143 | 7143 | | |
7144 | 7144 | | |
| |||
7424 | 7424 | | |
7425 | 7425 | | |
7426 | 7426 | | |
7427 | | - | |
| 7427 | + | |
7428 | 7428 | | |
7429 | 7429 | | |
7430 | 7430 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5124 | 5124 | | |
5125 | 5125 | | |
5126 | 5126 | | |
| 5127 | + | |
| 5128 | + | |
| 5129 | + | |
| 5130 | + | |
| 5131 | + | |
| 5132 | + | |
| 5133 | + | |
| 5134 | + | |
| 5135 | + | |
| 5136 | + | |
| 5137 | + | |
| 5138 | + | |
| 5139 | + | |
| 5140 | + | |
| 5141 | + | |
| 5142 | + | |
| 5143 | + | |
| 5144 | + | |
| 5145 | + | |
5127 | 5146 | | |
5128 | 5147 | | |
5129 | 5148 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
884 | 884 | | |
885 | 885 | | |
886 | 886 | | |
887 | | - | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
888 | 892 | | |
889 | 893 | | |
890 | 894 | | |
| |||
915 | 919 | | |
916 | 920 | | |
917 | 921 | | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
918 | 929 | | |
919 | 930 | | |
920 | 931 | | |
| |||
0 commit comments