Files
Memoh/db/migrations/0018_inbox_refactor.up.sql
T
Acbox Liu fe10abf3fc refactor: inbox (#137)
* refactor: inbox

* fix: migrations

* fix: migrations
2026-02-26 20:16:02 +08:00

28 lines
1.0 KiB
SQL

-- 0018_inbox_refactor
-- Refactor bot_inbox: split content JSONB into content TEXT + header JSONB, add action column.
-- 1. Add new columns (idempotent).
ALTER TABLE bot_inbox ADD COLUMN IF NOT EXISTS header JSONB NOT NULL DEFAULT '{}'::jsonb;
ALTER TABLE bot_inbox ADD COLUMN IF NOT EXISTS action TEXT NOT NULL DEFAULT 'notify';
-- 2. Migrate data and convert column type.
-- Only needed when content is still JSONB (upgrade path).
-- On a fresh DB (0001_init already defines content as TEXT), this is a no-op.
DO $$
BEGIN
IF EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'bot_inbox' AND column_name = 'content' AND data_type = 'jsonb'
) THEN
UPDATE bot_inbox
SET header = content - 'text',
action = 'notify'
WHERE content IS NOT NULL AND content::text <> '{}';
ALTER TABLE bot_inbox ALTER COLUMN content DROP DEFAULT;
ALTER TABLE bot_inbox ALTER COLUMN content TYPE TEXT USING COALESCE(content ->> 'text', '');
ALTER TABLE bot_inbox ALTER COLUMN content SET DEFAULT '';
END IF;
END
$$;