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
This commit is contained in:
Billy Chan 2022-05-10 23:24:23 +08:00 committed by GitHub
parent 5f8776babf
commit 9d2cae44b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 16 deletions

View File

@ -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

View File

@ -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" }

View File

@ -201,9 +201,10 @@ pub fn run_migrate_command(matches: &ArgMatches<'_>) -> Result<(), Box<dyn Error
println!("Initializing migration directory...");
macro_rules! write_file {
($filename: literal) => {
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<dyn Error
fs::create_dir_all(prefix).unwrap();
let mut file = fs::File::create(path)?;
let content = include_str!(concat!("../template/migration/", $template));
let content = $fn_content(content.to_string());
file.write_all(content.as_bytes())?;
};
}
write_file!("src/lib.rs");
write_file!("src/m20220101_000001_create_table.rs");
write_file!("src/main.rs");
write_file!("Cargo.toml", "_Cargo.toml");
write_file!("Cargo.toml", "_Cargo.toml", |content: String| {
let ver = format!(
"^{}.{}.0",
env!("CARGO_PKG_VERSION_MAJOR"),
env!("CARGO_PKG_VERSION_MINOR")
);
content.replace("<sea-orm-migration-version>", &ver)
});
write_file!("README.md");
println!("Done!");
// Early exit!

View File

@ -12,4 +12,4 @@ path = "src/lib.rs"
entity = { path = "../entity" }
[dependencies.sea-orm-migration]
version = "^0.8.0"
version = "<sea-orm-migration-version>"

View File

@ -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<E>(error: E)
where
E: Display,

View File

@ -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<T, C>(&self, table: T, column: C) -> Result<bool, DbErr>
@ -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")
}
}

View File

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