aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--yt/src/storage/migrate/mod.rs162
1 files changed, 51 insertions, 111 deletions
diff --git a/yt/src/storage/migrate/mod.rs b/yt/src/storage/migrate/mod.rs
index 9622abe..50a080a 100644
--- a/yt/src/storage/migrate/mod.rs
+++ b/yt/src/storage/migrate/mod.rs
@@ -21,6 +21,50 @@ use sqlx::{Sqlite, SqlitePool, Transaction, query};
use crate::app::App;
+macro_rules! make_upgrade {
+ ($app:expr, $old_version:expr, $new_version:expr, $update_path:expr) => {
+ add_error_context(
+ async {
+ let mut tx = $app
+ .database
+ .begin()
+ .await
+ .context("Failed to start the update transaction")?;
+ debug!("Migrating: {} -> {}", $old_version, $new_version);
+
+ sqlx::raw_sql(include_str!($update_path))
+ .execute(&mut *tx)
+ .await
+ .context("Failed to run the update sql script")?;
+
+ set_db_version(&mut tx, Some($old_version), $new_version)
+ .await
+ .context("Failed to set the new version")?;
+
+ tx.commit()
+ .await
+ .context("Failed to commit the update transaction")?;
+
+ // NOTE: This is needed, so that sqlite "sees" our changes to the table
+ // without having to reconnect. <2025-02-18>
+ query!("VACUUM")
+ .execute(&$app.database)
+ .await
+ .context("Failed to vacuum database")?;
+
+ Ok(())
+ },
+ $old_version,
+ )
+ .await?;
+
+ Box::pin($new_version.update($app)).await.context(concat!(
+ "Failed to update to version: ",
+ stringify!($new_version)
+ ))
+ };
+}
+
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub enum DbVersion {
/// The database is not yet initialized.
@@ -111,124 +155,20 @@ impl DbVersion {
#[allow(clippy::too_many_lines)]
async fn update(self, app: &App) -> Result<()> {
match self {
- DbVersion::Empty => {
- add_error_context(
- async {
- let mut tx = app
- .database
- .begin()
- .await
- .context("Failed to start transaction")?;
- debug!("Migrate: Empty -> Zero");
-
- sqlx::raw_sql(include_str!("./sql/00_empty_to_zero.sql"))
- .execute(&mut *tx)
- .await
- .context("Failed to execute sql update script")?;
-
- set_db_version(&mut tx, None, DbVersion::Zero)
- .await
- .context("Failed to set new version")?;
-
- tx.commit().await.context("Failed to commit changes")?;
-
- // NOTE: This is needed, so that sqlite "sees" our changes to the table
- // without having to reconnect. <2025-02-18>
- query!("VACUUM")
- .execute(&app.database)
- .await
- .context("Failed to vacuum database")?;
-
- Ok(())
- },
- DbVersion::One,
- )
- .await?;
- Box::pin(Self::Zero.update(app)).await
+ Self::Empty => {
+ make_upgrade! {app, Self::Empty, Self::Zero, "./sql/00_empty_to_zero.sql"}
}
- DbVersion::Zero => {
- add_error_context(
- async {
- let mut tx = app
- .database
- .begin()
- .await
- .context("Failed to start transaction")?;
- debug!("Migrate: Zero -> One");
-
- sqlx::raw_sql(include_str!("./sql/01_zero_to_one.sql"))
- .execute(&mut *tx)
- .await
- .context("Failed to execute the update sql script")?;
-
- set_db_version(&mut tx, Some(DbVersion::Zero), DbVersion::One)
- .await
- .context("Failed to set the new version")?;
-
- tx.commit()
- .await
- .context("Failed to commit the update transaction")?;
-
- // NOTE: This is needed, so that sqlite "sees" our changes to the table
- // without having to reconnect. <2025-02-18>
- query!("VACUUM")
- .execute(&app.database)
- .await
- .context("Failed to vacuum database")?;
-
- Ok(())
- },
- DbVersion::Zero,
- )
- .await?;
-
- Box::pin(Self::One.update(app)).await
+ Self::Zero => {
+ make_upgrade! {app, Self::Zero, Self::One, "./sql/01_zero_to_one.sql"}
}
- DbVersion::One => {
- add_error_context(
- async {
- let mut tx = app
- .database
- .begin()
- .await
- .context("Failed to start the update transaction")?;
- debug!("Migrate: One -> Two");
-
- sqlx::raw_sql(include_str!("./sql/02_one_to_two.sql"))
- .execute(&mut *tx)
- .await
- .context("Failed to run the update sql script")?;
-
- set_db_version(&mut tx, Some(DbVersion::One), DbVersion::Two)
- .await
- .context("Failed to set the new version")?;
-
- tx.commit()
- .await
- .context("Failed to commit the update transaction")?;
-
- // NOTE: This is needed, so that sqlite "sees" our changes to the table
- // without having to reconnect. <2025-02-18>
- query!("VACUUM")
- .execute(&app.database)
- .await
- .context("Failed to vacuum database")?;
-
- Ok(())
- },
- DbVersion::One,
- )
- .await?;
-
- Box::pin(Self::Two.update(app))
- .await
- .context("Failed to update to version: Three")
+ Self::One => {
+ make_upgrade! {app, Self::One, Self::Two, "./sql/02_one_to_two.sql"}
}
// This is the current_version
- DbVersion::Two => {
+ Self::Two => {
assert_eq!(self, CURRENT_VERSION);
assert_eq!(self, get_version(app).await?);
Ok(())