Skip to content

Commit 8d340f7

Browse files
committed
Update README and refactor import/export classes
1 parent af88f48 commit 8d340f7

File tree

6 files changed

+103
-54
lines changed

6 files changed

+103
-54
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
automattic/wp-cli-sqlite-command
2+
================================
3+
4+
Imports and exports SQLite databases using WP-CLI.
5+
6+
## Using
7+
8+
### wp sqlite import
9+
10+
Imports a MySQL compatible file into an SQLite database.
11+
12+
```
13+
$ wp sqlite import <file>
14+
```
15+
16+
**OPTIONS**
17+
18+
<file>
19+
The path to the MySQL compatible dump file to import. When passing `-` as the file argument, the SQL commands are read from standard input.
20+
21+
### wp sqlite export
22+
23+
Exports an SQLite database to a MySQL compatible file.
24+
25+
```
26+
$ wp sqlite export [<file>] [--tables=<tables>] [--exclude-tables] [--porcelain]
27+
```
28+
29+
**OPTIONS**
30+
31+
[<file>]
32+
Path to the file to write the MySQL compatible dump to. If not provided, the SQL commands are written to standard output.
33+
34+
[--tables=<tables>]
35+
List of tables to export. Use commas to separate multiple table names.
36+
37+
[--exclude-tables]
38+
Exclude certain tables from the export. Use commas to separate multiple table names.
39+
40+
[--porcelain]
41+
Output just the SQL commands, without any comments.
42+

