Skip to content

Commit

Permalink
chore: use planetscale for db instead of neon (#31)
Browse files Browse the repository at this point in the history
* migrate from neon to planetscale

* remove migration step on ci

* db:studio command
  • Loading branch information
Din authored Feb 19, 2024
1 parent 0fb6376 commit 3e0fb22
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 217 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/deploy-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ jobs:
runs-on: ubuntu-latest
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
DATABASE_URL: ${{ secrets.PREVIEW_DATABASE_URL }}

steps:
- uses: actions/checkout@v3
Expand All @@ -22,6 +21,4 @@ jobs:

- run: pnpm run typecheck

- run: pnpm run --filter @dinstack/api db:migrate

- run: pnpm run deploy:preview
3 changes: 0 additions & 3 deletions .github/workflows/deploy-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ jobs:
runs-on: ubuntu-latest
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
DATABASE_URL: ${{ secrets.PRODUCTION_DATABASE_URL }}

steps:
- uses: actions/checkout@v3
Expand All @@ -22,6 +21,4 @@ jobs:

- run: pnpm run typecheck

- run: pnpm run --filter @dinstack/api db:migrate

- run: pnpm run deploy:production
1 change: 1 addition & 0 deletions @api/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL=
4 changes: 0 additions & 4 deletions @api/database/migrations/0000_youthful_alex_power.sql

This file was deleted.

38 changes: 0 additions & 38 deletions @api/database/migrations/meta/0000_snapshot.json

This file was deleted.

13 changes: 0 additions & 13 deletions @api/database/migrations/meta/_journal.json

This file was deleted.

20 changes: 17 additions & 3 deletions @api/database/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { pgTable, timestamp, serial } from 'drizzle-orm/pg-core'
import { sql } from 'drizzle-orm'
import { mysqlTableCreator, timestamp, serial, varchar } from 'drizzle-orm/mysql-core'

export const Tests = pgTable('tests', {
/**
* @see https://orm.drizzle.team/docs/goodies#multi-project-schema
* @see https://orm.drizzle.team/kit-docs/conf#multi-project-schema
*/
const mysqlTable = mysqlTableCreator((name) => `dinstack_${name}`)

export const Users = mysqlTable('users', {
id: serial('id'),
createdAt: timestamp('created_at').defaultNow().notNull(),
name: varchar('name', { length: 255 }),
updatedAt: timestamp('updated_at')
.notNull()
.default(sql`CURRENT_TIMESTAMP`)
.onUpdateNow(),
createdAt: timestamp('created_at')
.notNull()
.default(sql`CURRENT_TIMESTAMP`),
})
7 changes: 5 additions & 2 deletions @api/drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import type { Config } from 'drizzle-kit'
export default {
schema: './database/schema.ts',
out: './database/migrations',
driver: 'pg',
driver: 'mysql2',
dbCredentials: {
connectionString: process.env.DATABASE_URL!,
uri: process.env.DATABASE_URL!,
},
tablesFilter: ['dinstack_*'],
strict: true,
verbose: true,
} satisfies Config
25 changes: 12 additions & 13 deletions @api/lib/db.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import * as schema from '../database/schema'
import type { Env } from './env'
import { neon, neonConfig } from '@neondatabase/serverless'
import { connect } from '@planetscale/database'
import type { LogWriter } from 'drizzle-orm/logger'
import { DefaultLogger } from 'drizzle-orm/logger'
import { drizzle as drizzleNeon } from 'drizzle-orm/neon-http'
import { withReplicas } from 'drizzle-orm/pg-core'
import { drizzle as drizzlePostgres } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'

neonConfig.fetchConnectionCache = true
import { withReplicas } from 'drizzle-orm/mysql-core'
import { drizzle } from 'drizzle-orm/planetscale-serverless'

class MyLogWriter implements LogWriter {
write(message: string) {
Expand All @@ -22,17 +18,20 @@ class MyLogWriter implements LogWriter {
const logger = new DefaultLogger({ writer: new MyLogWriter() })

export function createDb({ env }: { env: Env }) {
const write = drizzlePostgres(postgres(env.DATABASE_URL), {
schema,
logger: env.WORKER_ENV === 'development' ? logger : false,
const dbConnection = connect({
url: env.DATABASE_URL,
fetch: (opts, init) => {
delete init?.cache
return fetch(opts, init)
},
})
const read = drizzleNeon(neon(env.DATABASE_URL), {

const write = drizzle(dbConnection, {
schema,
logger: env.WORKER_ENV === 'development' ? logger : false,
})

// @ts-expect-error TODO: fix type for withReplicas
return withReplicas(write, [read])
return withReplicas(write, [write])
}

export type Db = ReturnType<typeof createDb>
12 changes: 5 additions & 7 deletions @api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@
"build": "tsc --build && tsc-alias",
"preview": "wrangler dev --env local --port 8000",
"typecheck": "tsc --noEmit",
"db:migration": "drizzle-kit generate:pg",
"db:migrate": "tsx scripts/migrate.ts",
"db:push": "drizzle-kit push:mysql",
"db:studio": "drizzle-kit studio",
"deploy:preview": "wrangler deploy --env preview",
"deploy:production": "wrangler deploy --env production"
},
"dependencies": {
"@clerk/backend": "^0.37.3",
"@knocklabs/node": "^0.6.1",
"@lemonsqueezy/lemonsqueezy.js": "1.2.2",
"@neondatabase/serverless": "^0.6.1",
"@planetscale/database": "^1.16.0",
"@trpc/server": "11.0.0-next-beta.248",
"drizzle-orm": "^0.29.3",
"drizzle-zod": "^0.5.1",
"hono": "^3.12.6",
"postgres": "^3.4.3",
"posthog-node": "^3.5.0",
"superjson": "^2.2.1",
"svix": "^1.16.0",
Expand All @@ -35,7 +33,7 @@
"@cloudflare/workers-types": "^4.20231218.0",
"@total-typescript/ts-reset": "^0.5.1",
"@types/node": "^20.11.13",
"drizzle-kit": "^0.20.13",
"tsx": "^4.7.0"
"drizzle-kit": "^0.20.14",
"mysql2": "^3.9.1"
}
}
15 changes: 0 additions & 15 deletions @api/scripts/migrate.ts

This file was deleted.

2 changes: 1 addition & 1 deletion @api/wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "dinstack-api"
main = "core/worker.ts"
compatibility_date = "2023-12-30"
compatibility_date = "2024-02-19"
compatibility_flags = ["nodejs_compat"]

[placement]
Expand Down
Empty file removed @web/providers/notification.tsx
Empty file.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
"tsc-alias": "^1.8.8",
"turbo": "^1.11.3",
"typescript": "^5.3.3",
"wrangler": "^3.28.2"
"wrangler": "^3.28.3"
}
}
Loading

0 comments on commit 3e0fb22

Please sign in to comment.