Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions crates/defguard/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,12 @@ async fn main() -> Result<(), anyhow::Error> {
}
}

// Reload settings from database after setup completion to ensure any changes made during setup are reflected in the in-memory settings.
// Reload settings from database after setup completion to ensure any changes made during setup
// are reflected in the in-memory settings.
let settings = Settings::get(&pool).await?.ok_or_else(|| {
anyhow::anyhow!(
"Failed to retrieve settings from database after setup completion. This should not happen."
"Failed to retrieve settings from database after setup completion. This should not \
happen."
)
})?;
update_current_settings(&pool, settings).await?;
Expand Down
29 changes: 11 additions & 18 deletions crates/defguard_common/src/db/models/wizard.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;

use serde::{Deserialize, Serialize};
use sqlx::{FromRow, PgExecutor, Type};
use sqlx::{PgExecutor, Type, query, query_as};
use tracing::{error, info};
use url::Url;

Expand All @@ -16,7 +16,7 @@ use crate::{
};

/// Which wizard is currently active. Stored as a PostgreSQL enum column.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Type)]
#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Serialize, Type)]
#[sqlx(type_name = "active_wizard", rename_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum ActiveWizard {
Expand All @@ -41,29 +41,23 @@ impl fmt::Display for ActiveWizard {
///
/// `active_wizard` and `completed` are regular DB columns.
/// Each wizard type has its own JSONB column for step-tracking state.
#[derive(Debug, Serialize)]
#[derive(Serialize)]
pub struct Wizard {
pub active_wizard: ActiveWizard,
pub completed: bool,
}

#[derive(Debug, FromRow)]
struct WizardDbRow {
active_wizard: ActiveWizard,
completed: bool,
}

impl Wizard {
pub async fn save<'e, E>(&self, executor: E) -> Result<(), sqlx::Error>
where
E: PgExecutor<'e>,
{
sqlx::query(
query!(
"UPDATE wizard SET active_wizard = $1, completed = $2 \
WHERE is_singleton",
WHERE is_singleton",
self.active_wizard as ActiveWizard,
self.completed
)
.bind(self.active_wizard)
.bind(self.completed)
.execute(executor)
.await?;

Expand All @@ -74,11 +68,10 @@ impl Wizard {
where
E: PgExecutor<'e>,
{
let row = sqlx::query_as::<_, WizardDbRow>(
"SELECT active_wizard, completed \
FROM wizard \
WHERE is_singleton \
LIMIT 1",
let row = query_as!(
Wizard,
"SELECT active_wizard \"active_wizard!: ActiveWizard\", completed \
FROM wizard WHERE is_singleton LIMIT 1",
)
.fetch_one(executor)
.await?;
Expand Down
Loading