59 lines
1.7 KiB
SQL
59 lines
1.7 KiB
SQL
-- Add migration script here
|
||
|
||
-- 1️⃣ Source 表
|
||
CREATE TABLE source (
|
||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||
name TEXT NOT NULL,
|
||
protocol TEXT NOT NULL,
|
||
endpoint TEXT NOT NULL,
|
||
security_policy TEXT,
|
||
security_mode TEXT,
|
||
username TEXT,
|
||
password TEXT,
|
||
enabled BOOLEAN NOT NULL DEFAULT TRUE,
|
||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
||
unique (endpoint) -- 唯一约束:endpoint 不重复
|
||
);
|
||
|
||
-- 2️⃣ Node 表
|
||
CREATE TABLE node (
|
||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||
source_id UUID NOT NULL,
|
||
external_id TEXT NOT NULL, -- 主查字段
|
||
namespace_uri TEXT, -- 防止index变化
|
||
namespace_index INTEGER, -- 仅作记录
|
||
identifier_type TEXT, -- 仅作记录
|
||
identifier TEXT, -- 仅作记录
|
||
browse_name TEXT NOT NULL,
|
||
display_name TEXT,
|
||
node_class TEXT NOT NULL,
|
||
parent_id UUID,
|
||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
||
FOREIGN KEY (source_id) REFERENCES source(id),
|
||
FOREIGN KEY (parent_id) REFERENCES node(id),
|
||
UNIQUE(source_id, external_id)
|
||
);
|
||
|
||
-- Node 常用索引
|
||
CREATE INDEX idx_node_source_id ON node(source_id);
|
||
CREATE INDEX idx_node_parent_id ON node(parent_id);
|
||
|
||
-- 3️⃣ Point 表
|
||
CREATE TABLE point (
|
||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||
node_id UUID NOT NULL,
|
||
name TEXT NOT NULL,
|
||
description TEXT,
|
||
unit TEXT,
|
||
scan_interval_s INTEGER NOT NULL DEFAULT 1 CHECK (scan_interval_s > 0),
|
||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
||
FOREIGN KEY (node_id) REFERENCES node(id),
|
||
UNIQUE (node_id)
|
||
);
|
||
|
||
-- Point 常用索引
|
||
CREATE INDEX idx_point_node_id ON point(node_id);
|