command.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
'\Automattic\WP_CLI\SQLite\SQLite_Command',
1414
array(
1515
'before_invoke' => function () {
16-
// Load the SQLite driver
1716
$min_version = '7.4';
1817
if ( version_compare( PHP_VERSION, $min_version, '<' ) ) {
1918
WP_CLI::error( "The `wp server` command requires PHP {$min_version} or newer." );

src/Base.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
class Base {
77

8-
protected $unsupported_arguments = [];
9-
108
/**
119
* Get the version of the SQLite integration plugin if it is installed
1210
* and activated.
@@ -85,31 +83,11 @@ protected function load_dependencies() {
8583
}
8684

8785
// We also need to selectively load the necessary classes from the plugin.
88-
require_once $plugin_directory . '/php-polyfills.php';
8986
require_once $plugin_directory . '/constants.php';
9087
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-lexer.php';
9188
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-query-rewriter.php';
9289
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-translator.php';
9390
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-token.php';
9491
require_once $plugin_directory . '/wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php';
9592
}
96-
97-
/**
98-
* Check if the arguments passed to the command are supported.
99-
*
100-
* @param $args
101-
*
102-
* @return void
103-
* @throws WP_CLI\ExitException
104-
*/
105-
protected function check_arguments( $args ) {
106-
if ( array_intersect_key( $args, array_flip( $this->unsupported_arguments ) ) ) {
107-
WP_CLI::error(
108-
sprintf(
109-
'The following arguments are not supported by SQLite exports: %s',
110-
implode( ', ', $this->unsupported_arguments )
111-
)
112-
);
113-
}
114-
}
11593
}

src/Export.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@
88

99
class Export extends Base {
1010

11-
/**
12-
* List of arguments that are not supported by the export command.
13-
* @var string[]
14-
*/
15-
protected $unsupported_arguments = [
16-
'fields',
17-
'include-tablespaces',
18-
'defaults',
19-
'dbuser',
20-
'dbpass',
21-
];
22-
2311
protected $translator;
2412
protected $args = array();
2513
protected $is_stdout = false;
@@ -40,7 +28,6 @@ public function __construct() {
4028
*/
4129
public function run( $result_file, $args ) {
4230
$this->args = $args;
43-
$this->check_arguments( $args );
4431

4532
$handle = $this->open_output_stream( $result_file );
4633

src/Import.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@
88

99
class Import extends Base {
1010

11-
protected $unsupported_arguments = [
12-
'skip-optimization',
13-
'defaults',
14-
'fields',
15-
'dbuser',
16-
'dbpass',
17-
];
18-
1911
protected $translator;
2012
protected $args;
2113

@@ -34,7 +26,6 @@ public function __construct() {
3426
*/
3527
public function run( $sql_file_path, $args ) {
3628
$this->args = $args;
37-
$this->check_arguments( $args );
3829

3930
$is_stdin = '-' === $sql_file_path;
4031
$import_file = $is_stdin ? 'php://stdin' : $sql_file_path;

src/SQLite_Command.php

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,84 @@ class SQLite_Command extends WP_CLI_Command {
99

1010

1111
/**
12-
* Imports the database to SQLite.
12+
* Imports the database to SQLite from an MySQL dump file or from STDIN.
1313
*
1414
* ## OPTIONS
1515
*
1616
* <file>
17-
* : The file to import or - for stdin.
17+
* : The name of the SQL file to import. If '-', then reads from STDIN. If omitted, it will look for '{dbname}.sql'.
18+
*
19+
* ## EXAMPLES
20+
* # Import the database from a file
21+
* $ wp sqlite import wordpress_dbase.sql
22+
* Success: Imported from 'import wordpress_dbase.sql'.
1823
*/
1924
public function import( $args, $assoc_args ) {
20-
WP_CLI::success( 'Importing database...' );
25+
26+
if ( empty( $args[0] ) ) {
27+
WP_CLI::error( 'Please provide a file to import.' );
28+
}
29+
30+
if ( ! Import::get_sqlite_plugin_version() ) {
31+
WP_CLI::error( 'The SQLite integration plugin is not installed or activated.' );
32+
}
33+
2134
$import = new Import();
22-
$file = $args[0];
23-
$import->run( $file, $assoc_args );
35+
$import->run( $args[0], $assoc_args );
2436
}
2537

2638

2739
/**
28-
* Exports the database from SQLite
40+
* Exports the database from SQLite to a file or to STDOUT.
2941
*
3042
* ## OPTIONS
3143
*
32-
* <file>
33-
* : The file to export to or - for stdout.
44+
* [<file>]
45+
* : The name of the SQL file to export. If '-', then outputs to STDOUT. If
46+
* omitted, it will be '{dbname}-{Y-m-d}-{random-hash}.sql'.
47+
*
48+
* [--tables=<tables>]
49+
* : The tables to export. Separate multiple tables with a comma. If omitted, all tables will be exported.
50+
*
51+
* [--exclude_tables=<tables>]
52+
* : The comma separated list of specific tables that should be skipped from exporting. Excluding this parameter will export all tables in the database.
53+
*
54+
* [--porcelain]
55+
* : Output filename for the exported database.
56+
*
57+
* ## EXAMPLES
58+
* # Export the database to a file
59+
* $ wp sqlite export wordpress_dbase.sql
60+
* Success: Exported to 'wordpress_dbase.sql'.
61+
*
62+
* # Export the database to STDOUT
63+
* $ wp sqlite export -
64+
* -- Table structure for table wp_users
65+
* DROP TABLE IF EXISTS wp_users;
66+
* CREATE TABLE wp_users (
67+
* ...
68+
*
69+
* # Export only specific tables
70+
* $ wp sqlite export --tables=wp_posts,wp_users
71+
* Success: Exported to 'wordpress_dbase.sql'.
72+
*
73+
* # Export all tables except specific tables
74+
* $ wp sqlite export --exclude_tables=wp_posts,wp_users
75+
* Success: Exported to 'wordpress_dbase.sql'.
3476
*/
3577
public function export( $args, $assoc_args ) {
3678
WP_CLI::success( 'Exporting database...' );
3779
$export = new Export();
38-
$export->run( $args[0], $assoc_args );
80+
81+
if ( ! empty( $args[0] ) ) {
82+
$result_file = $args[0];
83+
} else {
84+
// phpcs:ignore WordPress.WP.AlternativeFunctions.rand_mt_rand -- WordPress is not loaded.
85+
$hash = substr( md5( mt_rand() ), 0, 7 );
86+
$result_file = sprintf( '%s-%s.sql', date( 'Y-m-d' ), $hash ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
87+
88+
}
89+
90+
$export->run( $result_file, $assoc_args );
3991
}
4092
}

0 commit comments

Comments
 (0)