plc_control/migrations/20260224065328_initial.sql

59 lines
1.7 KiB
SQL
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 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);