Skip to content

Commit db83ba2

Browse files
committed
Use MySQL SHOW TABLES insteand of SQLite schema tables
1 parent c5b9cd1 commit db83ba2

3 files changed

Lines changed: 25 additions & 34 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,18 @@ $ wp sqlite tables [--format=<list|csv>]
6666
```
6767
# List all tables
6868
$ wp sqlite tables
69-
wp_users
70-
wp_usermeta
71-
wp_termmeta
72-
wp_terms
73-
wp_term_taxonomy
74-
wp_term_relationships
7569
wp_commentmeta
7670
wp_comments
7771
wp_links
7872
wp_options
7973
wp_postmeta
8074
wp_posts
75+
wp_term_relationships
76+
wp_term_taxonomy
77+
wp_termmeta
78+
wp_terms
79+
wp_usermeta
80+
wp_users
8181
8282
* List all tables in CSV format
8383
$ wp sqlite tables --format=csv

‎features/sqlite_tables.feature‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,32 @@ Feature: WP-CLI SQLite Tables Command
1111
When I run `wp sqlite tables`
1212
Then STDOUT should contain:
1313
"""
14-
wp_users
15-
wp_usermeta
16-
wp_termmeta
17-
wp_terms
18-
wp_term_taxonomy
19-
wp_term_relationships
2014
wp_commentmeta
2115
wp_comments
2216
wp_links
2317
wp_options
2418
wp_postmeta
2519
wp_posts
20+
wp_term_relationships
21+
wp_term_taxonomy
22+
wp_termmeta
23+
wp_terms
24+
wp_usermeta
25+
wp_users
2626
"""
2727

2828
@require-sqlite
2929
Scenario: Successfully list the tables in the SQLite database in a CSV format
3030
When I run `wp sqlite tables --format=csv`
3131
Then STDOUT should contain:
3232
"""
33-
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
33+
wp_commentmeta,wp_comments,wp_links,wp_options,wp_postmeta,wp_posts,wp_term_relationships,wp_term_taxonomy,wp_termmeta,wp_terms,wp_usermeta,wp_users
3434
"""
3535

3636
@require-sqlite
3737
Scenario: Successfully list the tables in the SQLite database in a CSV format
3838
When I run `wp sqlite tables --format=json`
3939
Then STDOUT should contain:
4040
"""
41-
["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"]
41+
["wp_commentmeta","wp_comments","wp_links","wp_options","wp_postmeta","wp_posts","wp_term_relationships","wp_term_taxonomy","wp_termmeta","wp_terms","wp_usermeta","wp_users"]
4242
"""

‎src/Tables.php‎

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Automattic\WP_CLI\SQLite;
44

55
use WP_CLI;
6-
use PDO;
76
use WP_SQLite_Driver;
87
use WP_SQLite_Translator;
98

@@ -20,34 +19,26 @@ public function __construct() {
2019
$this->driver = SQLiteDriverFactory::create_driver();
2120
}
2221

23-
/**
24-
* Get the PDO instance.
25-
*
26-
* @return PDO
27-
*/
28-
protected function get_pdo() {
29-
if ( $this->driver instanceof WP_SQLite_Translator ) {
30-
return $this->driver->get_pdo();
31-
}
32-
return $this->driver->get_connection()->get_pdo();
33-
}
34-
3522
/**
3623
* Lists all tables in the SQLite database.
3724
*
3825
* @param array $assoc_args Associative array of options.
3926
* @return void
4027
*/
4128
public function run( $assoc_args = [] ) {
42-
$pdo = $this->get_pdo();
43-
4429
// Get all tables
45-
$stmt = $pdo->query( "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'" );
46-
$tables = $stmt->fetchAll( PDO::FETCH_COLUMN );
30+
$tables = array();
31+
foreach ( $this->driver->query( 'SHOW TABLES' ) as $row ) {
32+
$tables[] = array_values( (array) $row )[0];
33+
}
4734

48-
// Remove system tables
49-
$tables_to_exclude = array( '_mysql_data_types_cache' );
50-
$tables = array_diff( $tables, $tables_to_exclude );
35+
// With the legacy driver, we need to exclude system tables
36+
// and make sure the table names are alphabetically sorted.
37+
if ( $this->driver instanceof WP_SQLite_Translator ) {
38+
$tables_to_exclude = array( '_mysql_data_types_cache', 'sqlite_sequence' );
39+
$tables = array_diff( $tables, $tables_to_exclude );
40+
sort( $tables );
41+
}
5142

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

0 commit comments

Comments
 (0)