diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/database.rs | 26 | ||||
-rw-r--r-- | src/main.rs | 4 |
2 files changed, 26 insertions, 4 deletions
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<Self, Box<dyn std::error::Error>> { 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<dyn std::error::Error>> { + pub async fn create(&mut self) -> Result<(), Box<dyn std::error::Error>> { + 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(); } |