Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(db): add sqlx-migrate-validate and optimize backup logic #3620

Merged
merged 5 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Fixed and Improvements
body: Backup database only when there's pending schema migrations
time: 2024-12-26T20:23:37.914517+08:00
100 changes: 94 additions & 6 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ members = [
"crates/llama-cpp-server",
"crates/ollama-api-bindings",
"crates/tabby-index-cli",
"crates/hash-ids",
"crates/sqlx-migrate-validate",

"ee/tabby-webserver",
"ee/tabby-db",
"ee/tabby-db-macros",
"ee/tabby-schema",
"crates/hash-ids",
]

[workspace.package]
Expand Down Expand Up @@ -76,6 +77,8 @@ opentelemetry = { version = "0.27.0", features = ["trace", "metrics"] }
opentelemetry_sdk = { version = "0.27.0", default-features = false, features = ["trace", "rt-tokio"] }
opentelemetry-otlp = { version = "0.27.0" }
opentelemetry-semantic-conventions = { version = "0.27.0", features = ["semconv_experimental"] }
# https://github.com/wsxiaoys/sqlx/tree/fix-0-7-4-remove-datetime-utc-encode
sqlx = { git = "https://github.com/wsxiaoys/sqlx", rev = "8ca573c" }

[workspace.dependencies.uuid]
version = "1.3.3"
Expand Down
16 changes: 16 additions & 0 deletions crates/sqlx-migrate-validate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "sqlx-migrate-validate"
version.workspace = true
edition.workspace = true
authors.workspace = true
homepage.workspace = true

[dependencies]
thiserror.workspace = true
async-trait.workspace = true
sqlx = { workspace = true, features = ["sqlite", "runtime-tokio"] }

[dev-dependencies]
anyhow.workspace = true
tokio = { workspace = true, features = ["macros", "rt"] }
sqlx-rt = { version = "0.6", features = [ "runtime-tokio-rustls" ] }
3 changes: 3 additions & 0 deletions crates/sqlx-migrate-validate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# sqlx-migrate-validate

Vendored fork of https://github.com/tobikris/sqlx-migrate-validate
11 changes: 11 additions & 0 deletions crates/sqlx-migrate-validate/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use sqlx::migrate::MigrateError;

#[derive(Debug, thiserror::Error)]

Check warning on line 3 in crates/sqlx-migrate-validate/src/error.rs

View check run for this annotation

Codecov / codecov/patch

crates/sqlx-migrate-validate/src/error.rs#L3

Added line #L3 was not covered by tests
#[non_exhaustive]
pub enum ValidateError {
#[error("migration {0} was not applied")]
VersionNotApplied(i64),

#[error(transparent)]
MigrateError(#[from] MigrateError),
}
36 changes: 36 additions & 0 deletions crates/sqlx-migrate-validate/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! With [sqlx] and its [sqlx::migrate!] macro it is possible to create migrations and apply them to a database.
//! When starting an application it is important to validate that the database is in the correct state.
//! This crate provides a way to validate that the applied migrations match the desired migrations.
//! In combination with the [sqlx::migrate!] macro it is possible to validate that the database schema
//! matches the migrations present in the source code at the time of compilation.
//!
//! While this does not ensure full compatibility between the database and the application it can help
//! to detect issues early.
//!
//! Examples:
//!
//! ```rust,no_run
//! use sqlx_migrate_validate::{Validate, Validator};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), anyhow::Error> {
//! let pool = sqlx::SqlitePool::connect("sqlite::memory:").await?;
//! let mut conn = pool.acquire().await?;
//!
//! sqlx::migrate!("./tests/migrations-1")
//! .validate(&mut *conn)
//! .await?;
//!
//! Ok(())
//! }
//! ```

mod error;
mod validate;

pub use error::ValidateError;
pub use validate::*;

#[doc = include_str!("../README.md")]
#[cfg(doctest)]
pub struct ReadmeDoctests;
Loading
Loading