Skip to content

Commit a6ff503

Browse files
Copilotswissspidy
andcommitted
Fix sqlite_import() failing when tables already exist
sqlite3 .dump produces 'CREATE TABLE' without 'IF NOT EXISTS', so importing the dump back into the same database fails with "table X already exists" and exit code 1. Add a regex replacement to insert 'IF NOT EXISTS' into CREATE TABLE statements from the dump, consistent with the existing handling for CREATE INDEX statements. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 93be0b6 commit a6ff503

1 file changed

Lines changed: 1 addition & 0 deletions

File tree

src/DB_Command_SQLite.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ protected function sqlite_import( $file, $assoc_args ) {
424424

425425
// Ignore errors about unique constraints and existing indexes.
426426
$contents = str_replace( 'INSERT INTO', 'INSERT OR IGNORE INTO', $contents );
427+
$contents = preg_replace( '/\bCREATE TABLE (?!IF NOT EXISTS\b)/i', 'CREATE TABLE IF NOT EXISTS ', $contents );
427428
$contents = str_replace( 'CREATE INDEX "', 'CREATE INDEX IF NOT EXISTS "', $contents );
428429
$contents = str_replace( 'CREATE UNIQUE INDEX "', 'CREATE UNIQUE INDEX IF NOT EXISTS "', $contents );
429430

0 commit comments

Comments
 (0)