From de36bb07246f88b942eefc83b9d9a81406b740c2 Mon Sep 17 00:00:00 2001 From: iven Date: Sun, 5 Apr 2026 08:19:10 +0800 Subject: [PATCH] fix(saas): migration idempotency fixes + SCHEMA_VERSION bump to 14 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add IF NOT EXISTS to accounts_template_assignment ALTER COLUMN - Add IF NOT EXISTS to webhooks CREATE INDEX statements - Add created_at/updated_at columns + ON CONFLICT DO NOTHING to industry templates - Bump SCHEMA_VERSION 13→14 to force migration re-run on existing DB --- ...403000001_accounts_template_assignment.sql | 2 +- .../migrations/20260403000002_webhooks.sql | 6 ++--- ...0260405000001_industry_agent_templates.sql | 24 ++++++++++++------- crates/zclaw-saas/src/db.rs | 2 +- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/crates/zclaw-saas/migrations/20260403000001_accounts_template_assignment.sql b/crates/zclaw-saas/migrations/20260403000001_accounts_template_assignment.sql index 883699d..81c263b 100644 --- a/crates/zclaw-saas/migrations/20260403000001_accounts_template_assignment.sql +++ b/crates/zclaw-saas/migrations/20260403000001_accounts_template_assignment.sql @@ -2,7 +2,7 @@ -- 用户选择行业模板后记录分配关系,用于跟踪和跳过 onboarding -- ON DELETE SET NULL: 模板被删除时不影响账户 -ALTER TABLE accounts ADD COLUMN assigned_template_id TEXT +ALTER TABLE accounts ADD COLUMN IF NOT EXISTS assigned_template_id TEXT REFERENCES agent_templates(id) ON DELETE SET NULL; COMMENT ON COLUMN accounts.assigned_template_id IS diff --git a/crates/zclaw-saas/migrations/20260403000002_webhooks.sql b/crates/zclaw-saas/migrations/20260403000002_webhooks.sql index 6934fb9..3e7a92d 100644 --- a/crates/zclaw-saas/migrations/20260403000002_webhooks.sql +++ b/crates/zclaw-saas/migrations/20260403000002_webhooks.sql @@ -23,6 +23,6 @@ CREATE TABLE IF NOT EXISTS webhook_deliveries ( created_at TIMESTAMPTZ NOT NULL DEFAULT now() ); -CREATE INDEX idx_webhook_subscriptions_account ON webhook_subscriptions(account_id); -CREATE INDEX idx_webhook_subscriptions_events ON webhook_subscriptions USING gin(events); -CREATE INDEX idx_webhook_deliveries_pending ON webhook_deliveries(subscription_id) WHERE delivered_at IS NULL; +CREATE INDEX IF NOT EXISTS idx_webhook_subscriptions_account ON webhook_subscriptions(account_id); +CREATE INDEX IF NOT EXISTS idx_webhook_subscriptions_events ON webhook_subscriptions USING gin(events); +CREATE INDEX IF NOT EXISTS idx_webhook_deliveries_pending ON webhook_deliveries(subscription_id) WHERE delivered_at IS NULL; diff --git a/crates/zclaw-saas/migrations/20260405000001_industry_agent_templates.sql b/crates/zclaw-saas/migrations/20260405000001_industry_agent_templates.sql index ba86cb5..11155d2 100644 --- a/crates/zclaw-saas/migrations/20260405000001_industry_agent_templates.sql +++ b/crates/zclaw-saas/migrations/20260405000001_industry_agent_templates.sql @@ -10,7 +10,8 @@ INSERT INTO agent_templates ( model, system_prompt, tools, capabilities, temperature, max_tokens, visibility, status, current_version, soul_content, scenarios, welcome_message, quick_commands, - personality, communication_style, source_id, version + personality, communication_style, source_id, version, + created_at, updated_at ) VALUES ( 'edu-teacher-001', '教学助手', @@ -55,8 +56,9 @@ INSERT INTO agent_templates ( 'friendly', '温暖、耐心、善于用易懂的语言解释复杂概念', 'education-teacher', - 1 -); + 1, + NOW(), NOW() +) ON CONFLICT (id) DO NOTHING; -- ============================================================ -- 2. Healthcare — 医疗行政助手 @@ -66,7 +68,8 @@ INSERT INTO agent_templates ( model, system_prompt, tools, capabilities, temperature, max_tokens, visibility, status, current_version, soul_content, scenarios, welcome_message, quick_commands, - personality, communication_style, source_id, version + personality, communication_style, source_id, version, + created_at, updated_at ) VALUES ( 'healthcare-admin-001', '医疗行政助手', @@ -111,8 +114,9 @@ INSERT INTO agent_templates ( 'professional', '专业、准确、注重细节,提供技术深度和可操作建议', 'healthcare-admin', - 1 -); + 1, + NOW(), NOW() +) ON CONFLICT (id) DO NOTHING; -- ============================================================ -- 3. Design (Shantou) — 设计助手 @@ -122,7 +126,8 @@ INSERT INTO agent_templates ( model, system_prompt, tools, capabilities, temperature, max_tokens, visibility, status, current_version, soul_content, scenarios, welcome_message, quick_commands, - personality, communication_style, source_id, version + personality, communication_style, source_id, version, + created_at, updated_at ) VALUES ( 'design-shantou-001', '设计助手', @@ -167,5 +172,6 @@ INSERT INTO agent_templates ( 'creative', '富有创意、思维开放,鼓励探索新想法和解决方案', 'design-shantou', - 1 -); + 1, + NOW(), NOW() +) ON CONFLICT (id) DO NOTHING; diff --git a/crates/zclaw-saas/src/db.rs b/crates/zclaw-saas/src/db.rs index 286b64c..f398d55 100644 --- a/crates/zclaw-saas/src/db.rs +++ b/crates/zclaw-saas/src/db.rs @@ -5,7 +5,7 @@ use sqlx::PgPool; use crate::config::DatabaseConfig; use crate::error::SaasResult; -const SCHEMA_VERSION: i32 = 13; +const SCHEMA_VERSION: i32 = 14; /// 初始化数据库 pub async fn init_db(config: &DatabaseConfig) -> SaasResult {