techtools
Головна / proto / flow-old / db / schema.sql

schema.sql

SQL · 137 рядків

1CREATE EXTENSION IF NOT EXISTS pgcrypto;
2
3CREATE TABLE workflows (
4  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
5  name text NOT NULL,
6  version integer NOT NULL DEFAULT 1,
7  status text NOT NULL DEFAULT 'draft',
8  definition jsonb NOT NULL,
9  created_at timestamptz NOT NULL DEFAULT now(),
10  updated_at timestamptz NOT NULL DEFAULT now()
11);
12
13CREATE TABLE workflow_executions (
14  id uuid PRIMARY KEY,
15  workflow_id uuid REFERENCES workflows(id),
16  entity_type text NOT NULL,
17  entity_id text NOT NULL,
18  status text NOT NULL CHECK (
19    status IN ('pending','running','waiting','completed','failed','cancelled')
20  ),
21  current_node_id text,
22  waiting_for text,
23  context jsonb NOT NULL DEFAULT '{}',
24  started_at timestamptz NOT NULL DEFAULT now(),
25  updated_at timestamptz NOT NULL DEFAULT now(),
26  completed_at timestamptz
27);
28CREATE INDEX executions_waiting_idx
29  ON workflow_executions(waiting_for, entity_id)
30  WHERE status = 'waiting';
31
32CREATE TABLE workflow_execution_logs (
33  id uuid PRIMARY KEY,
34  execution_id uuid NOT NULL REFERENCES workflow_executions(id),
35  node_id text,
36  event text NOT NULL,
37  input jsonb,
38  output jsonb,
39  error text,
40  created_at timestamptz NOT NULL DEFAULT now()
41);
42
43CREATE TABLE workflow_tasks (
44  id uuid PRIMARY KEY,
45  execution_id uuid REFERENCES workflow_executions(id),
46  entity_type text NOT NULL,
47  entity_id text NOT NULL,
48  parent_task_id uuid REFERENCES workflow_tasks(id),
49  order_id text,
50  type text NOT NULL,
51  handler_workflow_id uuid,
52  handler_workflow_version integer,
53  title text,
54  description text,
55  priority text,
56  status text NOT NULL DEFAULT 'open' CHECK (
57    status IN ('open','in_progress','completed','cancelled')
58  ),
59  state text NOT NULL DEFAULT 'created',
60  progress text NOT NULL DEFAULT 'waiting',
61  assignee jsonb,
62  context jsonb NOT NULL DEFAULT '{}',
63  payload jsonb NOT NULL DEFAULT '{}',
64  version integer NOT NULL DEFAULT 1,
65  created_at timestamptz NOT NULL DEFAULT now(),
66  updated_at timestamptz NOT NULL DEFAULT now(),
67  completed_at timestamptz
68);
69CREATE UNIQUE INDEX workflow_tasks_one_active_entity_idx
70  ON workflow_tasks(entity_type, entity_id)
71  WHERE status IN ('open', 'in_progress');
72CREATE INDEX workflow_tasks_handler_idx
73  ON workflow_tasks(handler_workflow_id, handler_workflow_version, status);
74CREATE INDEX workflow_tasks_parent_idx
75  ON workflow_tasks(parent_task_id, status);
76
77CREATE TABLE inbox_events (
78  event_id text PRIMARY KEY,
79  event text NOT NULL,
80  entity_type text NOT NULL,
81  entity_id text NOT NULL,
82  payload jsonb NOT NULL,
83  status text NOT NULL CHECK (
84    status IN ('accepted','processing','processed','rejected','failed')
85  ),
86  result jsonb,
87  execution_id uuid REFERENCES workflow_executions(id),
88  received_at timestamptz NOT NULL DEFAULT now(),
89  processed_at timestamptz
90);
91CREATE INDEX inbox_events_entity_idx
92  ON inbox_events(entity_type, entity_id, received_at);
93
94CREATE TABLE warehouse_items (
95  id text PRIMARY KEY,
96  product_id text,
97  warehouse_id text,
98  quantity numeric NOT NULL DEFAULT 1,
99  status text NOT NULL,
100  location_id text,
101  active_workflow_id uuid,
102  active_execution_id uuid,
103  created_at timestamptz NOT NULL DEFAULT now(),
104  updated_at timestamptz NOT NULL DEFAULT now()
105);
106
107CREATE TABLE warehouse_item_events (
108  id uuid PRIMARY KEY,
109  warehouse_item_id text NOT NULL REFERENCES warehouse_items(id),
110  event text NOT NULL,
111  from_status text,
112  to_status text,
113  workflow_id uuid,
114  execution_id uuid,
115  node_id text,
116  actor_type text,
117  actor_id text,
118  data jsonb,
119  created_at timestamptz NOT NULL DEFAULT now()
120);
121
122CREATE TABLE outbox_events (
123  id uuid PRIMARY KEY,
124  event text NOT NULL,
125  payload jsonb NOT NULL,
126  processed_at timestamptz,
127  attempts integer NOT NULL DEFAULT 0,
128  created_at timestamptz NOT NULL DEFAULT now()
129);
130CREATE INDEX outbox_unprocessed_idx
131  ON outbox_events(created_at)
132  WHERE processed_at IS NULL;
133
134-- UPDATE_ENTITY must perform the item update, audit insert, and outbox insert
135-- in one database transaction. Consumers should lock outbox rows with
136-- FOR UPDATE SKIP LOCKED before publishing.
137