This commit is contained in:
4cuck committed 2023-07-27 04:14:47 +02:00
1 parent 906bfbb70c
commit a304acc505
36 files changed
+9888

No files matched your search

+22
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]
workspace = true
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-native-tls", # `ASYNC_RUNTIME` feature
"sqlx-sqlite", # `DATABASE_DRIVER` feature
]
+52
View File
@@ -0,0 +1,52 @@
# Running Migrator CLI
`cargo run -p migration -- up`
New entities:
Run the following command to re-generate the entities.
`sea-orm-cli generate entity -o entities/src --with-serde serialize`
Note that you have to check for unwanted changes such as `DateTime` & `Date` types. These are likely replaced with String by the sea-orm CLI. Same with additional modules like `web`.
May require setting the ENV `$Env:DATABASE_URL='sqlite:./sqlite.db?mode=rwc'`
- 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
View File
@@ -0,0 +1,12 @@
pub use sea_orm_migration::prelude::*;
mod m20220101_000001_create_table;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20220101_000001_create_table::Migration)]
}
}
@@ -0,0 +1,104 @@
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(Host::Table)
.if_not_exists()
.col(
ColumnDef::new(Host::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(Host::Domain)
.string()
.not_null()
.unique_key(),
)
.col(ColumnDef::new(Host::URL).string().not_null())
.col(ColumnDef::new(Host::Version).string())
.col(ColumnDef::new(Host::Enabled).boolean().not_null())
.col(ColumnDef::new(Host::RSS).boolean().not_null())
.col(ColumnDef::new(Host::Updated).date_time().not_null())
.to_owned(),
)
.await
.unwrap();
manager
.create_table(
Table::create()
.table(UpdateCheck::Table)
.if_not_exists()
.col(ColumnDef::new(UpdateCheck::Time).date_time().not_null())
.col(ColumnDef::new(UpdateCheck::Host).integer().not_null())
.col(ColumnDef::new(UpdateCheck::RespTime).integer())
.col(ColumnDef::new(UpdateCheck::Healthy).boolean().not_null())
.col(ColumnDef::new(UpdateCheck::ResponseCode).integer())
.foreign_key(
ForeignKey::create()
.name("FK_updatecheck_host")
.from(UpdateCheck::Table, UpdateCheck::Host)
.to(Host::Table, Host::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.primary_key(
Index::create()
.col(UpdateCheck::Host)
.col(UpdateCheck::Time)
.name("pk_updatecheck"),
)
.to_owned(),
)
.await
.unwrap();
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(
Table::drop()
.table(UpdateCheck::Table)
.if_exists()
.to_owned(),
)
.await
.unwrap();
manager
.drop_table(Table::drop().table(Host::Table).if_exists().to_owned())
.await
}
}
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum Host {
Table,
Id,
Domain,
URL,
RSS,
Version,
Enabled,
Updated,
}
#[derive(Iden)]
enum UpdateCheck {
Table,
Host,
Time,
Healthy,
RespTime,
ResponseCode,
}
+6
View File
@@ -0,0 +1,6 @@
use sea_orm_migration::prelude::*;
#[async_std::main]
async fn main() {
cli::run_cli(migration::Migrator).await;
}