Browse Source
Change SQLite backup to use VACUUM INTO query (#6989 )
* Refactor SQLite backup to use VACUUM INTO query
Replaced manual file creation for SQLite backup with a VACUUM INTO query.
* Fix VACUUM INTO query error handling
pull/7065/head
Aaron Brager
1 week ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with
7 additions and
11 deletions
src/db/mod.rs
@ -387,7 +387,6 @@ pub mod models;
#[ cfg(sqlite) ]
#[ cfg(sqlite) ]
pub fn backup_sqlite ( ) -> Result < String , Error > {
pub fn backup_sqlite ( ) -> Result < String , Error > {
use diesel ::Connection ;
use diesel ::Connection ;
use std ::{ fs ::File , io ::Write } ;
let db_url = CONFIG . database_url ( ) ;
let db_url = CONFIG . database_url ( ) ;
if DbConnType ::from_url ( & CONFIG . database_url ( ) ) . map ( | t | t = = DbConnType ::Sqlite ) . unwrap_or ( false ) {
if DbConnType ::from_url ( & CONFIG . database_url ( ) ) . map ( | t | t = = DbConnType ::Sqlite ) . unwrap_or ( false ) {
@ -401,16 +400,13 @@ pub fn backup_sqlite() -> Result<String, Error> {
. to_string_lossy ( )
. to_string_lossy ( )
. into_owned ( ) ;
. into_owned ( ) ;
match File ::create ( backup_file . clone ( ) ) {
diesel ::sql_query ( "VACUUM INTO ?" )
Ok ( mut f ) = > {
. bind ::< diesel ::sql_types ::Text , _ > ( & backup_file )
let serialized_db = conn . serialize_database_to_buffer ( ) ;
. execute ( & mut conn )
f . write_all ( serialized_db . as_slice ( ) ) . expect ( "Error writing SQLite backup" ) ;
. map ( | _ | ( ) )
Ok ( backup_file )
. map_res ( "VACUUM INTO failed" ) ? ;
}
Err ( e ) = > {
Ok ( backup_file )
err_silent ! ( format ! ( "Unable to save SQLite backup: {e:?}" ) )
}
}
} else {
} else {
err_silent ! ( "The database type is not SQLite. Backups only works for SQLite databases" )
err_silent ! ( "The database type is not SQLite. Backups only works for SQLite databases" )
}
}