fix(saas): migration idempotency fixes + SCHEMA_VERSION bump to 14
Some checks failed
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Rust Check (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled

- 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
This commit is contained in:
iven
2026-04-05 08:19:10 +08:00
parent af0acff2aa
commit de36bb0724
4 changed files with 20 additions and 14 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<PgPool> {