Skip to content

Commit 9f77d72

Browse files
Copilotswissspidy
andcommitted
Fix sqlite_import() to handle CREATE TRIGGER and CREATE VIEW in dumps
sqlite3 .dump also outputs CREATE TRIGGER and CREATE VIEW statements without IF NOT EXISTS, causing re-import to fail with "already exists". Extend the transformations to add IF NOT EXISTS to all CREATE statements (TABLE, TRIGGER, VIEW, INDEX, UNIQUE INDEX) using regex, so that re-importing a dump into the same database succeeds. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent a6ff503 commit 9f77d72

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

src/DB_Command_SQLite.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,10 @@ protected function sqlite_import( $file, $assoc_args ) {
425425
// Ignore errors about unique constraints and existing indexes.
426426
$contents = str_replace( 'INSERT INTO', 'INSERT OR IGNORE INTO', $contents );
427427
$contents = preg_replace( '/\bCREATE TABLE (?!IF NOT EXISTS\b)/i', 'CREATE TABLE IF NOT EXISTS ', $contents );
428-
$contents = str_replace( 'CREATE INDEX "', 'CREATE INDEX IF NOT EXISTS "', $contents );
429-
$contents = str_replace( 'CREATE UNIQUE INDEX "', 'CREATE UNIQUE INDEX IF NOT EXISTS "', $contents );
428+
$contents = preg_replace( '/\bCREATE TRIGGER (?!IF NOT EXISTS\b)/i', 'CREATE TRIGGER IF NOT EXISTS ', $contents );
429+
$contents = preg_replace( '/\bCREATE VIEW (?!IF NOT EXISTS\b)/i', 'CREATE VIEW IF NOT EXISTS ', $contents );
430+
$contents = preg_replace( '/\bCREATE INDEX (?!IF NOT EXISTS\b)/i', 'CREATE INDEX IF NOT EXISTS ', $contents );
431+
$contents = preg_replace( '/\bCREATE UNIQUE INDEX (?!IF NOT EXISTS\b)/i', 'CREATE UNIQUE INDEX IF NOT EXISTS ', $contents );
430432

431433
$import_file = tempnam( sys_get_temp_dir(), 'temp.db' );
432434
file_put_contents( $import_file, $contents );

0 commit comments

Comments
 (0)