Skip to content

Commit

Permalink
update cli and database opt with schema dir path
Browse files Browse the repository at this point in the history
  • Loading branch information
filipmacek committed Apr 25, 2024
1 parent 0016f7b commit c5c2c73
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions nautilus_core/cli/src/database/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub async fn run_database_command(opt: DatabaseOpt) -> anyhow::Result<()> {
&pg,
pg_connect_options.database,
pg_connect_options.password,
config.schema,
)
.await?
}
Expand Down
3 changes: 3 additions & 0 deletions nautilus_core/cli/src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ pub struct DatabaseConfig {
/// Password for connecting to the database
#[arg(long)]
pub password: Option<String>,
/// Directory path to the schema files
#[arg(long)]
pub schema: Option<String>,
}

#[derive(Parser, Debug, Clone)]
Expand Down
9 changes: 7 additions & 2 deletions nautilus_core/infrastructure/src/sql/pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ fn get_schema_dir() -> anyhow::Result<String> {
})
}

pub async fn init_postgres(pg: &PgPool, database: String, password: String) -> anyhow::Result<()> {
pub async fn init_postgres(
pg: &PgPool,
database: String,
password: String,
schema_dir: Option<String>,
) -> anyhow::Result<()> {
info!("Initializing Postgres database with target permissions and schema");
// create public schema
match sqlx::query("CREATE SCHEMA IF NOT EXISTS public;")
Expand All @@ -155,7 +160,7 @@ pub async fn init_postgres(pg: &PgPool, database: String, password: String) -> a
}
}
// execute all the sql files in schema dir
let schema_dir = get_schema_dir()?;
let schema_dir = schema_dir.unwrap_or_else(|| get_schema_dir().unwrap());
let mut sql_files =
std::fs::read_dir(schema_dir)?.collect::<Result<Vec<_>, std::io::Error>>()?;
for file in &mut sql_files {
Expand Down

0 comments on commit c5c2c73

Please sign in to comment.