From e08667927d92eafb42eefbebd2381df76fa2f2fc Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 30 Sep 2026 09:36:42 -0400 Subject: [PATCH] Add sessions, users tables to schema --- .../20260829174406_create_auth_tables.sql | 21 +++++ api/db/schema.sql | 93 ++++++++++++++++++- 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 api/db/migrations/20260829174406_create_auth_tables.sql diff --git a/api/db/migrations/20260829174406_create_auth_tables.sql b/api/db/migrations/20260829174406_create_auth_tables.sql new file mode 100644 index 0000000..0f7edb3 --- /dev/null +++ b/api/db/migrations/20260829174406_create_auth_tables.sql @@ -0,0 +1,21 @@ +-- migrate:up +CREATE TABLE users ( + id SERIAL PRIMARY KEY, + username TEXT UNIQUE NOT NULL, + password_hash TEXT NOT NULL, + created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL +); + +CREATE TABLE sessions ( + id VARCHAR(64) PRIMARY KEY, + user_id INT REFERENCES users(id) ON DELETE CASCADE NOT NULL, + expires_at TIMESTAMPTZ NOT NULL, + created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL +); + +CREATE INDEX idx_sessions_expires_at ON sessions(expires_at); + +-- migrate:down +DROP TABLE sessions; +DROP TABLE users; + diff --git a/api/db/schema.sql b/api/db/schema.sql index e2a99cd..332aafd 100644 --- a/api/db/schema.sql +++ b/api/db/schema.sql @@ -73,6 +73,50 @@ CREATE TABLE public.schema_migrations ( ); +-- +-- Name: sessions; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.sessions ( + id character varying(64) NOT NULL, + user_id integer NOT NULL, + expires_at timestamp with time zone NOT NULL, + created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL +); + + +-- +-- Name: users; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.users ( + id integer NOT NULL, + username text NOT NULL, + password_hash text NOT NULL, + created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL +); + + +-- +-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.users_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id; + + -- -- Name: categories id; Type: DEFAULT; Schema: public; Owner: - -- @@ -80,6 +124,13 @@ CREATE TABLE public.schema_migrations ( ALTER TABLE ONLY public.categories ALTER COLUMN id SET DEFAULT nextval('public.categories_id_seq'::regclass); +-- +-- Name: users id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass); + + -- -- Name: categories categories_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -96,6 +147,30 @@ ALTER TABLE ONLY public.schema_migrations ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version); +-- +-- Name: sessions sessions_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.sessions + ADD CONSTRAINT sessions_pkey PRIMARY KEY (id); + + +-- +-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.users + ADD CONSTRAINT users_pkey PRIMARY KEY (id); + + +-- +-- Name: users users_username_key; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.users + ADD CONSTRAINT users_username_key UNIQUE (username); + + -- -- Name: idx_categories_path_gist; Type: INDEX; Schema: public; Owner: - -- @@ -103,6 +178,21 @@ ALTER TABLE ONLY public.schema_migrations CREATE INDEX idx_categories_path_gist ON public.categories USING gist (path); +-- +-- Name: idx_sessions_expires_at; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX idx_sessions_expires_at ON public.sessions USING btree (expires_at); + + +-- +-- Name: sessions sessions_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.sessions + ADD CONSTRAINT sessions_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; + + -- -- PostgreSQL database dump complete -- @@ -115,4 +205,5 @@ CREATE INDEX idx_categories_path_gist ON public.categories USING gist (path); -- INSERT INTO public.schema_migrations (version) VALUES - ('20260824235922'); + ('20260824235922'), + ('20260829174406');