|
| 1 | +-- Migration: Worker Deployments |
| 2 | +-- Adds versioned code storage with support for JavaScript, TypeScript, WebAssembly, and Snapshots |
| 3 | + |
| 4 | +BEGIN; |
| 5 | + |
| 6 | +-- Create new code_type enum (replaces enum_workers_language) |
| 7 | +CREATE TYPE enum_code_type AS ENUM ( |
| 8 | + 'javascript', |
| 9 | + 'typescript', |
| 10 | + 'wasm', |
| 11 | + 'snapshot' |
| 12 | +); |
| 13 | + |
| 14 | +-- Add current_version to workers |
| 15 | +ALTER TABLE workers ADD COLUMN current_version INT; |
| 16 | + |
| 17 | +-- Create worker_deployments table |
| 18 | +CREATE TABLE worker_deployments ( |
| 19 | + worker_id UUID NOT NULL REFERENCES workers(id) ON UPDATE CASCADE ON DELETE CASCADE, |
| 20 | + version INT NOT NULL, |
| 21 | + hash VARCHAR(64) NOT NULL, |
| 22 | + code_type enum_code_type NOT NULL, |
| 23 | + code BYTEA NOT NULL, |
| 24 | + deployed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 25 | + deployed_by UUID REFERENCES users(id) ON UPDATE CASCADE ON DELETE SET NULL, |
| 26 | + message TEXT, |
| 27 | + PRIMARY KEY (worker_id, version) |
| 28 | +); |
| 29 | + |
| 30 | +-- Create index for fast lookups by hash |
| 31 | +CREATE INDEX idx_worker_deployments_hash ON worker_deployments(worker_id, hash); |
| 32 | + |
| 33 | +-- Migrate existing worker scripts to worker_deployments |
| 34 | +INSERT INTO worker_deployments (worker_id, version, hash, code_type, code, deployed_at, deployed_by) |
| 35 | +SELECT |
| 36 | + w.id, |
| 37 | + 1, |
| 38 | + encode(sha256(convert_to(w.script, 'UTF8')), 'hex'), |
| 39 | + w.language::text::enum_code_type, |
| 40 | + convert_to(w.script, 'UTF8'), |
| 41 | + w.updated_at, |
| 42 | + w.user_id |
| 43 | +FROM workers w |
| 44 | +WHERE w.script IS NOT NULL AND w.script != ''; |
| 45 | + |
| 46 | +-- Set current_version for migrated workers |
| 47 | +UPDATE workers SET current_version = 1 |
| 48 | +WHERE script IS NOT NULL AND script != ''; |
| 49 | + |
| 50 | +-- Drop old columns (script and language) |
| 51 | +ALTER TABLE workers DROP COLUMN script; |
| 52 | +ALTER TABLE workers DROP COLUMN language; |
| 53 | + |
| 54 | +-- Drop old enum type |
| 55 | +DROP TYPE enum_workers_language; |
| 56 | + |
| 57 | +COMMIT; |
0 commit comments