Skip to content

Add SQLite compatibility to wp db commands#299

Merged
swissspidy merged 59 commits intomainfrom
copilot/add-sqlite-compatibility
Mar 17, 2026
Merged

Add SQLite compatibility to wp db commands#299
swissspidy merged 59 commits intomainfrom
copilot/add-sqlite-compatibility

Conversation

Copy link
Contributor

Copilot AI commented Nov 20, 2025

Add SQLite compatibility to wp db commands

This PR implements SQLite support for database commands when using the SQLite Database Integration plugin. Commands now detect SQLite via DB_ENGINE constant, SQLITE_DB_DROPIN_VERSION, or db.php drop-in inspection.

Changes

  • New DB_Command_SQLite trait - Isolated SQLite operations using PDO:

    • create/drop/reset - File-based database lifecycle with sqlite3 binary
    • query - Direct PDO execution with formatted output (now supports --skip-column-names)
    • export/import - SQL dump/restore with proper identifier escaping
    • size - File size calculation
    • Database path resolution supporting FQDB, FQDBDIR, DB_FILE constants
  • Modified DB_Command methods - Detect SQLite and route accordingly:

    • create, drop, reset, query, export, import - Full SQLite implementation
    • size - SQLite file size vs MySQL information_schema query
    • check, optimize, repair, cli - Warning messages (not applicable to SQLite)
    • tables, prefix, columns, search, clean - Unchanged (work via $wpdb)
  • Test scenarios - Updated to support SQLite for --skip-column-names flag

  • Documentation (README.md) - Supported commands, configuration, detection methods

Security Updates

  • Fixed command injection vulnerabilities: All sqlite3 CLI commands now use Utils\esc_cmd to properly escape arguments
  • Added sqlite3 binary check: Preflight validation with clear error messages if sqlite3 is not installed

Recent Updates

  • Added --skip-column-names support for SQLite: The sqlite_query() method now accepts $assoc_args and respects the --skip-column-names flag
  • Replaced custom display_table() with Formatter class: Using the standard WP-CLI Formatter class for consistency with other commands
  • Updated tests: Removed MySQL-only restriction from --skip-column-names test as SQLite now supports it

Example Usage

# Works automatically when SQLite is detected
wp db query "SELECT * FROM wp_posts"
wp db query "SELECT * FROM wp_posts" --skip-column-names
wp db export backup.sql
wp db size

Technical Notes

  • Requires sqlite3 CLI binary for create/drop/reset/export/import operations
  • PDO used for query execution, enabling functionality at after_wp_config_load stage
  • SQLite identifiers escaped with double-quotes, values via PDO::quote()
  • Default database path: wp-content/database/.ht.sqlite
  • Full backward compatibility - MySQL/MariaDB code paths unchanged
  • Uses WP-CLI Formatter class for query output formatting
  • All shell commands properly escaped using Utils\esc_cmd

Fixes #234


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits November 20, 2025 16:30
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Add SQLite compatibility to wp db commands Add SQLite compatibility to wp db commands Nov 20, 2025
Copilot AI requested a review from swissspidy November 20, 2025 16:41
@codecov
Copy link

codecov bot commented Nov 20, 2025

Codecov Report

❌ Patch coverage is 14.76923% with 277 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/DB_Command_SQLite.php 4.06% 236 Missing ⚠️
src/DB_Command.php 48.10% 41 Missing ⚠️

📢 Thoughts on this report? Let us know!

swissspidy and others added 2 commits March 16, 2026 22:17
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI requested a review from swissspidy March 16, 2026 21:48
@swissspidy

This comment was marked as resolved.

The command_parts array was being passed directly to \WP_CLI\Process::create()
which requires a string command. This caused proc_open() to fail with
"expects parameter 1 to be string, array given".

Also fixes the .read argument: sqlite3 expects ".read FILENAME" as a single
quoted argument, not as two separate arguments.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

sqlite3 .dump produces 'CREATE TABLE' without 'IF NOT EXISTS', so
importing the dump back into the same database fails with
"table X already exists" and exit code 1.

Add a regex replacement to insert 'IF NOT EXISTS' into CREATE TABLE
statements from the dump, consistent with the existing handling for
CREATE INDEX statements.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

sqlite3 .dump also outputs CREATE TRIGGER and CREATE VIEW statements
without IF NOT EXISTS, causing re-import to fail with "already exists".

Extend the transformations to add IF NOT EXISTS to all CREATE
statements (TABLE, TRIGGER, VIEW, INDEX, UNIQUE INDEX) using regex,
so that re-importing a dump into the same database succeeds.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

@swissspidy swissspidy added this to the 2.1.5 milestone Mar 17, 2026
@swissspidy swissspidy merged commit 358e2a0 into main Mar 17, 2026
52 of 60 checks passed
@swissspidy swissspidy deleted the copilot/add-sqlite-compatibility branch March 17, 2026 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add SQLite compatibility to wp db commands

3 participants