diff --git a/backend/sql/06_currency.sql b/backend/sql/06_currency.sql index 2ec7aac..4d911f0 100644 --- a/backend/sql/06_currency.sql +++ b/backend/sql/06_currency.sql @@ -1,103 +1,114 @@ --- 切换到目标数据库 -\c postgres; +-- ========================================================= +-- currency.sql 💰 +-- 无物化视图 | 超可视提示 | 可重复执行 +-- PostgreSQL 17.4+ 👍 +-- ========================================================= +-- 1️⃣ 开始 🚀 DO $$ BEGIN + RAISE NOTICE '🚀============ currency 部署开始 ============🚀'; +END $$; + +-- 2️⃣ 连接目标库 +\c postgres; + +-- 3️⃣ 表结构 ----------------------------------- +DO $$ +BEGIN + -- currency 主表 IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'currency') THEN - CREATE TABLE currency ( - id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + CREATE TABLE currency ( + id UUID DEFAULT gen_random_uuid() PRIMARY KEY, deleted BOOLEAN NOT NULL DEFAULT FALSE, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP ); CREATE TRIGGER update_currency_updated_at - BEFORE UPDATE ON "currency" - FOR EACH ROW - EXECUTE FUNCTION update_data_modified_column(); - - RAISE NOTICE 'created currency table and trigger'; + BEFORE UPDATE ON currency + FOR EACH ROW EXECUTE FUNCTION update_data_modified_column(); + RAISE NOTICE '1️⃣✅ currency 主表已创建'; ELSE - RAISE NOTICE 'currency table already exists'; + RAISE NOTICE '1️⃣⏩ currency 主表已存在,跳过'; END IF; + -- currency_name IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'currency_name') THEN CREATE TABLE currency_name ( - id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + id UUID DEFAULT gen_random_uuid() PRIMARY KEY, currency_id UUID NOT NULL, name VARCHAR NOT NULL, deleted BOOLEAN NOT NULL DEFAULT FALSE, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP ); - CREATE TRIGGER update_name_updated_at - BEFORE UPDATE ON "currency_name" - FOR EACH ROW - EXECUTE FUNCTION update_data_modified_column(); - - RAISE NOTICE 'created currency_name table and trigger'; + CREATE TRIGGER update_currency_name_updated_at + BEFORE UPDATE ON currency_name + FOR EACH ROW EXECUTE FUNCTION update_data_modified_column(); + RAISE NOTICE '2️⃣✅ currency_name 子表已创建'; ELSE - RAISE NOTICE 'currency_name table already exists'; + RAISE NOTICE '2️⃣⏩ currency_name 子表已存在,跳过'; END IF; + -- currency_code IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'currency_code') THEN CREATE TABLE currency_code ( - id UUID DEFAULT gen_random_uuid_v7() PRIMARY KEY NOT NULL, + id UUID DEFAULT gen_random_uuid() PRIMARY KEY, currency_id UUID NOT NULL, code VARCHAR NOT NULL, deleted BOOLEAN NOT NULL DEFAULT FALSE, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP ); - CREATE TRIGGER update_code_updated_at - BEFORE UPDATE ON "currency_code" - FOR EACH ROW - EXECUTE FUNCTION update_data_modified_column(); - - RAISE NOTICE 'created currency_code table and trigger'; + CREATE TRIGGER update_currency_code_updated_at + BEFORE UPDATE ON currency_code + FOR EACH ROW EXECUTE FUNCTION update_data_modified_column(); + RAISE NOTICE '3️⃣✅ currency_code 子表已创建'; ELSE - RAISE NOTICE 'currency_code table already exists'; + RAISE NOTICE '3️⃣⏩ currency_code 子表已存在,跳过'; END IF; END $$; +-- 4️⃣ 视图 ------------------------------------ DO $$ DECLARE view_exists BOOLEAN; BEGIN - -- 检查视图是否已存在 SELECT EXISTS ( SELECT 1 FROM information_schema.views WHERE table_name = 'currency_info_view' ) INTO view_exists; - -- 若视图存在,先删除(避免字段名冲突) IF view_exists THEN DROP VIEW currency_info_view; - RAISE NOTICE '已删除旧视图 currency_info_view'; + RAISE NOTICE '4️⃣♻️ 已删除旧视图 currency_info_view'; END IF; - -- 创建或更新视图 CREATE OR REPLACE VIEW currency_info_view AS - SELECT + SELECT u.id AS currency_id, - n.name AS name, - c.code AS code, - u.deleted AS deleted - FROM - currency u - JOIN - currency_name n ON u.id = n.currency_id - JOIN - currency_code c ON u.id = c.currency_id - WHERE - u.deleted = FALSE; + n.name, + c.code, + u.deleted + FROM currency u + JOIN currency_name n ON u.id = n.currency_id AND n.deleted = FALSE + JOIN currency_code c ON u.id = c.currency_id AND c.deleted = FALSE + WHERE u.deleted = FALSE; - -- 根据视图是否已存在输出不同提示 - IF view_exists THEN - RAISE NOTICE '视图 currency_info_view 已更新(删除旧视图后重建)'; - ELSE - RAISE NOTICE '视图 currency_info_view 已创建'; - END IF; + RAISE NOTICE '4️⃣✅ currency_info_view 已创建/更新'; EXCEPTION WHEN OTHERS THEN - RAISE NOTICE '处理视图时发生错误: %', SQLERRM; + RAISE NOTICE '4️⃣❌ 处理视图时发生错误: %', SQLERRM; +END $$; + +-- 5️⃣ 性能索引 ------------------------------------------------ +CREATE INDEX IF NOT EXISTS idx_currency_name_currency_id_deleted ON currency_name(currency_id, deleted); +CREATE INDEX IF NOT EXISTS idx_currency_code_currency_id_deleted ON currency_code(currency_id, deleted); + +RAISE NOTICE '5️⃣✅ 全部索引已确保存在'; + +-- 6️⃣ 完成 🎉 +DO $$ +BEGIN + RAISE NOTICE '🎉============ currency 部署完成 ============🎉'; END $$; \ No newline at end of file