Add CLI application with database migration and entity generation commands

- Introduced a new CLI application in the `apps/cli` directory.
- Implemented commands for database migration and entity generation.
- Updated `Cargo.toml` files to include necessary dependencies.
- Enhanced the `justfile` to facilitate CLI command execution.
- Modified workspace configuration to include the new CLI application.
This commit is contained in:
GW_MC
2025-11-13 21:26:31 +08:00
parent 373065c95f
commit 829c4ef3e3
9 changed files with 517 additions and 13 deletions

View File

@@ -11,6 +11,7 @@ path = "src/lib.rs"
[dependencies]
tokio = { version = "1", features = ["macros", "rt", "rt-multi-thread"] }
sea-orm-cli = { version = "2.0.0-rc", features = ["sqlx-postgres", "sqlx-mysql", "sqlx-sqlite", "runtime-tokio"] }
[dependencies.sea-orm-migration]
version = "2.0.0-rc"

View File

@@ -2,6 +2,7 @@ pub use sea_orm_migration::prelude::*;
mod migrations;
use migrations::*;
use sea_orm_migration::sea_orm::Database;
pub struct Migrator;
@@ -14,3 +15,49 @@ impl MigratorTrait for Migrator {
]
}
}
pub async fn migrate_database(db_url: &str) -> Result<(), DbErr> {
let db = Database::connect(db_url).await?;
Migrator::up(&db, None).await
}
pub async fn generate_entity(
db_url: &str,
output_dir: &str,
) -> Result<(), Box<dyn std::error::Error>> {
use sea_orm_cli::commands::generate::run_generate_command;
run_generate_command(
sea_orm_cli::GenerateSubcommands::Entity {
compact_format: true,
expanded_format: false,
frontend_format: false,
include_hidden_tables: false,
tables: vec![],
ignore_tables: vec!["seaql_migrations".to_string()],
max_connections: 1,
acquire_timeout: 30,
output_dir: output_dir.to_string(),
database_schema: Some("public".to_string()),
database_url: db_url.to_string(),
with_prelude: "all".to_string(),
with_serde: "both".to_string(),
serde_skip_deserializing_primary_key: false,
serde_skip_hidden_column: false,
with_copy_enums: true,
date_time_crate: sea_orm_cli::DateTimeCrate::Chrono,
lib: false,
model_extra_derives: vec![],
model_extra_attributes: vec![],
enum_extra_derives: vec![],
enum_extra_attributes: vec![],
seaography: false,
impl_active_model_behavior: true,
big_integer_type: sea_orm_cli::BigIntegerType::I64,
column_extra_derives: vec![],
entity_format: Some("dense".to_string()),
preserve_user_modifications: false,
},
false,
)
.await
}