Skip to content

Commit

Permalink
refactor cli
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbuilds committed Nov 19, 2023
1 parent 71e48fd commit 9759e3a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 31 deletions.
10 changes: 5 additions & 5 deletions cli/src/command/down.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use std::path::Path;
use anyhow::{Error, Result};
use clap::Parser;

use ormlite::postgres::PgArguments;
use ormlite::{Acquire, Executor};
use ormlite::postgres::{PgArguments, PgConnection};
use ormlite::{Acquire, Connection, Executor};
use crate::command::{get_executed_migrations, get_pending_migrations, MigrationType};
use ormlite_core::config::{get_var_snapshot_folder, get_var_database_url, get_var_migration_folder};
use crate::util::{CommandSuccess, create_connection, create_runtime};
use crate::util::{CommandSuccess, create_runtime};
use ormlite::Arguments;
use url::Url;

Expand Down Expand Up @@ -49,10 +49,10 @@ impl Down {
let folder = get_var_migration_folder();
let runtime = create_runtime();
let url = get_var_database_url();
let mut conn = create_connection(&url, &runtime)?;
let mut conn = runtime.block_on(PgConnection::connect(&url))?;
let conn = runtime.block_on(conn.acquire())?;

let mut executed = get_executed_migrations(&runtime, &mut *conn)?;
let mut executed = runtime.block_on(get_executed_migrations(&mut *conn))?;
let pending = get_pending_migrations(&folder)?
.into_iter()
.filter(|m| m.migration_type() != MigrationType::Up)
Expand Down
2 changes: 1 addition & 1 deletion cli/src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ormlite::Executor;
use ormlite::postgres::PgConnection;

use ormlite_core::config::{get_var_database_url};
use crate::util::{create_connection, create_runtime};
use crate::util::{create_runtime};

const INIT_QUERY: &str = r#"
CREATE TABLE public._sqlx_migrations (
Expand Down
16 changes: 8 additions & 8 deletions cli/src/command/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use time::macros::format_description;
use time::OffsetDateTime as DateTime;
use tokio::runtime::Runtime;

use ormlite::Row;
use ormlite::Acquire;
use ormlite::{Acquire, Connection};
use ormlite::postgres::PgConnection;
use ormlite::Row;
use ormlite_core::config;
use ormlite_core::config::get_var_model_folders;
use ormlite_core::schema::TryFromOrmlite;

use crate::util::{create_connection, create_runtime};
use crate::util::create_runtime;

const GET_MIGRATIONS_QUERY: &str = "SELECT
version || '_' || description AS name
Expand Down Expand Up @@ -105,9 +105,9 @@ fn create_migration(folder: &Path, file_name: String, migration: MigrationType,
}

/// Migrations are sorted asc. Last is most recent.
pub fn get_executed_migrations(runtime: &Runtime, conn: &mut PgConnection) -> Result<Vec<MigrationMetadata>> {
let migrations = runtime.block_on(ormlite::query(GET_MIGRATIONS_QUERY)
.fetch_all(conn))?;
pub async fn get_executed_migrations(conn: &mut PgConnection) -> Result<Vec<MigrationMetadata>> {
let migrations = ormlite::query(GET_MIGRATIONS_QUERY)
.fetch_all(conn).await?;
let migrations = migrations.into_iter().map(|m: ormlite::postgres::PgRow| {
let name: String = m.get("name");
let version: i64 = m.get("version");
Expand Down Expand Up @@ -144,7 +144,7 @@ pub fn get_pending_migrations(folder: &Path) -> Result<Vec<MigrationMetadata>> {

// Returns the type of migration environment, either None (any), Simple, or Up (it doesn't return Down)
fn check_for_pending_migrations(folder: &Path, runtime: &Runtime, conn: &mut PgConnection) -> Result<Option<MigrationType>> {
let executed = get_executed_migrations(runtime, conn)?;
let executed = runtime.block_on(get_executed_migrations(conn))?;
let pending = get_pending_migrations(folder)?;

if executed.len() < pending.len() {
Expand Down Expand Up @@ -209,7 +209,7 @@ impl Migrate {
let folder = config::get_var_migration_folder();
let url = config::get_var_database_url();

let mut conn = create_connection(&url, &runtime)?;
let mut conn = runtime.block_on(PgConnection::connect(&url))?;
let conn = runtime.block_on(conn.acquire())?;

fs::create_dir_all(&folder)?;
Expand Down
10 changes: 5 additions & 5 deletions cli/src/command/up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::fs::File;
use std::time::Instant;
use anyhow::Result;
use clap::Parser;
use ormlite::{Executor, Arguments, Acquire};
use ormlite::postgres::PgArguments;
use ormlite::{Executor, Arguments, Acquire, Connection};
use ormlite::postgres::{PgArguments, PgConnection};
use crate::command::{get_executed_migrations, get_pending_migrations, MigrationType};
use ormlite_core::config::{get_var_snapshot_folder, get_var_database_url, get_var_migration_folder};
use crate::util::{CommandSuccess, create_connection, create_runtime};
use crate::util::{CommandSuccess, create_runtime};
use sha2::{Digest, Sha384};


Expand All @@ -31,10 +31,10 @@ impl Up {
let folder = get_var_migration_folder();
let runtime = create_runtime();
let url = get_var_database_url();
let mut conn = create_connection(&url, &runtime).unwrap();
let mut conn = runtime.block_on(PgConnection::connect(&url))?;
let conn = runtime.block_on(conn.acquire()).unwrap();

let executed = get_executed_migrations(&runtime, &mut *conn).unwrap();
let executed = runtime.block_on(get_executed_migrations(&mut *conn))?;
let pending = get_pending_migrations(&folder).unwrap()
.into_iter()
.filter(|m| m.migration_type() != MigrationType::Down)
Expand Down
15 changes: 3 additions & 12 deletions cli/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use anyhow::Error;
use tokio::runtime::Runtime;

use ormlite::Connection;
use ormlite::postgres::PgConnection;

pub(crate) fn create_runtime() -> tokio::runtime::Runtime {
tokio::runtime::Builder::new_current_thread()
Expand All @@ -12,17 +8,12 @@ pub(crate) fn create_runtime() -> tokio::runtime::Runtime {
.unwrap()
}

pub fn create_connection(url: &str, runtime: &Runtime) -> anyhow::Result<PgConnection> {
let conn = runtime.block_on(ormlite::postgres::PgConnection::connect(url))?;
Ok(conn)
}

pub trait CommandSuccess {
fn ok_or(&mut self, message: &str) -> Result<(), anyhow::Error>;
fn ok_or(&mut self, message: &str) -> Result<(), Error>;
}

impl CommandSuccess for std::process::Command {
fn ok_or(&mut self, message: &str) -> Result<(), anyhow::Error> {
fn ok_or(&mut self, message: &str) -> Result<(), Error> {
let status = self.status()?;
if status.success() {
Ok(())
Expand All @@ -33,7 +24,7 @@ impl CommandSuccess for std::process::Command {
}

impl CommandSuccess for std::process::Output {
fn ok_or(&mut self, message: &str) -> Result<(), anyhow::Error> {
fn ok_or(&mut self, message: &str) -> Result<(), Error> {
if self.status.success() {
Ok(())
} else {
Expand Down

0 comments on commit 9759e3a

Please sign in to comment.