Skip to content

Commit

Permalink
convert to sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
omfj committed Aug 22, 2024
1 parent 87accca commit 6fe45a0
Show file tree
Hide file tree
Showing 90 changed files with 1,763 additions and 29,331 deletions.
13 changes: 1 addition & 12 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

env:
ADMIN_KEY: foobar
DATABASE_URL: postgres://postgres:postgres@localhost:5432/echo-web
DATABASE_URL: file:./dev.db
NEXT_PUBLIC_SANITY_DATASET: "testing"
FEIDE_CLIENT_ID: ${{ secrets.FEIDE_CLIENT_ID }}
FEIDE_CLIENT_SECRET: ${{ secrets.FEIDE_CLIENT_SECRET }}
Expand All @@ -18,17 +18,6 @@ jobs:
ci:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:15.7
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: echo-web
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/pnpm-install
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/staging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Staging environment

on:
pull_request:
types: [opened, synchronize, closed]

jobs:
setup-db:
if: !always()
runs-on: ubuntu-latest

steps:
- name: Checkout branch
uses: actions/checkout@v4

- name: Setup node
uses: ./.github/actions/pnpm-install

- name: Create Database
uses: tursodatabase/create-database-action@v1
with:
organization_name: ${{ secrets.TURSO_ORGANIZATION_NAME }}
api_token: ${{ secrets.TURSO_API_TOKEN }}
existing_database_name: ${{ secrets.TURSO_DATABASE_NAME }}
new_database_name: ${{ github.ref_name }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package-lock.json

# db
postgres-data/
*.db

# email
.react-email/
Expand Down
4 changes: 4 additions & 0 deletions apps/web/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const config = {
},
},

experimental: {
esmExternals: false,
},

