Skip to content

Commit 711e37b

Browse files
Copilotswissspidy
andcommitted
Fix sqlite_reset to not double-call sqlite_create
Co-authored-by: swissspidy <[email protected]>
1 parent cbcf158 commit 711e37b

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/DB_Command_SQLite.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,24 @@ protected function sqlite_reset( $assoc_args ) {
169169
}
170170
}
171171

172-
// Recreate.
173-
$this->sqlite_create( $assoc_args );
172+
// Create directory if needed.
173+
$db_dir = dirname( $db_path );
174+
if ( ! is_dir( $db_dir ) ) {
175+
if ( ! mkdir( $db_dir, 0755, true ) ) {
176+
WP_CLI::error( "Could not create directory: {$db_dir}" );
177+
}
178+
}
179+
180+
// Recreate the SQLite database file.
181+
try {
182+
$pdo = new PDO( 'sqlite:' . $db_path );
183+
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
184+
// Execute a simple query to initialize the database.
185+
$pdo->exec( 'CREATE TABLE IF NOT EXISTS _wpcli_test (id INTEGER)' );
186+
$pdo->exec( 'DROP TABLE _wpcli_test' );
187+
} catch ( PDOException $e ) {
188+
WP_CLI::error( 'Could not create SQLite database: ' . $e->getMessage() );
189+
}
174190

175191
WP_CLI::success( 'Database reset.' );
176192
}

0 commit comments

Comments
 (0)