refactor: move migrations to db folder

This commit is contained in:
Max Hohlfeld 2025-06-27 12:11:17 +02:00
parent 9893c37f80
commit 93574c3ac5
9 changed files with 39 additions and 8 deletions

1
Cargo.lock generated
View File

@ -775,6 +775,7 @@ dependencies = [
"anyhow", "anyhow",
"async-std", "async-std",
"brass-config", "brass-config",
"chrono",
"clap", "clap",
"sqlx", "sqlx",
] ]

View File

@ -15,3 +15,4 @@ brass-config = { path = "../config" }
async-std = { version = "1.13.0", features = ["attributes"] } async-std = { version = "1.13.0", features = ["attributes"] }
sqlx = { version = "0.8.2", features = ["runtime-async-std", "postgres"] } sqlx = { version = "0.8.2", features = ["runtime-async-std", "postgres"] }
anyhow = "1.0.94" anyhow = "1.0.94"
chrono = "0.4.41"

View File

@ -1,6 +1,10 @@
use anyhow::Context; use anyhow::Context;
use chrono::{Local, NaiveDateTime, Utc};
use sqlx::migrate::Migrate; use sqlx::migrate::Migrate;
use sqlx::{migrate::Migrator, Executor}; use sqlx::{migrate::Migrator, Executor};
use std::fs::File;
use std::io::Write;
use std::time::SystemTime;
use std::{ use std::{
collections::HashMap, collections::HashMap,
path::{Path, PathBuf}, path::{Path, PathBuf},
@ -28,6 +32,8 @@ enum Command {
Reset, Reset,
#[command(about = "Run all pending migrations on database")] #[command(about = "Run all pending migrations on database")]
Migrate, Migrate,
#[command(about = "Create a new migration")]
NewMigration { title: String },
} }
#[async_std::main] #[async_std::main]
@ -59,12 +65,17 @@ async fn main() {
migrate_db(&db_config) migrate_db(&db_config)
.await .await
.expect("Failed migrating database."); .expect("Failed migrating database.");
}, }
Command::Migrate => { Command::Migrate => {
migrate_db(&db_config) migrate_db(&db_config)
.await .await
.expect("Failed migrating database."); .expect("Failed migrating database.");
} }
Command::NewMigration { title } => {
create_new_migration(&title)
.await
.expect("Failed creating new migration.");
}
} }
} }
@ -111,13 +122,7 @@ async fn migrate_db(db_config: &PgConnectOptions) -> anyhow::Result<()> {
.await .await
.context("Connection to database failed!")?; .context("Connection to database failed!")?;
let migrations_path = PathBuf::from( let migrations_path = db_package_root()?.join("migrations");
std::env::var("CARGO_MANIFEST_DIR").expect("This command needs to be invoked using cargo"),
)
.join("..")
.join("migrations")
.canonicalize()
.unwrap();
let migrator = Migrator::new(Path::new(&migrations_path)) let migrator = Migrator::new(Path::new(&migrations_path))
.await .await
@ -148,3 +153,27 @@ async fn migrate_db(db_config: &PgConnectOptions) -> anyhow::Result<()> {
Ok(()) Ok(())
} }
async fn create_new_migration(title: &str) -> anyhow::Result<()> {
let now = Local::now();
let timestamp = now.format("%Y%m%d%H%M%S");
let file_name = format!("{timestamp}_{title}.sql");
let path = db_package_root()?.join("migrations").join(&file_name);
let mut file = File::create(&path).context(format!(r#"Could not create file "{:?}""#, path))?;
file.write_all("".as_bytes())
.context(format!(r#"Could not write file "{:?}""#, path))?;
println!("Created migration {file_name}.");
Ok(())
}
fn db_package_root() -> Result<PathBuf, anyhow::Error> {
Ok(PathBuf::from(
std::env::var("CARGO_MANIFEST_DIR").expect("This command needs to be invoked using cargo"),
)
.join("..")
.join("db")
.canonicalize()?)
}