Skip to content

Preserve index prefix lengths and order in SHOW CREATE TABLE primary keys - #500

Open
samuelmbabhazi wants to merge 1 commit into
WordPress:trunkfrom
samuelmbabhazi:fix/primary-key-sub-part
Open

Preserve index prefix lengths and order in SHOW CREATE TABLE primary keys#500
samuelmbabhazi wants to merge 1 commit into
WordPress:trunkfrom
samuelmbabhazi:fix/primary-key-sub-part

Conversation

@samuelmbabhazi

@samuelmbabhazi samuelmbabhazi commented Aug 31, 2026

Copy link
Copy Markdown

Summary

SHOW CREATE TABLE reconstructs the PRIMARY KEY clause from the information schema, but unlike the branch that handles all other indexes, it emitted only the quoted column names. Index prefix lengths (SUB_PART) and descending order (COLLATION = 'D') were dropped, so a table created with PRIMARY KEY (session_id(100)) on a MEDIUMTEXT column came back as PRIMARY KEY (session_id).

The practical impact: exports built on top of SHOW CREATE TABLE (for example wp sqlite export, which WordPress Studio uses for its push feature) produce a dump that MySQL rejects with ERROR 1170 (42000): BLOB/TEXT column used in key specification without a key length. The prefix is recorded correctly in _wp_sqlite_mysql_information_schema_statistics (SUB_PART = 100); the bug was emission only. Discovered through Automattic/studio#4739.

Fixes #501

Fix

The column formatting closure that already handled SUB_PART and DESC for regular keys is hoisted and shared by the PRIMARY KEY branch, so both paths emit identical column definitions.

Testing

  • New regression test: a PRIMARY KEY (session_id(100)) on MEDIUMTEXT round-trips through SHOW CREATE TABLE with its prefix, a composite key PRIMARY KEY (a, b(50)) keeps the prefix only where defined, and PRIMARY KEY (a DESC, b) keeps its descending key part. The test fails on the previous code.
  • Verified against a real MySQL 8 server: its SHOW CREATE TABLE emits PRIMARY KEY (session_id(100)) and PRIMARY KEY (a DESC,b) for the same tables, matching the fixed output; the previous output fails to import there with ERROR 1170, the fixed output imports cleanly.
  • Full mysql-on-sqlite unit suite: 875 tests, 1428750 assertions, no failures (the same 17 skipped and 2 incomplete as on trunk, PHP 8.5).
  • composer run check-cs is clean on both changed files.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected SHOW CREATE TABLE output to preserve primary-key index prefix lengths for single-column and composite keys.
    • Ensured descending key parts and column definitions are represented accurately in generated table-creation statements.
    • Improved consistency between primary-key and secondary-index formatting.

@coderabbitai

coderabbitai Bot commented Aug 31, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: abbc7d20-fbfe-4e16-b42d-54fc03150ff5

📥 Commits

Reviewing files that changed from the base of the PR and between abf0dac and 9e45b6e.

📒 Files selected for processing (2)
  • packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php
  • packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Metadata_Tests.php

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The table-definition generator now uses shared formatting for primary and secondary index columns. The formatter preserves column names, prefix lengths, and descending order. Metadata tests cover single-column and composite primary keys.

Changes

MySQL-on-SQLite metadata

Layer / File(s) Summary
Index formatting and validation
packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php, packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Metadata_Tests.php
A shared formatter handles primary and secondary index columns. Tests verify primary-key prefix lengths and descending order in SHOW CREATE TABLE output.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 9e45b

This localized change preserves primary-key prefix lengths and descending order in generated table definitions, preventing invalid exports while leaving other index formatting unchanged. No actionable merge-blocking risk remains after normal checks and review.

Suggested reviewers: janjakes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preserving index prefix lengths and order when reconstructing primary keys in SHOW CREATE TABLE.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@samuelmbabhazi
samuelmbabhazi force-pushed the fix/primary-key-sub-part branch from 9e45b6e to ae52afd Compare August 31, 2026 19:38
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.

SHOW CREATE TABLE drops index prefix lengths and DESC from primary keys

1 participant