Skip to content

Commit cbcf158

Browse files
Copilotswissspidy
andcommitted
Add SQLite support trait and integrate into DB_Command
Co-authored-by: swissspidy <[email protected]>
1 parent 733e172 commit cbcf158

File tree

2 files changed

+567
-16
lines changed

2 files changed

+567
-16
lines changed

src/DB_Command.php

Lines changed: 118 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use WP_CLI\Formatter;
44
use WP_CLI\Utils;
55

6+
require_once __DIR__ . '/DB_Command_SQLite.php';
7+
68
/**
79
* Performs basic database operations using credentials stored in wp-config.php.
810
*
@@ -27,6 +29,8 @@
2729
*/
2830
class DB_Command extends WP_CLI_Command {
2931

32+
use DB_Command_SQLite;
33+
3034
/**
3135
* Legacy UTF-8 encoding for MySQL.
3236
*
@@ -82,6 +86,12 @@ class DB_Command extends WP_CLI_Command {
8286
* Success: Database created.
8387
*/
8488
public function create( $_, $assoc_args ) {
89+
$this->maybe_load_sqlite_dropin();
90+
91+
if ( $this->is_sqlite() ) {
92+
$this->sqlite_create( $assoc_args );
93+
return;
94+
}
8595

8696
$this->run_query( self::get_create_query(), $assoc_args );
8797

@@ -115,6 +125,15 @@ public function create( $_, $assoc_args ) {
115125
* Success: Database dropped.
116126
*/
117127
public function drop( $_, $assoc_args ) {
128+
$this->maybe_load_sqlite_dropin();
129+
130+
if ( $this->is_sqlite() ) {
131+
$db_path = $this->get_sqlite_db_path();
132+
WP_CLI::confirm( "Are you sure you want to drop the SQLite database at '{$db_path}'?", $assoc_args );
133+
$this->sqlite_drop( $assoc_args );
134+
return;
135+
}
136+
118137
WP_CLI::confirm( "Are you sure you want to drop the '" . DB_NAME . "' database?", $assoc_args );
119138

120139
$this->run_query( sprintf( 'DROP DATABASE `%s`', DB_NAME ), $assoc_args );
@@ -149,6 +168,15 @@ public function drop( $_, $assoc_args ) {
149168
* Success: Database reset.
150169
*/
151170
public function reset( $_, $assoc_args ) {
171+
$this->maybe_load_sqlite_dropin();
172+
173+
if ( $this->is_sqlite() ) {
174+
$db_path = $this->get_sqlite_db_path();
175+
WP_CLI::confirm( "Are you sure you want to reset the SQLite database at '{$db_path}'?", $assoc_args );
176+
$this->sqlite_reset( $assoc_args );
177+
return;
178+
}
179+
152180
WP_CLI::confirm( "Are you sure you want to reset the '" . DB_NAME . "' database?", $assoc_args );
153181

154182
$this->run_query( sprintf( 'DROP DATABASE IF EXISTS `%s`', DB_NAME ), $assoc_args );
@@ -249,6 +277,12 @@ public function clean( $_, $assoc_args ) {
249277
* Success: Database checked.
250278
*/
251279
public function check( $_, $assoc_args ) {
280+
$this->maybe_load_sqlite_dropin();
281+
282+
if ( $this->is_sqlite() ) {
283+
WP_CLI::warning( 'Database check is not supported for SQLite databases.' );
284+
return;
285+
}
252286

253287
$command = sprintf(
254288
'/usr/bin/env %s%s %s',
@@ -298,6 +332,13 @@ public function check( $_, $assoc_args ) {
298332
* Success: Database optimized.
299333
*/
300334
public function optimize( $_, $assoc_args ) {
335+
$this->maybe_load_sqlite_dropin();
336+
337+
if ( $this->is_sqlite() ) {
338+
WP_CLI::warning( 'Database optimization is not supported for SQLite databases. SQLite automatically optimizes on VACUUM.' );
339+
return;
340+
}
341+
301342
$command = sprintf(
302343
'/usr/bin/env %s%s %s',
303344
Utils\get_sql_check_command(),
@@ -346,6 +387,13 @@ public function optimize( $_, $assoc_args ) {
346387
* Success: Database repaired.
347388
*/
348389
public function repair( $_, $assoc_args ) {
390+
$this->maybe_load_sqlite_dropin();
391+
392+
if ( $this->is_sqlite() ) {
393+
WP_CLI::warning( 'Database repair is not supported for SQLite databases.' );
394+
return;
395+
}
396+
349397
$command = sprintf(
350398
'/usr/bin/env %s%s %s',
351399
Utils\get_sql_check_command(),
@@ -396,6 +444,12 @@ public function repair( $_, $assoc_args ) {
396444
* @alias connect
397445
*/
398446
public function cli( $_, $assoc_args ) {
447+
$this->maybe_load_sqlite_dropin();
448+
449+
if ( $this->is_sqlite() ) {
450+
WP_CLI::warning( 'Interactive console (cli) is not supported for SQLite databases. Use `wp db query` instead.' );
451+
return;
452+
}
399453

400454
$command = sprintf(
401455
'/usr/bin/env %s%s --no-auto-rehash',
@@ -499,6 +553,24 @@ public function cli( $_, $assoc_args ) {
499553
* +---------+-----------------------+
500554
*/
501555
public function query( $args, $assoc_args ) {
556+
$this->maybe_load_sqlite_dropin();
557+
558+
if ( $this->is_sqlite() ) {
559+
// Get the query from args or STDIN.
560+
$query = '';
561+
if ( ! empty( $args ) ) {
562+
$query = $args[0];
563+
} else {
564+
$query = stream_get_contents( STDIN );
565+
}
566+
567+
if ( empty( $query ) ) {
568+
WP_CLI::error( 'No query specified.' );
569+
}
570+
571+
$this->sqlite_query( $query, $assoc_args );
572+
return;
573+
}
502574

503575
$command = sprintf(
504576
'/usr/bin/env %s%s --no-auto-rehash',
@@ -629,14 +701,23 @@ public function query( $args, $assoc_args ) {
629701
* @alias dump
630702
*/
631703
public function export( $args, $assoc_args ) {
704+
$this->maybe_load_sqlite_dropin();
705+
632706
if ( ! empty( $args[0] ) ) {
633707
$result_file = $args[0];
634708
} else {
635709
// phpcs:ignore WordPress.WP.AlternativeFunctions.rand_mt_rand -- WordPress is not loaded.
636710
$hash = substr( md5( (string) mt_rand() ), 0, 7 );
637-
$result_file = sprintf( '%s-%s-%s.sql', DB_NAME, date( 'Y-m-d' ), $hash ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
711+
$db_name = $this->is_sqlite() ? 'sqlite-db' : DB_NAME;
712+
$result_file = sprintf( '%s-%s-%s.sql', $db_name, date( 'Y-m-d' ), $hash ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
713+
714+
}
638715

716+
if ( $this->is_sqlite() ) {
717+
$this->sqlite_export( $result_file, $assoc_args );
718+
return;
639719
}
720+
640721
$stdout = ( '-' === $result_file );
641722
$porcelain = Utils\get_flag_value( $assoc_args, 'porcelain' );
642723

@@ -798,10 +879,17 @@ private function get_posts_table_charset( $assoc_args ) {
798879
* Success: Imported from 'wordpress_dbase.sql'.
799880
*/
800881
public function import( $args, $assoc_args ) {
882+
$this->maybe_load_sqlite_dropin();
883+
801884
if ( ! empty( $args[0] ) ) {
802885
$result_file = $args[0];
803886
} else {
804-
$result_file = sprintf( '%s.sql', DB_NAME );
887+
$result_file = $this->is_sqlite() ? 'sqlite-db.sql' : sprintf( '%s.sql', DB_NAME );
888+
}
889+
890+
if ( $this->is_sqlite() ) {
891+
$this->sqlite_import( $result_file, $assoc_args );
892+
return;
805893
}
806894

807895
// Process options to MySQL.
@@ -1080,19 +1168,27 @@ public function size( $args, $assoc_args ) {
10801168

10811169
$default_unit = ( empty( $size_format ) && ! $human_readable ) ? ' B' : '';
10821170

1171+
$is_sqlite = $this->is_sqlite();
1172+
10831173
if ( $tables || $all_tables || $all_tables_with_prefix ) {
10841174

10851175
// Add all of the table sizes.
10861176
foreach ( Utils\wp_get_table_names( $args, $assoc_args ) as $table_name ) {
10871177

10881178
// Get the table size.
1089-
$table_bytes = $wpdb->get_var(
1090-
$wpdb->prepare(
1091-
'SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = %s and Table_Name = %s GROUP BY Table_Name LIMIT 1',
1092-
DB_NAME,
1093-
$table_name
1094-
)
1095-
);
1179+
if ( $is_sqlite ) {
1180+
// For SQLite, we cannot get individual table sizes easily.
1181+
// Just report 0 as a placeholder.
1182+
$table_bytes = 0;
1183+
} else {
1184+
$table_bytes = $wpdb->get_var(
1185+
$wpdb->prepare(
1186+
'SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = %s and Table_Name = %s GROUP BY Table_Name LIMIT 1',
1187+
DB_NAME,
1188+
$table_name
1189+
)
1190+
);
1191+
}
10961192

10971193
// Add the table size to the list.
10981194
$rows[] = [
@@ -1104,16 +1200,22 @@ public function size( $args, $assoc_args ) {
11041200
} else {
11051201

11061202
// Get the database size.
1107-
$db_bytes = $wpdb->get_var(
1108-
$wpdb->prepare(
1109-
'SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = %s GROUP BY table_schema;',
1110-
DB_NAME
1111-
)
1112-
);
1203+
if ( $is_sqlite ) {
1204+
$db_bytes = $this->sqlite_size();
1205+
$db_name = basename( $this->get_sqlite_db_path() );
1206+
} else {
1207+
$db_bytes = $wpdb->get_var(
1208+
$wpdb->prepare(
1209+
'SELECT SUM(data_length + index_length) FROM information_schema.TABLES where table_schema = %s GROUP BY table_schema;',
1210+
DB_NAME
1211+
)
1212+
);
1213+
$db_name = DB_NAME;
1214+
}
11131215

11141216
// Add the database size to the list.
11151217
$rows[] = [
1116-
'Name' => DB_NAME,
1218+
'Name' => $db_name,
11171219
'Size' => strtoupper( $db_bytes ) . $default_unit,
11181220
'Bytes' => strtoupper( $db_bytes ),
11191221
];

0 commit comments

Comments
 (0)