meta: add database migrations; set up migration CLI
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I0cbc9798243134d36f788099ecc3ee5a6a6a6964
This commit is contained in:
parent
63e1fbe554
commit
a4c3cd1517
9 changed files with 366 additions and 0 deletions
|
|
@ -1,7 +1,10 @@
|
|||
//! Common types and utilities for CI
|
||||
|
||||
pub mod error;
|
||||
pub mod migrate;
|
||||
pub mod migrate_cli;
|
||||
pub mod models;
|
||||
|
||||
pub use error::*;
|
||||
pub use migrate::*;
|
||||
pub use models::*;
|
||||
|
|
|
|||
69
crates/common/src/migrate.rs
Normal file
69
crates/common/src/migrate.rs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
//! Database migration utilities
|
||||
|
||||
use sqlx::{PgPool, Postgres, migrate::MigrateDatabase};
|
||||
use tracing::{error, info, warn};
|
||||
|
||||
/// Runs database migrations and ensures the database exists
|
||||
pub async fn run_migrations(database_url: &str) -> anyhow::Result<()> {
|
||||
info!("Starting database migrations");
|
||||
|
||||
// Check if database exists, create if it doesn't
|
||||
if !Postgres::database_exists(database_url).await? {
|
||||
warn!("Database does not exist, creating it");
|
||||
Postgres::create_database(database_url).await?;
|
||||
info!("Database created successfully");
|
||||
}
|
||||
|
||||
// Set up connection pool with retry logic, then run migrations
|
||||
let pool = create_connection_pool(database_url).await?;
|
||||
match sqlx::migrate!("./migrations").run(&pool).await {
|
||||
Ok(()) => {
|
||||
info!("Database migrations completed successfully");
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to run database migrations: {}", e);
|
||||
Err(anyhow::anyhow!("Migration failed: {e}"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a connection pool with proper configuration
|
||||
async fn create_connection_pool(database_url: &str) -> anyhow::Result<PgPool> {
|
||||
let pool = PgPool::connect(database_url).await?;
|
||||
|
||||
// Test the connection
|
||||
sqlx::query("SELECT 1").fetch_one(&pool).await?;
|
||||
|
||||
Ok(pool)
|
||||
}
|
||||
|
||||
/// Validates that all required tables exist and have the expected structure
|
||||
pub async fn validate_schema(pool: &PgPool) -> anyhow::Result<()> {
|
||||
info!("Validating database schema");
|
||||
|
||||
let required_tables = vec![
|
||||
"projects",
|
||||
"jobsets",
|
||||
"evaluations",
|
||||
"builds",
|
||||
"build_products",
|
||||
"build_steps",
|
||||
];
|
||||
|
||||
for table in required_tables {
|
||||
let result = sqlx::query_scalar::<_, i64>(
|
||||
"SELECT COUNT(*) FROM information_schema.tables WHERE table_name = $1",
|
||||
)
|
||||
.bind(table)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
|
||||
if result == 0 {
|
||||
return Err(anyhow::anyhow!("Required table '{table}' does not exist"));
|
||||
}
|
||||
}
|
||||
|
||||
info!("Database schema validation passed");
|
||||
Ok(())
|
||||
}
|
||||
85
crates/common/src/migrate_cli.rs
Normal file
85
crates/common/src/migrate_cli.rs
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
//! CLI utility for database migrations
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
use tracing::info;
|
||||
use tracing_subscriber::fmt::init;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "fc-migrate")]
|
||||
#[command(about = "Database migration utility for FC CI")]
|
||||
pub struct Cli {
|
||||
#[command(subcommand)]
|
||||
pub command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum Commands {
|
||||
/// Run all pending migrations
|
||||
Up {
|
||||
/// Database connection URL
|
||||
database_url: String,
|
||||
},
|
||||
/// Validate the current schema
|
||||
Validate {
|
||||
/// Database connection URL
|
||||
database_url: String,
|
||||
},
|
||||
/// Create a new migration file
|
||||
Create {
|
||||
/// Migration name
|
||||
#[arg(required = true)]
|
||||
name: String,
|
||||
},
|
||||
}
|
||||
|
||||
pub async fn run() -> anyhow::Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
// Initialize logging
|
||||
init();
|
||||
|
||||
match cli.command {
|
||||
Commands::Up { database_url } => {
|
||||
info!("Running database migrations");
|
||||
crate::run_migrations(&database_url).await?;
|
||||
info!("Migrations completed successfully");
|
||||
}
|
||||
Commands::Validate { database_url } => {
|
||||
info!("Validating database schema");
|
||||
let pool = sqlx::PgPool::connect(&database_url).await?;
|
||||
crate::validate_schema(&pool).await?;
|
||||
info!("Schema validation passed");
|
||||
}
|
||||
Commands::Create { name } => {
|
||||
create_migration(&name)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn create_migration(name: &str) -> anyhow::Result<()> {
|
||||
use chrono::Utc;
|
||||
use std::fs;
|
||||
|
||||
let timestamp = Utc::now().format("%Y%m%d_%H%M%S");
|
||||
let filename = format!("{timestamp}_{name}.sql");
|
||||
let filepath = format!("crates/common/migrations/{filename}");
|
||||
|
||||
let content = format!(
|
||||
"-- Migration: {}\n\
|
||||
-- Created: {}\n\
|
||||
\n\
|
||||
-- Add your migration SQL here\n\
|
||||
\n\
|
||||
-- Uncomment below for rollback SQL\n\
|
||||
-- ROLLBACK;\n",
|
||||
name,
|
||||
Utc::now().to_rfc3339()
|
||||
);
|
||||
|
||||
fs::write(&filepath, content)?;
|
||||
println!("Created migration file: {filepath}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue