Init database

This commit is contained in:
Evie Viau-Chow-Stuart 2023-03-22 03:28:47 -07:00
parent 0f04eed0c9
commit 494719409d
Signed by: evie
GPG key ID: 928652CDFCEC8099
13 changed files with 2541 additions and 11 deletions

278
Cargo.lock generated
View file

@ -123,6 +123,113 @@ version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341"
[[package]]
name = "async-attributes"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3203e79f4dd9bdda415ed03cf14dae5a2bf775c683a00f94e9cd1faf0f596e5"
dependencies = [
"quote",
"syn 1.0.109",
]
[[package]]
name = "async-channel"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf46fee83e5ccffc220104713af3292ff9bc7c64c7de289f66dae8e38d826833"
dependencies = [
"concurrent-queue",
"event-listener",
"futures-core",
]
[[package]]
name = "async-executor"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17adb73da160dfb475c183343c8cccd80721ea5a605d3eb57125f0a7b7a92d0b"
dependencies = [
"async-lock",
"async-task",
"concurrent-queue",
"fastrand",
"futures-lite",
"slab",
]
[[package]]
name = "async-global-executor"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1b6f5d7df27bd294849f8eec66ecfc63d11814df7a4f5d74168a2394467b776"
dependencies = [
"async-channel",
"async-executor",
"async-io",
"async-lock",
"blocking",
"futures-lite",
"once_cell",
"tokio",
]
[[package]]
name = "async-io"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c374dda1ed3e7d8f0d9ba58715f924862c63eae6849c92d3a18e7fbde9e2794"
dependencies = [
"async-lock",
"autocfg",
"concurrent-queue",
"futures-lite",
"libc",
"log",
"parking",
"polling",
"slab",
"socket2",
"waker-fn",
"windows-sys 0.42.0",
]
[[package]]
name = "async-lock"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa24f727524730b077666307f2734b4a1a1c57acb79193127dcc8914d5242dd7"
dependencies = [
"event-listener",
]
[[package]]
name = "async-std"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62565bb4402e926b29953c785397c6dc0391b7b446e45008b0049eb43cec6f5d"
dependencies = [
"async-attributes",
"async-channel",
"async-global-executor",
"async-io",
"async-lock",
"crossbeam-utils",
"futures-channel",
"futures-core",
"futures-io",
"futures-lite",
"gloo-timers",
"kv-log-macro",
"log",
"memchr",
"once_cell",
"pin-project-lite",
"pin-utils",
"slab",
"wasm-bindgen-futures",
]
[[package]]
name = "async-stream"
version = "0.3.4"
@ -145,6 +252,12 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "async-task"
version = "4.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a40729d2133846d9ed0ea60a8b9541bccddab49cd30f0715a1da672fe9a2524"
[[package]]
name = "async-trait"
version = "0.1.67"
@ -165,6 +278,12 @@ dependencies = [
"num-traits",
]
[[package]]
name = "atomic-waker"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "debc29dde2e69f9e47506b525f639ed42300fc014a3e007832592448fa8e4599"
[[package]]
name = "autocfg"
version = "1.1.0"
@ -327,6 +446,20 @@ dependencies = [
"generic-array",
]
[[package]]
name = "blocking"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c67b173a56acffd6d2326fb7ab938ba0b00a71480e14902b2591c87bc5741e8"
dependencies = [
"async-channel",
"async-lock",
"async-task",
"atomic-waker",
"fastrand",
"futures-lite",
]
[[package]]
name = "borsh"
version = "0.10.2"
@ -483,6 +616,15 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "concurrent-queue"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c278839b831783b70278b14df4d45e1beb1aad306c07bb796637de9a0e323e8e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "constant_time_eq"
version = "0.2.5"
@ -504,6 +646,7 @@ dependencies = [
"askama_axum",
"axum",
"blake3",
"migration",
"rand",
"sea-orm",
"sea-orm-migration",
@ -553,6 +696,16 @@ dependencies = [
"typenum",
]
[[package]]
name = "ctor"
version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096"
dependencies = [
"quote",
"syn 1.0.109",
]
[[package]]
name = "cxx"
version = "1.0.93"
@ -722,6 +875,15 @@ dependencies = [
"regex",
]
[[package]]
name = "fastrand"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be"
dependencies = [
"instant",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -784,6 +946,21 @@ version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89d422fa3cbe3b40dca574ab087abb5bc98258ea57eea3fd6f1fa7162c778b91"
[[package]]
name = "futures-lite"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7694489acd39452c77daa48516b894c153f192c3578d5a839b62c58099fcbf48"
dependencies = [
"fastrand",
"futures-core",
"futures-io",
"memchr",
"parking",
"pin-project-lite",
"waker-fn",
]
[[package]]
name = "futures-sink"
version = "0.3.27"
@ -834,6 +1011,18 @@ dependencies = [
"wasi",
]
[[package]]
name = "gloo-timers"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b995a66bb87bebce9a0f4a95aed01daca4872c050bfcb21653361c03bc35e5c"
dependencies = [
"futures-channel",
"futures-core",
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "hashbrown"
version = "0.12.3"
@ -1061,6 +1250,15 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "kv-log-macro"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f"
dependencies = [
"log",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -1105,6 +1303,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if",
"value-bag",
]
[[package]]
@ -1137,6 +1336,14 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "migration"
version = "0.1.0"
dependencies = [
"async-std",
"sea-orm-migration",
]
[[package]]
name = "mime"
version = "0.3.17"
@ -1168,7 +1375,7 @@ dependencies = [
"libc",
"log",
"wasi",
"windows-sys",
"windows-sys 0.45.0",
]
[[package]]
@ -1272,6 +1479,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]]
name = "parking"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72"
[[package]]
name = "parking_lot"
version = "0.11.2"
@ -1317,7 +1530,7 @@ dependencies = [
"libc",
"redox_syscall",
"smallvec",
"windows-sys",
"windows-sys 0.45.0",
]
[[package]]
@ -1375,6 +1588,22 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "polling"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e1f879b2998099c2d69ab9605d145d5b661195627eccc680002c4918a7fb6fa"
dependencies = [
"autocfg",
"bitflags",
"cfg-if",
"concurrent-queue",
"libc",
"log",
"pin-project-lite",
"windows-sys 0.45.0",
]
[[package]]
name = "ppv-lite86"
version = "0.2.17"
@ -2217,7 +2446,7 @@ dependencies = [
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys",
"windows-sys 0.45.0",
]
[[package]]
@ -2482,12 +2711,28 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "value-bag"
version = "1.0.0-alpha.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2209b78d1249f7e6f3293657c9779fe31ced465df091bbd433a1cf88e916ec55"
dependencies = [
"ctor",
"version_check",
]
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "waker-fn"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca"
[[package]]
name = "want"
version = "0.3.0"
@ -2529,6 +2774,18 @@ dependencies = [
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f219e0d211ba40266969f6dbdd90636da12f75bee4fc9d6c23d1260dadb51454"
dependencies = [
"cfg-if",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.84"
@ -2637,6 +2894,21 @@ dependencies = [
"windows-targets",
]
[[package]]
name = "windows-sys"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows-sys"
version = "0.45.0"

View file

@ -18,6 +18,7 @@ askama_axum = "0.3.0"
## Database
sea-orm = { version = "^0.11", features = ["sqlx-postgres", "runtime-tokio-rustls", "macros"] }
sea-orm-migration = "0.11.1"
migration = { path = "./migration" }
## Auth
rand = { version = "0.8.5", features = ["std_rng"] }

1
migration/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

2066
migration/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

22
migration/Cargo.toml Normal file
View file

@ -0,0 +1,22 @@
[package]
name = "migration"
version = "0.1.0"
edition = "2021"
publish = false
[lib]
name = "migration"
path = "src/lib.rs"
[dependencies]
async-std = { version = "1", features = ["attributes", "tokio1"] }
[dependencies.sea-orm-migration]
version = "0.11.0"
features = [
# Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI.
# View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime.
# e.g.
"runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
"sqlx-postgres", # `DATABASE_DRIVER` feature
]

41
migration/README.md Normal file
View file

@ -0,0 +1,41 @@
# Running Migrator CLI
- Generate a new migration file
```sh
cargo run -- migrate generate MIGRATION_NAME
```
- Apply all pending migrations
```sh
cargo run
```
```sh
cargo run -- up
```
- Apply first 10 pending migrations
```sh
cargo run -- up -n 10
```
- Rollback last applied migrations
```sh
cargo run -- down
```
- Rollback last 10 applied migrations
```sh
cargo run -- down -n 10
```
- Drop all tables from the database, then reapply all migrations
```sh
cargo run -- fresh
```
- Rollback all applied migrations, then reapply all migrations
```sh
cargo run -- refresh
```
- Rollback all applied migrations
```sh
cargo run -- reset
```
- Check the status of all migrations
```sh
cargo run -- status
```

12
migration/src/lib.rs Normal file
View file

@ -0,0 +1,12 @@
pub use sea_orm_migration::prelude::*;
mod m20220101_000001_create_user;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20220101_000001_create_user::Migration)]
}
}

