mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-03 07:45:30 +00:00
Add source param to command line
This commit is contained in:
parent
9d71a7f372
commit
a4729cdcc8
@ -31,9 +31,9 @@ hint: This command only works in the manifest directory of a Cargo package."#
|
|||||||
|
|
||||||
match opt.command {
|
match opt.command {
|
||||||
Command::Migrate(migrate) => match migrate.command {
|
Command::Migrate(migrate) => match migrate.command {
|
||||||
MigrateCommand::Add { description } => migrate::add(&description)?,
|
MigrateCommand::Add { description } => migrate::add(&migrate.source, &description)?,
|
||||||
MigrateCommand::Run => migrate::run(&database_url).await?,
|
MigrateCommand::Run => migrate::run(&migrate.source, &database_url).await?,
|
||||||
MigrateCommand::Info => migrate::info(&database_url).await?,
|
MigrateCommand::Info => migrate::info(&migrate.source, &database_url).await?,
|
||||||
},
|
},
|
||||||
|
|
||||||
Command::Database(database) => match database.command {
|
Command::Database(database) => match database.command {
|
||||||
|
@ -8,11 +8,12 @@ use std::path::Path;
|
|||||||
|
|
||||||
const MIGRATION_FOLDER: &'static str = "migrations";
|
const MIGRATION_FOLDER: &'static str = "migrations";
|
||||||
|
|
||||||
pub fn add(description: &str) -> anyhow::Result<()> {
|
pub fn add(source_path: &Option<String>, description: &str) -> anyhow::Result<()> {
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
let migration_folder = migration_path(source_path);
|
||||||
|
|
||||||
fs::create_dir_all(MIGRATION_FOLDER).context("Unable to create migrations directory")?;
|
fs::create_dir_all(migration_folder).context("Unable to create migrations directory")?;
|
||||||
|
|
||||||
let dt = Utc::now();
|
let dt = Utc::now();
|
||||||
let mut file_name = dt.format("%Y%m%d%H%M%S").to_string();
|
let mut file_name = dt.format("%Y%m%d%H%M%S").to_string();
|
||||||
@ -21,7 +22,7 @@ pub fn add(description: &str) -> anyhow::Result<()> {
|
|||||||
file_name.push_str(".sql");
|
file_name.push_str(".sql");
|
||||||
|
|
||||||
let mut path = PathBuf::new();
|
let mut path = PathBuf::new();
|
||||||
path.push(MIGRATION_FOLDER);
|
path.push(migration_folder);
|
||||||
path.push(&file_name);
|
path.push(&file_name);
|
||||||
|
|
||||||
println!("Creating {}", style(path.display()).cyan());
|
println!("Creating {}", style(path.display()).cyan());
|
||||||
@ -33,8 +34,9 @@ pub fn add(description: &str) -> anyhow::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn info(uri: &str) -> anyhow::Result<()> {
|
pub async fn info(source_path: &Option<String>, uri: &str) -> anyhow::Result<()> {
|
||||||
let migrator = Migrator::new(Path::new(MIGRATION_FOLDER)).await?;
|
let migration_folder = migration_path(source_path);
|
||||||
|
let migrator = Migrator::new(Path::new(migration_folder)).await?;
|
||||||
let mut conn = AnyConnection::connect(uri).await?;
|
let mut conn = AnyConnection::connect(uri).await?;
|
||||||
|
|
||||||
conn.ensure_migrations_table().await?;
|
conn.ensure_migrations_table().await?;
|
||||||
@ -57,8 +59,9 @@ pub async fn info(uri: &str) -> anyhow::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run(uri: &str) -> anyhow::Result<()> {
|
pub async fn run(source_path: &Option<String>, uri: &str) -> anyhow::Result<()> {
|
||||||
let migrator = Migrator::new(Path::new(MIGRATION_FOLDER)).await?;
|
let migration_folder = migration_path(source_path);
|
||||||
|
let migrator = Migrator::new(Path::new(migration_folder)).await?;
|
||||||
let mut conn = AnyConnection::connect(uri).await?;
|
let mut conn = AnyConnection::connect(uri).await?;
|
||||||
|
|
||||||
conn.ensure_migrations_table().await?;
|
conn.ensure_migrations_table().await?;
|
||||||
@ -87,3 +90,7 @@ pub async fn run(uri: &str) -> anyhow::Result<()> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn migration_path<'a>(source_path: &'a Option<String>) -> &'a str {
|
||||||
|
source_path.as_deref().unwrap_or(MIGRATION_FOLDER)
|
||||||
|
}
|
||||||
|
@ -71,6 +71,10 @@ pub enum DatabaseCommand {
|
|||||||
/// Group of commands for creating and running migrations.
|
/// Group of commands for creating and running migrations.
|
||||||
#[derive(Clap, Debug)]
|
#[derive(Clap, Debug)]
|
||||||
pub struct MigrateOpt {
|
pub struct MigrateOpt {
|
||||||
|
/// Path to folder containing migrations. Defaults to 'migrations'
|
||||||
|
#[clap(long)]
|
||||||
|
pub source: Option<String>,
|
||||||
|
|
||||||
#[clap(subcommand)]
|
#[clap(subcommand)]
|
||||||
pub command: MigrateCommand,
|
pub command: MigrateCommand,
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user