diff --git a/server/migrations/000001_init_schema.up.sql b/server/migrations/000001_init_schema.up.sql index 8400ce2..1e9de64 100644 --- a/server/migrations/000001_init_schema.up.sql +++ b/server/migrations/000001_init_schema.up.sql @@ -55,9 +55,9 @@ CREATE TABLE IF NOT EXISTS tweets ( created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP ); -CREATE INDEX idx_tweets_x_tweet_id ON tweets(x_tweet_id); -CREATE INDEX idx_tweets_heat_score ON tweets(heat_score DESC); -CREATE INDEX idx_tweets_crawl_queue ON tweets(crawl_queue, last_crawled_at); +CREATE INDEX IF NOT EXISTS idx_tweets_x_tweet_id ON tweets(x_tweet_id); +CREATE INDEX IF NOT EXISTS idx_tweets_heat_score ON tweets(heat_score DESC); +CREATE INDEX IF NOT EXISTS idx_tweets_crawl_queue ON tweets(crawl_queue, last_crawled_at); -- generated_replies 表:生成的 AI 评论记录 CREATE TABLE IF NOT EXISTS generated_replies ( @@ -71,8 +71,8 @@ CREATE TABLE IF NOT EXISTS generated_replies ( created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP ); -CREATE INDEX idx_generated_replies_user_id ON generated_replies(user_id); -CREATE INDEX idx_generated_replies_tweet_id ON generated_replies(tweet_id); +CREATE INDEX IF NOT EXISTS idx_generated_replies_user_id ON generated_replies(user_id); +CREATE INDEX IF NOT EXISTS idx_generated_replies_tweet_id ON generated_replies(tweet_id); -- reply_performance 表:针对已发布评论的效果数据回拨 CREATE TABLE IF NOT EXISTS reply_performance ( @@ -85,8 +85,8 @@ CREATE TABLE IF NOT EXISTS reply_performance ( check_time TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP ); -CREATE INDEX idx_reply_performance_reply_id ON reply_performance(reply_id); -CREATE INDEX idx_reply_performance_user_id ON reply_performance(user_id); +CREATE INDEX IF NOT EXISTS idx_reply_performance_reply_id ON reply_performance(reply_id); +CREATE INDEX IF NOT EXISTS idx_reply_performance_user_id ON reply_performance(user_id); -- ==================================================== -- 新增表 (v1.1) @@ -106,8 +106,8 @@ CREATE TABLE IF NOT EXISTS api_usage_logs ( created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP ); -CREATE INDEX idx_api_usage_logs_user_id ON api_usage_logs(user_id); -CREATE INDEX idx_api_usage_logs_created_at ON api_usage_logs(created_at DESC); +CREATE INDEX IF NOT EXISTS idx_api_usage_logs_user_id ON api_usage_logs(user_id); +CREATE INDEX IF NOT EXISTS idx_api_usage_logs_created_at ON api_usage_logs(created_at DESC); -- subscriptions 表:用户订阅记录(支付历史) CREATE TABLE IF NOT EXISTS subscriptions ( @@ -122,8 +122,8 @@ CREATE TABLE IF NOT EXISTS subscriptions ( created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP ); -CREATE INDEX idx_subscriptions_user_id ON subscriptions(user_id); -CREATE INDEX idx_subscriptions_status ON subscriptions(status); +CREATE INDEX IF NOT EXISTS idx_subscriptions_user_id ON subscriptions(user_id); +CREATE INDEX IF NOT EXISTS idx_subscriptions_status ON subscriptions(status); -- user_style_profiles 表:用户风格画像(用于个性化 Prompt) CREATE TABLE IF NOT EXISTS user_style_profiles ( @@ -147,7 +147,7 @@ CREATE TABLE IF NOT EXISTS crawl_snapshots ( created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP ); -CREATE INDEX idx_crawl_snapshots_created_at ON crawl_snapshots(created_at DESC); +CREATE INDEX IF NOT EXISTS idx_crawl_snapshots_created_at ON crawl_snapshots(created_at DESC); -- ==================================================== -- 新增表 (v1.2) — 用户可配置系统 @@ -191,7 +191,7 @@ CREATE TABLE IF NOT EXISTS user_custom_strategies ( UNIQUE (user_id, strategy_key) ); -CREATE INDEX idx_user_custom_strategies_user_id ON user_custom_strategies(user_id); +CREATE INDEX IF NOT EXISTS idx_user_custom_strategies_user_id ON user_custom_strategies(user_id); -- competitor_monitors 表:竞品品牌监控(复用后端雷达,按品牌词自动抓取) CREATE TABLE IF NOT EXISTS competitor_monitors ( @@ -204,7 +204,7 @@ CREATE TABLE IF NOT EXISTS competitor_monitors ( UNIQUE (user_id, brand_name) ); -CREATE INDEX idx_competitor_monitors_user_id ON competitor_monitors(user_id); +CREATE INDEX IF NOT EXISTS idx_competitor_monitors_user_id ON competitor_monitors(user_id); -- ==================================================== -- 触发器:自动更新 updated_at @@ -219,12 +219,20 @@ END; $$ language 'plpgsql'; -- 为所有需要追踪更新时间的表添加触发器 -CREATE TRIGGER update_users_modtime - BEFORE UPDATE ON users - FOR EACH ROW - EXECUTE FUNCTION update_modified_column(); +DO $$ BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_users_modtime') THEN + CREATE TRIGGER update_users_modtime + BEFORE UPDATE ON users + FOR EACH ROW + EXECUTE FUNCTION update_modified_column(); + END IF; +END $$; -CREATE TRIGGER update_user_style_profiles_modtime - BEFORE UPDATE ON user_style_profiles - FOR EACH ROW - EXECUTE FUNCTION update_modified_column(); +DO $$ BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_user_style_profiles_modtime') THEN + CREATE TRIGGER update_user_style_profiles_modtime + BEFORE UPDATE ON user_style_profiles + FOR EACH ROW + EXECUTE FUNCTION update_modified_column(); + END IF; +END $$;