View file

@ -0,0 +1,47 @@
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(User::Table)
.if_not_exists()
.col(
ColumnDef::new(User::Id)
.text()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(User::Username).text().not_null())
.col(ColumnDef::new(User::Email).text().not_null())
.col(ColumnDef::new(User::PasswordHash).text().not_null())
.col(ColumnDef::new(User::Bio).string().not_null().default(""))
.col(ColumnDef::new(User::Flags).integer().not_null().default(0))
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(User::Table).to_owned())
.await
}
}
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum User {
Table,
Id,
Username,
Email,
PasswordHash,
Bio,
Flags,
}

6
migration/src/main.rs Normal file
View file

@ -0,0 +1,6 @@
use sea_orm_migration::prelude::*;
#[async_std::main]
async fn main() {
cli::run_cli(migration::Migrator).await;
}

6
src/api/mod.rs Normal file
View file

@ -0,0 +1,6 @@
use axum::Router;
use crate::AppState;
pub fn api() -> Router<AppState> {
Router::new()
}

6
src/frontend/mod.rs Normal file
View file

@ -0,0 +1,6 @@
use axum::Router;
use crate::AppState;
pub fn frontend() -> Router<AppState> {
Router::new()
}

View file

@ -1,8 +1,15 @@
mod options;
mod api;
mod frontend;
use std::net::SocketAddr;
use axum::Router;
use tracing::info;
use migration::Migrator;
use sea_orm::{ConnectOptions, Database, DatabaseConnection};
use sea_orm_migration::MigratorTrait;
use tracing::{info, log};
use crate::api::api;
use crate::frontend::frontend;
use crate::options::{Opt, Options};
const VERSION: &str = env!("CARGO_PKG_VERSION");
@ -15,23 +22,60 @@ async fn main() {
info!("Starting CorrugatedCardboard v{}", VERSION);
info!("\tInstance Name: {}", options.name);
info!("\tListen Address: {}", options.listen_addr);
info!("\tRegistrations Enabled: {}", options.registration);
info!("\tConfig:");
info!("\t\tInstance Name: {}", options.name);
info!("\t\tListen Address: {}", options.listen_addr);
info!("\t\tRegistrations Enabled: {}", options.registration);
info!("\t\tURL: {}", options.url);
info!("\t\tImages Endpoint: {}", options.image_endpoint);
info!("\t\tAssets Endpoint: {}", options.asset_endpoint);
// Database
info!("Connecting to database & checking for migrations");
info!("Starting server");
let mut migration_db_opt = migration::sea_orm::ConnectOptions::new(options.database_url.clone());
migration_db_opt.sqlx_logging_level(log::LevelFilter::Debug);
let migrator_conn = migration::sea_orm::Database::connect(migration_db_opt)
.await
.expect("Failed to connect to the database!");
Migrator::up(&migrator_conn, None)
.await
.expect("Failed to run migrations!");
let mut db_opt = ConnectOptions::new(options.database_url.clone());
db_opt.sqlx_logging_level(log::LevelFilter::Debug);
let conn: DatabaseConnection = Database::connect(db_opt)
.await
.expect("Failed to connect to database!");
info!("Connected to database");
// Axum setup
let app = Router::new();
info!("Starting server");
let state = AppState { conn };
let app = Router::new()
.merge(frontend())
.nest("/api", api())
.with_state(state);
let addr: SocketAddr = options.listen_addr
.parse()
.expect("Unable to parse listen address!");
info!("Listening on {}", &addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.expect("Failed to start the server!");
}
#[derive(Clone)]
pub struct AppState {
pub conn: DatabaseConnection,
}

View file

@ -5,7 +5,10 @@ pub struct Options {
pub name: String,
pub listen_addr: String,
pub database_url: String,
pub registration: bool
pub registration: bool,
pub url: String,
pub image_endpoint: String,
pub asset_endpoint: String
}
pub trait Opt {
fn get() -> Options;
@ -16,7 +19,10 @@ impl Opt for Options {
name: env::var("NAME").unwrap_or_else(|_| "CorrugatedCardboard".to_string()),
listen_addr: env::var("HTTP_LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0:3926".to_string()),
database_url: env::var("DATABASE_URL").expect("DATABASE_URL is required!"),
registration: env::var("REGISTRATION").unwrap_or_else(|_| "false".to_string()).parse().unwrap()
registration: env::var("REGISTRATION").unwrap_or_else(|_| "false".to_string()).parse().unwrap(),
url: env::var("URL").expect("URL is required!"),
image_endpoint: env::var("IMAGE_ENDPOINT").expect("IMAGE_ENDPOINT is required!"),
asset_endpoint: env::var("ASSET_ENDPOINT").expect("ASSET_ENDPOINT is required!"),
}
}
}