Skip to content

Commit

Permalink
WIP refactor: migrate database from PostgreSQL to SQLite
Browse files Browse the repository at this point in the history
??? not sure
  • Loading branch information
jasonribble committed Sep 6, 2024
1 parent 7c00cf0 commit e5cb7dd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async-trait = "0.1.80"
clap = { version = "4.5.9", features = ["derive"] }
dotenvy = "0.15.0"
regex = "1.5.4"
sqlx = { version = "0.7.4", features = ["runtime-tokio-native-tls", "postgres"] }
sqlx = { version = "0.7.4", features = ["runtime-tokio-native-tls", "sqlite"] }
tokio = { version = "1.28.0", features = ["full", "test-util"] }

[dev-dependencies]
Expand Down
22 changes: 11 additions & 11 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use crate::models;
use async_trait::async_trait;
use sqlx::postgres::PgPool;
use sqlx::SqlitePool;

#[cfg_attr(test, mockall::automock)]
#[async_trait]
Expand All @@ -13,20 +13,20 @@ pub trait ContactRepo {
async fn get_by_id(&self, id: i64) -> anyhow::Result<models::IndexedContact>;
}

pub struct PostgresContactRepo {
pg_pool: Arc<PgPool>,
pub struct SqliteContactRepo {
sqlite_pool: Arc<SqlitePool>,
}

impl PostgresContactRepo {
pub fn new(pg_pool: PgPool) -> Self {
impl SqliteContactRepo {
pub fn new(pool: SqlitePool) -> Self {
Self {
pg_pool: Arc::new(pg_pool),
sqlite_pool: Arc::new(pool),
}
}
}

#[async_trait]
impl ContactRepo for PostgresContactRepo {
impl ContactRepo for SqliteContactRepo {
async fn save(&self, contact: models::Contact) -> anyhow::Result<i64> {
let query = "INSERT INTO contacts
(first_name, last_name, display_name, email, phone_number)
Expand All @@ -39,7 +39,7 @@ impl ContactRepo for PostgresContactRepo {
.bind(&contact.display_name)
.bind(&contact.email)
.bind(&contact.phone_number)
.fetch_one(&*self.pg_pool)
.fetch_one(&*self.sqlite_pool)
.await?;

Ok(id)
Expand All @@ -53,7 +53,7 @@ impl ContactRepo for PostgresContactRepo {

let contacts_with_id: Vec<models::IndexedContact> =
sqlx::query_as::<_, models::IndexedContact>(get_contacts_query)
.fetch_all(&*self.pg_pool)
.fetch_all(&*self.sqlite_pool)
.await?;

Ok(contacts_with_id)
Expand All @@ -78,7 +78,7 @@ impl ContactRepo for PostgresContactRepo {
contact.update.phone_number,
contact.id
)
.execute(&*self.pg_pool)
.execute(&*self.sqlite_pool)
.await?;

println!("Contact updated");
Expand All @@ -92,7 +92,7 @@ impl ContactRepo for PostgresContactRepo {
let contact: models::IndexedContact =
sqlx::query_as::<_, models::IndexedContact>(query_get_by_id)
.bind(id)
.fetch_one(&*self.pg_pool)
.fetch_one(&*self.sqlite_pool)
.await?;

Ok(contact)
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ mod utils;

use clap::Parser;
use commander::{Cli, Commands};
use db::{ContactRepo, PostgresContactRepo};
use db::{ContactRepo, SqliteContactRepo};
use models::{Contact, ContactBuilder};
use sqlx::PgPool;
use sqlx::SqlitePool;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenvy::dotenv().ok();

let pool = PgPool::connect(&env::var("DATABASE_URL")?).await?;
let pool = SqlitePool::connect(&env::var("DATABASE_URL")?).await?;

let contact_repo = PostgresContactRepo::new(pool);
let contact_repo = SqliteContactRepo::new(pool);

let cli = Cli::parse();

Expand Down

0 comments on commit e5cb7dd

Please sign in to comment.