From ee35c15ff4641898e1f465f4cc546fda632f6058 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Mon, 14 Nov 2022 19:13:42 +0000 Subject: [PATCH] drop migration lock emergency helper command --- web3_proxy/src/app.rs | 33 ++++++++++++++++------- web3_proxy/src/bin/web3_proxy_cli/main.rs | 9 ++++++- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/web3_proxy/src/app.rs b/web3_proxy/src/app.rs index 8f13ddf3..c92fda8e 100644 --- a/web3_proxy/src/app.rs +++ b/web3_proxy/src/app.rs @@ -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( 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 { +) -> Result { // 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 { + 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?; diff --git a/web3_proxy/src/bin/web3_proxy_cli/main.rs b/web3_proxy/src/bin/web3_proxy_cli/main.rs index 49d3a1b6..96f6a65e 100644 --- a/web3_proxy/src/bin/web3_proxy_cli/main.rs +++ b/web3_proxy/src/bin/web3_proxy_cli/main.rs @@ -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 + } } }