|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use anyhow::Context; |
| 4 | +use clap::{Parser, Subcommand}; |
| 5 | +use slog::{debug, Logger}; |
| 6 | + |
| 7 | +use mithril_common::StdResult; |
| 8 | + |
| 9 | +use crate::{dependency_injection::DependenciesBuilder, Configuration, ExecutionEnvironment}; |
| 10 | + |
| 11 | +/// Database tools |
| 12 | +#[derive(Parser, Debug, Clone)] |
| 13 | +pub struct DatabaseCommand { |
| 14 | + /// commands |
| 15 | + #[clap(subcommand)] |
| 16 | + pub database_subcommand: DatabaseSubCommand, |
| 17 | +} |
| 18 | + |
| 19 | +impl DatabaseCommand { |
| 20 | + pub async fn execute(&self, root_logger: Logger) -> StdResult<()> { |
| 21 | + self.database_subcommand.execute(root_logger).await |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Debug, Clone, Subcommand)] |
| 26 | +pub enum DatabaseSubCommand { |
| 27 | + /// Migrate databases located in the given stores directory |
| 28 | + Migrate(MigrateCommand), |
| 29 | +} |
| 30 | + |
| 31 | +impl DatabaseSubCommand { |
| 32 | + pub async fn execute(&self, root_logger: Logger) -> StdResult<()> { |
| 33 | + match self { |
| 34 | + Self::Migrate(cmd) => cmd.execute(root_logger).await, |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +#[derive(Parser, Debug, Clone)] |
| 40 | +pub struct MigrateCommand { |
| 41 | + /// Stores directory |
| 42 | + #[clap(long, env = "STORES_DIRECTORY")] |
| 43 | + stores_directory: PathBuf, |
| 44 | +} |
| 45 | + |
| 46 | +impl MigrateCommand { |
| 47 | + pub async fn execute(&self, root_logger: Logger) -> StdResult<()> { |
| 48 | + let config = Configuration { |
| 49 | + environment: ExecutionEnvironment::Production, |
| 50 | + data_stores_directory: self.stores_directory.clone(), |
| 51 | + // Temporary solution to avoid the need to provide a full configuration |
| 52 | + ..Configuration::new_sample() |
| 53 | + }; |
| 54 | + debug!(root_logger, "DATABASE MIGRATE command"; "config" => format!("{config:?}")); |
| 55 | + println!( |
| 56 | + "Migrating databases from stores directory: {}", |
| 57 | + self.stores_directory.to_string_lossy() |
| 58 | + ); |
| 59 | + let mut dependencies_builder = |
| 60 | + DependenciesBuilder::new(root_logger.clone(), config.clone()); |
| 61 | + |
| 62 | + dependencies_builder |
| 63 | + .get_sqlite_connection() |
| 64 | + .await |
| 65 | + .with_context(|| "Dependencies Builder can not get sqlite connection")?; |
| 66 | + |
| 67 | + dependencies_builder |
| 68 | + .get_event_store_sqlite_connection() |
| 69 | + .await |
| 70 | + .with_context(|| "Dependencies Builder can not get event store sqlite connection")?; |
| 71 | + |
| 72 | + dependencies_builder |
| 73 | + .get_sqlite_connection_cardano_transaction_pool() |
| 74 | + .await |
| 75 | + .with_context(|| { |
| 76 | + "Dependencies Builder can not get cardano transaction pool sqlite connection" |
| 77 | + })?; |
| 78 | + |
| 79 | + Ok(()) |
| 80 | + } |
| 81 | +} |
0 commit comments