refactor(database): 补全客户管理模块表结构与索引

新增客户业务编号、业务员关联字段,补全updated_at列和更新触发器,优化索引命名与新增必要索引,新增增量迁移脚本
This commit is contained in:
andyesfly committed 2026-08-23 19:25:45 +08:00
1 parent 1c80a24d1d
commit dc86bf1040
2 files changed
+248 -6

No files matched your search

@@ -0,0 +1,227 @@
-- ============================================================
-- 增量更新脚本:客户管理模块结构补全
-- 日期:2026-08-23
-- 数据库:PostgreSQL 15+ (xintong)
-- 说明:客户管理核心模块(客户信息/客户部门/客户访问账号)表结构
-- 与 Sequelize Entity 定义对齐,补齐缺失列、索引及 updated_at 触发器。
-- 脚本幂等,可重复执行。
--
-- 更新内容:
-- 1. customer 表
-- - 新增列 customer_no VARCHAR(64) NOT NULL UNIQUE(客户业务编号)
-- - 新增列 salesman_id BIGINT REFERENCES sys_user(id)(负责业务员)
-- - 新增索引 idx_customer_salesman (salesman_id)
-- - 新增索引 idx_customer_status (status)
-- 2. customer_department 表
-- - 新增列 updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
-- - 新增触发器 trg_customer_department_updated_at
-- - 重命名索引 idx_cust_dept_customer → idx_dept_customer(若新索引已存在则删除旧索引)
-- 3. customer_account 表
-- - 新增列 updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
-- - 新增触发器 trg_customer_account_updated_at
-- - 新增索引 idx_account_customer (customer_id)
-- - 重命名唯一索引 idx_cust_account_phone → uk_account_phone
--
-- 用法:
-- psql -U postgres -d xintong -f 20260823_customer_module.sql
-- ============================================================
BEGIN;
-- ============================================================
-- 1. customer 表:补 customer_no、salesman_id 列及索引
-- ============================================================
-- 1.1 新增 customer_no 列(客户业务编号 KH+YYYYMMDD+3位序号)
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'customer' AND column_name = 'customer_no'
) THEN
ALTER TABLE customer ADD COLUMN customer_no VARCHAR(64) NOT NULL;
ALTER TABLE customer ADD CONSTRAINT uk_customer_no UNIQUE (customer_no);
COMMENT ON COLUMN customer.customer_no IS '客户业务编号(KH+YYYYMMDD+3位序号)';
RAISE NOTICE 'customer: 已新增列 customer_no';
ELSE
RAISE NOTICE 'customer: 列 customer_no 已存在,跳过';
END IF;
END $$;
-- 1.2 新增 salesman_id 列(负责业务员,关联 sys_user.id)
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'customer' AND column_name = 'salesman_id'
) THEN
ALTER TABLE customer ADD COLUMN salesman_id BIGINT REFERENCES sys_user(id);
COMMENT ON COLUMN customer.salesman_id IS '负责业务员(→ sys_user.id)';
RAISE NOTICE 'customer: 已新增列 salesman_id';
ELSE
RAISE NOTICE 'customer: 列 salesman_id 已存在,跳过';
END IF;
END $$;
-- 1.3 新增索引 idx_customer_salesman
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_indexes WHERE tablename = 'customer' AND indexname = 'idx_customer_salesman'
) THEN
CREATE INDEX idx_customer_salesman ON customer (salesman_id);
RAISE NOTICE 'customer: 已创建索引 idx_customer_salesman';
ELSE
RAISE NOTICE 'customer: 索引 idx_customer_salesman 已存在,跳过';
END IF;
END $$;
-- 1.4 新增索引 idx_customer_status
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_indexes WHERE tablename = 'customer' AND indexname = 'idx_customer_status'
) THEN
CREATE INDEX idx_customer_status ON customer (status);
RAISE NOTICE 'customer: 已创建索引 idx_customer_status';
ELSE
RAISE NOTICE 'customer: 索引 idx_customer_status 已存在,跳过';
END IF;
END $$;
-- ============================================================
-- 2. customer_department 表:补 updated_at 列、触发器,重命名索引
-- ============================================================
-- 2.1 新增 updated_at 列
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'customer_department' AND column_name = 'updated_at'
) THEN
ALTER TABLE customer_department
ADD COLUMN updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW();
COMMENT ON COLUMN customer_department.updated_at IS '更新时间';
RAISE NOTICE 'customer_department: 已新增列 updated_at';
ELSE
RAISE NOTICE 'customer_department: 列 updated_at 已存在,跳过';
END IF;
END $$;
-- 2.2 新增 updated_at 触发器
DROP TRIGGER IF EXISTS trg_customer_department_updated_at ON customer_department;
CREATE TRIGGER trg_customer_department_updated_at BEFORE UPDATE ON customer_department
FOR EACH ROW EXECUTE FUNCTION trigger_set_updated_at();
-- 2.3 重命名索引 idx_cust_dept_customer → idx_dept_customer
DO $$
BEGIN
IF EXISTS (
SELECT 1 FROM pg_indexes WHERE tablename = 'customer_department' AND indexname = 'idx_cust_dept_customer'
) AND NOT EXISTS (
SELECT 1 FROM pg_indexes WHERE tablename = 'customer_department' AND indexname = 'idx_dept_customer'
) THEN
ALTER INDEX idx_cust_dept_customer RENAME TO idx_dept_customer;
RAISE NOTICE 'customer_department: 已重命名索引 idx_cust_dept_customer → idx_dept_customer';
ELSE
RAISE NOTICE 'customer_department: 索引重命名已跳过(源不存在或目标已存在)';
END IF;
END $$;
-- 2.4 删除可能残留的旧索引 idx_cust_dept_customer(新旧并存时清理冗余)
DROP INDEX IF EXISTS idx_cust_dept_customer;
-- 2.5 兜底:确保 idx_dept_customer 存在
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_indexes WHERE tablename = 'customer_department' AND indexname = 'idx_dept_customer'
) THEN
CREATE INDEX idx_dept_customer ON customer_department (customer_id);
RAISE NOTICE 'customer_department: 已创建索引 idx_dept_customer';
ELSE
RAISE NOTICE 'customer_department: 索引 idx_dept_customer 已存在,跳过';
END IF;
END $$;
-- ============================================================
-- 3. customer_account 表:补 updated_at 列、触发器,新增/重命名索引
-- ============================================================
-- 3.1 新增 updated_at 列
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_name = 'customer_account' AND column_name = 'updated_at'
) THEN
ALTER TABLE customer_account
ADD COLUMN updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW();
COMMENT ON COLUMN customer_account.updated_at IS '更新时间';
RAISE NOTICE 'customer_account: 已新增列 updated_at';
ELSE
RAISE NOTICE 'customer_account: 列 updated_at 已存在,跳过';
END IF;
END $$;
-- 3.2 新增 updated_at 触发器
DROP TRIGGER IF EXISTS trg_customer_account_updated_at ON customer_account;
CREATE TRIGGER trg_customer_account_updated_at BEFORE UPDATE ON customer_account
FOR EACH ROW EXECUTE FUNCTION trigger_set_updated_at();
-- 3.3 新增 customer_id 普通索引 idx_account_customer
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_indexes WHERE tablename = 'customer_account' AND indexname = 'idx_account_customer'
) THEN
CREATE INDEX idx_account_customer ON customer_account (customer_id);
RAISE NOTICE 'customer_account: 已创建索引 idx_account_customer';
ELSE
RAISE NOTICE 'customer_account: 索引 idx_account_customer 已存在,跳过';
END IF;
END $$;
-- 3.4 重命名唯一索引 idx_cust_account_phone → uk_account_phone
DO $$
BEGIN
IF EXISTS (
SELECT 1 FROM pg_indexes WHERE tablename = 'customer_account' AND indexname = 'idx_cust_account_phone'
) AND NOT EXISTS (
SELECT 1 FROM pg_indexes WHERE tablename = 'customer_account' AND indexname = 'uk_account_phone'
) THEN
ALTER INDEX idx_cust_account_phone RENAME TO uk_account_phone;
RAISE NOTICE 'customer_account: 已重命名索引 idx_cust_account_phone → uk_account_phone';
ELSE
RAISE NOTICE 'customer_account: 索引重命名已跳过(源不存在或目标已存在)';
END IF;
END $$;
-- 3.5 兜底:确保 uk_account_phone 存在
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_indexes WHERE tablename = 'customer_account' AND indexname = 'uk_account_phone'
) THEN
CREATE UNIQUE INDEX uk_account_phone ON customer_account (phone);
RAISE NOTICE 'customer_account: 已创建唯一索引 uk_account_phone';
ELSE
RAISE NOTICE 'customer_account: 索引 uk_account_phone 已存在,跳过';
END IF;
END $$;
COMMIT;
-- ============================================================
-- 验证查询(可选,不报错即表示结构正确)
-- ============================================================
SELECT 'customer' AS tbl, column_name FROM information_schema.columns
WHERE table_name = 'customer' AND column_name IN ('customer_no', 'salesman_id')
UNION ALL
SELECT 'customer_department', column_name FROM information_schema.columns
WHERE table_name = 'customer_department' AND column_name = 'updated_at'
UNION ALL
SELECT 'customer_account', column_name FROM information_schema.columns
WHERE table_name = 'customer_account' AND column_name = 'updated_at'
ORDER BY tbl, column_name;