-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI commands to run DB migrations
This prompted a refactor of the `RunCommand` trait to add `A: App` and pass the `App` instance as a parameter in `RunCommand::run`. This is possibly useful for other commands, and potentially for the consumer's custom commands as well. Another benefit of passing the `App` instance as a parameter is it simplifies the syntax for specifying the `A` generic argument. #41
- Loading branch information
1 parent
257e915
commit f394e3f
Showing
9 changed files
with
187 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use anyhow::bail; | ||
use async_trait::async_trait; | ||
use clap::{Parser, Subcommand}; | ||
use sea_orm_migration::MigratorTrait; | ||
use tracing::warn; | ||
|
||
use crate::app::App; | ||
use crate::app_context::AppContext; | ||
use crate::cli::{RoadsterCli, RunRoadsterCommand}; | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct MigrateArgs { | ||
#[clap(subcommand)] | ||
pub command: MigrateCommand, | ||
} | ||
|
||
#[async_trait] | ||
impl<A> RunRoadsterCommand<A> for MigrateArgs | ||
where | ||
A: App, | ||
{ | ||
async fn run(&self, app: &A, cli: &RoadsterCli, context: &AppContext) -> anyhow::Result<bool> { | ||
self.command.run(app, cli, context).await | ||
} | ||
} | ||
|
||
#[derive(Debug, Subcommand)] | ||
pub enum MigrateCommand { | ||
/// Apply pending migrations | ||
Up(UpArgs), | ||
/// Rollback applied migrations | ||
Down(DownArgs), | ||
/// Rollback all applied migrations, then reapply all migrations | ||
Refresh, | ||
/// Rollback all applied migrations | ||
Reset, | ||
/// Drop all tables from the database, then reapply all migrations | ||
Fresh, | ||
/// Check the status of all migrations | ||
Status, | ||
} | ||
|
||
#[async_trait] | ||
impl<A> RunRoadsterCommand<A> for MigrateCommand | ||
where | ||
A: App, | ||
{ | ||
async fn run(&self, _app: &A, cli: &RoadsterCli, context: &AppContext) -> anyhow::Result<bool> { | ||
if is_destructive(self) && !cli.allow_dangerous(context) { | ||
bail!("Running destructive command `{:?}` is not allowed in environment `{:?}`. To override, provide the `--allow-dangerous` CLI arg.", self, context.config.environment); | ||
} else if is_destructive(self) { | ||
warn!( | ||
"Running destructive command `{:?}` in environment `{:?}`", | ||
self, context.config.environment | ||
); | ||
} | ||
match self { | ||
MigrateCommand::Up(args) => A::M::up(&context.db, args.steps).await?, | ||
MigrateCommand::Down(args) => A::M::down(&context.db, args.steps).await?, | ||
MigrateCommand::Refresh => A::M::refresh(&context.db).await?, | ||
MigrateCommand::Reset => A::M::reset(&context.db).await?, | ||
MigrateCommand::Fresh => A::M::fresh(&context.db).await?, | ||
MigrateCommand::Status => A::M::status(&context.db).await?, | ||
}; | ||
Ok(true) | ||
} | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct UpArgs { | ||
/// The number of pending migration steps to apply. | ||
#[clap(short = 'n', long)] | ||
pub steps: Option<u32>, | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct DownArgs { | ||
/// The number of applied migration steps to rollback. | ||
#[clap(short = 'n', long)] | ||
pub steps: Option<u32>, | ||
} | ||
|
||
fn is_destructive(command: &MigrateCommand) -> bool { | ||
!matches!(command, MigrateCommand::Status) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.