|
| 1 | +# wp-cli-sqlite-command |
| 2 | + |
| 3 | +WP-CLI package for importing and exporting SQLite databases, used by WordPress Studio to power the Import, Export, and Sync features. |
| 4 | + |
| 5 | +## SQLiteCommands |
| 6 | + |
| 7 | +### `wp sqlite import <file>` |
| 8 | + |
| 9 | +Imports a MySQL dump file into the SQLite database. Parses SQL statements using a streaming character-by-character parser, sets default SQL modes (`NO_AUTO_VALUE_ON_ZERO`, disables unique/foreign key checks) to match `mysqldump` defaults, and attempts UTF-8 encoding conversion as a fallback when queries fail. |
| 10 | + |
| 11 | +**Parameters:** |
| 12 | + |
| 13 | +- `<file>` (required), Path to the SQL dump file. Use `-` to read from STDIN. |
| 14 | +- `[--enable-ast-driver]` Use the new AST-based `WP_SQLite_Driver` instead of the legacy `WP_SQLite_Translator` for full MySQL compatibility. |
| 15 | + |
| 16 | +### `wp sqlite export [<file>]` |
| 17 | + |
| 18 | +Exports the SQLite database to a MySQL-compatible SQL dump file. Generates `DROP TABLE IF EXISTS`, `CREATE TABLE`, and `INSERT INTO` statements for each table. Automatically excludes internal SQLite system tables (`_mysql_data_types_cache`, `sqlite_master`, `sqlite_sequence`, `_wp_sqlite_*`). |
| 19 | + |
| 20 | +**Parameters:** |
| 21 | + |
| 22 | +- `[<file>]` (optional), Output file path. Use `-` to write to STDOUT. Defaults to `{Y-m-d}-{random-hash}.sql`. |
| 23 | +- `[--tables=<tables>]` Comma-separated list of tables to include. If omitted, all tables are exported. |
| 24 | +- `[--exclude_tables=<tables>]` Comma-separated list of tables to skip. |
| 25 | +- `[--porcelain]` Output only the filename (for scripting). |
| 26 | +- `[--enable-ast-driver]` Use the new AST-based driver. |
| 27 | + |
| 28 | +### `wp sqlite tables` |
| 29 | + |
| 30 | +Lists all user tables in the SQLite database, excluding internal/system tables. With the legacy driver, results are sorted alphabetically. |
| 31 | + |
| 32 | +**Parameters:** |
| 33 | + |
| 34 | +- `[--format=<format>]` Output format: `list` (default, one table per line), `csv`, or `json`. |
| 35 | +- `[--enable-ast-driver]` Use the new AST-based driver. |
| 36 | + |
| 37 | +## Tech Stack |
| 38 | + |
| 39 | +- PHP >=7.4, WP-CLI package (`wp-cli/wp-cli ^2.5`) |
| 40 | +- WordPress SQLite Database Integration plugin, translation layer between MySQL queries and SQLite |
| 41 | +- Two driver modes: legacy `WP_SQLite_Translator` and new AST-based `WP_SQLite_Driver` (via `--enable-ast-driver`) |
| 42 | + |
| 43 | +## Directory Structure |
| 44 | + |
| 45 | +```txt |
| 46 | +command.php # Entry point, registers the `wp sqlite` CLI command |
| 47 | +src/ # Core classes: SQLite_Command, Import, Export, Tables |
| 48 | +features/ # Behat BDD test scenarios (.feature files) |
| 49 | +features/bootstrap/ # Behat step definitions (SQLiteFeatureContext) |
| 50 | +``` |
| 51 | + |
| 52 | +## Validation |
| 53 | + |
| 54 | +### Lint |
| 55 | + |
| 56 | +```sh |
| 57 | +composer lint |
| 58 | +``` |
| 59 | + |
| 60 | +### Code Style (PHPCS) |
| 61 | + |
| 62 | +```sh |
| 63 | +composer phpcs |
| 64 | +``` |
| 65 | + |
| 66 | +Autofix: |
| 67 | + |
| 68 | +```sh |
| 69 | +composer phpcbf |
| 70 | +``` |
| 71 | + |
| 72 | +Uses the `WP_CLI_CS` ruleset (WordPress Coding Standards). Config in `phpcs.xml.dist`. |
| 73 | + |
| 74 | +### Test |
| 75 | + |
| 76 | +```sh |
| 77 | +WP_CLI_TEST_DBTYPE=sqlite vendor/bin/behat |
| 78 | +``` |
| 79 | + |
| 80 | +Tests require a WordPress installation. Using `WP_CLI_TEST_DBTYPE=sqlite` avoids the MySQL dependency. |
| 81 | + |
| 82 | +### All Checks |
| 83 | + |
| 84 | +```sh |
| 85 | +composer test |
| 86 | +``` |
| 87 | + |
| 88 | +Runs lint, phpcs, phpunit, and behat sequentially. |
| 89 | + |
| 90 | +**IMPORTANT - Post-Change Verification**: After applying code changes, always run the linter (`composer lint`), code style checks (`composer phpcs`), and relevant tests (`WP_CLI_TEST_DBTYPE=sqlite vendor/bin/behat`) before considering the work complete. |
| 91 | + |
| 92 | +## Conventions |
| 93 | + |
| 94 | +- **Namespace:** `Automattic\WP_CLI\SQLite` all classes under `src/` |
| 95 | +- **Global prefix:** `automattic_wpcli_sqlite` for any global variables or functions |
| 96 | +- **Commits:** Imperative mood, descriptive subject. Reference PR/issue numbers (e.g., `Fix import query parsing #14`) |
| 97 | +- **Branches:** Create from `main`. Prefixed with `fix/`, `update/`, or `add/` followed by a descriptive name |
| 98 | +- **PRs:** Target `main` branch. MUST pass all CI checks (code quality + testing) before merge |
| 99 | +- **Indentation:** Tabs for PHP files, spaces (2) for YAML, JSON, and `.feature` files (see `.editorconfig`) |
| 100 | +- **Code style:** Enforced by PHPCS with `WP_CLI_CS` ruleset, do not duplicate linter rules |
| 101 | +- **IMPORTANT:** Prefer merging `main` into your branch over rebasing. Avoid force pushes to `main` or already-pushed branches |
| 102 | + |
| 103 | +## Architecture |
| 104 | + |
| 105 | +- `command.php` is the entry point, it registers `wp sqlite` as a WP-CLI command pointing to `SQLite_Command` |
| 106 | +- `SQLite_Command` delegates to `Import`, `Export`, and `Tables` classes |
| 107 | +- `SQLiteDatabaseIntegrationLoader::load_plugin()` bootstraps the WordPress SQLite Database Integration plugin, which MUST be loaded before any database operations |
| 108 | +- `SQLiteDriverFactory::create_driver()` creates the appropriate driver based on whether the AST driver is enabled |
| 109 | +- The import parser (`Import::parse_statements()`) is a streaming SQL parser using PHP generators, it handles quotes, comments, escape sequences, and multi-line statements character by character |
| 110 | + |
| 111 | +## Common Pitfalls |
| 112 | + |
| 113 | +- **MUST load the SQLite plugin first:** All database operations require `SQLiteDatabaseIntegrationLoader::load_plugin()` to be called before creating a driver. The driver depends on classes from the WordPress SQLite Database Integration plugin. |
| 114 | +- **Two driver APIs:** The legacy `WP_SQLite_Translator` and new `WP_SQLite_Driver` (AST) have different class names. Use `SQLiteDriverFactory::create_driver()` to get the correct one, do not instantiate drivers directly. |
| 115 | +- **SQL parser edge cases:** The `Import::parse_statements()` method handles escape sequences, nested quotes, and multi-line comments manually. Changes to this parser MUST be tested with the existing Behat scenarios in `features/sqlite-import.feature`. |
| 116 | +- **Encoding handling:** Import attempts UTF-8 conversion as a fallback when queries fail. This is intentional error recovery, not a bug. |
| 117 | +- **`@when after_wp_config_load`:** All command methods use this WP-CLI hook annotation. New commands MUST include it to ensure WordPress config is loaded before execution. |
| 118 | +- **PHPCS inline suppressions:** The codebase uses `@phpcs:disable` / `@phpcs:enable` comments around `define('WP_SQLITE_AST_DRIVER')` because the constant name doesn't follow the project prefix convention. This is intentional. |
| 119 | + |
| 120 | +## Documentation |
| 121 | + |
| 122 | +- **Testing guide:** `TESTING.md` How to run the Behat test suite locally with SQLite |
| 123 | +- **README:** `README.md` CLI usage, options, and examples for all commands |
0 commit comments