Skip to content

Commit 950f468

Browse files
committed
Add priority field to request_resolution type
Add priority to request_resolution type and resolve_worker_from_request function so the runner can map route priorities to AssetType enum (Immutable/Static/Prerendered) for proper path transformation.
1 parent b403430 commit 950f468

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "openworkers-cli"
3-
version = "0.2.8"
3+
version = "0.2.9"
44
edition = "2024"
55
license = "MIT"
66
description = "CLI for OpenWorkers - Self-hosted Cloudflare Workers runtime"
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
--
2+
-- OpenWorkers Database Schema - Add priority to request_resolution
3+
--
4+
-- Adds priority field to request_resolution type so the runner can
5+
-- map route priorities to AssetType enum (Immutable/Static/Prerendered).
6+
--
7+
8+
BEGIN;
9+
10+
-- ============================================================================
11+
-- Update request_resolution type to include priority
12+
-- ============================================================================
13+
14+
-- Drop the function first (depends on the type)
15+
DROP FUNCTION IF EXISTS resolve_worker_from_request(varchar, uuid, varchar, varchar);
16+
17+
-- Drop and recreate the type with priority field
18+
DROP TYPE IF EXISTS request_resolution CASCADE;
19+
CREATE TYPE request_resolution AS (
20+
worker_id uuid,
21+
project_id uuid,
22+
backend_type enum_backend_type,
23+
assets_storage_id uuid,
24+
priority int
25+
);
26+
27+
-- ============================================================================
28+
-- Recreate resolve_worker_from_request with priority support
29+
-- ============================================================================
30+
31+
CREATE OR REPLACE FUNCTION resolve_worker_from_request(
32+
p_domain varchar DEFAULT NULL,
33+
p_worker_id uuid DEFAULT NULL,
34+
p_worker_name varchar DEFAULT NULL,
35+
p_path varchar DEFAULT '/'
36+
)
37+
RETURNS request_resolution AS $$
38+
DECLARE
39+
v_result request_resolution;
40+
v_worker_id_temp uuid;
41+
v_project_id_temp uuid;
42+
v_route_record RECORD;
43+
BEGIN
44+
-- Priority 1: Direct worker_id (used for service bindings, worker-to-worker calls)
45+
IF p_worker_id IS NOT NULL THEN
46+
v_result.worker_id := p_worker_id;
47+
v_result.project_id := NULL;
48+
v_result.backend_type := 'worker'::enum_backend_type;
49+
v_result.assets_storage_id := NULL;
50+
v_result.priority := NULL;
51+
RETURN v_result;
52+
END IF;
53+
54+
-- Priority 2: Resolve project_id/worker_id from worker_name (subdomain) or domain (custom domain)
55+
IF p_worker_name IS NOT NULL THEN
56+
-- Subdomain case: name.workers.rocks → lookup in endpoints
57+
-- Projects have priority over standalone workers for routing
58+
SELECT project_id, worker_id
59+
INTO v_project_id_temp, v_worker_id_temp
60+
FROM endpoints
61+
WHERE name = p_worker_name
62+
ORDER BY type DESC -- 'project' > 'worker' alphabetically
63+
LIMIT 1;
64+
65+
IF NOT FOUND THEN
66+
RETURN NULL; -- Endpoint not found
67+
END IF;
68+
ELSIF p_domain IS NOT NULL THEN
69+
-- Custom domain case: example.com → lookup in domains
70+
SELECT project_id, worker_id
71+
INTO v_project_id_temp, v_worker_id_temp
72+
FROM domains
73+
WHERE name = p_domain;
74+
75+
IF NOT FOUND THEN
76+
RETURN NULL; -- Domain not found
77+
END IF;
78+
ELSE
79+
-- No identifier provided
80+
RETURN NULL;
81+
END IF;
82+
83+
-- If we have a project, match route pattern
84+
IF v_project_id_temp IS NOT NULL THEN
85+
SELECT backend_type, worker_id, priority
86+
INTO v_route_record
87+
FROM project_routes
88+
WHERE project_id = v_project_id_temp
89+
AND (
90+
pattern = p_path
91+
OR (pattern LIKE '%/*' AND p_path LIKE REPLACE(pattern, '/*', '') || '%')
92+
)
93+
ORDER BY priority DESC
94+
LIMIT 1;
95+
96+
IF NOT FOUND THEN
97+
RETURN NULL; -- No route matches
98+
END IF;
99+
100+
v_result.project_id := v_project_id_temp;
101+
v_result.worker_id := v_route_record.worker_id;
102+
v_result.backend_type := v_route_record.backend_type;
103+
v_result.priority := v_route_record.priority;
104+
105+
-- If storage backend, get ASSETS storage_config_id from main worker's environment
106+
IF v_route_record.backend_type = 'storage' THEN
107+
SELECT value::uuid
108+
INTO v_result.assets_storage_id
109+
FROM environment_values ev
110+
JOIN workers w ON w.environment_id = ev.environment_id
111+
WHERE w.id = v_project_id_temp -- main worker has same id as project
112+
AND ev.key = 'ASSETS'
113+
AND ev.type = 'assets';
114+
115+
IF v_result.assets_storage_id IS NULL THEN
116+
RETURN NULL; -- ASSETS binding not found
117+
END IF;
118+
ELSE
119+
v_result.assets_storage_id := NULL;
120+
END IF;
121+
122+
RETURN v_result;
123+
END IF;
124+
125+
-- If we have a standalone worker (not in a project)
126+
IF v_worker_id_temp IS NOT NULL THEN
127+
v_result.worker_id := v_worker_id_temp;
128+
v_result.project_id := NULL;
129+
v_result.backend_type := 'worker'::enum_backend_type;
130+
v_result.assets_storage_id := NULL;
131+
v_result.priority := NULL;
132+
RETURN v_result;
133+
END IF;
134+
135+
-- Nothing matched
136+
RETURN NULL;
137+
END;
138+
$$ LANGUAGE plpgsql;
139+
140+
COMMENT ON FUNCTION resolve_worker_from_request(varchar, uuid, varchar, varchar) IS 'Resolves endpoint and route in a single query to determine which worker or storage to use, including route priority';
141+
142+
COMMIT;

0 commit comments

Comments
 (0)