mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-18 19:06:21 +00:00
feat(init::schema) init database schema
This commit is contained in:
6 files changed
+71
-13
No files matched your search
@@ -1,9 +1,10 @@
|
||||
use crate::blog;
|
||||
use crate::scheduler::Scheduler;
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
pub fn start() {
|
||||
pub fn start(db_pool: Pool<Postgres>) {
|
||||
let scheduler = Scheduler::new();
|
||||
let _ = vec![blog::cron::get_tasks()]
|
||||
let _ = vec![blog::cron::get_tasks(db_pool.clone())]
|
||||
.into_iter()
|
||||
.map(|list| scheduler.insert_list(list))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
mod cron;
|
||||
mod database;
|
||||
pub mod logger;
|
||||
mod schema;
|
||||
pub mod server;
|
||||
|
||||
use macros::panic_with_log;
|
||||
@@ -39,9 +40,10 @@ pub async fn start() {
|
||||
};
|
||||
|
||||
// schema init
|
||||
schema::init(&db_pool).await;
|
||||
|
||||
// load cron task
|
||||
cron::start();
|
||||
cron::start(db_pool.clone());
|
||||
|
||||
// api serve
|
||||
match server::start(db_pool).await {
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
use sqlx::{query, Pool, Postgres};
|
||||
use tracing::info;
|
||||
|
||||
const POST_BASE_MODEL: &str = r##"
|
||||
CREATE TABLE IF NOT EXISTS Post
|
||||
(
|
||||
post_id UUID PRIMARY KEY,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
category VARCHAR(255) NOT NULL,
|
||||
header_img TEXT,
|
||||
summary TEXT,
|
||||
sub_title TEXT,
|
||||
content TEXT NOT NULL,
|
||||
last_update TIMESTAMPTZ NOT NULL,
|
||||
first_update TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
"##;
|
||||
|
||||
// base model, will add more cols at runtime
|
||||
const POST_META_MODEL: &str = r##"
|
||||
CREATE TABLE IF NOT EXISTS Meta
|
||||
(
|
||||
post_id UUID PRIMARY KEY
|
||||
);
|
||||
"##;
|
||||
|
||||
const POST_HASH_MODEL: &str = r##"
|
||||
CREATE TABLE IF NOT EXISTS Hash
|
||||
(
|
||||
post_id UUID PRIMARY KEY,
|
||||
hash VARCHAR(44)
|
||||
);
|
||||
"##;
|
||||
|
||||
const TAG_MODEL: &str = r##"
|
||||
CREATE TABLE IF NOT EXISTS Tag
|
||||
(
|
||||
post_id UUID NOT NULL,
|
||||
tag VARCHAR(255) NOT NULL
|
||||
);
|
||||
"##;
|
||||
|
||||
pub async fn init(db_connection: &Pool<Postgres>) {
|
||||
query(POST_BASE_MODEL).execute(db_connection).await.unwrap();
|
||||
query(POST_META_MODEL).execute(db_connection).await.unwrap();
|
||||
query(TAG_MODEL).execute(db_connection).await.unwrap();
|
||||
query(POST_HASH_MODEL).execute(db_connection).await.unwrap();
|
||||
info!("database schema initialized");
|
||||
}
|
||||
Reference in new issue
Block a user