Skip to content

Commit

Permalink
feat: migration only #400
Browse files Browse the repository at this point in the history
  • Loading branch information
jondot committed Feb 1, 2024
1 parent ebf04ce commit df547d4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
16 changes: 15 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ enum ComponentArg {
#[arg(short, long, action)]
link: bool,

/// Generate migration code only. Don't run the migration automatically.
#[arg(short, long, action)]
migration_only: bool,

/// Model fields, eg. title:string hits:int
#[clap(value_parser = parse_key_val::<String,String>)]
fields: Vec<(String, String)>,
Expand Down Expand Up @@ -168,7 +172,17 @@ impl From<ComponentArg> for Component {
fn from(value: ComponentArg) -> Self {
match value {
#[cfg(feature = "with-db")]
ComponentArg::Model { name, link, fields } => Self::Model { name, link, fields },
ComponentArg::Model {
name,
link,
migration_only,
fields,
} => Self::Model {
name,
link,
migration_only,
fields,
},
#[cfg(feature = "with-db")]
ComponentArg::Migration { name } => Self::Migration { name },
#[cfg(feature = "with-db")]
Expand Down
15 changes: 13 additions & 2 deletions src/gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ pub enum Component {

/// Model fields, eg. title:string hits:int
fields: Vec<(String, String)>,

/// Generate migration code and stop, don't run the migration
migration_only: bool,
},
#[cfg(feature = "with-db")]
Migration {
Expand Down Expand Up @@ -109,8 +112,16 @@ pub fn generate<H: Hooks>(component: Component, config: &Config) -> Result<()> {

match component {
#[cfg(feature = "with-db")]
Component::Model { name, link, fields } => {
println!("{}", model::generate::<H>(&rrgen, &name, link, &fields)?);
Component::Model {
name,
link,
fields,
migration_only,
} => {
println!(
"{}",
model::generate::<H>(&rrgen, &name, link, migration_only, &fields)?
);
}
#[cfg(feature = "with-db")]
Component::Scaffold { name, fields } => {
Expand Down
41 changes: 22 additions & 19 deletions src/gen/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub fn generate<H: Hooks>(
rrgen: &RRgen,
name: &str,
is_link: bool,
migration_only: bool,
fields: &[(String, String)],
) -> Result<String> {
let pkg_name: &str = H::app_name();
Expand Down Expand Up @@ -101,25 +102,27 @@ pub fn generate<H: Hooks>(
let res1 = rrgen.generate(MODEL_T, &vars)?;
let res2 = rrgen.generate(MODEL_TEST_T, &vars)?;

let cwd = current_dir()?;
let _ = cmd!("cargo", "loco", "db", "migrate",)
.stderr_to_stdout()
.dir(cwd.as_path())
.run()
.map_err(|err| {
Error::Message(format!(
"failed to run loco db migration. error details: `{err}`",
))
})?;
let _ = cmd!("cargo", "loco", "db", "entities",)
.stderr_to_stdout()
.dir(cwd.as_path())
.run()
.map_err(|err| {
Error::Message(format!(
"failed to run loco db entities. error details: `{err}`",
))
})?;
if !migration_only {
let cwd = current_dir()?;
let _ = cmd!("cargo", "loco", "db", "migrate",)
.stderr_to_stdout()
.dir(cwd.as_path())
.run()
.map_err(|err| {
Error::Message(format!(
"failed to run loco db migration. error details: `{err}`",
))
})?;
let _ = cmd!("cargo", "loco", "db", "entities",)
.stderr_to_stdout()
.dir(cwd.as_path())
.run()
.map_err(|err| {
Error::Message(format!(
"failed to run loco db entities. error details: `{err}`",
))
})?;
}

let messages = collect_messages(vec![res1, res2]);
Ok(messages)
Expand Down
6 changes: 4 additions & 2 deletions src/gen/scaffold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ pub fn generate<H: Hooks>(
name: &str,
fields: &[(String, String)],
) -> Result<String> {
// scaffold is never a link table
let model_messages = model::generate::<H>(rrgen, name, false, fields)?;
// - scaffold is never a link table
// - never run with migration_only, because the controllers will refer to the
// models. the models only arrive after migration and entities sync.
let model_messages = model::generate::<H>(rrgen, name, false, false, fields)?;

let mut columns = Vec::new();
for (fname, ftype) in fields {
Expand Down

0 comments on commit df547d4

Please sign in to comment.