ohlcv_ctl/cli/command/drop.rs
1use std::path::PathBuf;
2
3use ohlcv::Database;
4use tracing::instrument;
5
6use crate::{
7 config::{CoinConfig, Config},
8 Error,
9};
10
11use super::root_credentials;
12
13/// Drop tables from the database.
14///
15/// # Arguments
16///
17/// * `all` - Whether to drop all tables. If false, only tables for the
18/// configured coins will be dropped.
19/// * `config` - Optional path to the configuration file. If not provided, the
20/// default configuration file will be used. This file is expected to be in
21/// TOML format. The default file is `ohlcv.toml` and is expected to be in the
22/// current working directory or in `/etc/ohlcv`.
23///
24/// # Errors
25///
26/// Returns an error if the tables cannot be dropped or if the configuration
27/// file cannot be loaded.
28#[instrument]
29pub async fn drop(all: bool, config: Option<&PathBuf>) -> Result<(), Error> {
30 let mut config = Config::load(config)?;
31 let creds = root_credentials(&config.database)?;
32
33 if all {
34 config.database.drop_schema(creds, None).await?;
35 } else {
36 let coins = config
37 .coins
38 .iter()
39 .map(CoinConfig::as_coin)
40 .collect::<Vec<_>>();
41
42 config
43 .database
44 .drop_schema(creds, Some(coins.as_slice()))
45 .await?;
46 }
47 Ok(())
48}