Skip to content

Commit 1551bf8

Browse files
committed
SQL: Add 'migration' file
Allows for the database to be created without running Prisma. The file is based on the output of ``pg_dump --schema-only willa`` in a developer container. I cleaned up the syntax a bit and grouped related elements. Adds a ``bin/dbinit`` script that can be run to create the initial database structure. Reorder the Dockerfile a bit to ensure files that are more likely to change are in the more outer layers. Ref: AP-412
1 parent 98c93bc commit 1551bf8

File tree

4 files changed

+228
-4
lines changed

4 files changed

+228
-4
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/venv
99

1010
# Don't include LanceDB data.
11+
/.data
1112
/data
1213

1314
# Don't include default storage location.

Dockerfile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ FROM python:3.13-slim AS reqs
55
WORKDIR /app
66

77
RUN apt-get update -y && apt-get upgrade -y \
8-
&& rm -rf /var/lib/apt/lists/
8+
&& apt-get install -y postgresql-client && rm -rf /var/lib/apt/lists/
99

1010
RUN python -m venv /venv
1111
ENV PATH=/venv/bin:$PATH
@@ -17,13 +17,15 @@ COPY pyproject.toml pyproject.toml
1717
RUN pip install --no-cache-dir -q .[dev]
1818

1919
FROM reqs AS app
20-
COPY willa willa
21-
COPY README.rst README.rst
22-
COPY CHANGELOG.rst CHANGELOG.rst
2320
COPY public public
21+
COPY sql sql
2422
COPY chainlit.md chainlit.md
2523
COPY .chainlit .chainlit
24+
COPY bin bin
2625
COPY tests tests
26+
COPY README.rst README.rst
27+
COPY CHANGELOG.rst CHANGELOG.rst
28+
COPY willa willa
2729
RUN pip install --no-cache-dir -e .
2830

2931
CMD ["chainlit", "run", "/app/willa/web/app.py", "-h", "--host", "0.0.0.0"]

bin/dbinit

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
3+
# Initialise the configured database environment for use with Willa,
4+
# setting up the needed tables and indexes.
5+
#
6+
# Copyright © 2025 The Regents of the University of California. MIT license.
7+
8+
# Use the credentials from the app's environment.
9+
10+
# If `createdb` fails, that's fine; the database may already be present.
11+
PGPASSWORD=${POSTGRES_PASSWORD} createdb -O ${POSTGRES_USER} \
12+
-U ${POSTGRES_USER} -h ${POSTGRES_HOST:-db} -p ${POSTGRES_PORT:-5432} ${POSTGRES_DB}
13+
14+
# -1: Ensure atomicity: if any statement fails, no changes are persisted.
15+
PGPASSWORD=${POSTGRES_PASSWORD} psql -1 -f sql/willa.sql \
16+
-U ${POSTGRES_USER} -h ${POSTGRES_HOST:-db} -p ${POSTGRES_PORT:-5432} ${POSTGRES_DB}

