Skip to content

Commit

Permalink
refactor(backend): migrate to psql (#10)
Browse files Browse the repository at this point in the history
* sqlite to pg

* tried generating schema

* fix schema generation

* initialize db in docker

* added seed file

* fixed seed file

---------

Co-authored-by: Dropheart <jay@abussaud.dev>
  • Loading branch information
cybercoder-naj and Dropheart committed Sep 21, 2024
1 parent ea35eb3 commit 4897c2d
Show file tree
Hide file tree
Showing 19 changed files with 296 additions and 189 deletions.
8 changes: 7 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ CLIENT_ID=
CLIENT_SECRET=
BASE_URL=http(s)://
JWT_SECRET=
WEBMASTERS=shortcode,shortcode
WEBMASTERS=shortcode,shortcode

PGUSER=
PGPASSWORD=
PGHOST=
PGPORT=
PGDB=
Binary file modified bun.lockb
Binary file not shown.
28 changes: 28 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
services:
postgres:
image: postgres:16
container_name: mad3_postgres
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: rootpasswd
POSTGRES_DB: postgres
ports:
- '5432:5432'
volumes:
- postgres_data:/var/lib/postgresql/data
- ./drizzle:/docker-entrypoint-initdb.d

web:
build: .
container_name: web
depends_on:
- postgres
ports:
- '3000:3000'
env_file:
- .env
environment:
- PGHOST=postgres

volumes:
postgres_data:
8 changes: 0 additions & 8 deletions drizzle.config.json

This file was deleted.

7 changes: 7 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'drizzle-kit';

export default defineConfig({
schema: './hono/**/schema.ts',
dialect: 'postgresql',
verbose: true
});
89 changes: 89 additions & 0 deletions drizzle/0000_reflective_madame_hydra.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
DO $$ BEGIN
CREATE TYPE "public"."app_state" AS ENUM('parents_open', 'parents_close', 'freshers_open', 'closed');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
CREATE TYPE "public"."gender" AS ENUM('male', 'female', 'other', 'n/a');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
CREATE TYPE "public"."student_role" AS ENUM('fresher', 'parent');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "meta" (
"id" integer PRIMARY KEY DEFAULT 1 CHECK (id = 1) NOT NULL,
"state" "app_state" NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "family" (
"kid" text PRIMARY KEY NOT NULL,
"id" integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "marriage" (
"id" serial PRIMARY KEY NOT NULL,
"parent1" text NOT NULL,
"parent2" text NOT NULL,
CONSTRAINT "marriage_parent1_unique" UNIQUE("parent1"),
CONSTRAINT "marriage_parent2_unique" UNIQUE("parent2")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "proposals" (
"proposer" text NOT NULL,
"proposee" text NOT NULL,
CONSTRAINT "proposals_proposer_proposee_pk" PRIMARY KEY("proposer","proposee")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "student" (
"shortcode" text PRIMARY KEY NOT NULL,
"role" "student_role" NOT NULL,
"completed_survey" boolean NOT NULL,
"jmc" boolean,
"name" text,
"gender" "gender",
"interests" json,
"socials" text[],
"about_me" text
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "family" ADD CONSTRAINT "family_kid_student_shortcode_fk" FOREIGN KEY ("kid") REFERENCES "public"."student"("shortcode") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "family" ADD CONSTRAINT "family_id_marriage_id_fk" FOREIGN KEY ("id") REFERENCES "public"."marriage"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "marriage" ADD CONSTRAINT "marriage_parent1_student_shortcode_fk" FOREIGN KEY ("parent1") REFERENCES "public"."student"("shortcode") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "marriage" ADD CONSTRAINT "marriage_parent2_student_shortcode_fk" FOREIGN KEY ("parent2") REFERENCES "public"."student"("shortcode") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "proposals" ADD CONSTRAINT "proposals_proposer_student_shortcode_fk" FOREIGN KEY ("proposer") REFERENCES "public"."student"("shortcode") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "proposals" ADD CONSTRAINT "proposals_proposee_student_shortcode_fk" FOREIGN KEY ("proposee") REFERENCES "public"."student"("shortcode") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
Loading

0 comments on commit 4897c2d

Please sign in to comment.