drop migration lock emergency helper command

This commit is contained in:
Bryan Stitt 2022-11-14 19:13:42 +00:00
parent bcf7f9183d
commit ee35c15ff4
2 changed files with 32 additions and 10 deletions

View File

@ -29,7 +29,7 @@ use log::{debug, error, info, warn};
use metered::{metered, ErrorCount, HitCount, ResponseTime, Throughput};
use migration::sea_orm::{self, ConnectionTrait, Database, DatabaseConnection};
use migration::sea_query::table::ColumnDef;
use migration::{Alias, Migrator, MigratorTrait, Table};
use migration::{Alias, DbErr, Migrator, MigratorTrait, Table};
use moka::future::Cache;
use redis_rate_limiter::{DeadpoolRuntime, RedisConfig, RedisPool, RedisRateLimiter};
use serde::Serialize;
@ -149,12 +149,11 @@ pub async fn flatten_handles<T>(
Ok(())
}
/// Connect to the database and run migrations
pub async fn get_migrated_db(
pub async fn get_db(
db_url: String,
min_connections: u32,
max_connections: u32,
) -> anyhow::Result<DatabaseConnection> {
) -> Result<DatabaseConnection, DbErr> {
// TODO: scrub credentials and then include the db_url in logs
info!("Connecting to db");
@ -169,19 +168,35 @@ pub async fn get_migrated_db(
.sqlx_logging(false);
// .sqlx_logging_level(log::LevelFilter::Info);
let db_conn = Database::connect(db_opt).await?;
Database::connect(db_opt).await
}
pub async fn drop_migration_lock(db_conn: &DatabaseConnection) -> Result<(), DbErr> {
let db_backend = db_conn.get_database_backend();
let migration_lock_table_ref = Alias::new("migration_lock");
let drop_lock_statment = db_backend.build(Table::drop().table(Alias::new("migration_lock")));
db_conn.execute(drop_lock_statment).await?;
Ok(())
}
/// Connect to the database and run migrations
pub async fn get_migrated_db(
db_url: String,
min_connections: u32,
max_connections: u32,
) -> anyhow::Result<DatabaseConnection> {
let db_conn = get_db(db_url, min_connections, max_connections).await?;
let db_backend = db_conn.get_database_backend();
// TODO: put the timestamp into this?
let create_lock_statment = db_backend.build(
Table::create()
.table(migration_lock_table_ref.clone())
.table(Alias::new("migration_lock"))
.col(ColumnDef::new(Alias::new("locked")).boolean().default(true)),
);
let drop_lock_statment = db_backend.build(Table::drop().table(migration_lock_table_ref));
loop {
if Migrator::get_pending_migrations(&db_conn).await?.is_empty() {
@ -207,7 +222,7 @@ pub async fn get_migrated_db(
let migration_result = Migrator::up(&db_conn, None).await;
// drop the distributed lock
db_conn.execute(drop_lock_statment).await?;
drop_migration_lock(&db_conn).await?;
// return if migrations erred
migration_result?;

View File

@ -1,8 +1,9 @@
mod check_config;
mod clear_migration_lock;
mod create_user;
use argh::FromArgs;
use web3_proxy::app::get_migrated_db;
use web3_proxy::app::{get_db, get_migrated_db};
#[derive(Debug, FromArgs)]
/// Command line interface for admins to interact with web3_proxy
@ -24,6 +25,7 @@ pub struct TopConfig {
enum SubCommand {
CreateUser(create_user::CreateUserSubCommand),
CheckConfig(check_config::CheckConfigSubCommand),
DropMigrationLock(clear_migration_lock::DropMigrationLockSubCommand),
// TODO: sub command to downgrade migrations?
// TODO: sub command to add new api keys to an existing user?
}
@ -51,5 +53,10 @@ async fn main() -> anyhow::Result<()> {
x.main(&db_conn).await
}
SubCommand::CheckConfig(x) => x.main().await,
SubCommand::DropMigrationLock(x) => {
let db_conn = get_db(cli_config.db_url, 1, 1).await?;
x.main(&db_conn).await
}
}
}