2024-09-14 16:45:33 +08:00
|
|
|
-- 初始化数据库SCHEMA(所有轨道交通信号系统仿真的表、类型等都在rtss SCHEMA下)
|
|
|
|
CREATE SCHEMA rtss;
|
|
|
|
|
2024-10-22 17:24:50 +08:00
|
|
|
-- 创建mqtt客户端id序列
|
|
|
|
CREATE SEQUENCE rtss.mqtt_client_id_seq;
|
|
|
|
|
2024-09-25 18:35:45 +08:00
|
|
|
-- 创建用户表
|
|
|
|
CREATE TABLE
|
|
|
|
rtss.user (
|
|
|
|
id SERIAL PRIMARY KEY, -- id 自增主键
|
|
|
|
username VARCHAR(128) NOT NULL, -- 用户名
|
|
|
|
password VARCHAR(128) NOT NULL, -- 密码
|
|
|
|
email VARCHAR(128) NULL, -- 邮箱
|
|
|
|
mobile VARCHAR(16) NULL, -- 手机号
|
|
|
|
roles JSONB NOT NULL DEFAULT '[]', -- 角色列表
|
|
|
|
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建时间
|
|
|
|
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP -- 更新时间
|
|
|
|
);
|
|
|
|
|
2024-09-26 20:45:48 +08:00
|
|
|
-- 创建用户名称索引
|
|
|
|
CREATE INDEX ON rtss.user (username);
|
|
|
|
|
|
|
|
-- 创建用户邮箱索引
|
|
|
|
CREATE INDEX ON rtss.user (email);
|
|
|
|
|
|
|
|
-- 创建用户手机号索引
|
|
|
|
CREATE INDEX ON rtss.user (mobile);
|
|
|
|
|
|
|
|
-- 创建用户角色索引
|
|
|
|
CREATE INDEX ON rtss.user USING GIN (roles);
|
|
|
|
|
|
|
|
-- 注释用户表
|
|
|
|
COMMENT ON TABLE rtss.user IS '用户表';
|
|
|
|
|
|
|
|
-- 注释用户表字段
|
|
|
|
COMMENT ON COLUMN rtss.user.id IS 'id 自增主键';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.user.username IS '用户名';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.user.password IS '密码';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.user.email IS '邮箱';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.user.mobile IS '手机号';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.user.roles IS '角色列表';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.user.created_at IS '创建时间';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.user.updated_at IS '更新时间';
|
|
|
|
|
2024-09-14 16:45:33 +08:00
|
|
|
-- 创建草稿数据表
|
|
|
|
CREATE TABLE
|
|
|
|
rtss.draft_data (
|
|
|
|
id SERIAL PRIMARY KEY, -- id 自增主键
|
|
|
|
name VARCHAR(128) NOT NULL, -- 草稿名称
|
|
|
|
data_type INT NOT NULL, -- 数据类型
|
2024-09-19 00:07:53 +08:00
|
|
|
options JSONB NULL, -- 数据相关的参数项或配置项
|
2024-09-14 16:45:33 +08:00
|
|
|
data BYTEA, -- 草稿数据
|
|
|
|
default_release_data_id INT NULL, -- 默认发布数据id
|
|
|
|
user_id INT NOT NULL, -- 创建用户id
|
2024-09-18 15:10:11 +08:00
|
|
|
is_shared BOOLEAN NOT NULL DEFAULT FALSE, -- 是否共享
|
2024-09-19 11:03:05 +08:00
|
|
|
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建时间
|
|
|
|
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 更新时间
|
2024-09-25 18:35:45 +08:00
|
|
|
FOREIGN KEY (user_id) REFERENCES rtss.user (id) ON DELETE CASCADE, -- 用户外键
|
2024-09-26 20:45:48 +08:00
|
|
|
UNIQUE (name, data_type, user_id) -- 一个用户的某个类型的草稿名称唯一
|
2024-09-14 16:45:33 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
-- 创建草稿数据用户索引
|
|
|
|
CREATE INDEX ON rtss.draft_data (user_id);
|
|
|
|
|
|
|
|
-- 创建草稿数据类型索引
|
|
|
|
CREATE INDEX ON rtss.draft_data (data_type);
|
|
|
|
|
2024-09-19 00:07:53 +08:00
|
|
|
-- 创建草稿数据配置项索引
|
|
|
|
CREATE INDEX ON rtss.draft_data USING GIN (options);
|
|
|
|
|
2024-09-14 16:45:33 +08:00
|
|
|
-- 注释草稿数据表
|
|
|
|
COMMENT ON TABLE rtss.draft_data IS '草稿数据表';
|
|
|
|
|
|
|
|
-- 注释草稿数据表字段
|
|
|
|
COMMENT ON COLUMN rtss.draft_data.id IS 'id 自增主键';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.draft_data.name IS '草稿名称';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.draft_data.data_type IS '数据类型';
|
|
|
|
|
2024-09-19 00:07:53 +08:00
|
|
|
COMMENT ON COLUMN rtss.draft_data.options IS '数据相关的参数项或配置项';
|
|
|
|
|
2024-09-14 16:45:33 +08:00
|
|
|
COMMENT ON COLUMN rtss.draft_data.data IS '草稿数据';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.draft_data.user_id IS '创建用户id';
|
|
|
|
|
2024-09-18 15:10:11 +08:00
|
|
|
COMMENT ON COLUMN rtss.draft_data.is_shared IS '是否共享';
|
|
|
|
|
2024-09-14 16:45:33 +08:00
|
|
|
COMMENT ON COLUMN rtss.draft_data.created_at IS '创建时间';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.draft_data.updated_at IS '更新时间';
|
|
|
|
|
|
|
|
-- 创建发布数据表
|
|
|
|
CREATE TABLE
|
|
|
|
rtss.release_data (
|
|
|
|
id SERIAL PRIMARY KEY, -- id 自增主键
|
2024-09-20 15:45:10 +08:00
|
|
|
name VARCHAR(128) NOT NULL, -- 发布数据名称(数据唯一标识)
|
2024-09-14 16:45:33 +08:00
|
|
|
data_type INT NOT NULL, -- 数据类型
|
2024-09-19 00:07:53 +08:00
|
|
|
options JSONB NULL, -- 数据相关的参数项或配置项
|
2024-09-14 16:45:33 +08:00
|
|
|
used_version_id INT NULL, -- 使用的版本数据id
|
|
|
|
user_id INT NOT NULL, -- 发布/更新用户id
|
|
|
|
is_published BOOLEAN NOT NULL DEFAULT TRUE, -- 是否上架
|
2024-09-19 11:03:05 +08:00
|
|
|
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建时间
|
2024-09-20 15:45:10 +08:00
|
|
|
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 更新时间
|
2024-09-25 18:35:45 +08:00
|
|
|
FOREIGN KEY (user_id) REFERENCES rtss.user (id) ON DELETE CASCADE, -- 用户外键
|
2024-09-20 15:45:10 +08:00
|
|
|
UNIQUE(data_type, name) -- 数据类型和名称唯一
|
2024-09-14 16:45:33 +08:00
|
|
|
);
|
|
|
|
|
2024-09-26 20:45:48 +08:00
|
|
|
-- 创建发布数据名称索引
|
|
|
|
CREATE INDEX ON rtss.release_data (name);
|
|
|
|
|
|
|
|
-- 创建发布数据用户索引
|
|
|
|
CREATE INDEX ON rtss.release_data (user_id);
|
|
|
|
|
|
|
|
-- 创建发布数据类型索引
|
|
|
|
CREATE INDEX ON rtss.release_data (data_type);
|
|
|
|
|
|
|
|
-- 创建发布数据配置项索引
|
|
|
|
CREATE INDEX ON rtss.release_data USING GIN (options);
|
|
|
|
|
2024-09-14 16:45:33 +08:00
|
|
|
-- 注释发布数据表
|
|
|
|
COMMENT ON TABLE rtss.release_data IS '发布数据表';
|
|
|
|
|
|
|
|
-- 注释发布数据表字段
|
|
|
|
COMMENT ON COLUMN rtss.release_data.id IS 'id 自增主键';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.release_data.name IS '发布数据名称(数据唯一标识)';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.release_data.data_type IS '数据类型';
|
|
|
|
|
2024-09-19 00:07:53 +08:00
|
|
|
COMMENT ON COLUMN rtss.release_data.options IS '数据相关的参数项或配置项';
|
|
|
|
|
2024-09-14 16:45:33 +08:00
|
|
|
COMMENT ON COLUMN rtss.release_data.used_version_id IS '使用的版本数据id';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.release_data.user_id IS '发布/更新用户id';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.release_data.is_published IS '是否上架';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.release_data.created_at IS '创建时间';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.release_data.updated_at IS '更新时间';
|
|
|
|
|
|
|
|
-- 创建发布数据版本表
|
|
|
|
CREATE TABLE
|
|
|
|
rtss.release_data_version (
|
|
|
|
id SERIAL PRIMARY KEY, -- id 自增主键
|
|
|
|
release_data_id INT NOT NULL, -- 发布数据id
|
2024-09-20 12:31:18 +08:00
|
|
|
options JSONB NULL, -- 数据相关的参数项或配置项
|
2024-09-14 16:45:33 +08:00
|
|
|
data BYTEA NOT NULL, -- 数据
|
|
|
|
description TEXT NOT NULL, -- 版本描述
|
|
|
|
user_id INT NOT NULL, -- 发布用户id
|
2024-09-19 11:03:05 +08:00
|
|
|
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建时间
|
2024-09-25 18:35:45 +08:00
|
|
|
FOREIGN KEY (user_id) REFERENCES rtss.user (id) ON DELETE CASCADE, -- 用户外键
|
2024-09-14 16:45:33 +08:00
|
|
|
FOREIGN KEY (release_data_id) REFERENCES rtss.release_data (id) ON DELETE CASCADE
|
|
|
|
);
|
|
|
|
|
2024-09-26 20:45:48 +08:00
|
|
|
-- 创建发布数据版本发布数据索引
|
|
|
|
CREATE INDEX ON rtss.release_data_version (release_data_id);
|
|
|
|
|
|
|
|
-- 创建发布数据版本用户索引
|
|
|
|
CREATE INDEX ON rtss.release_data_version (user_id);
|
|
|
|
|
|
|
|
-- 创建发布数据版本配置项索引
|
|
|
|
CREATE INDEX ON rtss.release_data_version USING GIN (options);
|
|
|
|
|
2024-09-14 16:45:33 +08:00
|
|
|
-- 创建发布数据当前版本外键
|
|
|
|
ALTER TABLE rtss.release_data ADD FOREIGN KEY (used_version_id) REFERENCES rtss.release_data_version (id) ON DELETE SET NULL;
|
|
|
|
|
|
|
|
-- 创建草稿数据默认发布数据外键
|
|
|
|
ALTER TABLE rtss.draft_data ADD FOREIGN KEY (default_release_data_id) REFERENCES rtss.release_data (id) ON DELETE SET NULL;
|
|
|
|
|
|
|
|
-- 注释发布数据版本表
|
|
|
|
COMMENT ON TABLE rtss.release_data_version IS '发布数据版本表';
|
|
|
|
|
|
|
|
-- 注释发布数据版本表字段
|
|
|
|
COMMENT ON COLUMN rtss.release_data_version.id IS 'id 自增主键';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.release_data_version.release_data_id IS '发布数据id';
|
|
|
|
|
2024-09-20 12:31:18 +08:00
|
|
|
COMMENT ON COLUMN rtss.release_data_version.options IS '数据相关的参数项或配置项';
|
|
|
|
|
2024-09-14 16:45:33 +08:00
|
|
|
COMMENT ON COLUMN rtss.release_data_version.data IS '数据';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.release_data_version.description IS '版本描述';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.release_data_version.user_id IS '发布用户id';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.release_data_version.created_at IS '创建时间';
|
|
|
|
|
|
|
|
-- 创建feature表
|
|
|
|
CREATE TABLE
|
|
|
|
rtss.feature (
|
|
|
|
id SERIAL PRIMARY KEY, -- id 自增主键
|
|
|
|
feature_type INT NOT NULL, -- feature类型
|
|
|
|
name VARCHAR(128) NOT NULL UNIQUE, -- feature名称
|
|
|
|
description TEXT NOT NULL, -- feature描述
|
2024-09-26 20:45:48 +08:00
|
|
|
config JSONB NOT NULL, -- feature配置
|
2024-09-14 16:45:33 +08:00
|
|
|
is_published BOOLEAN NOT NULL DEFAULT TRUE, -- 是否上架
|
|
|
|
creator_id INT NOT NULL, -- 创建用户id
|
|
|
|
updater_id INT NOT NULL, -- 更新用户id
|
2024-09-19 11:03:05 +08:00
|
|
|
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建时间
|
2024-09-25 18:35:45 +08:00
|
|
|
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 更新时间
|
|
|
|
FOREIGN KEY (creator_id) REFERENCES rtss.user (id) ON DELETE CASCADE, -- 用户外键
|
|
|
|
FOREIGN KEY (updater_id) REFERENCES rtss.user (id) ON DELETE CASCADE -- 用户外键
|
2024-09-14 16:45:33 +08:00
|
|
|
);
|
|
|
|
|
2024-09-26 20:45:48 +08:00
|
|
|
-- 创建feature类型索引
|
|
|
|
CREATE INDEX ON rtss.feature (feature_type);
|
|
|
|
|
|
|
|
-- 创建feature名称索引
|
|
|
|
CREATE INDEX ON rtss.feature (name);
|
|
|
|
|
2024-09-14 16:45:33 +08:00
|
|
|
-- 注释仿真feature表
|
|
|
|
COMMENT ON TABLE rtss.feature IS 'feature表';
|
|
|
|
|
|
|
|
-- 注释仿真feature表字段
|
|
|
|
COMMENT ON COLUMN rtss.feature.id IS 'id 自增主键';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.feature.feature_type IS 'feature类型';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.feature.name IS 'feature名称';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.feature.description IS 'feature描述';
|
|
|
|
|
2024-09-26 20:45:48 +08:00
|
|
|
COMMENT ON COLUMN rtss.feature.config IS 'feature配置';
|
|
|
|
|
2024-09-14 16:45:33 +08:00
|
|
|
COMMENT ON COLUMN rtss.feature.is_published IS '是否上架';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.feature.creator_id IS '创建用户id';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.feature.created_at IS '创建时间';
|
|
|
|
|
|
|
|
COMMENT ON COLUMN rtss.feature.updated_at IS '更新时间';
|
|
|
|
|
2024-09-26 20:45:48 +08:00
|
|
|
-- 创建用户配置表
|
2024-09-14 16:45:33 +08:00
|
|
|
CREATE TABLE
|
2024-09-26 20:45:48 +08:00
|
|
|
rtss.user_config (
|
2024-09-14 16:45:33 +08:00
|
|
|
id SERIAL PRIMARY KEY, -- id 自增主键
|
|
|
|
user_id INT NOT NULL, -- 用户id
|
2024-09-26 20:45:48 +08:00
|
|
|
config_type INT NOT NULL, -- 配置类型
|
2024-09-14 16:45:33 +08:00
|
|
|
config BYTEA NOT NULL, -- 配置
|
2024-09-19 11:03:05 +08:00
|
|
|
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建时间
|
|
|
|
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 更新时间
|
2024-09-26 20:45:48 +08:00
|
|
|
FOREIGN KEY (user_id) REFERENCES rtss.user (id) ON DELETE CASCADE -- 用户外键
|
2024-09-14 16:45:33 +08:00
|
|
|
);
|
|
|
|
|
2024-09-26 20:45:48 +08:00
|
|
|
-- 创建用户配置用户索引
|
|
|
|
CREATE INDEX ON rtss.user_config (user_id);
|
|
|
|
|
|
|
|
-- 创建用户配置类型索引
|
|
|
|
CREATE INDEX ON rtss.user_config (config_type);
|
|
|
|
|
2024-09-14 16:45:33 +08:00
|
|
|
-- 注释用户feature配置表
|
2024-09-26 20:45:48 +08:00
|
|
|
COMMENT ON TABLE rtss.user_config IS '用户feature配置表';
|
2024-09-14 16:45:33 +08:00
|
|
|
|
|
|
|
-- 注释用户feature配置表字段
|
2024-09-26 20:45:48 +08:00
|
|
|
COMMENT ON COLUMN rtss.user_config.id IS 'id 自增主键';
|
2024-09-14 16:45:33 +08:00
|
|
|
|
2024-09-26 20:45:48 +08:00
|
|
|
COMMENT ON COLUMN rtss.user_config.user_id IS '用户id';
|
2024-09-14 16:45:33 +08:00
|
|
|
|
2024-09-26 20:45:48 +08:00
|
|
|
COMMENT ON COLUMN rtss.user_config.config_type IS '配置类型';
|
2024-09-14 16:45:33 +08:00
|
|
|
|
2024-09-26 20:45:48 +08:00
|
|
|
COMMENT ON COLUMN rtss.user_config.config IS '配置';
|
2024-09-14 16:45:33 +08:00
|
|
|
|
2024-09-26 20:45:48 +08:00
|
|
|
COMMENT ON COLUMN rtss.user_config.created_at IS '创建时间';
|
2024-09-14 16:45:33 +08:00
|
|
|
|
2024-09-26 20:45:48 +08:00
|
|
|
COMMENT ON COLUMN rtss.user_config.updated_at IS '更新时间';
|