This commit is contained in:
2024-11-02 19:34:53 +02:00
commit f7fefe300d
32 changed files with 5835 additions and 0 deletions

2201
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 = "1.1.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-sqlite", # `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 -- 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
```

19
migration/src/lib.rs Normal file
View File

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

View File

@@ -0,0 +1,68 @@
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)
.uuid()
.unique_key()
.not_null()
.primary_key()
)
.col(ColumnDef::new(User::Username)
.string()
.unique_key()
.not_null()
)
.col(ColumnDef::new(User::Email)
.string()
.unique_key()
.not_null()
)
.col(ColumnDef::new(User::PwdHash)
.string()
.not_null()
)
.col(ColumnDef::new(User::PwdSalt)
.string()
.not_null()
)
.col(ColumnDef::new(User::IsAdministrator)
.boolean()
.not_null()
)
.col(ColumnDef::new(User::CreatedAt)
.date_time()
.not_null()
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(User::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum User {
Table,
Id,
Username,
Email,
PwdHash,
PwdSalt,
IsAdministrator,
CreatedAt
}

View File

@@ -0,0 +1,63 @@
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> {
// Replace the sample below with your own migration scripts
manager
.create_table(
Table::create()
.table(Computer::Table)
.if_not_exists()
.col(ColumnDef::new(Computer::Id)
.uuid()
.not_null()
.primary_key()
)
.col(ColumnDef::new(Computer::Name)
.string()
.unique_key()
.not_null()
)
.col(ColumnDef::new(Computer::Group)
.string()
.not_null()
)
.col(ColumnDef::new(Computer::Type)
.string()
.not_null()
)
.col(ColumnDef::new(Computer::AccessToken)
.string()
.unique_key()
.not_null()
)
.col(ColumnDef::new(Computer::CreatedAt)
.date_time()
.not_null()
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Computer::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum Computer {
Table,
Id,
Name,
Group,
Type,
AccessToken,
CreatedAt
}

View File

@@ -0,0 +1,46 @@
use sea_orm_migration::{prelude::*, schema::*};
#[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(PcGroup::Table)
.if_not_exists()
.col(ColumnDef::new(PcGroup::Id)
.uuid()
.unique_key()
.primary_key()
.not_null()
)
.col(ColumnDef::new(PcGroup::Name)
.string()
.not_null()
)
.col(ColumnDef::new(PcGroup::AvailableActions)
.json()
.not_null()
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(PcGroup::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum PcGroup {
Table,
Id,
Name,
AvailableActions,
}

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;
}