posimai-root/docs/synology/synology-events-migration.sql

50 lines
2.0 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Posimai Events - PostgreSQL スキーマSynology NAS用
-- Brain DB と同じ PostgreSQL インスタンスに events テーブルを追加する
-- 実行: psql -U postgres -d posimai -f synology-events-migration.sql
CREATE TABLE IF NOT EXISTS events (
id SERIAL PRIMARY KEY,
event_id TEXT UNIQUE NOT NULL, -- 外部IDPeatix ID等または UUID
title TEXT NOT NULL,
start_date DATE NOT NULL,
start_time TIME,
end_date DATE,
end_time TIME,
location TEXT,
address TEXT,
description TEXT,
category TEXT,
url TEXT,
source TEXT, -- 情報元 (Peatix / 市役所HP / n8n-scrape 等)
-- パーソナライズ用タグGemini が自動付与)
interest_tags TEXT[] DEFAULT '{}', -- ['sake', 'food', 'market'] 等
audience_tags TEXT[] DEFAULT '{}', -- ['couple', 'family', 'solo'] 等
is_free BOOLEAN DEFAULT FALSE,
no_rsvp BOOLEAN DEFAULT FALSE,
is_outdoor BOOLEAN DEFAULT FALSE,
-- メタデータ
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
scraped_at TIMESTAMPTZ DEFAULT NOW() -- n8n が最後に取得した日時
);
-- インデックス: 日付範囲・ステータス検索用
CREATE INDEX IF NOT EXISTS idx_events_start_date ON events(start_date);
CREATE INDEX IF NOT EXISTS idx_events_end_date ON events(end_date);
CREATE INDEX IF NOT EXISTS idx_events_source ON events(source);
CREATE INDEX IF NOT EXISTS idx_events_interest ON events USING GIN(interest_tags);
CREATE INDEX IF NOT EXISTS idx_events_audience ON events USING GIN(audience_tags);
-- 更新時刻の自動更新
CREATE OR REPLACE FUNCTION update_events_updated_at()
RETURNS TRIGGER AS $$
BEGIN NEW.updated_at = NOW(); RETURN NEW; END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS trg_events_updated_at ON events;
CREATE TRIGGER trg_events_updated_at
BEFORE UPDATE ON events
FOR EACH ROW EXECUTE FUNCTION update_events_updated_at();