Skip to content

Commit e8a0970

Browse files
authored
Merge pull request #5 from Automattic/add/sqlite-tables-command
Add SQLite tables command supporting list and csv format
2 parents 9dd8078 + f6dce6f commit e8a0970

File tree

4 files changed

+146
-2
lines changed

4 files changed

+146
-2
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
build:
99
runs-on: ubuntu-latest
1010
steps:
11-
- uses: actions/checkout@v2
11+
- uses: actions/checkout@v4
1212

1313
- name: Setup PHP
1414
uses: shivammathur/setup-php@v2
@@ -45,7 +45,7 @@ jobs:
4545
run: rm -rf temp_dist version
4646

4747
- name: Upload distribution as artifact
48-
uses: actions/upload-artifact@v2
48+
uses: actions/upload-artifact@v4
4949
with:
5050
name: wp-cli-sqlite-command-${{ env.RELEASE_VERSION }}
5151
path: wp-cli-sqlite-command-${{ env.RELEASE_VERSION }}.zip

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,44 @@ $ wp sqlite export [<file>] [--tables=<tables>] [--exclude-tables] [--porcelain]
4242
[--porcelain]
4343
Output filename for the exported database.
4444

45+
### wp sqlite tables
46+
47+
Lists the SQLite database tables.
48+
49+
```
50+
$ wp sqlite tables [--format=<list|csv>]
51+
```
52+
53+
**OPTIONS**
54+
55+
[--format=<format>]
56+
Render output in a specific format.
57+
---
58+
Default: list
59+
Options:
60+
- list
61+
- csv
62+
---
63+
64+
**EXAMPLES**
65+
66+
```
67+
# List all tables
68+
$ wp sqlite tables
69+
wp_users
70+
wp_usermeta
71+
wp_termmeta
72+
wp_terms
73+
wp_term_taxonomy
74+
wp_term_relationships
75+
wp_commentmeta
76+
wp_comments
77+
wp_links
78+
wp_options
79+
wp_postmeta
80+
wp_posts
81+
82+
* List all tables in CSV format
83+
$ wp sqlite tables --format=csv
84+
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
85+
```

src/SQLite_Command.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,49 @@ public function export( $args, $assoc_args ) {
9797

9898
$export->run( $result_file, $assoc_args );
9999
}
100+
101+
/**
102+
* Lists the database tables.
103+
*
104+
* Defaults to all tables in the SQLite database.
105+
*
106+
* ## OPTIONS
107+
*
108+
* [--format=<format>]
109+
* : Render output in a particular format.
110+
*
111+
* ---
112+
* default: list
113+
* options:
114+
* - list
115+
* - csv
116+
* ---
117+
*
118+
* ## EXAMPLES
119+
*
120+
* # List all tables in the database
121+
* $ wp sqlite tables
122+
* wp_commentmeta
123+
* wp_comments
124+
* wp_links
125+
* wp_options
126+
* wp_postmeta
127+
* wp_posts
128+
* wp_terms
129+
* wp_termmeta
130+
* wp_term_relationships
131+
* wp_term_taxonomy
132+
* wp_usermeta
133+
* wp_users
134+
*
135+
* @when before_wp_load
136+
*/
137+
public function tables( $args, $assoc_args ) {
138+
if ( ! Base::get_sqlite_plugin_version() ) {
139+
WP_CLI::error( 'The SQLite integration plugin is not installed or activated.' );
140+
}
141+
142+
$tables = new Tables();
143+
$tables->run( $assoc_args );
144+
}
100145
}

src/Tables.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Automattic\WP_CLI\SQLite;
4+
5+
use WP_CLI;
6+
use PDO;
7+
use WP_SQLite_Translator;
8+
9+
class Tables extends Base {
10+
11+
protected $translator;
12+
13+
public function __construct() {
14+
$this->load_dependencies();
15+
$this->translator = new WP_SQLite_Translator();
16+
}
17+
18+
/**
19+
* Get the PDO instance.
20+
*
21+
* @return PDO
22+
*/
23+
protected function get_pdo() {
24+
return $this->translator->get_pdo();
25+
}
26+
27+
/**
28+
* Lists all tables in the SQLite database.
29+
*
30+
* @param array $assoc_args Associative array of options.
31+
* @return void
32+
*/
33+
public function run( $assoc_args = [] ) {
34+
$pdo = $this->get_pdo();
35+
36+
// Get all tables
37+
$stmt = $pdo->query( "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'" );
38+
$tables = $stmt->fetchAll( PDO::FETCH_COLUMN );
39+
40+
// Remove system tables
41+
$tables_to_exclude = array( '_mysql_data_types_cache' );
42+
$tables = array_diff( $tables, $tables_to_exclude );
43+
44+
if ( empty( $tables ) ) {
45+
WP_CLI::error( 'No tables found in the database.' );
46+
}
47+
48+
$format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format' );
49+
50+
if ( 'csv' === $format ) {
51+
WP_CLI::line( implode( ',', $tables ) );
52+
} else {
53+
foreach ( $tables as $table ) {
54+
WP_CLI::line( $table );
55+
}
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)