Report and adjust AUTO_INCREMENT values in metadata commands#367
Draft
Report and adjust AUTO_INCREMENT values in metadata commands#367
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds support for reporting and adjusting
AUTO_INCREMENTcounters, which the driver previously ignored.Reporting (commit 1)
SHOW TABLE STATUS,SHOW CREATE TABLE, andINFORMATION_SCHEMA.TABLESnow compute theAuto_incrementvalue live fromsqlite_sequencerather than returningNULL. The counter is preserved pastDELETEand reset byTRUNCATE, matching InnoDB semantics.This retires the first half of
patchWp15AdminPostAutoIncrementin WordPress, which existed to work around WP 1.5'swp-admin/post.phpreading theSHOW TABLE STATUS Auto_incrementcolumn.Adjusting (commit 2)
Three MySQL forms are now honored:
CREATE TABLE t (...) AUTO_INCREMENT = N— seeds the counter.ALTER TABLE t AUTO_INCREMENT = N— instant; no table rebuild.ALTER TABLE t <actions>, AUTO_INCREMENT = N— applied alongside other alter items.The requested value is written as
N - 1intosqlite_sequence(MySQL stores the next value, SQLite the last used), clamped toMAX(col)so the effective counter never drops below an existing row — matching MySQL. When the table has noAUTO_INCREMENTcolumn, 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'sAUTOINCREMENTalways 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_sequencehas noUNIQUEconstraint onname, so native upsert is unavailable; the row is maintained withUPDATE-then-INSERT(second query only runs when the first matches nothing).TEXT, and SQLite's type affinity ranksTEXTaboveINTEGERinMAX(), which would silently produce wrong results.ALTER TABLEwith only table options (noalterListItem) now skips the full-table copy — previously a pointless rewrite.Test plan
WP_SQLite_Driver_Metadata_Testspass.