From 307419d37f73feea9429cad5313ec177172a0e9d Mon Sep 17 00:00:00 2001 From: Milim Date: Sat, 7 Dec 2024 10:04:34 +0100 Subject: [PATCH] add sqlite database creation migrations --- .../20241207082654_initial_table_setup.sql | 25 +++++++++++++++++++ src/db/mod.rs | 2 ++ 2 files changed, 27 insertions(+) create mode 100644 migrations/sqlite/20241207082654_initial_table_setup.sql diff --git a/migrations/sqlite/20241207082654_initial_table_setup.sql b/migrations/sqlite/20241207082654_initial_table_setup.sql new file mode 100644 index 0000000..e6842a0 --- /dev/null +++ b/migrations/sqlite/20241207082654_initial_table_setup.sql @@ -0,0 +1,25 @@ +CREATE TABLE IF NOT EXISTS users( + user_id INTEGER PRIMARY KEY, + name TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS books( + book_id INTEGER PRIMARY KEY, + book_title TEXT NOT NULL, + book_description TEXT, + book_creation_date INTEGER NOT NULL, + author_id INTEGER NOT NULL, + FOREIGN KEY (author_id) REFERENCES users(user_id) ON DELETE CASCADE + +); + +CREATE TABLE IF NOT EXISTS chapters( + chapter_id INTEGER PRIMARY KEY, + chapter_title TEXT NOT NULL, + chapter_text TEXT NOT NULL, + chapter_creation_date INT NOT NULL, + book_id INTEGER NOT NULL, + author_id INTEGER NOT NULL, + FOREIGN KEY (book_id) REFERENCES books(book_id) ON DELETE CASCADE, + FOREIGN KEY (author_id) REFERENCES users(user_id) ON DELETE CASCADE +); \ No newline at end of file diff --git a/src/db/mod.rs b/src/db/mod.rs index 0c93318..e4c4b70 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -22,10 +22,12 @@ impl DataBase { } let pool = SqlitePool::connect("sqlite:gunnhildr.db").await.unwrap(); + sqlx::migrate!("migrations/sqlite") .run(&pool) .await .expect("Failed to apply migration!"); + info!("Applied migrations."); Self::Sqlite(pool) }