server: Create schema command
This commit is contained in:
@@ -26,6 +26,9 @@ pub enum Command {
|
||||
/// Send a test email.
|
||||
#[clap(name = "send_test_email")]
|
||||
SendTestEmail(TestEmailOpts),
|
||||
/// Create database schema.
|
||||
#[clap(name = "create_schema")]
|
||||
CreateSchema(RunOpts),
|
||||
}
|
||||
|
||||
#[derive(Debug, Parser, Clone)]
|
||||
@@ -74,6 +77,10 @@ pub struct RunOpts {
|
||||
#[clap(long, env = "LLDAP_HTTP_URL")]
|
||||
pub http_url: Option<String>,
|
||||
|
||||
/// Database connection URL
|
||||
#[clap(short, long, env = "LLDAP_DATABASE_URL")]
|
||||
pub database_url: Option<String>,
|
||||
|
||||
#[clap(flatten)]
|
||||
pub smtp_opts: SmtpOpts,
|
||||
|
||||
|
||||
@@ -209,6 +209,10 @@ impl ConfigOverrider for RunOpts {
|
||||
if let Some(url) = self.http_url.as_ref() {
|
||||
config.http_url = url.to_string();
|
||||
}
|
||||
|
||||
if let Some(database_url) = self.database_url.as_ref() {
|
||||
config.database_url = database_url.to_string();
|
||||
}
|
||||
self.smtp_opts.override_config(config);
|
||||
self.ldaps_opts.override_config(config);
|
||||
}
|
||||
|
||||
@@ -189,6 +189,38 @@ fn run_healthcheck(opts: RunOpts) -> Result<()> {
|
||||
std::process::exit(i32::from(failure))
|
||||
}
|
||||
|
||||
async fn create_schema(database_url: String) -> Result<()> {
|
||||
let sql_pool = {
|
||||
let mut sql_opt = sea_orm::ConnectOptions::new(database_url.clone());
|
||||
sql_opt
|
||||
.max_connections(1)
|
||||
.sqlx_logging(true)
|
||||
.sqlx_logging_level(log::LevelFilter::Debug);
|
||||
Database::connect(sql_opt).await?
|
||||
};
|
||||
domain::sql_tables::init_table(&sql_pool)
|
||||
.await
|
||||
.context("while creating base tables")?;
|
||||
infra::jwt_sql_tables::init_table(&sql_pool)
|
||||
.await
|
||||
.context("while creating jwt tables")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn create_schema_command(opts: RunOpts) -> Result<()> {
|
||||
debug!("CLI: {:#?}", &opts);
|
||||
let config = infra::configuration::init(opts)?;
|
||||
infra::logging::init(&config)?;
|
||||
let database_url = config.database_url;
|
||||
|
||||
actix::run(
|
||||
create_schema(database_url).unwrap_or_else(|e| error!("Could not create schema: {:#}", e)),
|
||||
)?;
|
||||
|
||||
info!("Schema created successfully.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let cli_opts = infra::cli::init();
|
||||
match cli_opts.command {
|
||||
@@ -196,5 +228,6 @@ fn main() -> Result<()> {
|
||||
Command::Run(opts) => run_server_command(opts),
|
||||
Command::HealthCheck(opts) => run_healthcheck(opts),
|
||||
Command::SendTestEmail(opts) => send_test_email_command(opts),
|
||||
Command::CreateSchema(opts) => create_schema_command(opts),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user