mithril_signer/commands/
database_command.rs

1use std::path::PathBuf;
2
3use anyhow::Context;
4use clap::{Parser, Subcommand};
5use slog::{debug, Logger};
6
7use mithril_common::StdResult;
8
9use crate::{
10    dependency_injection::DependenciesBuilder, Configuration, SQLITE_FILE,
11    SQLITE_FILE_CARDANO_TRANSACTION,
12};
13
14/// Database tools
15#[derive(Parser, Debug, Clone)]
16pub struct DatabaseCommand {
17    /// commands
18    #[clap(subcommand)]
19    pub database_subcommand: DatabaseSubCommand,
20}
21
22impl 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)]
31pub enum DatabaseSubCommand {
32    /// Migrate databases located in the given stores directory
33    Migrate(MigrateCommand),
34}
35
36impl 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)]
47pub struct MigrateCommand {
48    /// Stores directory
49    #[clap(long, env = "STORES_DIRECTORY")]
50    stores_directory: PathBuf,
51}
52
53impl 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}