From 86b5f83140b649f026d1f8e9ea0494450cb133e4 Mon Sep 17 00:00:00 2001 From: vikingowl Date: Tue, 5 Aug 2025 03:16:36 +0200 Subject: [PATCH] added database migration and initialization logic to backend, including migration loader and async migration runner --- backend-rust/.gitignore | 1 + backend-rust/Cargo.lock | 503 ++++++++++++++++++ backend-rust/Cargo.toml | 10 + backend-rust/src/db.rs | 13 + backend-rust/src/main.rs | 22 + backend-rust/src/migrations.rs | 245 +++++++++ .../src/migrations/001_initial_schema.sql | 45 ++ .../migrations/002_add_category_to_news.sql | 23 + 8 files changed, 862 insertions(+) create mode 100644 backend-rust/.gitignore create mode 100644 backend-rust/Cargo.lock create mode 100644 backend-rust/Cargo.toml create mode 100644 backend-rust/src/db.rs create mode 100644 backend-rust/src/main.rs create mode 100644 backend-rust/src/migrations.rs create mode 100644 backend-rust/src/migrations/001_initial_schema.sql create mode 100644 backend-rust/src/migrations/002_add_category_to_news.sql diff --git a/backend-rust/.gitignore b/backend-rust/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/backend-rust/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/backend-rust/Cargo.lock b/backend-rust/Cargo.lock new file mode 100644 index 0000000..c5694d9 --- /dev/null +++ b/backend-rust/Cargo.lock @@ -0,0 +1,503 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "ahash" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy", +] + +[[package]] +name = "anyhow" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "backtrace" +version = "0.3.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6806a6321ec58106fea15becdad98371e28d92ccbc7c8f1b3b6dd724fe8f1002" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets", +] + +[[package]] +name = "bitflags" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" + +[[package]] +name = "bytes" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" + +[[package]] +name = "cfg-if" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268" + +[[package]] +name = "crossbeam-channel" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + +[[package]] +name = "fallible-iterator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +dependencies = [ + "ahash", +] + +[[package]] +name = "hashlink" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af" +dependencies = [ + "hashbrown", +] + +[[package]] +name = "io-uring" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d93587f37623a1a17d94ef2bc9ada592f5465fe7732084ab7beefabe5c77c0c4" +dependencies = [ + "bitflags", + "cfg-if", + "libc", +] + +[[package]] +name = "libc" +version = "0.2.174" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" + +[[package]] +name = "libsqlite3-sys" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149" +dependencies = [ + "pkg-config", + "vcpkg", +] + +[[package]] +name = "lock_api" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "memchr" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", +] + +[[package]] +name = "mio" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78bed444cc8a2160f01cbcf811ef18cac863ad68ae8ca62092e8db51d51c761c" +dependencies = [ + "libc", + "wasi", + "windows-sys", +] + +[[package]] +name = "object" +version = "0.36.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "owly-news-summariser" +version = "0.1.0" +dependencies = [ + "anyhow", + "rusqlite", + "tokio", + "tokio-rusqlite", +] + +[[package]] +name = "parking_lot" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70d58bf43669b5795d1576d0641cfb6fbb2057bf629506267a92807158584a13" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc838d2a56b5b1a6c25f55575dfc605fabb63bb2365f6c2353ef9159aa69e4a5" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "pkg-config" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" + +[[package]] +name = "proc-macro2" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5407465600fb0548f1442edf71dd20683c6ed326200ace4b1ef0763521bb3b77" +dependencies = [ + "bitflags", +] + +[[package]] +name = "rusqlite" +version = "0.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cdbe9230a57259b37f7257d0aff38b8c9dbda3513edba2105e59b130189d82f" +dependencies = [ + "bitflags", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "smallvec", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "signal-hook-registry" +version = "1.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a4719bff48cee6b39d12c020eeb490953ad2443b7055bd0b21fca26bd8c28b" +dependencies = [ + "libc", +] + +[[package]] +name = "slab" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04dc19736151f35336d325007ac991178d504a119863a2fcb3758cdb5e52c50d" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "socket2" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "233504af464074f9d066d7b5416c5f9b894a5862a6506e306f7b816cdd6f1807" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "syn" +version = "2.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tokio" +version = "1.47.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89e49afdadebb872d3145a5638b59eb0691ea23e46ca484037cfab3b76b95038" +dependencies = [ + "backtrace", + "bytes", + "io-uring", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "slab", + "socket2", + "tokio-macros", + "windows-sys", +] + +[[package]] +name = "tokio-macros" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-rusqlite" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b65501378eb676f400c57991f42cbd0986827ab5c5200c53f206d710fb32a945" +dependencies = [ + "crossbeam-channel", + "rusqlite", + "tokio", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "zerocopy" +version = "0.8.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/backend-rust/Cargo.toml b/backend-rust/Cargo.toml new file mode 100644 index 0000000..73d03c4 --- /dev/null +++ b/backend-rust/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "owly-news-summariser" +version = "0.1.0" +edition = "2024" + +[dependencies] +anyhow = "1.0" +tokio = { version = "1", features = ["full"] } +tokio-rusqlite = "0.6.0" +rusqlite = "=0.32.0" diff --git a/backend-rust/src/db.rs b/backend-rust/src/db.rs new file mode 100644 index 0000000..c2a4e02 --- /dev/null +++ b/backend-rust/src/db.rs @@ -0,0 +1,13 @@ +use anyhow::Result; +use std::path::{Path}; +use tokio_rusqlite::Connection as AsyncConn; +use crate::migrations::Migrator; + +pub async fn initialize_db(db_path: &Path, migrations_dir: &Path) -> Result { + let conn = AsyncConn::open(db_path).await?; + let migrator = Migrator::new(migrations_dir.to_path_buf())?; + + migrator.migrate_up_async(&conn).await?; + + Ok(conn) +} diff --git a/backend-rust/src/main.rs b/backend-rust/src/main.rs new file mode 100644 index 0000000..948e307 --- /dev/null +++ b/backend-rust/src/main.rs @@ -0,0 +1,22 @@ +use std::path::Path; + +mod db; +mod migrations; + +#[tokio::main] +async fn main() { + let migrations_folder = String::from("src/migrations"); + + let db_path = Path::new("owlynews.sqlite3"); + let migrations_path = Path::new(&migrations_folder); + + match db::initialize_db(&db_path, migrations_path).await { + Ok(_conn) => { + println!("Database initialized successfully"); + // Logic goes here + } + Err(e) => { + println!("Error initializing database: {:?}", e); + } + } +} diff --git a/backend-rust/src/migrations.rs b/backend-rust/src/migrations.rs new file mode 100644 index 0000000..8354875 --- /dev/null +++ b/backend-rust/src/migrations.rs @@ -0,0 +1,245 @@ +use anyhow::{Context, Result}; +use rusqlite::{Connection, params}; +use std::collections::HashSet; +use std::path::PathBuf; +use std::time::{SystemTime, UNIX_EPOCH}; +use tokio::fs; +use tokio_rusqlite::Connection as AsyncConn; + +pub struct Migration { + pub version: i64, + pub name: String, + pub sql_up: String, + #[allow(dead_code)] + pub sql_down: String, +} + +#[derive(Clone)] +pub struct Migrator { + migrations_dir: PathBuf, +} + +impl Migrator { + pub fn new(migrations_dir: PathBuf) -> Result { + Ok(Migrator { migrations_dir }) + } + + fn initialize(&self, conn: &mut Connection) -> Result<()> { + let tx = conn + .transaction() + .context("Failed to start transaction for initialization")?; + + tx.execute( + "CREATE TABLE IF NOT EXISTS migrations (version INTEGER PRIMARY KEY)", + [], + ) + .context("Failed to create migrations table")?; + + let columns: HashSet = { + let mut stmt = tx.prepare("PRAGMA table_info(migrations)")?; + stmt.query_map([], |row| row.get(1))? + .collect::, _>>()? + }; + + if !columns.contains("name") { + tx.execute("ALTER TABLE migrations ADD COLUMN name TEXT", []) + .context("Failed to add 'name' column to migrations table")?; + } + if !columns.contains("applied_at") { + tx.execute("ALTER TABLE migrations ADD COLUMN applied_at INTEGER", []) + .context("Failed to add 'applied_at' column to migrations table")?; + } + + tx.commit() + .context("Failed to commit migrations table initialization")?; + Ok(()) + } + + pub async fn load_migrations_async(&self) -> Result> { + let mut migrations = Vec::new(); + + // Use async-aware try_exists + if !fs::try_exists(&self.migrations_dir).await? { + return Ok(migrations); + } + + let mut entries = fs::read_dir(&self.migrations_dir) + .await + .context("Failed to read migrations directory")?; + + while let Some(entry) = entries.next_entry().await? { + let path = entry.path(); + + if path.is_file() && path.extension().unwrap_or_default() == "sql" { + let file_name = path.file_stem().unwrap().to_string_lossy(); + + // Format should be: VERSION_NAME.sql (e.g. 001_create_users.sql + if let Some((version_str, name)) = file_name.split_once('_') { + if let Ok(version) = version_str.parse::() { + let content = fs::read_to_string(&path).await.with_context(|| { + format!("Failed to read migration file: {}", path.display()) + })?; + + // Split content into up and down migrations if they exist + let parts: Vec<&str> = content.split("-- DOWN").collect(); + let sql_up = parts[0].trim().to_string(); + let sql_down = parts.get(1).map_or(String::new(), |s| s.trim().to_string()); + + migrations.push(Migration { + version, + name: name.to_string(), + sql_up, + sql_down, + }); + } + } + } + } + + migrations.sort_by_key(|m| m.version); + Ok(migrations) + } + + pub fn get_applied_migrations(&self, conn: &mut Connection) -> Result> { + let mut stmt = conn + .prepare("SELECT version FROM migrations ORDER BY version") + .context("Failed to prepare query for applied migrations")?; + let versions = stmt + .query_map([], |row| row.get(0))? + .collect::, _>>()?; + Ok(versions) + } + + pub async fn migrate_up_async(&self, async_conn: &AsyncConn) -> Result<()> { + let migrations = self.load_migrations_async().await?; + let migrator = self.clone(); + + // Perform all database operations within a blocking-safe context + async_conn + .call(move |conn| { + migrator.initialize(conn).expect("TODO: panic message"); + let applied = migrator + .get_applied_migrations(conn) + .expect("TODO: panic message"); + + let tx = conn + .transaction() + .context("Failed to start transaction for migrations") + .expect("TODO: panic message"); + + for migration in migrations { + if !applied.contains(&migration.version) { + println!( + "Applying migration {}: {}", + migration.version, migration.name + ); + + tx.execute_batch(&migration.sql_up) + .with_context(|| { + format!( + "Failed to execute migration {}: {}", + migration.version, migration.name + ) + }) + .expect("TODO: panic message"); + + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("TODO: panic message") + .as_secs() as i64; + tx.execute( + "INSERT INTO migrations (version, name, applied_at) VALUES (?, ?, ?)", + params![migration.version, migration.name, now], + ) + .with_context(|| { + format!("Failed to record migration {}", migration.version) + }) + .expect("TODO: panic message"); + } + } + + tx.commit() + .context("Failed to commit migrations") + .expect("TODO: panic message"); + Ok(()) + }) + .await?; + + Ok(()) + } + + #[allow(dead_code)] + pub async fn migrate_down_async( + &self, + async_conn: &AsyncConn, + target_version: Option, + ) -> Result<()> { + let migrations = self.load_migrations_async().await?; + let migrator = self.clone(); + + // Perform all database operations within a blocking-safe context + async_conn + .call(move |conn| { + migrator.initialize(conn).expect("TODO: panic message"); + let applied = migrator + .get_applied_migrations(conn) + .expect("TODO: panic message"); + + // If no target specified, roll back only the latest migration + let max_applied = *applied.iter().max().unwrap_or(&0); + let target = + target_version.unwrap_or(if max_applied > 0 { max_applied - 1 } else { 0 }); + + let tx = conn + .transaction() + .context("Failed to start transaction for migrations") + .expect("TODO: panic message"); + + // Find migrations to roll back (in reverse order) + let mut to_rollback: Vec<&Migration> = migrations + .iter() + .filter(|m| applied.contains(&m.version) && m.version > target) + .collect(); + + to_rollback.sort_by_key(|m| std::cmp::Reverse(m.version)); + + for migration in to_rollback { + println!( + "Rolling back migration {}: {}", + migration.version, migration.name + ); + + if !migration.sql_down.is_empty() { + tx.execute_batch(&migration.sql_down) + .with_context(|| { + format!( + "Failed to rollback migration {}: {}", + migration.version, migration.name + ) + }) + .expect("TODO: panic message"); + } else { + println!("Warning: No down migration defined for {}", migration.name); + } + + // Remove the migration record + tx.execute( + "DELETE FROM migrations WHERE version = ?", + [&migration.version], + ) + .with_context(|| { + format!("Failed to remove migration record {}", migration.version) + }) + .expect("TODO: panic message"); + } + + tx.commit() + .context("Failed to commit rollback") + .expect("TODO: panic message"); + Ok(()) + }) + .await?; + + Ok(()) + } +} diff --git a/backend-rust/src/migrations/001_initial_schema.sql b/backend-rust/src/migrations/001_initial_schema.sql new file mode 100644 index 0000000..631f8ab --- /dev/null +++ b/backend-rust/src/migrations/001_initial_schema.sql @@ -0,0 +1,45 @@ +-- Initial database schema for Owly News Summariser + +-- News table to store articles +CREATE TABLE IF NOT EXISTS news +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title TEXT NOT NULL, + summary TEXT, + url TEXT NOT NULL, + published TIMESTAMP NOT NULL, + country TEXT NOT NULL, + created_at INTEGER DEFAULT (strftime('%s', 'now')) +); + +-- Index for faster queries on published date +CREATE INDEX IF NOT EXISTS idx_news_published ON news (published); + +-- Feeds table to store RSS feed sources +CREATE TABLE IF NOT EXISTS feeds +( + id INTEGER PRIMARY KEY, + country TEXT, + url TEXT UNIQUE NOT NULL +); + +-- Settings table for application configuration +CREATE TABLE IF NOT EXISTS settings +( + key TEXT PRIMARY KEY, + val TEXT NOT NULL +); + +-- Meta table for application metadata +CREATE TABLE IF NOT EXISTS meta +( + key TEXT PRIMARY KEY, + val TEXT NOT NULL +); + +-- DOWN +DROP TABLE IF EXISTS meta; +DROP TABLE IF EXISTS settings; +DROP TABLE IF EXISTS feeds; +DROP INDEX IF EXISTS idx_news_published; +DROP TABLE IF EXISTS news; diff --git a/backend-rust/src/migrations/002_add_category_to_news.sql b/backend-rust/src/migrations/002_add_category_to_news.sql new file mode 100644 index 0000000..9811734 --- /dev/null +++ b/backend-rust/src/migrations/002_add_category_to_news.sql @@ -0,0 +1,23 @@ +-- Add category field to news table +ALTER TABLE news + ADD COLUMN category TEXT; + +-- DOWN +CREATE TABLE news_backup +( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title TEXT NOT NULL, + summary TEXT, + url TEXT NOT NULL, + published TIMESTAMP NOT NULL, + country TEXT NOT NULL, + created_at INTEGER DEFAULT (strftime('%s', 'now')) +); + +INSERT INTO news_backup +SELECT id, title, summary, url, published, country, created_at +FROM news; +DROP TABLE news; +ALTER TABLE news_backup + RENAME TO news; +CREATE INDEX IF NOT EXISTS idx_news_published ON news (published);