1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
use anyhow::Result;
use sqlx::migrate::{MigrateError, Migrator};
use std::path::Path;
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
pub struct Opt {
#[structopt(subcommand)]
pub command: Command,
}
#[derive(StructOpt, Debug)]
pub enum Command {
#[structopt(alias = "db")]
Database(DatabaseOpt),
#[structopt(alias = "mig")]
Migrate(MigrateOpt),
#[structopt(alias = "gen")]
Generate(GenerateOpt),
}
/// Group of commands for creating and dropping your database.
#[derive(StructOpt, Debug)]
pub struct DatabaseOpt {
#[structopt(subcommand)]
pub command: DatabaseCommand,
}
#[derive(StructOpt, Debug)]
pub enum DatabaseCommand {
/// Creates the database specified in your DATABASE_URL.
Create {
/// Location of the DB, by default will be read from the DATABASE_URL env var
#[structopt(long, short = "D", env)]
database_url: String,
},
/// Drops the database specified in your DATABASE_URL.
Drop {
/// Automatic confirmation. Without this option, you will be prompted before dropping
/// your database.
#[structopt(short)]
yes: bool,
/// Location of the DB, by default will be read from the DATABASE_URL env var
#[structopt(long, short = "D", env)]
database_url: String,
},
/// Drops the database specified in your DATABASE_URL, re-creates it, and runs any pending migrations.
Reset {
/// Automatic confirmation. Without this option, you will be prompted before dropping
/// your database.
#[structopt(short)]
yes: bool,
/// Path to folder containing migrations.
#[structopt(long, default_value = "migrations")]
source: String,
/// Location of the DB, by default will be read from the DATABASE_URL env var
#[structopt(long, short = "D", env)]
database_url: String,
},
/// Creates the database specified in your DATABASE_URL and runs any pending migrations.
Setup {
/// Path to folder containing migrations.
#[structopt(long, default_value = "migrations")]
source: String,
/// Location of the DB, by default will be read from the DATABASE_URL env var
#[structopt(long, short = "D", env)]
database_url: String,
},
}
/// Group of commands for creating and running migrations.
#[derive(StructOpt, Debug)]
pub struct MigrateOpt {
/// Path to folder containing migrations.
#[structopt(long, default_value = "migrations")]
pub source: String,
#[structopt(subcommand)]
pub command: MigrateCommand,
}
/// Commands related to automatic migration generation.
#[derive(StructOpt, Debug)]
pub struct GenerateOpt {
/// Location of the DB, by default will be read from the DATABASE_URL env var
#[structopt(long, short = "D", env)]
pub database_url: String,
/// Path to folder containing migrations.
#[structopt(long, default_value = "migrations")]
pub source: String,
/// Used to filter through the sql_from_models to execute.
#[structopt(long)]
pub table: Option<String>,
/// Used to generate a down migrations along with up migrations.
#[structopt(short)]
pub reversible: bool,
}
impl GenerateOpt {
pub async fn validate(&self) -> Result<()> {
url::Url::parse(&self.database_url)?;
let migrator = Migrator::new(Path::new(&self.source)).await?;
for migration in migrator.iter() {
if migration.migration_type.is_reversible() != self.reversible {
Err(MigrateError::InvalidMixReversibleAndSimple)?
}
}
Ok(())
}
}
#[derive(StructOpt, Debug)]
pub enum MigrateCommand {
/// Create a new migration with the given description,
/// and the current time as the version.
Add {
description: String,
/// If true, creates a pair of up and down migration files with same version
/// else creates a single sql file
#[structopt(short)]
reversible: bool,
},
/// Run all pending migrations.
Run {
/// List all the migrations to be run without applying
#[structopt(long)]
dry_run: bool,
/// Ignore applied migrations that missing in the resolved migrations
#[structopt(long)]
ignore_missing: bool,
/// Location of the DB, by default will be read from the DATABASE_URL env var
#[structopt(long, short = "D", env)]
database_url: String,
},
/// Revert the latest migration with a down file.
Revert {
/// List the migration to be reverted without applying
#[structopt(long)]
dry_run: bool,
/// Ignore applied migrations that missing in the resolved migrations
#[structopt(long)]
ignore_missing: bool,
/// Location of the DB, by default will be read from the DATABASE_URL env var
#[structopt(long, short = "D", env)]
database_url: String,
},
/// List all available migrations.
Info {
/// Location of the DB, by default will be read from the DATABASE_URL env var
#[structopt(long, env)]
database_url: String,
},
/// Generate a `build.rs` to trigger recompilation when a new migration is added.
///
/// Must be run in a Cargo project root.
BuildScript {
/// Overwrite the build script if it already exists.
#[structopt(long)]
force: bool,
},
}