From 9d2cae44b3612b6fdc3dee7d030ae43916150e6d Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 10 May 2022 23:24:23 +0800 Subject: [PATCH] Migrator CLI Fixup (#708) * CI compile migrator CLI * sea-orm-migration's CLI with only migration subcommand * Fix clippy warnings * Fixup * `sea-orm-cli migrate init`: write sea-orm-migration version based on CLI version --- .github/workflows/rust.yml | 13 ++++++++++ examples/rocket_example/migration/Cargo.toml | 2 +- sea-orm-cli/src/commands.rs | 15 ++++++++--- sea-orm-cli/template/migration/_Cargo.toml | 2 +- sea-orm-migration/src/cli.rs | 26 +++++++++++++++++--- sea-orm-migration/src/manager.rs | 10 ++++---- sea-orm-migration/src/prelude.rs | 3 +-- 7 files changed, 55 insertions(+), 16 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 44815e18..cc126405 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -309,6 +309,19 @@ jobs: args: > --manifest-path examples/${{ matrix.path }}/Cargo.toml + - name: Check existence of migration directory + id: migration_dir_exists + uses: andstor/file-existence-action@v1 + with: + files: examples/${{ matrix.path }}/migration/Cargo.toml + + - uses: actions-rs/cargo@v1 + if: steps.migration_dir_exists.outputs.files_exists == 'true' + with: + command: build + args: > + --manifest-path examples/${{ matrix.path }}/migration/Cargo.toml + issues: name: Issues needs: init diff --git a/examples/rocket_example/migration/Cargo.toml b/examples/rocket_example/migration/Cargo.toml index aa4cc3a8..db890058 100644 --- a/examples/rocket_example/migration/Cargo.toml +++ b/examples/rocket_example/migration/Cargo.toml @@ -10,8 +10,8 @@ path = "src/lib.rs" [dependencies] entity = { path = "../entity" } +rocket = { version = "0.5.0-rc.1" } [dependencies.sea-orm-migration] path = "../../../sea-orm-migration" # remove this line in your own project version = "^0.8.0" -rocket = { version = "0.5.0-rc.1" } diff --git a/sea-orm-cli/src/commands.rs b/sea-orm-cli/src/commands.rs index bf2b745a..dadb3ad2 100644 --- a/sea-orm-cli/src/commands.rs +++ b/sea-orm-cli/src/commands.rs @@ -201,9 +201,10 @@ pub fn run_migrate_command(matches: &ArgMatches<'_>) -> Result<(), Box { - write_file!($filename, $filename); + let fn_content = |content: String| content; + write_file!($filename, $filename, fn_content); }; - ($filename: literal, $template: literal) => { + ($filename: literal, $template: literal, $fn_content: expr) => { let filepath = [&migration_dir, $filename].join(""); println!("Creating file `{}`", filepath); let path = Path::new(&filepath); @@ -211,13 +212,21 @@ pub fn run_migrate_command(matches: &ArgMatches<'_>) -> Result<(), Box", &ver) + }); write_file!("README.md"); println!("Done!"); // Early exit! diff --git a/sea-orm-cli/template/migration/_Cargo.toml b/sea-orm-cli/template/migration/_Cargo.toml index 9f1f16d3..70817c3c 100644 --- a/sea-orm-cli/template/migration/_Cargo.toml +++ b/sea-orm-cli/template/migration/_Cargo.toml @@ -12,4 +12,4 @@ path = "src/lib.rs" entity = { path = "../entity" } [dependencies.sea-orm-migration] -version = "^0.8.0" +version = "" diff --git a/sea-orm-migration/src/cli.rs b/sea-orm-migration/src/cli.rs index c00e4824..c5e15771 100644 --- a/sea-orm-migration/src/cli.rs +++ b/sea-orm-migration/src/cli.rs @@ -1,10 +1,10 @@ -use clap::App; +use clap::{App, Arg, AppSettings}; use dotenv::dotenv; use std::{fmt::Display, process::exit}; use tracing_subscriber::{prelude::*, EnvFilter}; use sea_orm::{Database, DbConn}; -use sea_orm_cli::build_cli; +use sea_orm_cli::migration::get_subcommands; use super::MigratorTrait; @@ -26,13 +26,13 @@ where let matches = app.get_matches(); let mut verbose = false; let filter = match matches.subcommand() { - (_, None) => "sea_schema::migration=info", + (_, None) => "sea_orm_migration=info", (_, Some(args)) => match args.is_present("VERBOSE") { true => { verbose = true; "debug" } - false => "sea_schema::migration=info", + false => "sea_orm_migration=info", }, }; let filter_layer = EnvFilter::try_new(filter).unwrap(); @@ -74,6 +74,24 @@ where .unwrap_or_else(handle_error); } +pub fn build_cli() -> App<'static, 'static> { + let mut app = App::new("sea-orm-migration") + .version(env!("CARGO_PKG_VERSION")) + .setting(AppSettings::VersionlessSubcommands) + .arg( + Arg::with_name("VERBOSE") + .long("verbose") + .short("v") + .help("Show debug messages") + .takes_value(false) + .global(true), + ); + for subcommand in get_subcommands() { + app = app.subcommand(subcommand); + } + app +} + fn handle_error(error: E) where E: Display, diff --git a/sea-orm-migration/src/manager.rs b/sea-orm-migration/src/manager.rs index ef0f5f38..e6d618b0 100644 --- a/sea-orm-migration/src/manager.rs +++ b/sea-orm-migration/src/manager.rs @@ -1,10 +1,10 @@ use sea_orm::sea_query::{ extension::postgres::{TypeAlterStatement, TypeCreateStatement, TypeDropStatement}, - Alias, Expr, ForeignKeyCreateStatement, ForeignKeyDropStatement, IndexCreateStatement, - IndexDropStatement, Query, TableAlterStatement, TableCreateStatement, TableDropStatement, + ForeignKeyCreateStatement, ForeignKeyDropStatement, IndexCreateStatement, + IndexDropStatement, TableAlterStatement, TableCreateStatement, TableDropStatement, TableRenameStatement, TableTruncateStatement, }; -use sea_orm::{Condition, ConnectionTrait, DbBackend, DbConn, DbErr, Statement, StatementBuilder}; +use sea_orm::{ConnectionTrait, DbBackend, DbConn, DbErr, StatementBuilder}; use sea_schema::{mysql::MySql, postgres::Postgres, probe::SchemaProbe, sqlite::Sqlite}; /// Helper struct for writing migration scripts in migration file @@ -107,7 +107,7 @@ impl<'c> SchemaManager<'c> { .await? .ok_or_else(|| DbErr::Custom("Failed to check table exists".to_owned()))?; - Ok(res.try_get("", "has_table")?) + res.try_get("", "has_table") } pub async fn has_column(&self, table: T, column: C) -> Result @@ -128,6 +128,6 @@ impl<'c> SchemaManager<'c> { .await? .ok_or_else(|| DbErr::Custom("Failed to check column exists".to_owned()))?; - Ok(res.try_get("", "has_column")?) + res.try_get("", "has_column") } } diff --git a/sea-orm-migration/src/prelude.rs b/sea-orm-migration/src/prelude.rs index 21122408..dfce2d6a 100644 --- a/sea-orm-migration/src/prelude.rs +++ b/sea-orm-migration/src/prelude.rs @@ -1,5 +1,4 @@ -pub use sea_orm_cli::migration as cli; - +pub use super::cli; pub use super::manager::SchemaManager; pub use super::migrator::MigratorTrait; pub use super::{MigrationName, MigrationTrait};