diff --git a/.sqlx/query-311137f954e7d0919262bcd8eac047577055879335c91c1935d2cd2f0565a086.json b/.sqlx/query-311137f954e7d0919262bcd8eac047577055879335c91c1935d2cd2f0565a086.json new file mode 100644 index 000000000..61e4c9bdf --- /dev/null +++ b/.sqlx/query-311137f954e7d0919262bcd8eac047577055879335c91c1935d2cd2f0565a086.json @@ -0,0 +1,27 @@ +{ + "db_name": "PostgreSQL", + "query": "UPDATE wizard SET active_wizard = $1, completed = $2 WHERE is_singleton", + "describe": { + "columns": [], + "parameters": { + "Left": [ + { + "Custom": { + "name": "active_wizard", + "kind": { + "Enum": [ + "none", + "initial", + "auto_adoption", + "migration" + ] + } + } + }, + "Bool" + ] + }, + "nullable": [] + }, + "hash": "311137f954e7d0919262bcd8eac047577055879335c91c1935d2cd2f0565a086" +} diff --git a/.sqlx/query-75d092089964632abcf831388e1eb2b7b2626eee8c4426ba5c0797385f505b4d.json b/.sqlx/query-75d092089964632abcf831388e1eb2b7b2626eee8c4426ba5c0797385f505b4d.json new file mode 100644 index 000000000..30021c7d1 --- /dev/null +++ b/.sqlx/query-75d092089964632abcf831388e1eb2b7b2626eee8c4426ba5c0797385f505b4d.json @@ -0,0 +1,38 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT active_wizard \"active_wizard!: ActiveWizard\", completed FROM wizard WHERE is_singleton LIMIT 1", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "active_wizard!: ActiveWizard", + "type_info": { + "Custom": { + "name": "active_wizard", + "kind": { + "Enum": [ + "none", + "initial", + "auto_adoption", + "migration" + ] + } + } + } + }, + { + "ordinal": 1, + "name": "completed", + "type_info": "Bool" + } + ], + "parameters": { + "Left": [] + }, + "nullable": [ + false, + false + ] + }, + "hash": "75d092089964632abcf831388e1eb2b7b2626eee8c4426ba5c0797385f505b4d" +} diff --git a/crates/defguard/src/main.rs b/crates/defguard/src/main.rs index 051566f19..1e72f79fe 100644 --- a/crates/defguard/src/main.rs +++ b/crates/defguard/src/main.rs @@ -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?; diff --git a/crates/defguard_common/src/db/models/wizard.rs b/crates/defguard_common/src/db/models/wizard.rs index 34609bd6e..7d47633ec 100644 --- a/crates/defguard_common/src/db/models/wizard.rs +++ b/crates/defguard_common/src/db/models/wizard.rs @@ -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; @@ -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 { @@ -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?; @@ -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?;