From 1a9eddb6aa543f2cd12caa17b346e9549e40f939 Mon Sep 17 00:00:00 2001 From: Ludo Galabru Date: Wed, 21 Jun 2023 11:14:06 -0400 Subject: [PATCH] feat: always try to initialize tables when starting service --- components/chainhook-cli/src/cli/mod.rs | 3 ++ components/chainhook-sdk/src/hord/db/mod.rs | 56 ++++++++++----------- components/chainhook-sdk/src/hord/mod.rs | 7 ++- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/components/chainhook-cli/src/cli/mod.rs b/components/chainhook-cli/src/cli/mod.rs index 1511b318..c2ce1f6d 100644 --- a/components/chainhook-cli/src/cli/mod.rs +++ b/components/chainhook-cli/src/cli/mod.rs @@ -441,6 +441,9 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> { if cmd.predicates_paths.len() > 0 && !cmd.start_http_api { config.http_api = PredicatesApi::Off; } + + let _ = initialize_hord_db(&config.expected_cache_path(), &ctx); + let predicates = cmd .predicates_paths .iter() diff --git a/components/chainhook-sdk/src/hord/db/mod.rs b/components/chainhook-sdk/src/hord/db/mod.rs index 7c19552e..8b9ca4cf 100644 --- a/components/chainhook-sdk/src/hord/db/mod.rs +++ b/components/chainhook-sdk/src/hord/db/mod.rs @@ -68,36 +68,36 @@ pub fn initialize_hord_db(path: &PathBuf, ctx: &Context) -> Connection { )", [], ) { - ctx.try_log(|logger| slog::error!(logger, "{}", e.to_string())); - } - if let Err(e) = conn.execute( - "CREATE TABLE IF NOT EXISTS transfers ( - block_height INTEGER NOT NULL PRIMARY KEY - )", - [], - ) { - ctx.try_log(|logger| slog::error!(logger, "{}", e.to_string())); - } + ctx.try_log(|logger| slog::warn!(logger, "{}", e.to_string())); + } else { + if let Err(e) = conn.execute( + "CREATE TABLE IF NOT EXISTS transfers ( + block_height INTEGER NOT NULL PRIMARY KEY + )", + [], + ) { + ctx.try_log(|logger| slog::warn!(logger, "{}", e.to_string())); + } - if let Err(e) = conn.execute( - "CREATE INDEX IF NOT EXISTS index_inscriptions_on_outpoint_to_watch ON inscriptions(outpoint_to_watch);", - [], - ) { - ctx.try_log(|logger| slog::error!(logger, "{}", e.to_string())); - } - if let Err(e) = conn.execute( - "CREATE INDEX IF NOT EXISTS index_inscriptions_on_ordinal_number ON inscriptions(ordinal_number);", - [], - ) { - ctx.try_log(|logger| slog::error!(logger, "{}", e.to_string())); - } - if let Err(e) = conn.execute( - "CREATE INDEX IF NOT EXISTS index_inscriptions_on_block_height ON inscriptions(block_height);", - [], - ) { - ctx.try_log(|logger| slog::error!(logger, "{}", e.to_string())); + if let Err(e) = conn.execute( + "CREATE INDEX IF NOT EXISTS index_inscriptions_on_outpoint_to_watch ON inscriptions(outpoint_to_watch);", + [], + ) { + ctx.try_log(|logger| slog::warn!(logger, "{}", e.to_string())); + } + if let Err(e) = conn.execute( + "CREATE INDEX IF NOT EXISTS index_inscriptions_on_ordinal_number ON inscriptions(ordinal_number);", + [], + ) { + ctx.try_log(|logger| slog::warn!(logger, "{}", e.to_string())); + } + if let Err(e) = conn.execute( + "CREATE INDEX IF NOT EXISTS index_inscriptions_on_block_height ON inscriptions(block_height);", + [], + ) { + ctx.try_log(|logger| slog::warn!(logger, "{}", e.to_string())); + } } - conn } diff --git a/components/chainhook-sdk/src/hord/mod.rs b/components/chainhook-sdk/src/hord/mod.rs index e98730dd..f6aac2c1 100644 --- a/components/chainhook-sdk/src/hord/mod.rs +++ b/components/chainhook-sdk/src/hord/mod.rs @@ -41,10 +41,9 @@ use crate::{ use self::db::{ find_inscription_with_id, find_latest_cursed_inscription_number_at_block_height, - find_latest_inscription_number_at_block_height, - parse_satpoint_to_watch, remove_entry_from_blocks, - remove_entry_from_inscriptions, LazyBlock, LazyBlockTransaction, TraversalResult, - WatchedSatpoint, + find_latest_inscription_number_at_block_height, parse_satpoint_to_watch, + remove_entry_from_blocks, remove_entry_from_inscriptions, LazyBlock, LazyBlockTransaction, + TraversalResult, WatchedSatpoint, }; use self::inscription::InscriptionParser; use self::ord::inscription_id::InscriptionId;