Skip to content

Commit 169950f

Browse files
committed
feat(signer): add database migrate clap command
1 parent 0a04339 commit 169950f

File tree

4 files changed

+110
-14
lines changed

4 files changed

+110
-14
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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::{
10+
dependency_injection::DependenciesBuilder, Configuration, SQLITE_FILE,
11+
SQLITE_FILE_CARDANO_TRANSACTION,
12+
};
13+
14+
/// Database tools
15+
#[derive(Parser, Debug, Clone)]
16+
pub struct DatabaseCommand {
17+
/// commands
18+
#[clap(subcommand)]
19+
pub database_subcommand: DatabaseSubCommand,
20+
}
21+
22+
impl DatabaseCommand {
23+
/// Execute the database command
24+
pub async fn execute(&self, root_logger: Logger) -> StdResult<()> {
25+
self.database_subcommand.execute(root_logger).await
26+
}
27+
}
28+
29+
/// Database subcommands
30+
#[derive(Debug, Clone, Subcommand)]
31+
pub enum DatabaseSubCommand {
32+
/// Migrate databases located in the given stores directory
33+
Migrate(MigrateCommand),
34+
}
35+
36+
impl DatabaseSubCommand {
37+
/// Execute the database subcommand
38+
pub async fn execute(&self, root_logger: Logger) -> StdResult<()> {
39+
match self {
40+
Self::Migrate(cmd) => cmd.execute(root_logger).await,
41+
}
42+
}
43+
}
44+
45+
/// Migrate command
46+
#[derive(Parser, Debug, Clone)]
47+
pub struct MigrateCommand {
48+
/// Stores directory
49+
#[clap(long, env = "STORES_DIRECTORY")]
50+
stores_directory: PathBuf,
51+
}
52+
53+
impl MigrateCommand {
54+
/// Execute the migrate command
55+
pub async fn execute(&self, root_logger: Logger) -> StdResult<()> {
56+
let config = Configuration {
57+
data_stores_directory: self.stores_directory.clone(),
58+
// Temporary solution to avoid the need to provide a full configuration
59+
..Configuration::new_sample("0")
60+
};
61+
debug!(root_logger, "DATABASE MIGRATE command"; "config" => format!("{config:?}"));
62+
println!(
63+
"Migrating databases from stores directory: {}",
64+
self.stores_directory.to_string_lossy()
65+
);
66+
let services = DependenciesBuilder::new(&config, root_logger.clone());
67+
68+
services
69+
.build_sqlite_connection(SQLITE_FILE, crate::database::migration::get_migrations())
70+
.await
71+
.with_context(|| "Dependencies Builder can not get sqlite connection")?;
72+
73+
services
74+
.build_sqlite_connection(
75+
SQLITE_FILE_CARDANO_TRANSACTION,
76+
mithril_persistence::database::cardano_transaction_migration::get_migrations(),
77+
)
78+
.await
79+
.with_context(|| {
80+
"Dependencies Builder can not get cardano transaction pool sqlite connection"
81+
})?;
82+
83+
Ok(())
84+
}
85+
}

mithril-signer/src/commands/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod database_command;
2+
3+
pub use database_command::*;

mithril-signer/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//! See the [Mithril documentation](https://mithril.network/doc/manual/developer-docs/nodes/mithril-signer)
77
//! for more information on how it works.
88
9+
mod commands;
910
mod configuration;
1011
pub mod database;
1112
pub mod dependency_injection;
@@ -16,6 +17,7 @@ mod runtime;
1617
pub mod services;
1718
pub mod store;
1819

20+
pub use commands::*;
1921
pub use configuration::{Configuration, DefaultConfiguration};
2022
pub use entities::SignerEpochSettings;
2123
pub use message_adapters::{FromEpochSettingsAdapter, ToRegisterSignerMessageAdapter};

mithril-signer/src/main.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use tokio::{
1515
use mithril_common::StdResult;
1616
use mithril_doc::{Documenter, DocumenterDefault, GenerateDocCommands, StructDoc};
1717
use mithril_metric::MetricsServer;
18-
use mithril_signer::dependency_injection::DependenciesBuilder;
1918
use mithril_signer::{
20-
Configuration, DefaultConfiguration, SignerRunner, SignerState, StateMachine,
19+
dependency_injection::DependenciesBuilder, Configuration, DatabaseCommand,
20+
DefaultConfiguration, SignerRunner, SignerState, StateMachine,
2121
};
2222

2323
/// CLI args
@@ -115,6 +115,7 @@ fn build_logger(min_level: Level) -> Logger {
115115

116116
#[derive(Subcommand, Debug, Clone)]
117117
enum SignerCommands {
118+
Database(DatabaseCommand),
118119
#[clap(alias("doc"), hide(true))]
119120
GenerateDoc(GenerateDocCommands),
120121
}
@@ -125,21 +126,26 @@ async fn main() -> StdResult<()> {
125126
let args = Args::parse();
126127
let root_logger = build_logger(args.log_level());
127128

128-
if let Some(SignerCommands::GenerateDoc(cmd)) = &args.command {
129-
let config_infos = vec![
130-
Args::extract(),
131-
Configuration::extract(),
132-
DefaultConfiguration::extract(),
133-
];
134-
return cmd
135-
.execute_with_configurations(&mut Args::command(), &config_infos)
136-
.map_err(|message| anyhow!(message));
137-
}
138-
139129
debug!(root_logger, "Starting"; "node_version" => env!("CARGO_PKG_VERSION"));
140130

131+
if let Some(cmd) = &args.command {
132+
match cmd {
133+
SignerCommands::Database(cmd) => return cmd.execute(root_logger).await,
134+
SignerCommands::GenerateDoc(cmd) => {
135+
let config_infos = vec![
136+
Args::extract(),
137+
Configuration::extract(),
138+
DefaultConfiguration::extract(),
139+
];
140+
return cmd
141+
.execute_with_configurations(&mut Args::command(), &config_infos)
142+
.map_err(|message| anyhow!(message));
143+
}
144+
}
145+
};
146+
141147
// Load config
142-
let config: Configuration = config::Config::builder()
148+
let config = config::Config::builder()
143149
.set_default("disable_digests_cache", args.disable_digests_cache)
144150
.with_context(|| "configuration error: could not set `disable_digests_cache`")?
145151
.set_default("reset_digests_cache", args.reset_digests_cache)

0 commit comments

Comments
 (0)