Skip to content

Commit

Permalink
Merge pull request #558 from thoth-pub/feature/503_enable_revert_migr…
Browse files Browse the repository at this point in the history
…ations

Feature/503 enable revert migrations
  • Loading branch information
ja573 authored Feb 14, 2024
2 parents f10a5c2 + 01183de commit 467bfd7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/run_migrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,8 @@ jobs:
with:
command: run
args: migrate
- name: Revert migrations
uses: actions-rs/cargo@v1
with:
command: run
args: migrate --revert
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [305](https://github.com/thoth-pub/thoth/issues/305) - Update rust edition to 2021

### Added
- [503](https://github.com/thoth-pub/thoth/issues/503) - Allow reverting migrations in the CLI and check that migrations can be reverted in run-migration github action
- [557](https://github.com/thoth-pub/thoth/pull/557) - Added github action to chech that the changelog has been updated on PRs

## [[0.11.15]](https://github.com/thoth-pub/thoth/releases/tag/v0.11.15) - 2024-01-18
Expand Down
20 changes: 16 additions & 4 deletions src/bin/thoth.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use clap::{crate_authors, crate_version, value_parser, Arg, Command};
use clap::{crate_authors, crate_version, value_parser, Arg, ArgAction, Command};
use dialoguer::{console::Term, theme::ColorfulTheme, Input, MultiSelect, Password, Select};
use dotenv::dotenv;
use std::env;
use thoth::api::account::model::{AccountData, LinkedPublisher};
use thoth::api::account::service::{all_emails, all_publishers, register, update_password};
use thoth::api::db::{establish_connection, run_migrations};
use thoth::api::db::{establish_connection, revert_migrations, run_migrations};
use thoth::api_server;
use thoth::app_server;
use thoth::export_server;
Expand Down Expand Up @@ -129,7 +129,16 @@ fn thoth_commands() -> Command {
.about(env!("CARGO_PKG_DESCRIPTION"))
.subcommand_required(true)
.arg_required_else_help(true)
.subcommand(Command::new("migrate").about("Run the database migrations"))
.subcommand(
Command::new("migrate")
.about("Run the database migrations")
.arg(
Arg::new("revert")
.long("revert")
.help("Revert all database migrations")
.action(ArgAction::SetTrue),
),
)
.subcommand(
Command::new("start")
.about("Start an instance of Thoth API or GUI")
Expand Down Expand Up @@ -240,7 +249,10 @@ fn main() -> ThothResult<()> {
}
_ => unreachable!(),
},
Some(("migrate", _)) => run_migrations(),
Some(("migrate", migrate_matches)) => match migrate_matches.get_flag("revert") {
true => revert_migrations(),
false => run_migrations(),
},
Some(("init", init_matches)) => {
let host = init_matches.get_one::<String>("host").unwrap().to_owned();
let port = init_matches.get_one::<String>("port").unwrap().to_owned();
Expand Down
8 changes: 8 additions & 0 deletions thoth-api/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ pub fn run_migrations() -> ThothResult<()> {
Err(error) => Err(ThothError::DatabaseError(error.to_string())),
}
}

pub fn revert_migrations() -> ThothResult<()> {
let mut connection = establish_connection().get().unwrap();
match connection.revert_all_migrations(MIGRATIONS) {
Ok(_) => Ok(()),
Err(error) => Err(ThothError::DatabaseError(error.to_string())),
}
}

0 comments on commit 467bfd7

Please sign in to comment.