Skip to content

Report and adjust AUTO_INCREMENT values in metadata commands#367

Draft
JanJakes wants to merge 2 commits intotrunkfrom
show-auto-increment-value
Draft

Report and adjust AUTO_INCREMENT values in metadata commands#367
JanJakes wants to merge 2 commits intotrunkfrom
show-auto-increment-value

Conversation

@JanJakes
Copy link
Copy Markdown
Member

Summary

Adds support for reporting and adjusting AUTO_INCREMENT counters, which the driver previously ignored.

Reporting (commit 1)

SHOW TABLE STATUS, SHOW CREATE TABLE, and INFORMATION_SCHEMA.TABLES now compute the Auto_increment value live from sqlite_sequence rather than returning NULL. The counter is preserved past DELETE and reset by TRUNCATE, matching InnoDB semantics.

This retires the first half of patchWp15AdminPostAutoIncrement in WordPress, which existed to work around WP 1.5's wp-admin/post.php reading the SHOW TABLE STATUS Auto_increment column.

Adjusting (commit 2)

Three MySQL forms are now honored:

  1. CREATE TABLE t (...) AUTO_INCREMENT = N — seeds the counter.
  2. ALTER TABLE t AUTO_INCREMENT = N — instant; no table rebuild.
  3. ALTER TABLE t <actions>, AUTO_INCREMENT = N — applied alongside other alter items.

The requested value is written as N - 1 into sqlite_sequence (MySQL stores the next value, SQLite the last used), clamped to MAX(col) so the effective counter never drops below an existing row — matching MySQL. When the table has no AUTO_INCREMENT column, the option is silently ignored, matching MySQL.

Not covered: SET @@auto_increment_increment / SET @@auto_increment_offset. These change the step and offset of generated values, not the counter, and SQLite's AUTOINCREMENT always steps by 1 — so emulating non-1 increments would require generating IDs in PHP for every insert. Out of scope here.

Tests

  • testInformationSchemaTablesAutoIncrement, testShowTableStatusAutoIncrement, testShowCreateTableAutoIncrement, testShowCreateTableAutoIncrementOnTemporaryTable, testShowTableStatusFilterByAutoIncrement — reporting.
  • testCreateTableAutoIncrementOption, testAlterTableAutoIncrementOption, testAlterTableAutoIncrementOptionMixedWithAlterItems, testAlterTableAutoIncrementOptionWithoutAutoIncrementColumn, testAlterTableAutoIncrementOptionAfterTruncate, testAlterTableAutoIncrementOptionOnTemporaryTable — adjustment, including MAX-clamp behavior, fast-path (no rebuild), mixed alter items, temp-vs-persistent isolation, and no-op when there's no AI column.

Notes

  • sqlite_sequence has no UNIQUE constraint on name, so native upsert is unavailable; the row is maintained with UPDATE-then-INSERT (second query only runs when the first matches nothing).
  • The clamp expression is inlined as an integer literal rather than a bound parameter. PDO binds PHP ints as TEXT, and SQLite's type affinity ranks TEXT above INTEGER in MAX(), which would silently produce wrong results.
  • ALTER TABLE with only table options (no alterListItem) now skips the full-table copy — previously a pointless rewrite.

Test plan

  • CI green.
  • New tests in WP_SQLite_Driver_Metadata_Tests pass.
  • Full suite (665 tests) still passes.

Compute the MySQL Auto_increment value live from SQLite's sqlite_sequence
so SHOW TABLE STATUS, SHOW CREATE TABLE, and queries against
INFORMATION_SCHEMA.TABLES report the next AUTO_INCREMENT value instead
of always returning NULL. The counter is preserved past DELETE and reset
by TRUNCATE, matching MySQL InnoDB semantics.

Retires the first half of patchWp15AdminPostAutoIncrement in WordPress,
which existed to work around WP 1.5's wp-admin/post.php reading the
SHOW TABLE STATUS Auto_increment column.
Implement the AUTO_INCREMENT table option in CREATE TABLE and ALTER TABLE
by writing the requested value as seq - 1 into SQLite's sqlite_sequence.
As in MySQL, the effective counter is clamped to MAX(col) so it never
drops below an existing row, and the option is silently ignored when the
table has no AUTO_INCREMENT column.

Also skip the full table rebuild in ALTER TABLE when the statement only
carries table options (no alterListItem) so that pure AUTO_INCREMENT = N
changes are instant rather than a no-op full-table copy.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant