Skip to content

Commit

Permalink
fixup! Neon migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Perdolique committed Aug 21, 2024
1 parent 1d8922d commit a976854
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 22 deletions.
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM postgres:16

ADD "https://github.com/pksunkara/pgx_ulid/releases/download/v0.1.5/pgx_ulid-v0.1.5-pg16-amd64-linux-gnu.deb" \
"/tmp/pgx_ulid.deb"

RUN dpkg -i /tmp/pgx_ulid.deb
RUN rm /tmp/pgx_ulid.deb

EXPOSE 5432

CMD ["postgres"]
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
services:
postgres:
build: .
container_name: postgres
environment:
POSTGRES_PASSWORD: perd
POSTGRES_USER: perd
POSTGRES_DB: perd
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data

volumes:
postgres_data:
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "module",
"scripts": {
"build": "nuxt build",
"deploy": "npm run build && wrangler pages deploy",
"deploy": "wrangler pages deploy",
"dev": "nuxt dev",
"generate": "nuxt generate",
"postinstall": "nuxt prepare",
Expand Down Expand Up @@ -32,6 +32,7 @@
"@vueuse/core": "11.0.1",
"drizzle-kit": "0.24.0",
"nitro-cloudflare-dev": "0.1.6",
"postgres": "3.4.4",
"sass": "1.77.8",
"ufo": "1.5.4",
"valibot": "0.37.0",
Expand Down
4 changes: 3 additions & 1 deletion server/database/migrations/0000_dry_wasp.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
CREATE EXTENSION IF NOT EXISTS ulid;

CREATE TABLE IF NOT EXISTS "checklistItems" (
"id" "ulid" PRIMARY KEY DEFAULT gen_monotonic_ulid() NOT NULL,
"checklistId" "ulid" NOT NULL,
Expand Down Expand Up @@ -63,4 +65,4 @@ EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "createdAtIndex" ON "checklists" USING btree ("createdAt");
CREATE INDEX IF NOT EXISTS "createdAtIndex" ON "checklists" USING btree ("createdAt");
3 changes: 2 additions & 1 deletion server/middleware/database.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NeonHttpDatabase } from 'drizzle-orm/neon-http'
import { PostgresJsDatabase } from 'drizzle-orm/postgres-js'

export default defineEventHandler(async ({ context }) => {
if (context.db === undefined) {
Expand All @@ -10,6 +11,6 @@ export default defineEventHandler(async ({ context }) => {

declare module 'h3' {
interface H3EventContext {
db: NeonHttpDatabase<typeof tables>
db: NeonHttpDatabase<typeof tables> | PostgresJsDatabase<typeof tables>
}
}
28 changes: 21 additions & 7 deletions server/utils/database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { drizzle } from 'drizzle-orm/neon-http'
import { neon } from '@neondatabase/serverless';
import { drizzle as drizzlePostgres, PostgresJsDatabase } from 'drizzle-orm/postgres-js'
import { drizzle as drizzleNeon, NeonHttpDatabase } from 'drizzle-orm/neon-http'
import postgres from 'postgres'
import { neon } from '@neondatabase/serverless'
import * as schema from '../database/schema'

export const tables = schema
Expand All @@ -9,12 +11,24 @@ export function createDrizzle() {
throw new Error('DATABASE_URL is not defined')
}

const db = neon(process.env.DATABASE_URL)
let drizzleDb : NeonHttpDatabase<typeof tables> | PostgresJsDatabase<typeof tables>

if (process.env.NODE_ENV === 'development') {
const db = postgres(process.env.DATABASE_URL)

drizzleDb = drizzlePostgres(db, {
schema,
logger: true
})
} else {
const db = neon(process.env.DATABASE_URL)

drizzleDb = drizzleNeon(db, {
schema,
logger: true
})
}

const drizzleDb = drizzle(db, {
schema,
logger: true
})

return drizzleDb
}
12 changes: 0 additions & 12 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,3 @@ upload_source_maps = true

[dev]
port = 8888

[[env.production.d1_databases]]
binding = "DB"
database_name = "perd-prod"
database_id = "313b89d7-745e-4adf-94d6-3c982743ed41"
migrations_dir = "server/database/migrations"

[[env.preview.d1_databases]]
binding = "DB"
database_name = "perd-stage"
database_id = "57811910-c52c-4a47-a48a-d0e7ac4c08fd"
migrations_dir = "server/database/migrations"

0 comments on commit a976854

Please sign in to comment.