39 lines
1.7 KiB
SQL
39 lines
1.7 KiB
SQL
-- 1. asset_registry 已有 asset_type 列('regular' | 'collection' | 'activity'),
|
|
-- VARCHAR(20) NOT NULL,无 CHECK 约束(见 migrate_create_collection_activity_registry_tables.sql:61)。
|
|
-- 本次不动 schema,只在新代码里允许 asset_type='peripheral' 写入。
|
|
|
|
-- 2. asset 表加 verify_count 列(若不存在)
|
|
-- 用于缓存"该周边累计被加入藏品的人次",详情见 §4.1
|
|
ALTER TABLE assets
|
|
ADD COLUMN IF NOT EXISTS verify_count INT NOT NULL DEFAULT 0;
|
|
|
|
-- 2.1 一次性回填已有 peripheral 周边
|
|
UPDATE assets a
|
|
SET verify_count = COALESCE((
|
|
SELECT COUNT(*)
|
|
FROM asset_registry r
|
|
WHERE r.asset_id = a.id AND r.asset_type = 'peripheral'
|
|
), 0);
|
|
|
|
-- 3. 新增 peripheral_info 表:周边验真详情(一对一关联 assets)
|
|
-- 复用现有 asset_registry 的 uk_registry_owner_star_type_asset 约束防重复
|
|
CREATE TABLE IF NOT EXISTS peripheral_info (
|
|
asset_id BIGINT PRIMARY KEY REFERENCES assets(id) ON DELETE CASCADE,
|
|
brand VARCHAR(100) NOT NULL DEFAULT '',
|
|
company VARCHAR(200) NOT NULL DEFAULT '',
|
|
hash VARCHAR(100) NOT NULL DEFAULT '',
|
|
verifier VARCHAR(100) NOT NULL DEFAULT '',
|
|
first_verified_at BIGINT NOT NULL DEFAULT 0,
|
|
created_at BIGINT NOT NULL,
|
|
updated_at BIGINT NOT NULL
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_peripheral_info_brand
|
|
ON peripheral_info(brand) WHERE brand <> '';
|
|
|
|
-- 4. 序列同步(asset_registry.id 是 BIGSERIAL,peripheral_info 用 asset_id 作主键无需序列)
|
|
SELECT setval(
|
|
pg_get_serial_sequence('asset_registry', 'id'),
|
|
(SELECT MAX(id) FROM asset_registry)
|
|
);
|