Skip to content

Commit 0a04339

Browse files
committed
feat(aggregator): add database migrate clap command
1 parent 3cea9b8 commit 0a04339

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}

mithril-aggregator/src/commands/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod database_command;
12
mod era_command;
23
mod genesis_command;
34
mod serve_command;
@@ -21,6 +22,7 @@ pub enum MainCommand {
2122
Era(era_command::EraCommand),
2223
Serve(serve_command::ServeCommand),
2324
Tools(tools_command::ToolsCommand),
25+
Database(database_command::DatabaseCommand),
2426
#[clap(alias("doc"), hide(true))]
2527
GenerateDoc(GenerateDocCommands),
2628
}
@@ -44,6 +46,7 @@ impl MainCommand {
4446
Self::Era(cmd) => cmd.execute(root_logger, config_builder).await,
4547
Self::Serve(cmd) => cmd.execute(root_logger, config_builder).await,
4648
Self::Tools(cmd) => cmd.execute(root_logger, config_builder).await,
49+
Self::Database(cmd) => cmd.execute(root_logger).await,
4750
Self::GenerateDoc(cmd) => {
4851
let config_infos = vec![Configuration::extract(), DefaultConfiguration::extract()];
4952
cmd.execute_with_configurations(&mut MainOpts::command(), &config_infos)
@@ -58,6 +61,7 @@ impl MainCommand {
5861
MainCommand::Genesis(_) => CommandType::CommandLine,
5962
MainCommand::Era(_) => CommandType::CommandLine,
6063
MainCommand::Tools(_) => CommandType::CommandLine,
64+
MainCommand::Database(_) => CommandType::CommandLine,
6165
MainCommand::GenerateDoc(_) => CommandType::CommandLine,
6266
}
6367
}

0 commit comments

Comments
 (0)