Removed creation of database logic, needs to stay in seperate crate
This commit is contained in:
parent
315bdd6841
commit
e6ea1c782e
4 changed files with 3 additions and 61 deletions
|
|
@ -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<dyn Error>> {
|
|
||||||
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<Postgres>, query: &str) -> Result<(), sqlx::Error> {
|
|
||||||
sqlx::query(query).execute(pool).await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
@ -9,7 +9,6 @@ use std::{
|
||||||
|
|
||||||
pub mod article;
|
pub mod article;
|
||||||
pub mod page;
|
pub mod page;
|
||||||
pub mod create;
|
|
||||||
|
|
||||||
pub async fn establish_connection() -> Result<PgPool, Box<dyn Error>> {
|
pub async fn establish_connection() -> Result<PgPool, Box<dyn Error>> {
|
||||||
let db_url = match env::var("DATABASE_URL") {
|
let db_url = match env::var("DATABASE_URL") {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
#![allow(async_fn_in_trait)]
|
#![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 sqlx::PgPool;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
|
@ -37,8 +37,3 @@ pub async fn run_load(pool: &PgPool) -> Result<(), Box<dyn Error>> {
|
||||||
load_pages(pool).await?;
|
load_pages(pool).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run_create(pool: &PgPool) -> Result<(), Box<dyn Error>> {
|
|
||||||
create_database(pool).await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
|
||||||
11
src/main.rs
11
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 clap::Parser;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use sqlx::postgres::PgPool;
|
use sqlx::postgres::PgPool;
|
||||||
|
|
@ -6,9 +6,6 @@ use sqlx::postgres::PgPool;
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(version, about, long_about = None)]
|
#[command(version, about, long_about = None)]
|
||||||
struct Args {
|
struct Args {
|
||||||
#[arg(short, long)]
|
|
||||||
setup: bool,
|
|
||||||
|
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
load: bool,
|
load: bool,
|
||||||
|
|
||||||
|
|
@ -25,15 +22,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
Err(_) => panic!("error connecting to database"),
|
Err(_) => panic!("error connecting to database"),
|
||||||
};
|
};
|
||||||
|
|
||||||
if args.setup {
|
|
||||||
run_create(&pool).await?
|
|
||||||
}
|
|
||||||
|
|
||||||
if args.load {
|
if args.load {
|
||||||
run_load(&pool).await?;
|
run_load(&pool).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if args.run || !(args.load || args.setup) {
|
if args.run || !args.load {
|
||||||
run_server(pool).await?;
|
run_server(pool).await?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue