Introduce Database handling through SeaORM
This commit is contained in:
parent
37331cd24d
commit
e4765a20ef
12 changed files with 169 additions and 2 deletions
4
.envrc
4
.envrc
|
|
@ -2,3 +2,7 @@ if ! has nix_direnv_version || ! nix_direnv_version 2.1.1; then
|
|||
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.1.1/direnvrc" "sha256-b6qJ4r34rbE23yWjMqbmu3ia2z4b2wIlZUksBke/ol0="
|
||||
fi
|
||||
use flake
|
||||
|
||||
export REGALADE_JWT_SECRET=B50D8FB3A6DC3073259062EFFD03A283416A81ED3183C44DEE179AFF76C00B58
|
||||
export REGALADE_DATABASE_URL=postgres://traxys/regalade?host=/var/run/postgresql
|
||||
export DATABASE_URL=$REGALADE_DATABASE_URL
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ authors = ["traxys <quentin@familleboyer.net>"]
|
|||
edition = "2021"
|
||||
|
||||
[workspace]
|
||||
members = [".", "api", "app"]
|
||||
members = [".", "api", "app", "migration"]
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.71"
|
||||
|
|
@ -18,5 +18,11 @@ tokio = { version = "1", features = ["macros", "rt", "rt-multi-thread"] }
|
|||
tracing = "0.1.37"
|
||||
tracing-subscriber = "0.3.17"
|
||||
api = { path = "./api" }
|
||||
migration = { path = "./migration" }
|
||||
thiserror = "1.0.40"
|
||||
tower-http = { version = "0.4.0", features = ["cors", "fs"] }
|
||||
sha2 = "0.10"
|
||||
|
||||
[dependencies.sea-orm]
|
||||
version = "0.11"
|
||||
features = ["runtime-tokio-rustls", "sqlx-postgres", "sqlx-sqlite"]
|
||||
|
|
|
|||
|
|
@ -26,7 +26,12 @@
|
|||
};
|
||||
in {
|
||||
devShell = pkgs.mkShell {
|
||||
nativeBuildInputs = [rust pkgs.trunk pkgs.httpie];
|
||||
nativeBuildInputs = [
|
||||
rust
|
||||
pkgs.trunk
|
||||
pkgs.httpie
|
||||
pkgs.sea-orm-cli
|
||||
];
|
||||
RUST_PATH = "${rust}";
|
||||
RUST_DOC_PATH = "${rust}/share/doc/rust/html/std/index.html";
|
||||
};
|
||||
|
|
|
|||
16
migration/Cargo.toml
Normal file
16
migration/Cargo.toml
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
[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 = ["runtime-tokio-rustls", "sqlx-postgres", "sqlx-sqlite"]
|
||||
41
migration/README.md
Normal file
41
migration/README.md
Normal 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
12
migration/src/lib.rs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
pub use sea_orm_migration::prelude::*;
|
||||
|
||||
mod m20220101_000001_account;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigratorTrait for Migrator {
|
||||
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
||||
vec![Box::new(m20220101_000001_account::Migration)]
|
||||
}
|
||||
}
|
||||
35
migration/src/m20220101_000001_account.rs
Normal file
35
migration/src/m20220101_000001_account.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[derive(Iden)]
|
||||
enum User {
|
||||
Table,
|
||||
Id,
|
||||
Name,
|
||||
Password,
|
||||
}
|
||||
|
||||
#[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).uuid().not_null().primary_key())
|
||||
.col(ColumnDef::new(User::Name).string().not_null().unique_key())
|
||||
.col(ColumnDef::new(User::Password).binary_len(64).not_null())
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(User::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
6
migration/src/main.rs
Normal file
6
migration/src/main.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() {
|
||||
cli::run_cli(migration::Migrator).await;
|
||||
}
|
||||
5
src/entity/mod.rs
Normal file
5
src/entity/mod.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
|
||||
|
||||
pub mod prelude;
|
||||
|
||||
pub mod user;
|
||||
3
src/entity/prelude.rs
Normal file
3
src/entity/prelude.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
|
||||
|
||||
pub use super::user::Entity as User;
|
||||
19
src/entity/user.rs
Normal file
19
src/entity/user.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||
#[sea_orm(table_name = "user")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub id: Uuid,
|
||||
#[sea_orm(unique)]
|
||||
pub name: String,
|
||||
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
|
||||
pub password: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
15
src/main.rs
15
src/main.rs
|
|
@ -4,9 +4,12 @@ use axum::Router;
|
|||
use base64::{engine::general_purpose, Engine};
|
||||
use config::{Config, ConfigError};
|
||||
use jwt_simple::prelude::HS256Key;
|
||||
use migration::{Migrator, MigratorTrait};
|
||||
use sea_orm::{ConnectOptions, Database, DatabaseConnection};
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
pub(crate) mod entity;
|
||||
mod routes;
|
||||
|
||||
#[derive(Clone)]
|
||||
|
|
@ -69,6 +72,8 @@ struct Settings {
|
|||
port: u16,
|
||||
api_allowed: Option<String>,
|
||||
serve_app: Option<PathBuf>,
|
||||
database_url: String,
|
||||
sqlx_logging: bool,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
|
|
@ -79,6 +84,7 @@ impl Settings {
|
|||
.set_default("port", "8085")?
|
||||
.set_default("api_allowed", None::<String>)?
|
||||
.set_default("serve_app", None::<String>)?
|
||||
.set_default("sqlx_logging", false)?
|
||||
.build()?;
|
||||
|
||||
cfg.try_deserialize()
|
||||
|
|
@ -87,6 +93,7 @@ impl Settings {
|
|||
|
||||
struct AppState {
|
||||
jwt_secret: Base64,
|
||||
db: DatabaseConnection,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
|
|
@ -102,10 +109,16 @@ async fn main() -> anyhow::Result<()> {
|
|||
|
||||
let addr: SocketAddr = format!("{}:{}", config.host, config.port).parse()?;
|
||||
|
||||
let mut opt = ConnectOptions::new(config.database_url);
|
||||
opt.sqlx_logging(config.sqlx_logging);
|
||||
|
||||
let state = Arc::new(AppState {
|
||||
jwt_secret: config.jwt_secret,
|
||||
db: Database::connect(opt).await?,
|
||||
});
|
||||
|
||||
Migrator::up(&state.db, None).await?;
|
||||
|
||||
let router = Router::new()
|
||||
.nest(
|
||||
"/api",
|
||||
|
|
@ -118,6 +131,8 @@ async fn main() -> anyhow::Result<()> {
|
|||
Some(path) => router.fallback_service(ServeDir::new(path)),
|
||||
};
|
||||
|
||||
tracing::info!("Listening on {addr}");
|
||||
|
||||
Ok(axum::Server::bind(&addr)
|
||||
.serve(router.into_make_service())
|
||||
.await?)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue