Commit cc882d6
authored
fix: strip the table prefix correctly when rebuilding a SQLite3 table (#10509)
SQLite cannot alter or drop a column in place, so `SQLite3\Table` rebuilds
the whole table and recreates its foreign keys from the metadata it
collected. That metadata holds prefixed table names, and `createTable()`
stripped the prefix with `trim($name, $this->db->DBPrefix)`.
`trim()`'s second argument is a set of characters, not a prefix. It removes
any of those characters from either end of the string, repeatedly, so with
the prefix `db_` it turns `db_bandit_fk` into `andit_fk` — the leading `b`
of the table's own name is eaten as well, and characters are stripped from
the end too.
The rebuilt table's foreign keys then reference tables that do not exist.
Nothing fails at that point, because foreign key enforcement is off for the
duration of the rebuild; the error surfaces at the next write to the
referenced table, naming a table that appears nowhere in the schema. Whether
a given table is affected depends on which characters its name happens to
begin and end with, so most tables come through untouched.
Strip the prefix the way `fromTable()` in the same class already does.
The existing tests could not catch this: `AlterTableTest` builds its own
connection without a `DBPrefix`, and the damage is invisible until the
constraint is used. The regression test therefore sets a prefix and names
the referenced table so that it begins with a character the prefix also
contains, which is what makes the bug reproduce.1 parent 860b5ec commit cc882d6
3 files changed
Lines changed: 71 additions & 1 deletion
File tree
- system/Database/SQLite3
- tests/system/Database/Live/SQLite3
- user_guide_src/source/changelogs
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
333 | 333 | | |
334 | 334 | | |
335 | 335 | | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
336 | 343 | | |
337 | 344 | | |
338 | | - | |
| 345 | + | |
339 | 346 | | |
340 | 347 | | |
341 | 348 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
271 | 271 | | |
272 | 272 | | |
273 | 273 | | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
274 | 336 | | |
275 | 337 | | |
276 | 338 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
0 commit comments