Skip to content

Commit

Permalink
🍕 Update ci release workflow and refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Edward Fitz Abucay <ffimnsr@gmail.com>
  • Loading branch information
ffimnsr committed Oct 21, 2023
1 parent 7ef220b commit f486b87
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-push-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ jobs:
with:
context: .
file: ./Containerfile
platforms: linux/amd64, linux/arm64
platforms: linux/amd64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Expand Down
34 changes: 33 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Reference from here:
# https://github.com/dtolnay/syn/blob/master/.github/workflows/ci.yml
name: ci
on:
pull_request:
push:
branches:
- master
schedule:
- cron: '00 01 * * *'
- cron: '0 1 * * *'

permissions:
contents: read
Expand All @@ -15,8 +17,21 @@ env:
RUSTFLAGS: -Dwarnings

jobs:
check-pass:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
components: rustc-dev
- name: Run cargo check tool
run: cargo check --all-targets --all-features --tests --benches --release --verbose
test:
runs-on: ${{ matrix.os }}
needs:
- check-pass
env:
CARGO: cargo
TARGET_FLAGS: ""
Expand Down Expand Up @@ -71,6 +86,7 @@ jobs:
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
components: llvm-tools, rustc-dev
- name: Use cross compiler
if: matrix.os == 'ubuntu-latest' && matrix.target != ''
run: |
Expand Down Expand Up @@ -107,3 +123,19 @@ jobs:
components: rustfmt
- name: Check formatting
run: cargo fmt --all --check
clippy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
components: clippy, rustc-dev
- run: cargo clippy --all-features --all-targets --tests --benches -- -Dclippy::all
# outdated:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - uses: dtolnay/install@cargo-outdated
# - run: cargo outdated --exit-code 1
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "midas"
version = "0.5.18"
version = "0.5.19"
authors = ["Edward Fitz Abucay gh:@ffimnsr"]
edition = "2021"
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ For more info see `--help`.

If you're into **Rust** then you can use `cargo` to install.

* The minimum supported version of Rust is 1.37.0.
* The minimum supported version of Rust is `1.37.0`.

