diff --git a/Cargo.lock b/Cargo.lock index f5f03f674b04..66bc09aeef72 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5376,6 +5376,7 @@ dependencies = [ "serde", "sql_query_builder", "sqlx", + "sqlx-migrate-validate", "tabby-db-macros", "tokio", "uuid", diff --git a/ee/tabby-db/Cargo.toml b/ee/tabby-db/Cargo.toml index 8f3f720a97fe..98eb9a47f416 100644 --- a/ee/tabby-db/Cargo.toml +++ b/ee/tabby-db/Cargo.toml @@ -26,6 +26,7 @@ sqlx = { workspace = true, features = [ "macros", "json", ] } +sqlx-migrate-validate = { path = "../../crates/sqlx-migrate-validate" } [dev-dependencies] assert_matches.workspace = true diff --git a/ee/tabby-db/src/lib.rs b/ee/tabby-db/src/lib.rs index a2eaccfbc594..80e149c0d460 100644 --- a/ee/tabby-db/src/lib.rs +++ b/ee/tabby-db/src/lib.rs @@ -151,12 +151,12 @@ impl DbConn { pub async fn new(db_file: &Path) -> Result { tokio::fs::create_dir_all(db_file.parent().unwrap()).await?; - Self::backup_db(db_file).await?; let options = SqliteConnectOptions::new() .filename(db_file) .create_if_missing(true); let pool = SqlitePool::connect_with(options).await?; + Self::backup_db(db_file, &pool).await?; Self::init_db(pool).await } @@ -164,7 +164,15 @@ impl DbConn { /// backup format: /// for prod - db.backup-${date}.sqlite /// for non-prod - dev-db.backup-${date}.sqlite - async fn backup_db(db_file: &Path) -> Result<()> { + async fn backup_db(db_file: &Path, pool: &SqlitePool) -> Result<()> { + use sqlx_migrate_validate::{Validate, Validator}; + + let mut conn = pool.acquire().await?; + if sqlx::migrate!("./migrations").validate(&mut *conn).await.is_ok() { + // No migration is needed, skip the backup. + return Ok(()) + } + if !tokio::fs::try_exists(db_file).await? { return Ok(()); }