update:优化build

This commit is contained in:
Eric 1549169735@qq.com
2026-08-19 22:55:07 +08:00
parent 7526b72c63
commit ebaf9b0b05
23 changed files with 4 additions and 571 deletions

View File

@@ -1,17 +0,0 @@
DROP TABLE IF EXISTS refresh_tokens;
DROP TABLE IF EXISTS audit_logs;
DROP TABLE IF EXISTS sop_feedback;
DROP TABLE IF EXISTS sop_run_events;
DROP TABLE IF EXISTS sop_runs;
DROP TABLE IF EXISTS knowledge_card_versions;
DROP TABLE IF EXISTS knowledge_cards;
DROP TABLE IF EXISTS sop_edges;
DROP TABLE IF EXISTS sop_nodes;
DROP TABLE IF EXISTS sop_versions;
DROP TABLE IF EXISTS sops;
DROP TABLE IF EXISTS scenario_fields;
DROP TABLE IF EXISTS scenarios;
DROP TABLE IF EXISTS tenant_members;
DROP TABLE IF EXISTS roles;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS tenants;

View File

@@ -1,258 +0,0 @@
CREATE TABLE tenants (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(128) NOT NULL,
slug VARCHAR(64) NOT NULL,
status VARCHAR(24) NOT NULL DEFAULT 'active',
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), UNIQUE KEY uk_tenants_slug (slug)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE users (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(64) NOT NULL,
password_hash VARCHAR(255) NOT NULL,
display_name VARCHAR(128) NOT NULL,
status VARCHAR(24) NOT NULL DEFAULT 'active',
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), UNIQUE KEY uk_users_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE roles (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
name VARCHAR(64) NOT NULL,
code VARCHAR(64) NOT NULL,
permissions JSON NOT NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), UNIQUE KEY uk_roles_tenant_code (tenant_id, code),
CONSTRAINT fk_roles_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE tenant_members (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
role_id BIGINT UNSIGNED NOT NULL,
status VARCHAR(24) NOT NULL DEFAULT 'active',
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), UNIQUE KEY uk_tenant_members (tenant_id, user_id),
CONSTRAINT fk_members_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_members_user FOREIGN KEY (user_id) REFERENCES users(id),
CONSTRAINT fk_members_role FOREIGN KEY (role_id) REFERENCES roles(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE scenarios (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
name VARCHAR(128) NOT NULL,
industry VARCHAR(64) NOT NULL,
role_name VARCHAR(64) NOT NULL,
goal TEXT NOT NULL,
trigger_text TEXT NOT NULL,
visibility VARCHAR(24) NOT NULL DEFAULT 'tenant',
status VARCHAR(24) NOT NULL DEFAULT 'draft',
created_by BIGINT UNSIGNED NOT NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), KEY idx_scenarios_tenant_status (tenant_id, status),
CONSTRAINT fk_scenarios_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_scenarios_creator FOREIGN KEY (created_by) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE scenario_fields (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
scenario_id BIGINT UNSIGNED NOT NULL,
field_key VARCHAR(64) NOT NULL,
field_name VARCHAR(128) NOT NULL,
field_type VARCHAR(32) NOT NULL,
required TINYINT(1) NOT NULL DEFAULT 0,
options JSON NOT NULL,
validation JSON NOT NULL,
sort_order INT NOT NULL DEFAULT 0,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), UNIQUE KEY uk_scenario_fields_key (scenario_id, field_key),
CONSTRAINT fk_fields_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_fields_scenario FOREIGN KEY (scenario_id) REFERENCES scenarios(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE sops (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
scenario_id BIGINT UNSIGNED NOT NULL,
name VARCHAR(128) NOT NULL,
description TEXT NOT NULL,
status VARCHAR(24) NOT NULL DEFAULT 'draft',
created_by BIGINT UNSIGNED NOT NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), KEY idx_sops_scenario (tenant_id, scenario_id),
CONSTRAINT fk_sops_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_sops_scenario FOREIGN KEY (scenario_id) REFERENCES scenarios(id),
CONSTRAINT fk_sops_creator FOREIGN KEY (created_by) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE sop_versions (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
sop_id BIGINT UNSIGNED NOT NULL,
version INT NOT NULL,
status VARCHAR(24) NOT NULL DEFAULT 'draft',
start_node_key VARCHAR(64) NOT NULL DEFAULT '',
published_at DATETIME(3) NULL,
created_by BIGINT UNSIGNED NOT NULL,
reviewed_by BIGINT UNSIGNED NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), UNIQUE KEY uk_sop_versions (sop_id, version),
CONSTRAINT fk_versions_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_versions_sop FOREIGN KEY (sop_id) REFERENCES sops(id) ON DELETE CASCADE,
CONSTRAINT fk_versions_creator FOREIGN KEY (created_by) REFERENCES users(id),
CONSTRAINT fk_versions_reviewer FOREIGN KEY (reviewed_by) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE sop_nodes (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
sop_version_id BIGINT UNSIGNED NOT NULL,
node_key VARCHAR(64) NOT NULL,
type VARCHAR(32) NOT NULL,
title VARCHAR(128) NOT NULL,
content TEXT NOT NULL,
config JSON NOT NULL,
position_x INT NOT NULL DEFAULT 0,
position_y INT NOT NULL DEFAULT 0,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), UNIQUE KEY uk_sop_nodes_key (sop_version_id, node_key),
CONSTRAINT fk_nodes_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_nodes_version FOREIGN KEY (sop_version_id) REFERENCES sop_versions(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE sop_edges (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
sop_version_id BIGINT UNSIGNED NOT NULL,
source_node_key VARCHAR(64) NOT NULL,
target_node_key VARCHAR(64) NOT NULL,
`condition` JSON NOT NULL,
priority INT NOT NULL DEFAULT 0,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), KEY idx_sop_edges_source (sop_version_id, source_node_key, priority),
CONSTRAINT fk_edges_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_edges_version FOREIGN KEY (sop_version_id) REFERENCES sop_versions(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE knowledge_cards (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
scenario_id BIGINT UNSIGNED NOT NULL,
title VARCHAR(128) NOT NULL,
status VARCHAR(24) NOT NULL DEFAULT 'draft',
created_by BIGINT UNSIGNED NOT NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), KEY idx_cards_scenario (tenant_id, scenario_id),
CONSTRAINT fk_cards_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_cards_scenario FOREIGN KEY (scenario_id) REFERENCES scenarios(id),
CONSTRAINT fk_cards_creator FOREIGN KEY (created_by) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE knowledge_card_versions (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
knowledge_card_id BIGINT UNSIGNED NOT NULL,
version INT NOT NULL,
content JSON NOT NULL,
status VARCHAR(24) NOT NULL DEFAULT 'draft',
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), UNIQUE KEY uk_card_versions (knowledge_card_id, version),
CONSTRAINT fk_card_versions_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_card_versions_card FOREIGN KEY (knowledge_card_id) REFERENCES knowledge_cards(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE sop_runs (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
sop_id BIGINT UNSIGNED NOT NULL,
sop_version_id BIGINT UNSIGNED NOT NULL,
operator_id BIGINT UNSIGNED NOT NULL,
current_node_key VARCHAR(64) NOT NULL,
status VARCHAR(24) NOT NULL DEFAULT 'running',
answers JSON NOT NULL,
result VARCHAR(64) NOT NULL DEFAULT '',
started_at DATETIME(3) NOT NULL,
completed_at DATETIME(3) NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), KEY idx_runs_tenant_status (tenant_id, status),
CONSTRAINT fk_runs_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_runs_sop FOREIGN KEY (sop_id) REFERENCES sops(id),
CONSTRAINT fk_runs_version FOREIGN KEY (sop_version_id) REFERENCES sop_versions(id),
CONSTRAINT fk_runs_operator FOREIGN KEY (operator_id) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE sop_run_events (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
run_id BIGINT UNSIGNED NOT NULL,
node_key VARCHAR(64) NOT NULL,
action VARCHAR(64) NOT NULL,
payload JSON NOT NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), KEY idx_run_events (run_id, created_at),
CONSTRAINT fk_run_events_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_run_events_run FOREIGN KEY (run_id) REFERENCES sop_runs(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE sop_feedback (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
run_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
score INT NOT NULL,
comment TEXT NOT NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), UNIQUE KEY uk_feedback_run_user (run_id, user_id),
CONSTRAINT fk_feedback_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_feedback_run FOREIGN KEY (run_id) REFERENCES sop_runs(id) ON DELETE CASCADE,
CONSTRAINT fk_feedback_user FOREIGN KEY (user_id) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE audit_logs (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
action VARCHAR(64) NOT NULL,
resource VARCHAR(64) NOT NULL,
resource_id BIGINT UNSIGNED NOT NULL,
payload JSON NOT NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), KEY idx_audit_tenant_time (tenant_id, created_at),
CONSTRAINT fk_audit_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_audit_user FOREIGN KEY (user_id) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE refresh_tokens (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
token_hash VARCHAR(64) NOT NULL,
expires_at DATETIME(3) NOT NULL,
revoked_at DATETIME(3) NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id), UNIQUE KEY uk_refresh_token_hash (token_hash),
CONSTRAINT fk_refresh_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_refresh_user FOREIGN KEY (user_id) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

View File

@@ -1,4 +0,0 @@
UPDATE sop_nodes
SET config = JSON_REMOVE(config, '$.knowledge_card_version_id')
WHERE type = 'knowledge'
AND JSON_EXTRACT(config, '$.knowledge_card_version_id') IS NOT NULL;

View File

@@ -1,25 +0,0 @@
UPDATE sop_nodes n
JOIN sop_versions sv ON sv.id = n.sop_version_id
SET n.config = JSON_SET(
n.config,
'$.knowledge_card_version_id',
(
SELECT kv.id
FROM knowledge_card_versions kv
WHERE kv.tenant_id = n.tenant_id
AND kv.knowledge_card_id = CAST(JSON_UNQUOTE(JSON_EXTRACT(n.config, '$.knowledge_card_id')) AS UNSIGNED)
AND kv.created_at <= COALESCE(sv.published_at, sv.updated_at)
ORDER BY kv.version DESC
LIMIT 1
)
)
WHERE n.type = 'knowledge'
AND JSON_EXTRACT(n.config, '$.knowledge_card_version_id') IS NULL
AND JSON_EXTRACT(n.config, '$.knowledge_card_id') IS NOT NULL
AND EXISTS (
SELECT 1
FROM knowledge_card_versions kv
WHERE kv.tenant_id = n.tenant_id
AND kv.knowledge_card_id = CAST(JSON_UNQUOTE(JSON_EXTRACT(n.config, '$.knowledge_card_id')) AS UNSIGNED)
AND kv.created_at <= COALESCE(sv.published_at, sv.updated_at)
);

View File

@@ -1 +0,0 @@
DROP TABLE IF EXISTS multitable_outbox;

View File

@@ -1,23 +0,0 @@
CREATE TABLE multitable_outbox (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
audit_log_id BIGINT UNSIGNED NULL,
resource VARCHAR(64) NOT NULL,
resource_id BIGINT UNSIGNED NOT NULL,
action VARCHAR(64) NOT NULL,
payload JSON NOT NULL,
dedupe_key VARCHAR(191) NOT NULL,
status VARCHAR(24) NOT NULL DEFAULT 'pending',
attempts INT NOT NULL DEFAULT 0,
available_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
last_error TEXT NOT NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id),
UNIQUE KEY uk_multitable_outbox_audit (audit_log_id),
UNIQUE KEY uk_multitable_outbox_dedupe (dedupe_key),
KEY idx_multitable_outbox_pending (status, available_at),
KEY idx_multitable_outbox_resource (tenant_id, resource, resource_id),
CONSTRAINT fk_multitable_outbox_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_multitable_outbox_audit FOREIGN KEY (audit_log_id) REFERENCES audit_logs(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

View File

@@ -1 +0,0 @@
-- The review workflow is intentionally not restored by rollback.

View File

@@ -1,15 +0,0 @@
UPDATE sop_versions
SET status = 'draft', reviewed_by = NULL
WHERE status = 'reviewing';
UPDATE sops
SET status = CASE
WHEN EXISTS (
SELECT 1
FROM sop_versions
WHERE sop_versions.sop_id = sops.id
AND sop_versions.status = 'published'
) THEN 'published'
ELSE 'draft'
END
WHERE status = 'reviewing';

View File

@@ -1,4 +0,0 @@
ALTER TABLE sop_runs DROP INDEX uk_sop_runs_external_ref;
ALTER TABLE sop_runs DROP COLUMN outputs, DROP COLUMN derived, DROP COLUMN input, DROP COLUMN external_ref;
ALTER TABLE scenario_fields DROP COLUMN source_path;
ALTER TABLE scenarios DROP COLUMN output_schema, DROP COLUMN input_schema;

View File

@@ -1,20 +0,0 @@
ALTER TABLE scenarios
ADD COLUMN input_schema JSON NULL AFTER created_by,
ADD COLUMN output_schema JSON NULL AFTER input_schema;
UPDATE scenarios SET input_schema = JSON_OBJECT('fields', JSON_ARRAY()), output_schema = JSON_OBJECT('fields', JSON_ARRAY());
ALTER TABLE scenarios MODIFY input_schema JSON NOT NULL, MODIFY output_schema JSON NOT NULL;
ALTER TABLE scenario_fields
ADD COLUMN source_path VARCHAR(255) NOT NULL DEFAULT '' AFTER field_name;
ALTER TABLE sop_runs
ADD COLUMN external_ref VARCHAR(191) NULL AFTER operator_id,
ADD COLUMN input JSON NULL AFTER answers,
ADD COLUMN derived JSON NULL AFTER input,
ADD COLUMN outputs JSON NULL AFTER derived;
UPDATE sop_runs SET input = answers, derived = JSON_OBJECT(), outputs = JSON_ARRAY();
ALTER TABLE sop_runs MODIFY input JSON NOT NULL, MODIFY derived JSON NOT NULL, MODIFY outputs JSON NOT NULL;
ALTER TABLE sop_runs ADD UNIQUE KEY uk_sop_runs_external_ref (tenant_id, sop_id, external_ref);

View File

@@ -1,2 +0,0 @@
DROP TABLE IF EXISTS knowledge_relations;
DROP TABLE IF EXISTS knowledge_items;

View File

@@ -1,38 +0,0 @@
CREATE TABLE knowledge_items (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
scenario_id BIGINT UNSIGNED NOT NULL,
item_key VARCHAR(64) NOT NULL,
name VARCHAR(128) NOT NULL,
type VARCHAR(64) NOT NULL,
content JSON NOT NULL,
status VARCHAR(24) NOT NULL DEFAULT 'active',
sort_order INT NOT NULL DEFAULT 0,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id),
UNIQUE KEY uk_knowledge_items_key (scenario_id, item_key),
KEY idx_knowledge_items_type (tenant_id, scenario_id, type, status),
CONSTRAINT fk_knowledge_items_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_knowledge_items_scenario FOREIGN KEY (scenario_id) REFERENCES scenarios(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE knowledge_relations (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
scenario_id BIGINT UNSIGNED NOT NULL,
from_knowledge_id BIGINT UNSIGNED NOT NULL,
relation_type VARCHAR(64) NOT NULL,
to_knowledge_id BIGINT UNSIGNED NOT NULL,
`condition` JSON NOT NULL,
sort_order INT NOT NULL DEFAULT 0,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id),
UNIQUE KEY uk_knowledge_relation (scenario_id, from_knowledge_id, relation_type, to_knowledge_id),
KEY idx_knowledge_relation_from (tenant_id, scenario_id, from_knowledge_id, relation_type),
CONSTRAINT fk_knowledge_relations_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_knowledge_relations_scenario FOREIGN KEY (scenario_id) REFERENCES scenarios(id) ON DELETE CASCADE,
CONSTRAINT fk_knowledge_relations_from FOREIGN KEY (from_knowledge_id) REFERENCES knowledge_items(id) ON DELETE CASCADE,
CONSTRAINT fk_knowledge_relations_to FOREIGN KEY (to_knowledge_id) REFERENCES knowledge_items(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

View File

@@ -1,3 +0,0 @@
DROP TABLE IF EXISTS public_run_sessions;
ALTER TABLE scenarios DROP INDEX uk_scenarios_public_key, DROP INDEX uk_scenarios_scenario_key;
ALTER TABLE scenarios DROP COLUMN allowed_origins, DROP COLUMN public_key, DROP COLUMN scenario_key;

View File

@@ -1,31 +0,0 @@
ALTER TABLE scenarios
ADD COLUMN scenario_key VARCHAR(128) NULL AFTER tenant_id,
ADD COLUMN public_key VARCHAR(128) NULL AFTER scenario_key,
ADD COLUMN allowed_origins JSON NULL AFTER public_key;
UPDATE scenarios
SET scenario_key = CONCAT('scenario-', id),
public_key = CONCAT('pk_', REPLACE(UUID(), '-', '')),
allowed_origins = JSON_ARRAY();
ALTER TABLE scenarios
MODIFY scenario_key VARCHAR(128) NOT NULL,
MODIFY public_key VARCHAR(128) NOT NULL,
MODIFY allowed_origins JSON NOT NULL,
ADD UNIQUE KEY uk_scenarios_scenario_key (scenario_key),
ADD UNIQUE KEY uk_scenarios_public_key (public_key);
CREATE TABLE public_run_sessions (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
run_id BIGINT UNSIGNED NOT NULL,
token_hash VARCHAR(64) NOT NULL,
expires_at DATETIME(3) NOT NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id),
UNIQUE KEY uk_public_run_sessions_token (token_hash),
KEY idx_public_run_sessions_run (tenant_id, run_id),
CONSTRAINT fk_public_sessions_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_public_sessions_run FOREIGN KEY (run_id) REFERENCES sop_runs(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

View File

@@ -1 +0,0 @@
DROP TABLE IF EXISTS scenario_rules;

View File

@@ -1,18 +0,0 @@
CREATE TABLE scenario_rules (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
scenario_id BIGINT UNSIGNED NOT NULL,
rule_key VARCHAR(64) NOT NULL,
name VARCHAR(128) NOT NULL,
`condition` JSON NOT NULL,
actions JSON NOT NULL,
priority INT NOT NULL DEFAULT 0,
status VARCHAR(24) NOT NULL DEFAULT 'active',
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY (id),
UNIQUE KEY uk_scenario_rules_key (scenario_id, rule_key),
KEY idx_scenario_rules_active (tenant_id, scenario_id, status, priority),
CONSTRAINT fk_scenario_rules_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT fk_scenario_rules_scenario FOREIGN KEY (scenario_id) REFERENCES scenarios(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

View File

@@ -1 +0,0 @@
ALTER TABLE sop_runs DROP COLUMN knowledge_snapshot;

View File

@@ -1,3 +0,0 @@
ALTER TABLE sop_runs ADD COLUMN knowledge_snapshot JSON NULL AFTER outputs;
UPDATE sop_runs SET knowledge_snapshot = JSON_OBJECT('items', JSON_ARRAY(), 'relations', JSON_ARRAY());
ALTER TABLE sop_runs MODIFY knowledge_snapshot JSON NOT NULL;

View File

@@ -1,2 +0,0 @@
ALTER TABLE sop_runs DROP COLUMN final_result;
ALTER TABLE scenarios DROP COLUMN result_schema;

View File

@@ -1,8 +0,0 @@
ALTER TABLE scenarios
ADD COLUMN result_schema JSON NULL AFTER output_schema;
UPDATE scenarios SET result_schema = JSON_OBJECT('fields', JSON_ARRAY());
ALTER TABLE scenarios MODIFY result_schema JSON NOT NULL;
ALTER TABLE sop_runs
ADD COLUMN final_result JSON NULL AFTER result;