Skip to content

Commit

Permalink
refactor(db): add sqlx-migrate-validate and optimize backup logic
Browse files Browse the repository at this point in the history
This commit introduces the `sqlx-migrate-validate` dependency to validate migrations before performing a database backup. The backup logic in `DbConn::new` has been refactored to only proceed with the backup if migrations are needed, improving efficiency and reducing unnecessary operations.
  • Loading branch information
wsxiaoys committed Dec 26, 2024
1 parent dcd7190 commit d1b4a43
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ee/tabby-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ sqlx = { workspace = true, features = [
"macros",
"json",
] }
sqlx-migrate-validate = { path = "../../crates/sqlx-migrate-validate" }

[dev-dependencies]
assert_matches.workspace = true
Expand Down
12 changes: 10 additions & 2 deletions ee/tabby-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,28 @@ impl DbConn {

pub async fn new(db_file: &Path) -> Result<Self> {
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?;

Check warning on line 159 in ee/tabby-db/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/lib.rs#L159

Added line #L159 was not covered by tests
Self::init_db(pool).await
}

/// Backup existing database file before opening it.
/// 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<()> {

Check warning on line 167 in ee/tabby-db/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/lib.rs#L167

Added line #L167 was not covered by tests
use sqlx_migrate_validate::{Validate, Validator};

let mut conn = pool.acquire().await?;
if sqlx::migrate!("./migrations").validate(&mut *conn).await.is_ok() {

Check warning on line 171 in ee/tabby-db/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/lib.rs#L170-L171

Added lines #L170 - L171 were not covered by tests
// No migration is needed, skip the backup.
return Ok(())
}

Check warning on line 175 in ee/tabby-db/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/lib.rs#L173-L175

Added lines #L173 - L175 were not covered by tests
if !tokio::fs::try_exists(db_file).await? {
return Ok(());
}
Expand Down

0 comments on commit d1b4a43

Please sign in to comment.