From 8130bc32fa81821e5511500f2f64bd964c58f3db Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 29 May 2021 20:23:26 +0200 Subject: Create SQLite database Set up the database with a table for repositories so we can cache when they were last updated. --- src/database.rs | 26 +++++++++++++++++++++++--- src/main.rs | 4 +++- 2 files changed, 26 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/database.rs b/src/database.rs index 1b00377..7ce2c52 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,4 +1,4 @@ -use sqlx::{self, Connection}; +use sqlx::{self, ConnectOptions, Connection, Executor}; use crate::github::Repo; @@ -13,13 +13,33 @@ impl Db { ) -> Result> { Ok( Db { - connection: sqlx::SqliteConnection::connect(path) + connection: sqlx::sqlite::SqliteConnectOptions::new() + .filename(path) + .create_if_missing(true) + .connect() .await?, } ) } - pub fn create() -> Result<(), Box> { + pub async fn create(&mut self) -> Result<(), Box> { + let mut tx = self.connection.begin().await?; + + tx.execute(r#" + CREATE TABLE repositories ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + updated_at TEXT NOT NULL + ); + "#).await?; + + tx.execute(r#" + CREATE UNIQUE INDEX idx_repositories_id + ON repositories (id); + "#).await?; + + tx.commit().await?; + Ok(()) } diff --git a/src/main.rs b/src/main.rs index e27694d..acbcd23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,5 +20,7 @@ async fn main() { // Path::new("/tmp/grsvp"), // ).unwrap(); - let db = database::Db::connect("sqlite::memory:").await.unwrap(); + let mut db = database::Db::connect("test.db").await.unwrap(); + + db.create().await.unwrap(); } -- cgit v1.2.3