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
22pub async fn run_database_command(opt: DatabaseOpt) -> anyhow::Result<()> {
23 let command = opt.command.clone();
24
25 match command {
26 DatabaseCommand::Init(config) => {
27 let pg_connect_options = get_postgres_connect_options(
28 config.host,
29 config.port,
30 config.username,
31 config.password,
32 config.database,
33 );
34 let pg = connect_pg(pg_connect_options.clone().into()).await?;
35 log::info!(
36 "Connected with Postgres on url: {}",
37 pg_connect_options.connection_string()
38 );
39 init_postgres(
40 &pg,
41 pg_connect_options.database,
42 pg_connect_options.password,
43 config.schema,
44 )
45 .await?;
46 }
47 DatabaseCommand::Drop(config) => {
48 let pg_connect_options = get_postgres_connect_options(
49 config.host,
50 config.port,
51 config.username,
52 config.password,
53 config.database,
54 );
55 let pg = connect_pg(pg_connect_options.clone().into()).await?;
56 log::info!(
57 "Connected with Postgres on url: {}",
58 pg_connect_options.connection_string()
59 );
60 drop_postgres(&pg, pg_connect_options.database).await?;
61 }
62 }
63 Ok(())
64}