nautilus_cli/database/
postgres.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use nautilus_infrastructure::sql::pg::{
17    connect_pg, drop_postgres, get_postgres_connect_options, init_postgres,
18};
19
20use crate::opt::{DatabaseCommand, DatabaseOpt};
21
22/// Executes database management commands for PostgreSQL operations.
23///
24/// This function handles database initialization, schema setup, and database
25/// dropping operations based on the provided command options.
26///
27/// # Errors
28///
29/// Returns an error if:
30/// - Database connection fails
31/// - Schema initialization fails
32/// - Database dropping operation fails
33/// - Any PostgreSQL operation encounters an error
34pub async fn run_database_command(opt: DatabaseOpt) -> anyhow::Result<()> {
35    let command = opt.command.clone();
36
37    match command {
38        DatabaseCommand::Init(config) => {
39            let pg_connect_options = get_postgres_connect_options(
40                config.host,
41                config.port,
42                config.username,
43                config.password,
44                config.database,
45            );
46            let pg = connect_pg(pg_connect_options.clone().into()).await?;
47            log::info!(
48                "Connected with Postgres on url: {}",
49                pg_connect_options.connection_string()
50            );
51            init_postgres(
52                &pg,
53                pg_connect_options.database,
54                pg_connect_options.password,
55                config.schema,
56            )
57            .await?;
58        }
59        DatabaseCommand::Drop(config) => {
60            let pg_connect_options = get_postgres_connect_options(
61                config.host,
62                config.port,
63                config.username,
64                config.password,
65                config.database,
66            );
67            let pg = connect_pg(pg_connect_options.clone().into()).await?;
68            log::info!(
69                "Connected with Postgres on url: {}",
70                pg_connect_options.connection_string()
71            );
72            drop_postgres(&pg, pg_connect_options.database).await?;
73        }
74    }
75    Ok(())
76}