feat(cli): implement downgrade migration command
This commit is contained in:
parent
b5c2533bc6
commit
64381eb710
3 changed files with 28 additions and 2 deletions
|
@ -133,6 +133,16 @@ impl Migration {
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn downgrade(&self, client: &mut Client) -> Result<(), Box<dyn std::error::Error + 'static>> {
|
||||||
|
let content = fs::read_to_string(&self.downgrade_sql)?;
|
||||||
|
|
||||||
|
database::apply_sql(client, &content)?;
|
||||||
|
|
||||||
|
database::delete_migration_info(client, self.name())?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
|
|
@ -40,3 +40,7 @@ pub fn create_migration_table(client: &mut Client) -> Result<(), Error> {
|
||||||
pub fn insert_migration_info(client: &mut Client, name: &str) -> Result<u64, Error> {
|
pub fn insert_migration_info(client: &mut Client, name: &str) -> Result<u64, Error> {
|
||||||
client.execute("INSERT INTO migrations (name) VALUES ($1)", &[&name])
|
client.execute("INSERT INTO migrations (name) VALUES ($1)", &[&name])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn delete_migration_info(client: &mut Client, name: &str) -> Result<u64, Error> {
|
||||||
|
client.execute("DELETE FROM migrations WHERE name = $1", &[&name])
|
||||||
|
}
|
||||||
|
|
|
@ -123,13 +123,25 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|m| !applied_migrations.contains(m.name()))
|
.filter(|m| !applied_migrations.contains(m.name()))
|
||||||
{
|
{
|
||||||
println!("{}", m.name());
|
println!("upgrade {}...", m.name());
|
||||||
m.upgrade(&mut client)?;
|
m.upgrade(&mut client)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Command::Downgrade => {
|
Command::Downgrade => {
|
||||||
unimplemented!();
|
let config = Config::read(opt.config)?;
|
||||||
|
|
||||||
|
let mut client = database::connect(&config.database.connection)?;
|
||||||
|
|
||||||
|
let applied_migrations = database::applied_migrations(&mut client)?;
|
||||||
|
let migrations = config.migrations()?;
|
||||||
|
|
||||||
|
if let Some(first_applied_migration) = applied_migrations.first() {
|
||||||
|
if let Some(migration) = migrations.iter().find(|m| m.name() == first_applied_migration) {
|
||||||
|
println!("downgrade {}...", migration.name());
|
||||||
|
migration.downgrade(&mut client)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue