This commit is contained in:
qsc
2026-07-13 00:44:09 +08:00
parent 34ec65c8b5
commit ef96af7f21
22 changed files with 1827 additions and 587 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ CREATE TABLE IF NOT EXISTS collection_points (id UUID PRIMARY KEY DEFAULT gen_ra
CREATE UNIQUE INDEX IF NOT EXISTS idx_collection_points_name ON collection_points(name) WHERE deleted=FALSE;
CREATE INDEX IF NOT EXISTS idx_collection_points_device_id ON collection_points(device_id) WHERE deleted=FALSE;
CREATE INDEX IF NOT EXISTS idx_collection_points_group_name ON collection_points(group_name) WHERE deleted=FALSE;
CREATE TABLE IF NOT EXISTS write_points (id UUID PRIMARY KEY DEFAULT gen_random_uuid(),name VARCHAR(128) NOT NULL,group_name VARCHAR(64) NOT NULL DEFAULT 'default',device_id UUID NOT NULL REFERENCES devices(id),enabled BOOLEAN NOT NULL DEFAULT TRUE,write_enabled BOOLEAN NOT NULL DEFAULT FALSE,deleted BOOLEAN NOT NULL DEFAULT FALSE,address VARCHAR(256) NOT NULL,data_type VARCHAR(16) NOT NULL CHECK(data_type IN('BOOL','INT','REAL')),unit VARCHAR(32),readback_tolerance DOUBLE PRECISION NOT NULL DEFAULT .0001 CHECK(readback_tolerance BETWEEN 0 AND 1000000),created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),created_by VARCHAR(64),updated_by VARCHAR(64));
CREATE TABLE IF NOT EXISTS write_points (id UUID PRIMARY KEY DEFAULT gen_random_uuid(),name VARCHAR(128) NOT NULL,group_name VARCHAR(64) NOT NULL DEFAULT 'default',device_id UUID NOT NULL REFERENCES devices(id),write_enabled BOOLEAN NOT NULL DEFAULT FALSE,deleted BOOLEAN NOT NULL DEFAULT FALSE,address VARCHAR(256) NOT NULL,data_type VARCHAR(16) NOT NULL CHECK(data_type IN('BOOL','INT','REAL')),unit VARCHAR(32),created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),created_by VARCHAR(64),updated_by VARCHAR(64));
CREATE UNIQUE INDEX IF NOT EXISTS idx_write_points_name ON write_points(name) WHERE deleted=FALSE;
CREATE INDEX IF NOT EXISTS idx_write_points_device_id ON write_points(device_id) WHERE deleted=FALSE;
CREATE TABLE IF NOT EXISTS write_logs (id UUID PRIMARY KEY DEFAULT gen_random_uuid(),point_id UUID NOT NULL REFERENCES write_points(id),point_name VARCHAR(128) NOT NULL,device_id UUID NOT NULL REFERENCES devices(id),device_name VARCHAR(128) NOT NULL,address VARCHAR(256) NOT NULL,data_type VARCHAR(16) NOT NULL,unit VARCHAR(32),source VARCHAR(16) NOT NULL DEFAULT 'manual' CHECK(source='manual'),target_value TEXT NOT NULL,readback_value TEXT,result VARCHAR(16) NOT NULL CHECK(result IN('success','failed','timeout')),error_message TEXT,operator VARCHAR(64),reason VARCHAR(500),created_at TIMESTAMPTZ NOT NULL DEFAULT NOW());
@@ -0,0 +1,26 @@
-- Write points use one explicit permission switch; keep the previous
-- effective permission before removing the redundant columns. The guard
-- keeps startup migrations safe when the service is restarted.
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_schema = current_schema()
AND table_name = 'write_points'
AND column_name = 'enabled'
) THEN
EXECUTE 'UPDATE write_points SET write_enabled = enabled AND write_enabled WHERE enabled IS NOT NULL';
EXECUTE 'ALTER TABLE write_points DROP COLUMN enabled';
END IF;
IF EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_schema = current_schema()
AND table_name = 'write_points'
AND column_name = 'readback_tolerance'
) THEN
EXECUTE 'ALTER TABLE write_points DROP COLUMN readback_tolerance';
END IF;
END $$;