diff options
author | Teddy Wing | 2021-05-29 20:23:26 +0200 |
---|---|---|
committer | Teddy Wing | 2021-05-29 20:23:26 +0200 |
commit | 8130bc32fa81821e5511500f2f64bd964c58f3db (patch) | |
tree | 53767628047de09680923491e824e25c4772da8e /src/database.rs | |
parent | 9c2d269945af68e67d8daf8870be6d5664ab2153 (diff) | |
download | reflectub-8130bc32fa81821e5511500f2f64bd964c58f3db.tar.bz2 |
Create SQLite database
Set up the database with a table for repositories so we can cache when
they were last updated.
Diffstat (limited to 'src/database.rs')
-rw-r--r-- | src/database.rs | 26 |
1 files changed, 23 insertions, 3 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(()) } |