From 02c36a88882f2dda6eba9d916fe5e3a372c1b613 Mon Sep 17 00:00:00 2001 From: "Rodrigo Rodriguez (Pragmatismo)" Date: Wed, 5 Nov 2025 08:47:28 -0300 Subject: [PATCH] feat(db): add bot_id column, constraints, enforce DATABASE_URL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Extend `system_automations` with a non‑null `bot_id` UUID column, create an index on it, and add a unique constraint on `(bot_id, kind, param)` to support upserts. - Add a unique constraint on `bot_configuration.config_key` to prevent duplicate configuration keys. - Include migration guards to ensure the new constraint is only created once. - Remove automatic writing of drive configuration to a `.env` file, cleaning up side‑effects during config loading. - Change database connection handling to require `DATABASE_URL` to be set (no fallback), making the environment initialization explicit. --- migrations/6.0.10.sql | 15 --------------- migrations/6.0.5.sql | 36 ++++++++++++++++++++++++++++++++++++ migrations/6.0.8.sql | 2 -- migrations/6.0.9.sql | 16 ---------------- src/config/mod.rs | 5 ----- src/main.rs | 4 +++- src/shared/schema.rs | 1 + 7 files changed, 40 insertions(+), 39 deletions(-) delete mode 100644 migrations/6.0.10.sql delete mode 100644 migrations/6.0.8.sql delete mode 100644 migrations/6.0.9.sql create mode 100644 src/shared/schema.rs diff --git a/migrations/6.0.10.sql b/migrations/6.0.10.sql deleted file mode 100644 index 577572546..000000000 --- a/migrations/6.0.10.sql +++ /dev/null @@ -1,15 +0,0 @@ --- Migration 6.0.10: Add unique constraint for system_automations upsert --- Description: Creates a unique constraint matching the ON CONFLICT target in set_schedule.rs - -DO $$ -BEGIN - IF NOT EXISTS ( - SELECT 1 FROM pg_constraint - WHERE conname = 'system_automations_bot_kind_param_unique' - ) THEN - ALTER TABLE public.system_automations - ADD CONSTRAINT system_automations_bot_kind_param_unique - UNIQUE (bot_id, kind, param); - END IF; -END -$$; diff --git a/migrations/6.0.5.sql b/migrations/6.0.5.sql index f8f952001..84a262451 100644 --- a/migrations/6.0.5.sql +++ b/migrations/6.0.5.sql @@ -8,3 +8,39 @@ ALTER TABLE public.system_automations ADD COLUMN IF NOT EXISTS name VARCHAR(255) -- Create index on name column for faster lookups CREATE INDEX IF NOT EXISTS idx_system_automations_name ON public.system_automations(name); + +ALTER TABLE bot_configuration +ADD CONSTRAINT bot_configuration_config_key_unique UNIQUE (config_key); + +-- Migration 6.0.9: Add bot_id column to system_automations +-- Description: Introduces a bot_id column to associate automations with a specific bot. +-- The column is added as UUID and indexed for efficient queries. + +-- Add bot_id column if it does not exist +ALTER TABLE public.system_automations +ADD COLUMN IF NOT EXISTS bot_id UUID NOT NULL; + +-- Create an index on bot_id for faster lookups +CREATE INDEX IF NOT EXISTS idx_system_automations_bot_id +ON public.system_automations (bot_id); + + +ALTER TABLE public.system_automations +ADD CONSTRAINT IF NOT EXISTS system_automations_bot_kind_param_unique +UNIQUE (bot_id, kind, param); + +-- Migration 6.0.10: Add unique constraint for system_automations upsert +-- Description: Creates a unique constraint matching the ON CONFLICT target in set_schedule.rs + +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'system_automations_bot_kind_param_unique' + ) THEN + ALTER TABLE public.system_automations + ADD CONSTRAINT system_automations_bot_kind_param_unique + UNIQUE (bot_id, kind, param); + END IF; +END +$$; diff --git a/migrations/6.0.8.sql b/migrations/6.0.8.sql deleted file mode 100644 index 5850b86d1..000000000 --- a/migrations/6.0.8.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE bot_configuration -ADD CONSTRAINT bot_configuration_config_key_unique UNIQUE (config_key); diff --git a/migrations/6.0.9.sql b/migrations/6.0.9.sql deleted file mode 100644 index acdd9ee52..000000000 --- a/migrations/6.0.9.sql +++ /dev/null @@ -1,16 +0,0 @@ --- Migration 6.0.9: Add bot_id column to system_automations --- Description: Introduces a bot_id column to associate automations with a specific bot. --- The column is added as UUID and indexed for efficient queries. - --- Add bot_id column if it does not exist -ALTER TABLE public.system_automations -ADD COLUMN IF NOT EXISTS bot_id UUID NOT NULL; - --- Create an index on bot_id for faster lookups -CREATE INDEX IF NOT EXISTS idx_system_automations_bot_id -ON public.system_automations (bot_id); - - -ALTER TABLE public.system_automations -ADD CONSTRAINT IF NOT EXISTS system_automations_bot_kind_param_unique -UNIQUE (bot_id, kind, param); diff --git a/src/config/mod.rs b/src/config/mod.rs index a31c85f76..c8b72c125 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -132,11 +132,6 @@ impl AppConfig { use_ssl: get_bool("DRIVE_USE_SSL", false), }; - // Write drive config to .env file - if let Err(e) = write_drive_config_to_env(&drive) { - warn!("Failed to write drive config to .env: {}", e); - } - Ok(AppConfig { drive, server: ServerConfig { diff --git a/src/main.rs b/src/main.rs index 6d5c3c3dc..3e1612cf5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -109,9 +109,11 @@ async fn main() -> std::io::Result<()> { let env_path = std::env::current_dir()?.join("botserver-stack").join(".env"); let cfg = if env_path.exists() { info!("Environment already initialized, skipping bootstrap"); + + match diesel::Connection::establish( &std::env::var("DATABASE_URL") - .unwrap_or_else(|_| "postgres://gbuser:@localhost:5432/botserver".to_string()), + .unwrap() ) { Ok(mut conn) => AppConfig::from_database(&mut conn).expect("Failed to load config from DB"), Err(_) => AppConfig::from_env().expect("Failed to load config from env"), diff --git a/src/shared/schema.rs b/src/shared/schema.rs new file mode 100644 index 000000000..d9a52af7e --- /dev/null +++ b/src/shared/schema.rs @@ -0,0 +1 @@ +// @generated automatically by Diesel CLI.