sql/willa.sql

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
--
2+
-- PostgreSQL database dump
3+
--
4+
5+
SET statement_timeout = 0;
6+
SET lock_timeout = 0;
7+
SET idle_in_transaction_session_timeout = 0;
8+
SET client_encoding = 'UTF8';
9+
SET standard_conforming_strings = on;
10+
SELECT pg_catalog.set_config('search_path', '', false);
11+
SET check_function_bodies = false;
12+
SET xmloption = content;
13+
SET client_min_messages = warning;
14+
SET row_security = off;
15+
SET search_path TO public;
16+
17+
18+
GRANT CREATE ON SCHEMA public TO willa;
19+
20+
21+
CREATE EXTENSION IF NOT EXISTS pgcrypto;
22+
COMMENT ON EXTENSION pgcrypto IS 'cryptographic functions';
23+
24+
25+
CREATE TYPE "StepType" AS ENUM (
26+
'assistant_message',
27+
'embedding',
28+
'llm',
29+
'retrieval',
30+
'rerank',
31+
'run',
32+
'system_message',
33+
'tool',
34+
'undefined',
35+
'user_message'
36+
);
37+
38+
39+
SET default_tablespace = '';
40+
SET default_table_access_method = heap;
41+
42+
43+
CREATE TABLE "Element" (
44+
id text DEFAULT gen_random_uuid() NOT NULL,
45+
"createdAt" timestamp(3) without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
46+
"updatedAt" timestamp(3) without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
47+
"threadId" text,
48+
"stepId" text NOT NULL,
49+
metadata jsonb NOT NULL,
50+
mime text,
51+
name text NOT NULL,
52+
"objectKey" text,
53+
url text,
54+
"chainlitKey" text,
55+
display text,
56+
size text,
57+
language text,
58+
page integer,
59+
props jsonb
60+
);
61+
62+
63+
CREATE TABLE "Feedback" (
64+
id text DEFAULT gen_random_uuid() NOT NULL,
65+
"createdAt" timestamp(3) without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
66+
"updatedAt" timestamp(3) without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
67+
"stepId" text,
68+
name text NOT NULL,
69+
value double precision NOT NULL,
70+
comment text
71+
);
72+
73+
74+
CREATE TABLE "Step" (
75+
id text DEFAULT gen_random_uuid() NOT NULL,
76+
"createdAt" timestamp(3) without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
77+
"updatedAt" timestamp(3) without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
78+
"parentId" text,
79+
"threadId" text,
80+
input text,
81+
metadata jsonb NOT NULL,
82+
name text,
83+
output text,
84+
type "StepType" NOT NULL,
85+
"showInput" text DEFAULT 'json'::text,
86+
"isError" boolean DEFAULT false,
87+
"startTime" timestamp(3) without time zone NOT NULL,
88+
"endTime" timestamp(3) without time zone NOT NULL
89+
);
90+
91+
92+
CREATE TABLE "Thread" (
93+
id text DEFAULT gen_random_uuid() NOT NULL,
94+
"createdAt" timestamp(3) without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
95+
"updatedAt" timestamp(3) without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
96+
"deletedAt" timestamp(3) without time zone,
97+
name text,
98+
metadata jsonb NOT NULL,
99+
"userId" text,
100+
tags text[] DEFAULT ARRAY[]::text[]
101+
);
102+
103+
104+
CREATE TABLE "User" (
105+
id text DEFAULT gen_random_uuid() NOT NULL,
106+
"createdAt" timestamp(3) without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
107+
"updatedAt" timestamp(3) without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
108+
metadata jsonb NOT NULL,
109+
identifier text NOT NULL
110+
);
111+
112+
113+
CREATE TABLE _prisma_migrations (
114+
id character varying(36) NOT NULL,
115+
checksum character varying(64) NOT NULL,
116+
finished_at timestamp with time zone,
117+
migration_name character varying(255) NOT NULL,
118+
logs text,
119+
rolled_back_at timestamp with time zone,
120+
started_at timestamp with time zone DEFAULT now() NOT NULL,
121+
applied_steps_count integer DEFAULT 0 NOT NULL
122+
);
123+
124+
125+
ALTER TABLE ONLY "Element"
126+
ADD CONSTRAINT "Element_pkey" PRIMARY KEY (id);
127+
128+
ALTER TABLE ONLY "Feedback"
129+
ADD CONSTRAINT "Feedback_pkey" PRIMARY KEY (id);
130+
131+
ALTER TABLE ONLY "Step"
132+
ADD CONSTRAINT "Step_pkey" PRIMARY KEY (id);
133+
134+
ALTER TABLE ONLY "Thread"
135+
ADD CONSTRAINT "Thread_pkey" PRIMARY KEY (id);
136+
137+
ALTER TABLE ONLY "User"
138+
ADD CONSTRAINT "User_pkey" PRIMARY KEY (id);
139+
140+
ALTER TABLE ONLY _prisma_migrations
141+
ADD CONSTRAINT _prisma_migrations_pkey PRIMARY KEY (id);
142+
143+
144+
CREATE INDEX "Element_stepId_idx" ON "Element" USING btree ("stepId");
145+
146+
CREATE INDEX "Element_threadId_idx" ON "Element" USING btree ("threadId");
147+
148+
CREATE INDEX "Feedback_createdAt_idx" ON "Feedback" USING btree ("createdAt");
149+
150+
CREATE INDEX "Feedback_name_idx" ON "Feedback" USING btree (name);
151+
152+
CREATE INDEX "Feedback_name_value_idx" ON "Feedback" USING btree (name, value);
153+
154+
CREATE INDEX "Feedback_stepId_idx" ON "Feedback" USING btree ("stepId");
155+
156+
CREATE INDEX "Feedback_value_idx" ON "Feedback" USING btree (value);
157+
158+
CREATE INDEX "Step_createdAt_idx" ON "Step" USING btree ("createdAt");
159+
160+
CREATE INDEX "Step_endTime_idx" ON "Step" USING btree ("endTime");
161+
162+
CREATE INDEX "Step_name_idx" ON "Step" USING btree (name);
163+
164+
CREATE INDEX "Step_parentId_idx" ON "Step" USING btree ("parentId");
165+
166+
CREATE INDEX "Step_startTime_idx" ON "Step" USING btree ("startTime");
167+
168+
CREATE INDEX "Step_threadId_idx" ON "Step" USING btree ("threadId");
169+
170+
CREATE INDEX "Step_threadId_startTime_endTime_idx" ON "Step" USING btree ("threadId", "startTime", "endTime");
171+
172+
CREATE INDEX "Step_type_idx" ON "Step" USING btree (type);
173+
174+
CREATE INDEX "Thread_createdAt_idx" ON "Thread" USING btree ("createdAt");
175+
176+
CREATE INDEX "Thread_name_idx" ON "Thread" USING btree (name);
177+
178+
CREATE INDEX "User_identifier_idx" ON "User" USING btree (identifier);
179+
180+
181+
CREATE UNIQUE INDEX "User_identifier_key" ON "User" USING btree (identifier);
182+
183+
184+
ALTER TABLE ONLY "Element"
185+
ADD CONSTRAINT "Element_stepId_fkey" FOREIGN KEY ("stepId") REFERENCES "Step"(id) ON UPDATE CASCADE ON DELETE CASCADE;
186+
187+
ALTER TABLE ONLY "Element"
188+
ADD CONSTRAINT "Element_threadId_fkey" FOREIGN KEY ("threadId") REFERENCES "Thread"(id) ON UPDATE CASCADE ON DELETE CASCADE;
189+
190+
ALTER TABLE ONLY "Feedback"
191+
ADD CONSTRAINT "Feedback_stepId_fkey" FOREIGN KEY ("stepId") REFERENCES "Step"(id) ON UPDATE CASCADE ON DELETE SET NULL;
192+
193+
ALTER TABLE ONLY "Step"
194+
ADD CONSTRAINT "Step_parentId_fkey" FOREIGN KEY ("parentId") REFERENCES "Step"(id) ON UPDATE CASCADE ON DELETE CASCADE;
195+
196+
ALTER TABLE ONLY "Step"
197+
ADD CONSTRAINT "Step_threadId_fkey" FOREIGN KEY ("threadId") REFERENCES "Thread"(id) ON UPDATE CASCADE ON DELETE CASCADE;
198+
199+
ALTER TABLE ONLY "Thread"
200+
ADD CONSTRAINT "Thread_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"(id) ON UPDATE CASCADE ON DELETE SET NULL;
201+
202+
203+
--
204+
-- PostgreSQL database dump complete
205+
--

0 commit comments

Comments
 (0)