Skip to content

Commit

Permalink
fixes: fixed having multiple databases
Browse files Browse the repository at this point in the history
the previous implementation creates multiple database this has been resolved by using a single shared db in the OS home dir
  • Loading branch information
opeolluwa committed Aug 28, 2023
1 parent 9a79afd commit b37eb35
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ use serde::{Deserialize, Serialize};
use sqlx::{migrate::MigrateDatabase, FromRow, Pool, Row, Sqlite, SqlitePool};
use uuid::Uuid;

use crate::style::PrintColoredText;
const DB_URL: &str = "sqlite://utils.db";
use crate::{style::PrintColoredText, DB_URL};
pub struct Database;
#[allow(unused)]

impl Database {
pub async fn init() {
if !Sqlite::database_exists(DB_URL).await.unwrap_or(false) {
match Sqlite::create_database(DB_URL).await {
if !Sqlite::database_exists(&DB_URL).await.unwrap_or(false) {
match Sqlite::create_database(&DB_URL).await {
Ok(_) => PrintColoredText::success("Database initialized"),
Err(_error) => PrintColoredText::error("error creating utility store"),
}
Expand All @@ -26,7 +25,7 @@ impl Database {
let store_create_table =
"CREATE TABLE IF NOT EXISTS store (id VARCHAR, key VARCHAR, value TEXT, date_added TEXT, last_updated TEXT)";

let db = SqlitePool::connect(DB_URL).await.unwrap();
let db = SqlitePool::connect(&DB_URL).await.unwrap();
let _ = sqlx::query(store_create_table).execute(&db).await.unwrap();
let _ = sqlx::query(email_create_table).execute(&db).await.unwrap();

Expand All @@ -47,8 +46,7 @@ impl Database {

// return connection to the database;
pub async fn conn() -> Pool<Sqlite> {

SqlitePool::connect(DB_URL).await.unwrap()
SqlitePool::connect(&DB_URL).await.unwrap()
}

pub async fn tables() -> HashMap<usize, String> {
Expand Down Expand Up @@ -121,7 +119,7 @@ impl StoreModel {
/// find all
pub async fn find() -> Vec<Self> {
let db = Database::conn().await;

sqlx::query_as::<_, Self>("SELECT * FROM store")
.fetch_all(&db)
.await
Expand Down
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
use lazy_static::lazy_static;

use include_dir::{include_dir, Dir};

pub const SOURCE_DIR: Dir = include_dir!("src/templates");
lazy_static! {
pub static ref DB_URL: std::string::String = {
// create the database in the home directory of the system
//create "utils" directory in the home dir and / save files to $HOME/utils;
let os_default_home_dir = dirs::home_dir().unwrap();
let db_path = format!(
"{home_dir}/{upload_dir}",
home_dir = os_default_home_dir.display(),
upload_dir = "utils"
);
// create the path if not exist path if not exist
let _ = std::fs::create_dir_all(&db_path);

format!("sqlite://{db_path}/utils.db")
};
}
mod commands;
mod database;
mod parser;
Expand Down

0 comments on commit b37eb35

Please sign in to comment.