aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-02-22 11:39:29 +0100
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-02-22 11:39:29 +0100
commit8dbcf33c8c6114e5699472d47c39f114103dc02e (patch)
tree6324c8a1620fec1a1fdcce216c9691f4854138d2
parentfix(yt/storage/migrate/sql/01_zero_to_one.sql): Account for duration being NULL (diff)
downloadyt-8dbcf33c8c6114e5699472d47c39f114103dc02e.zip
feat(yt/storage/migrate): Add version two
Diffstat (limited to '')
-rw-r--r--yt/src/storage/migrate/mod.rs56
-rw-r--r--yt/src/storage/migrate/sql/02_one_to_two.sql1
2 files changed, 54 insertions, 3 deletions
diff --git a/yt/src/storage/migrate/mod.rs b/yt/src/storage/migrate/mod.rs
index 52f8c2b..767fe0f 100644
--- a/yt/src/storage/migrate/mod.rs
+++ b/yt/src/storage/migrate/mod.rs
@@ -22,8 +22,11 @@ pub enum DbVersion {
/// Introduced: 2025-02-17.
One,
+
+ /// Introduced: 2025-02-18.
+ Two,
}
-const CURRENT_VERSION: DbVersion = DbVersion::One;
+const CURRENT_VERSION: DbVersion = DbVersion::Two;
async fn add_error_context(
function: impl Future<Output = Result<()>>,
@@ -73,15 +76,18 @@ impl DbVersion {
DbVersion::Empty => unreachable!("A empty version does not have an associated integer"),
DbVersion::Zero => 0,
DbVersion::One => 1,
+ DbVersion::Two => 2,
}
}
fn from_db(number: i64, namespace: &str) -> Result<Self> {
match (number, namespace) {
(0, "yt") => Ok(DbVersion::Zero),
(1, "yt") => Ok(DbVersion::One),
+ (2, "yt") => Ok(DbVersion::Two),
(0, other) => bail!("Db version is Zero, but got unknown namespace: '{other}'"),
(1, other) => bail!("Db version is One, but got unknown namespace: '{other}'"),
+ (2, other) => bail!("Db version is Two, but got unknown namespace: '{other}'"),
(other, "yt") => bail!("Got unkown version for 'yt' namespace: {other}"),
(num, nasp) => bail!("Got unkown version number ({num}) and namespace ('{nasp}')"),
@@ -114,7 +120,9 @@ impl DbVersion {
.await
.context("Failed to set new version")?;
- tx.commit().await.context("Failed to commit changes")?;
+ 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>
@@ -169,8 +177,50 @@ impl DbVersion {
Box::pin(Self::One.update(app)).await
}
- // This is the current_version
+
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")
+ }
+
+ // This is the current_version
+ DbVersion::Two => {
assert_eq!(self, CURRENT_VERSION);
assert_eq!(self, get_version(app).await?);
Ok(())
diff --git a/yt/src/storage/migrate/sql/02_one_to_two.sql b/yt/src/storage/migrate/sql/02_one_to_two.sql
new file mode 100644
index 0000000..dd85737
--- /dev/null
+++ b/yt/src/storage/migrate/sql/02_one_to_two.sql
@@ -0,0 +1 @@
+ALTER TABLE videos DROP in_playlist;