diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d99664a..dc641a2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 @@ -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 diff --git a/README.md b/README.md index cebf4b8..c1eb583 100644 --- a/README.md +++ b/README.md @@ -42,3 +42,44 @@ $ wp sqlite export [] [--tables=] [--exclude-tables] [--porcelain] [--porcelain] Output filename for the exported database. +### wp sqlite tables + +Lists the SQLite database tables. + +``` +$ wp sqlite tables [--format=] +``` + +**OPTIONS** + + [--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 +``` diff --git a/src/SQLite_Command.php b/src/SQLite_Command.php index 77f29c7..860f6ba 100644 --- a/src/SQLite_Command.php +++ b/src/SQLite_Command.php @@ -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=] + * : 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 ); + } } diff --git a/src/Tables.php b/src/Tables.php new file mode 100644 index 0000000..5dbd283 --- /dev/null +++ b/src/Tables.php @@ -0,0 +1,58 @@ +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' ); + $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 ); + } + } + } +}