From e6ea1c782e150ec93ea3c2229a3214152515ec42 Mon Sep 17 00:00:00 2001 From: Awstin Date: Thu, 1 Aug 2024 06:03:09 -0400 Subject: [PATCH] Removed creation of database logic, needs to stay in seperate crate --- src/database/create.rs | 45 ------------------------------------------ src/database/mod.rs | 1 - src/lib.rs | 7 +------ src/main.rs | 11 ++--------- 4 files changed, 3 insertions(+), 61 deletions(-) delete mode 100644 src/database/create.rs diff --git a/src/database/create.rs b/src/database/create.rs deleted file mode 100644 index 07979f1..0000000 --- a/src/database/create.rs +++ /dev/null @@ -1,45 +0,0 @@ -use sqlx::{PgPool, Pool, Postgres}; -use std::error::Error; - -const LINK_TYPE: &str = "CREATE TYPE link_type as ENUM ('article', 'blog');"; - -const ARTICLE_TABLE: &str = "CREATE TABLE IF NOT EXISTS articles ( - reference varchar(20) not null, - title varchar(50) not null, - previous varchar(20), - next varchar(20), - description text, - content text not null, - date date not null, - id serial primary key);"; - -const PAGE_TABLE: &str = "CREATE TABLE IF NOT EXISTS pages ( - reference varchar(20) not null, - title varchar(50) not null, - content text not null, - date_created date not null, - date_last_updated date not null, - description text, - id serial primary key);"; - -const LINK_TABLE: &str = "CREATE TABLE IF NOT EXISTS links ( - url varchar(100) not null, - date_added date not null, - description text, - author varchar(50) not null, - type link_type not null, - id serial primary key);"; - -pub async fn create_database(pool: &PgPool) -> Result<(), Box> { - create(pool, LINK_TYPE).await?; - create(pool, ARTICLE_TABLE).await?; - create(pool, PAGE_TABLE).await?; - create(pool, LINK_TABLE).await?; - - Ok(()) -} - -async fn create(pool: &Pool, query: &str) -> Result<(), sqlx::Error> { - sqlx::query(query).execute(pool).await?; - Ok(()) -} diff --git a/src/database/mod.rs b/src/database/mod.rs index e3b520e..450c483 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -9,7 +9,6 @@ use std::{ pub mod article; pub mod page; -pub mod create; pub async fn establish_connection() -> Result> { let db_url = match env::var("DATABASE_URL") { diff --git a/src/lib.rs b/src/lib.rs index 1b88303..3a3f610 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ #![allow(async_fn_in_trait)] -use crate::database::{article::load_articles, create::create_database, page::load_pages}; +use crate::database::{article::load_articles, page::load_pages}; use sqlx::PgPool; use std::error::Error; use tracing::info; @@ -37,8 +37,3 @@ pub async fn run_load(pool: &PgPool) -> Result<(), Box> { load_pages(pool).await?; Ok(()) } - -pub async fn run_create(pool: &PgPool) -> Result<(), Box> { - create_database(pool).await?; - Ok(()) -} diff --git a/src/main.rs b/src/main.rs index 4b35986..58a5120 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use achubb_backend::{run_create, run_load, run_server, database::establish_connection}; +use achubb_backend::{run_load, run_server, database::establish_connection}; use clap::Parser; use std::error::Error; use sqlx::postgres::PgPool; @@ -6,9 +6,6 @@ use sqlx::postgres::PgPool; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Args { - #[arg(short, long)] - setup: bool, - #[arg(short, long)] load: bool, @@ -25,15 +22,11 @@ async fn main() -> Result<(), Box> { Err(_) => panic!("error connecting to database"), }; - if args.setup { - run_create(&pool).await? - } - if args.load { run_load(&pool).await?; } - if args.run || !(args.load || args.setup) { + if args.run || !args.load { run_server(pool).await?; } Ok(())