Skip to content

Commit

Permalink
Add init_schema to db.
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkbyers committed Mar 18, 2024
1 parent c7db200 commit 7c6355e
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
use libsql::{Builder, Database, Error};
use libsql::{params, Builder, Connection, Database, Error};

pub async fn new_local_db(path: &str) -> Result<Database, Error> {
Builder::new_local(path).build().await
let db = Builder::new_local(path).build().await?;
let conn = db.connect()?;
init_schema(&conn).await?;

Ok(db)
}

async fn init_schema(conn: &Connection) -> Result<(), Error> {
conn.execute(
r#"
CREATE TABLE IF NOT EXISTS subscriptions (
id uuid NOT NULL PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
subscribed_at timestampz NOT NULL
);
"#,
params!([])
)
.await?;
Ok(())
}

0 comments on commit 7c6355e

Please sign in to comment.