Skip to content

Commit b81fbba

Browse files
committed
feat(internal): 🗃️ Switch to Drizzle and Turso for the database
1 parent 0106415 commit b81fbba

File tree

31 files changed

+2605
-525
lines changed

31 files changed

+2605
-525
lines changed

.env.example

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
# When adding additional environment variables, the schema in "/src/env.js"
1010
# should be updated accordingly.
1111

12-
# Prisma
13-
# https://www.prisma.io/docs/reference/database-reference/connection-urls#env
14-
POSTGRES_PRISMA_URL="postgresql://postgres:password@localhost:5432/mcc-tools"
15-
POSTGRES_URL_NON_POOLING="postgresql://postgres:password@localhost:5432/mcc-tools"
12+
# Drizzle
13+
DATABASE_URL="file:./db.sqlite"
14+
# If using `turso dev`, only required if we start using turso extensions
15+
#DATABASE_URL="http://127.0.0.1:8080"
16+
TURSO_AUTH_TOKEN=""
1617

1718
# Next Auth
1819
# You can generate a new secret on the command line with:

.github/workflows/print_info.yaml

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
name: Automatically create and cleanup Turso DB branches for each PR
2+
3+
on:
4+
pull_request:
5+
types:
6+
- opened
7+
- reopened
8+
- synchronize
9+
- closed
10+
11+
jobs:
12+
setup:
13+
outputs:
14+
branch_id: ${{ steps.branch_db_id.outputs.result }}
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: read
18+
steps:
19+
- name: Get branch name
20+
id: branch_name
21+
uses: tj-actions/branch-names@v8
22+
- name: Checkout
23+
uses: actions/checkout@v4
24+
- name: Generate branched DB ID
25+
id: branch_db_id
26+
uses: actions/github-script@v7
27+
with:
28+
result-encoding: string
29+
script: |
30+
const { previewDbId } = await import("${{github.workspace}}/src/server/db/utils.js");
31+
const dbId = previewDbId(${{ github.event.number }}, "${{steps.branch_name.outputs.current_branch}}");
32+
console.log(`Branched DB ID: ${dbId}`);
33+
return dbId;
34+
35+
ensure_db_branch:
36+
name: Ensure the DB branch exists and is up to date
37+
needs: setup
38+
if: |
39+
github.event_name == 'pull_request' && (
40+
github.event.action == 'synchronize'
41+
|| github.event.action == 'opened'
42+
|| github.event.action == 'reopened')
43+
runs-on: ubuntu-latest
44+
steps:
45+
- name: Create DB branch
46+
id: create_db_branch
47+
run: |
48+
RESPONSE=$(curl -L -X POST \
49+
-H "Authorization: Bearer ${{ secrets.TURSO_API_TOKEN }}" \
50+
-H "Content-Type: application/json" \
51+
-d '{"name": "${{ needs.setup.outputs.branch_id }}", "group": "${{ secrets.TURSO_GROUP_NAME }}", "seed": {"type": "database", "name": "${{ secrets.TURSO_DATABASE_NAME }}"} }' \
52+
"https://api.turso.tech/v1/organizations/${{ secrets.TURSO_ORGANIZATION_NAME }}/databases")
53+
54+
if [ $? -ne 0 ]; then
55+
echo "Unable to create database, attempting to fetch the existing database instead"
56+
RESPONSE=$(curl -L 'https://api.turso.tech/v1/organizations/${{ secrets.TURSO_ORGANIZATION_NAME }}/databases/${{ needs.setup.outputs.branch_id }}' \
57+
-H 'Authorization: Bearer ${{ secrets.TURSO_API_TOKEN }}')
58+
59+
if [ $? -ne 0 ]; then
60+
echo "Could not create or fetch database"
61+
exit 1
62+
fi
63+
fi
64+
65+
echo $RESPONSE
66+
HOSTNAME=$(echo $RESPONSE | jq -r '.database.Hostname')
67+
if [ -z "$HOSTNAME" ]; then
68+
echo "Hostname not found in response"
69+
exit 1
70+
fi
71+
72+
echo "hostname=$HOSTNAME" >> $GITHUB_OUTPUT
73+
- name: Checkout
74+
uses: actions/checkout@v4
75+
- name: Run migrations
76+
env:
77+
DATABASE_URL: "libsql://${{ steps.create_db_branch.hostname }}"
78+
TURSO_AUTH_TOKEN: ${{ secrets.TURSO_API_TOKEN }}
79+
run: npm install && npm run db:generate && npm run db:migrate
80+
delete_db_branch:
81+
name: Delete DB branch and migrate prod
82+
needs: setup
83+
if: |
84+
github.event_name == 'pull_request' &&
85+
github.event.action == 'closed'
86+
runs-on: ubuntu-latest
87+
steps:
88+
- name: Delete DB branch
89+
run: |
90+
curl -L -X DELETE 'https://api.turso.tech/v1/organizations/${{ secrets.TURSO_ORGANIZATION_NAME }}/databases/${{ needs.setup.outputs.branch_id }}' \
91+
-H 'Authorization: Bearer ${{ secrets.TURSO_API_TOKEN }}'
92+
# Migrate prod DB, hopefully faster than the vercel build, or at least before anyone tries to open the website
93+
- name: Checkout
94+
if: github.event.pull_request.merged == true
95+
uses: actions/checkout@v4
96+
- name: Apply migrations to production
97+
if: github.event.pull_request.merged == true
98+
env:
99+
DATABASE_URL: "libsql://${{ secrets.TURSO_DATABASE_NAME }}"
100+
TURSO_AUTH_TOKEN: ${{ secrets.TURSO_API_TOKEN }}
101+
run: npm install && npm run db:generate && npm run db:migrate

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ yarn-error.log*
4040

4141
# typescript
4242
*.tsbuildinfo
43+
44+
# local db
45+
local.db*
46+
db.sqlite

drizzle.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { type Config } from "drizzle-kit";
2+
import "dotenv/config";
3+
4+
import { env } from "~/env";
5+
6+
export default {
7+
schema: "./src/server/db/schema.ts",
8+
out: "./src/server/db/migrations",
9+
dialect: "turso",
10+
dbCredentials: {
11+
url: env.DATABASE_URL,
12+
authToken: env.TURSO_AUTH_TOKEN,
13+
},
14+
tablesFilter: ["mcc-gadgets_*"],
15+
} satisfies Config;

0 commit comments

Comments
 (0)