Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down Expand Up @@ -45,7 +45,7 @@ jobs:
run: rm -rf temp_dist version

- name: Upload distribution as artifact
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: wp-cli-sqlite-command-${{ env.RELEASE_VERSION }}
path: wp-cli-sqlite-command-${{ env.RELEASE_VERSION }}.zip
Expand Down
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,44 @@ $ wp sqlite export [<file>] [--tables=<tables>] [--exclude-tables] [--porcelain]
[--porcelain]
Output filename for the exported database.

### wp sqlite tables

Lists the SQLite database tables.

```
$ wp sqlite tables [--format=<list|csv>]
```

**OPTIONS**

[--format=<format>]
Render output in a specific format.
---
Default: list
Options:
- list
- csv
---

**EXAMPLES**

```
# List all tables
$ wp sqlite tables
wp_users
wp_usermeta
wp_termmeta
wp_terms
wp_term_taxonomy
wp_term_relationships
wp_commentmeta
wp_comments
wp_links
wp_options
wp_postmeta
wp_posts

* List all tables in CSV format
$ wp sqlite tables --format=csv
wp_users,wp_usermeta,wp_termmeta,wp_terms,wp_term_taxonomy,wp_term_relationships,wp_commentmeta,wp_comments,wp_links,wp_options,wp_postmeta,wp_posts
```
45 changes: 45 additions & 0 deletions src/SQLite_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,49 @@ public function export( $args, $assoc_args ) {

$export->run( $result_file, $assoc_args );
}

/**
* Lists the database tables.
*
* Defaults to all tables in the SQLite database.
*
* ## OPTIONS
*
* [--format=<format>]
* : Render output in a particular format.
*
* ---
* default: list
* options:
* - list
* - csv
* ---
*
* ## EXAMPLES
*
* # List all tables in the database
* $ wp sqlite tables
* wp_commentmeta
* wp_comments
* wp_links
* wp_options
* wp_postmeta
* wp_posts
* wp_terms
* wp_termmeta
* wp_term_relationships
* wp_term_taxonomy
* wp_usermeta
* wp_users
*
* @when before_wp_load
*/
public function tables( $args, $assoc_args ) {
if ( ! Base::get_sqlite_plugin_version() ) {
WP_CLI::error( 'The SQLite integration plugin is not installed or activated.' );
}

$tables = new Tables();
$tables->run( $assoc_args );
}
}
58 changes: 58 additions & 0 deletions src/Tables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Automattic\WP_CLI\SQLite;

use WP_CLI;
use PDO;
use WP_SQLite_Translator;

class Tables extends Base {

protected $translator;

public function __construct() {
$this->load_dependencies();
$this->translator = new WP_SQLite_Translator();
}

/**
* Get the PDO instance.
*
* @return PDO
*/
protected function get_pdo() {
return $this->translator->get_pdo();
}

/**
* Lists all tables in the SQLite database.
*
* @param array $assoc_args Associative array of options.
* @return void
*/
public function run( $assoc_args = [] ) {
$pdo = $this->get_pdo();

// Get all tables
$stmt = $pdo->query( "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'" );
$tables = $stmt->fetchAll( PDO::FETCH_COLUMN );

// Remove system tables
$tables_to_exclude = array( '_mysql_data_types_cache' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we are ignoring some tables as a part of the SQL query, would it make sense to do the same for this one?

$tables = array_diff( $tables, $tables_to_exclude );

if ( empty( $tables ) ) {
WP_CLI::error( 'No tables found in the database.' );
}

$format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format' );

if ( 'csv' === $format ) {
WP_CLI::line( implode( ',', $tables ) );
} else {
foreach ( $tables as $table ) {
WP_CLI::line( $table );
}
}
}
}