init basic database folder structure
This commit is contained in:
19
public/database/Cargo.toml
Normal file
19
public/database/Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "database"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[lib]
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
shared = { path = "../shared" }
|
||||
migration = { path = "../migration" }
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
tokio = { version = "1.47.0", features = ["full"] }
|
||||
sea-orm = { version = "2.0.0-rc", features = [ "sqlx-postgres", "sqlx-mysql", "sqlx-sqlite", "runtime-tokio-rustls", "macros", "mock", "with-chrono", "with-json", "with-uuid", "sqlite-use-returning-for-3_35", "mariadb-use-returning" ] }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
22
public/database/src/lib.rs
Normal file
22
public/database/src/lib.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use sea_orm::ConnectOptions;
|
||||
|
||||
pub async fn get_connection<T: FnOnce(&mut ConnectOptions) -> &mut ConnectOptions>(
|
||||
connection_string: &str,
|
||||
option_fn: Option<T>,
|
||||
) -> Result<sea_orm::DatabaseConnection, sea_orm::DbErr> {
|
||||
use sea_orm::Database;
|
||||
|
||||
let mut opt = ConnectOptions::new(connection_string.to_string());
|
||||
opt.max_connections(10)
|
||||
.min_connections(0)
|
||||
.connect_timeout(std::time::Duration::from_secs(8))
|
||||
.idle_timeout(std::time::Duration::from_secs(8))
|
||||
.test_before_acquire(true)
|
||||
.sqlx_logging(false);
|
||||
|
||||
if let Some(option_fn) = option_fn {
|
||||
option_fn(&mut opt);
|
||||
}
|
||||
|
||||
Database::connect(opt).await
|
||||
}
|
||||
20
public/migration/Cargo.toml
Normal file
20
public/migration/Cargo.toml
Normal file
@@ -0,0 +1,20 @@
|
||||
[package]
|
||||
name = "migration"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
rust-version = "1.85.0"
|
||||
publish = false
|
||||
|
||||
[lib]
|
||||
name = "migration"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1", features = ["macros", "rt", "rt-multi-thread"] }
|
||||
|
||||
[dependencies.sea-orm-migration]
|
||||
version = "2.0.0-rc"
|
||||
features = [
|
||||
"runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
|
||||
"sqlx-postgres", "sqlx-mysql", "sqlx-sqlite" # `DATABASE_DRIVER` features
|
||||
]
|
||||
59
public/migration/README.md
Normal file
59
public/migration/README.md
Normal file
@@ -0,0 +1,59 @@
|
||||
# Running Migrator CLI
|
||||
|
||||
- Generate a new migration file
|
||||
|
||||
```sh
|
||||
cargo run -- 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
|
||||
```
|
||||
16
public/migration/src/lib.rs
Normal file
16
public/migration/src/lib.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
pub use sea_orm_migration::prelude::*;
|
||||
|
||||
mod migrations;
|
||||
use migrations::*;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigratorTrait for Migrator {
|
||||
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
||||
vec![
|
||||
Box::new(m20251011_000001_create_user_table::Migration),
|
||||
Box::new(m20251011_000002_create_config_table::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
6
public/migration/src/main.rs
Normal file
6
public/migration/src/main.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
cli::run_cli(migration::Migrator).await;
|
||||
}
|
||||
2
public/migration/src/migrations.rs
Normal file
2
public/migration/src/migrations.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod m20251011_000001_create_user_table;
|
||||
pub mod m20251011_000002_create_config_table;
|
||||
@@ -0,0 +1,60 @@
|
||||
use sea_orm_migration::{prelude::*, schema::*};
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum User {
|
||||
Table,
|
||||
Id,
|
||||
//
|
||||
Name,
|
||||
IsAdmin,
|
||||
PasswordHash,
|
||||
Salt,
|
||||
//
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
}
|
||||
|
||||
#[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(pk_uuid(User::Id))
|
||||
.col(ColumnDef::new(User::Name).string().not_null().unique_key())
|
||||
.col(
|
||||
ColumnDef::new(User::IsAdmin)
|
||||
.boolean()
|
||||
.default(false)
|
||||
.not_null(),
|
||||
)
|
||||
.col(ColumnDef::new(User::PasswordHash).string().not_null())
|
||||
.col(ColumnDef::new(User::Salt).string().not_null())
|
||||
.col(
|
||||
ColumnDef::new(User::CreatedAt)
|
||||
.timestamp()
|
||||
.default(SimpleExpr::Keyword(Keyword::CurrentTimestamp))
|
||||
.not_null(),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(User::UpdatedAt)
|
||||
.timestamp()
|
||||
.default(SimpleExpr::Keyword(Keyword::CurrentTimestamp))
|
||||
.not_null(),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(User::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum Config {
|
||||
Table,
|
||||
//
|
||||
Key,
|
||||
Value,
|
||||
//
|
||||
CreatedAt,
|
||||
UpdatedAt,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(Config::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(Config::Key).string().not_null().unique_key())
|
||||
.col(ColumnDef::new(Config::Value).string().not_null())
|
||||
.col(
|
||||
ColumnDef::new(Config::CreatedAt)
|
||||
.timestamp()
|
||||
.default(SimpleExpr::Keyword(Keyword::CurrentTimestamp))
|
||||
.not_null(),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(Config::UpdatedAt)
|
||||
.timestamp()
|
||||
.default(SimpleExpr::Keyword(Keyword::CurrentTimestamp))
|
||||
.not_null(),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(Config::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user