```shellbash
cargo install midas
Expand Down
11 changes: 5 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::time::Instant;

use clap::{App, AppSettings, Arg, SubCommand};

const PKG_VERSION: &'static str = env!("CARGO_PKG_VERSION");
const PKG_DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION");
const PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
const PKG_DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");

use super::commander::Migrator;
use super::sequel::postgres::Postgres;
Expand Down Expand Up @@ -101,10 +101,9 @@ pub(crate) fn midas_entry(
internal_matches
.subcommand_matches(command_name)
.ok_or(format!(
"cargo-{} not invoked via cargo command",
command_name
"cargo-{command_name} not invoked via cargo command",
))?
.to_owned()
.clone()
} else {
cli_app.get_matches()
};
Expand Down Expand Up @@ -137,7 +136,7 @@ pub(crate) fn midas_entry(
.value_of("name")
.ok_or("Slug is either malformed or undecipherable")?;

migrator.create(source_path, slug)?
migrator.create(source_path, slug)?;
}
Some("status") => migrator.status()?,
Some("up") => migrator.up()?,
Expand Down
53 changes: 25 additions & 28 deletions src/commander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::Path;
use log::trace;

use crate::lookup::{self, MigrationFiles, VecStr};
use crate::sequel::{SequelDriver, VecSerial};
use crate::sequel::{Driver as SequelDriver, VecSerial};

macro_rules! get_content_string {
($content: ident) => {
Expand Down Expand Up @@ -34,16 +34,16 @@ impl<T: SequelDriver + 'static> Migrator<T> {
path: &Path,
slug: &str,
) -> Result<(), super::GenericError> {
let fixed_slug = slug.to_ascii_lowercase().replace(" ", "_");
let _ = lookup::create_migration_file(path, &fixed_slug)?;
let fixed_slug = slug.to_ascii_lowercase().replace(' ', "_");
lookup::create_migration_file(path, &fixed_slug)?;

Ok(())
}

pub fn status(&mut self) -> Result<(), super::GenericError> {
let completed_migrations = self.executor.get_completed_migrations()?;
let available_migrations =
self.migrations.keys().cloned().collect::<VecSerial>();
self.migrations.keys().copied().collect::<VecSerial>();

if available_migrations.is_empty() {
println!("There are no available migration files.");
Expand All @@ -52,19 +52,20 @@ impl<T: SequelDriver + 'static> Migrator<T> {

println!("Building active migrations list...");
if completed_migrations.is_empty() {
for it in available_migrations.iter() {
println!("{:013} = Inactive", it);
for it in &available_migrations {
println!("{it:013} = Inactive");
}

return Ok(());
}

for it in available_migrations.iter() {
let does_have = match completed_migrations.contains(it) {
true => "Active",
_ => "Inactive",
for it in &available_migrations {
let does_have = if completed_migrations.contains(it) {
"Active"
} else {
"Inactive"
};
println!("{:013} = {}", it, does_have);
println!("{it:013} = {does_have}");
}

Ok(())
Expand All @@ -73,7 +74,7 @@ impl<T: SequelDriver + 'static> Migrator<T> {
pub fn up(&mut self) -> Result<(), super::GenericError> {
let completed_migrations = self.executor.get_completed_migrations()?;
let available_migrations =
self.migrations.keys().cloned().collect::<VecSerial>();
self.migrations.keys().copied().collect::<VecSerial>();

if available_migrations.is_empty() {
println!("There are no available migration files.");
Expand All @@ -82,18 +83,18 @@ impl<T: SequelDriver + 'static> Migrator<T> {

let filtered = available_migrations
.iter()
.filter(|s| completed_migrations.contains(s) == false)
.map(|s| s.to_owned())
.filter(|s| !completed_migrations.contains(s))
.map(std::borrow::ToOwned::to_owned)
.collect::<VecSerial>();

if filtered.is_empty() {
println!("Migrations are all up-to-date.");
return Ok(());
}

for it in filtered.iter() {
println!("[{:013}] Applying migration in the database.", it);
let migration = self.migrations.get(&it).unwrap();
for it in &filtered {
println!("[{it:013}] Applying migration in the database.");
let migration = self.migrations.get(it).unwrap();
let content_up = migration.content_up.as_ref().unwrap();
let content_up = get_content_string!(content_up);

Expand All @@ -116,16 +117,16 @@ impl<T: SequelDriver + 'static> Migrator<T> {
}

for it in completed_migrations.iter().rev() {
println!("[{:013}] Undo migration from database.", it);
let migration = self.migrations.get(&it).unwrap();
println!("[{it:013}] Undo migration from database.");
let migration = self.migrations.get(it).unwrap();
let content_down = migration.content_down.as_ref().unwrap();
let content_down = get_content_string!(content_down);

trace!("Running the following down query: {:?}", content_down);

self.executor.migrate(&content_down)?;

if !std::env::var("MIGRATIONS_SKIP_LAST").is_err() {
if std::env::var("MIGRATIONS_SKIP_LAST").is_ok() {
if !completed_migrations.first().eq(&Some(it)) {
self.executor.delete_completed_migration(it.to_owned())?;
}
Expand All @@ -149,8 +150,7 @@ impl<T: SequelDriver + 'static> Migrator<T> {

if current_state != -1 {
println!(
"[{:013}] Clearing recent migration from database.",
current
"[{current:013}] Clearing recent migration from database."
);
let content_down = migration.content_down.as_ref().unwrap();
let content_down = get_content_string!(content_down);
Expand All @@ -161,10 +161,7 @@ impl<T: SequelDriver + 'static> Migrator<T> {

trace!("Running the method `redo` {:?}", migration);

println!(
"[{:013}] Applying recent migration in the database.",
current
);
println!("[{current:013}] Applying recent migration in the database.");
let content_up = migration.content_up.as_ref().unwrap();
let content_up = get_content_string!(content_up);

Expand All @@ -184,14 +181,14 @@ impl<T: SequelDriver + 'static> Migrator<T> {
return Ok(());
}

println!("[{:013}] Reverting actions from last migration.", current);
println!("[{current:013}] Reverting actions from last migration.");
let migration = self.migrations.get(&current).unwrap();
let content_down = migration.content_down.as_ref().unwrap();
let content_down = get_content_string!(content_down);

self.executor.migrate(&content_down)?;

if !std::env::var("MIGRATIONS_SKIP_LAST").is_err() {
if std::env::var("MIGRATIONS_SKIP_LAST").is_ok() {
if migrations_count > 1 {
self.executor.delete_last_completed_migration()?;
}
Expand Down
19 changes: 9 additions & 10 deletions src/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ fn parse_file(filename: &str) -> Result<MigrationFile, super::GenericError> {

let res = match re.captures(filename) {
None => {
return Err(
format!("Invalid filename found on {}", filename).into()
)
return Err(format!("Invalid filename found on {filename}").into())
}
Some(c) => c,
};
Expand All @@ -61,20 +59,21 @@ pub fn build_migration_list(
for entry in read_dir(path)? {
let entry = entry?;
let filename = entry.file_name();
let info = match parse_file(
filename.to_str().ok_or("Filename is invalid")?,
) {
Ok(info) => info,
Err(_) => continue,
let Ok(info) =
parse_file(filename.to_str().ok_or("Filename is invalid")?)
else {
continue;
};

let file = File::open(entry.path())?;
let mut buf_reader = BufReader::new(file);
let mut content = String::new();
buf_reader.read_to_string(&mut content)?;

let split_vec: Vec<String> =
content.split("\n").map(|s| s.to_string()).collect();
let split_vec: Vec<String> = content
.split('\n')
.map(std::string::ToString::to_string)
.collect();

let pos_up = split_vec
.iter()
Expand Down
2 changes: 1 addition & 1 deletion src/midas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod sequel;

pub(crate) use cli::GenericError;

const PKG_NAME: &'static str = env!("CARGO_PKG_NAME");
const PKG_NAME: &str = env!("CARGO_PKG_NAME");

fn main() -> Result<(), GenericError> {
cli::midas_entry(PKG_NAME, false)?;
Expand Down
2 changes: 1 addition & 1 deletion src/sequel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod postgres;

pub type VecSerial = Vec<i64>;

pub trait SequelDriver {
pub trait Driver {
fn ensure_migration_schema_exists(&mut self) -> Result<(), Error>;
fn ensure_migration_table_exists(&mut self) -> Result<(), Error>;
fn drop_migration_table(&mut self) -> Result<(), Error>;
Expand Down
4 changes: 2 additions & 2 deletions src/sequel/postgres.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use log::trace;
use postgres::{Client, NoTls};

use super::{Error, SequelDriver, VecSerial};
use super::{Driver as SequelDriver, Error, VecSerial};

pub struct Postgres {
client: Client,
Expand Down Expand Up @@ -98,7 +98,7 @@ impl SequelDriver for Postgres {
}

fn migrate(&mut self, query: &str) -> Result<(), Error> {
self.client.simple_query(&query)?;
self.client.simple_query(query)?;
Ok(())
}
}

0 comments on commit f486b87

Please sign in to comment.