images: {
remotePatterns: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import { eq } from "drizzle-orm";

import { db, isPostgresIshError } from "@echo-webkom/db";
import { db } from "@echo-webkom/db";
import { isDatabaseError, SQLITE_CONSTRAINT_UNIQUE } from "@echo-webkom/db/error";
import { accessRequests, whitelist } from "@echo-webkom/db/schemas";
import { AccessGrantedEmail } from "@echo-webkom/email";
import { emailClient } from "@echo-webkom/email/client";
Expand Down Expand Up @@ -42,8 +43,8 @@ export const grantAccessAction = async (accessRequestId: string) => {

await db.delete(accessRequests).where(eq(accessRequests.id, accessRequestId));
} catch (e) {
if (isPostgresIshError(e)) {
if (e.code === "23505") {
if (isDatabaseError(e)) {
if (e.code === SQLITE_CONSTRAINT_UNIQUE) {
return {
success: false,
message: "E-posten er allerede i whitelist",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import { nanoid } from "nanoid";
import { z } from "zod";

import { db, isPostgresIshError } from "@echo-webkom/db";
import { db } from "@echo-webkom/db";
import { isDatabaseError, SQLITE_CONSTRAINT_UNIQUE } from "@echo-webkom/db/error";
import { accessRequests } from "@echo-webkom/db/schemas";
import { AccessRequestNotificationEmail } from "@echo-webkom/email";
import { emailClient } from "@echo-webkom/email/client";
Expand Down Expand Up @@ -59,8 +60,8 @@ export const requestAccess = async (data: IRequestAccessForm): Promise<RequestAc
};
}

if (isPostgresIshError(e)) {
if (e.code === "23505") {
if (isDatabaseError(e)) {
if (e.code === SQLITE_CONSTRAINT_UNIQUE) {
return {
success: false,
message: "Du har allerede sendt en forespørsel",
Expand Down
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
"db:generate": "pnpm --filter=db db:generate",
"db:migrate": "pnpm --filter=db db:migrate",
"db:check": "pnpm --filter=db db:check",
"db:up": "pnpm --filter=db db:up",
"db:down": "pnpm --filter=db db:down",
"db:remove": "pnpm db:down && rm -rf packages/db/postgres-data",
"db:setup": "pnpm db:remove && pnpm db:up && pnpm sleep2s && pnpm db:migrate",
"seed": "pnpm --filter=seeder seed:start",
"sleep2s": "node -e \"setTimeout(() => process.exit(0), 2000)\"",
"email:preview": "pnpm --filter=email preview",
"lint": "turbo run lint",
"lint:fix": "turbo run lint -- --fix",
Expand Down
4 changes: 2 additions & 2 deletions packages/auth/src/drizzle-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import crypto from "node:crypto";
import { and, eq } from "drizzle-orm";
import { type PgDatabase } from "drizzle-orm/pg-core";
import { type BaseSQLiteDatabase } from "drizzle-orm/sqlite-core";
import { type Adapter } from "next-auth/adapters";

import { accounts, sessions, users, verificationTokens } from "@echo-webkom/db/schemas";

export const DrizzleAdapter = (client: InstanceType<typeof PgDatabase>): Adapter => {
export const DrizzleAdapter = (client: InstanceType<typeof BaseSQLiteDatabase>): Adapter => {
return {
async createUser(data) {
return await client
Expand Down
7 changes: 5 additions & 2 deletions packages/db/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { defineConfig } from "drizzle-kit";

export default defineConfig({
dialect: "postgresql",
out: "./drizzle/migrations",
schema: "./src/schemas",
schema: "./src/schemas/index.ts",
dialect: "sqlite",
driver: "turso",
dbCredentials: {
url: process.env.DATABASE_URL!,
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
authToken: process.env.DATABASE_AUTH_TOKEN || undefined,
},
});
2 changes: 1 addition & 1 deletion packages/db/drizzle/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
import process from "node:process";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import { migrate } from "drizzle-orm/libsql/migrator";

import { db } from "../src";

Expand Down
223 changes: 223 additions & 0 deletions packages/db/drizzle/migrations/0000_curved_jazinda.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
CREATE TABLE `access_request` (
`id` text PRIMARY KEY NOT NULL,
`email` text NOT NULL,
`reason` text NOT NULL,
`created_at` integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE `account` (
`user_id` text NOT NULL,
`type` text NOT NULL,
`provider` text NOT NULL,
`provider_account_id` text NOT NULL,
`refresh_token` text,
`access_token` text,
`expires_at` integer,
`token_type` text,
`scope` text,
`id_token` text,
`session_state` text,
PRIMARY KEY(`provider`, `provider_account_id`),
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `answer` (
`user_id` text NOT NULL,
`happening_id` text NOT NULL,
`question_id` text NOT NULL,
`answer` blob,
PRIMARY KEY(`happening_id`, `question_id`, `user_id`),
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`happening_id`) REFERENCES `happening`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`question_id`) REFERENCES `question`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `comment` (
`id` text PRIMARY KEY NOT NULL,
`post_id` text NOT NULL,
`parent_comment_id` text,
`user_id` text,
`content` text NOT NULL,
`created_at` integer NOT NULL,
`updated_at` integer NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE TABLE `degree` (
`id` text PRIMARY KEY NOT NULL,
`name` text NOT NULL
);
--> statement-breakpoint
CREATE TABLE `group` (
`id` text(255) PRIMARY KEY NOT NULL,
`name` text(255) NOT NULL
);
--> statement-breakpoint
CREATE TABLE `happenings_to_groups` (
`happening_id` text NOT NULL,
`group_id` text NOT NULL,
PRIMARY KEY(`group_id`, `happening_id`),
FOREIGN KEY (`happening_id`) REFERENCES `happening`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`group_id`) REFERENCES `group`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `happening` (
`id` text PRIMARY KEY NOT NULL,
`slug` text NOT NULL,
`title` text NOT NULL,
`type` text DEFAULT 'event' NOT NULL,
`date` integer,
`registration_groups` blob,
`registration_start_groups` integer,
`registration_start` integer,
`registration_end` integer
);
--> statement-breakpoint
CREATE TABLE `kv` (
`key` text PRIMARY KEY NOT NULL,
`value` blob,
`ttl` integer
);
--> statement-breakpoint
CREATE TABLE `question` (
`id` text PRIMARY KEY NOT NULL,
`title` text NOT NULL,
`required` integer DEFAULT false NOT NULL,
`type` text DEFAULT 'text' NOT NULL,
`is_sensitive` integer DEFAULT false NOT NULL,
`options` blob,
`happening_id` text NOT NULL,
FOREIGN KEY (`happening_id`) REFERENCES `happening`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `reaction` (
`react_to_key` text NOT NULL,
`emoji_id` integer NOT NULL,
`user_id` text NOT NULL,
`created_at` integer NOT NULL,
PRIMARY KEY(`emoji_id`, `react_to_key`, `user_id`),
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `registration` (
`user_id` text NOT NULL,
`happening_id` text NOT NULL,
`status` text DEFAULT 'waiting' NOT NULL,
`unregister_reason` text,
`created_at` integer NOT NULL,
`prev_status` text,
`changed_at` integer,
`changed_by` text,
PRIMARY KEY(`happening_id`, `user_id`),
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`happening_id`) REFERENCES `happening`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `session` (
`session_token` text PRIMARY KEY NOT NULL,
`user_id` text NOT NULL,
`expires` integer NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `site_feedback` (
`id` text PRIMARY KEY NOT NULL,
`name` text,
`email` text,
`message` text NOT NULL,
`category` text NOT NULL,
`is_read` integer DEFAULT false NOT NULL,
`created_at` integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE `spot_range` (
`id` text PRIMARY KEY NOT NULL,
`happening_id` text NOT NULL,
`spots` integer NOT NULL,
`min_year` integer NOT NULL,
`max_year` integer NOT NULL,
FOREIGN KEY (`happening_id`) REFERENCES `happening`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `users_to_groups` (
`user_id` text NOT NULL,
`group_id` text NOT NULL,
`is_leader` integer DEFAULT false NOT NULL,
PRIMARY KEY(`group_id`, `user_id`),
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`group_id`) REFERENCES `group`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `user` (
`id` text PRIMARY KEY NOT NULL,
`name` text,
`email` text NOT NULL,
`email_verified` integer,
`image` text,
`alternative_email` text,
`degree_id` text,
`year` integer,
`type` text DEFAULT 'student' NOT NULL,
`is_banned` integer DEFAULT false NOT NULL,
`banned_from_strike` integer,
`last_sign_in_at` integer,
`updated_at` integer,
`created_at` integer,
FOREIGN KEY (`degree_id`) REFERENCES `degree`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `verification_token` (
`identifier` text NOT NULL,
`token` text NOT NULL,
`expires` integer NOT NULL,
PRIMARY KEY(`identifier`, `token`)
);
--> statement-breakpoint
CREATE TABLE `shopping_list_item` (
`id` text PRIMARY KEY NOT NULL,
`user_id` text NOT NULL,
`name` text NOT NULL,
`created_at` integer NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `users_to_shopping_list_items` (
`user_id` text NOT NULL,
`item_id` text NOT NULL,
`created_at` integer NOT NULL,
PRIMARY KEY(`item_id`, `user_id`),
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`item_id`) REFERENCES `shopping_list_item`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `whitelist` (
`email` text PRIMARY KEY NOT NULL,
`expires_at` integer NOT NULL,
`reason` text NOT NULL
);
--> statement-breakpoint
CREATE TABLE `strike` (
`id` integer PRIMARY KEY NOT NULL,
`user_id` text NOT NULL,
`strike_info_id` text NOT NULL,
`is_deleted` integer DEFAULT false NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`strike_info_id`) REFERENCES `strike_info`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `strike_info` (
`id` text PRIMARY KEY NOT NULL,
`happening_id` text NOT NULL,
`issuer_id` text NOT NULL,
`reason` text NOT NULL,
`created_at` integer NOT NULL,
FOREIGN KEY (`happening_id`) REFERENCES `happening`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`issuer_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE UNIQUE INDEX `access_request_email_unique` ON `access_request` (`email`);--> statement-breakpoint
CREATE INDEX `post_idx` ON `comment` (`post_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `happening_slug_unique` ON `happening` (`slug`);--> statement-breakpoint
CREATE INDEX `slug_idx` ON `happening` (`slug`);--> statement-breakpoint
CREATE INDEX `email_idx` ON `user` (`email`);--> statement-breakpoint
CREATE INDEX `user_idx` ON `strike` (`user_id`,`id`);
Loading

0 comments on commit 6fe45a0

Please sign in to comment.