Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
# When adding additional environment variables, the schema in "/src/env.js"
# should be updated accordingly.

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

# Next Auth
# You can generate a new secret on the command line with:
Expand Down
37 changes: 0 additions & 37 deletions .github/workflows/print_info.yaml

This file was deleted.

120 changes: 120 additions & 0 deletions .github/workflows/turso_branching.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
name: Automatically create and cleanup Turso DB branches for each PR

on:
pull_request_target:
types:
- opened
- reopened
- synchronize
- closed

jobs:
setup:
outputs:
branch_id: ${{ steps.branch_db_id.outputs.result }}
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Get user permissions
id: permissions
uses: actions-cool/check-user-permission@v2
with:
require: write
username: ${{ github.triggering_actor }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Check user permissions
if: steps.permissions.outputs.require-result == 'false'
run: |
echo "${{ github.triggering_actor }} does not have permissions on this repo."
echo "A user with write permissions should very carefully check the diff of this PR and then re-run the workflow if it is safe"
exit 1
- name: Get branch name
id: branch_name
uses: tj-actions/branch-names@v8
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }} # This is dangerous without the first access check
- name: Generate branched DB ID
id: branch_db_id
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const { previewDbId } = await import("${{github.workspace}}/src/server/db/utils.js");
const dbId = previewDbId(${{ github.event.number }}, "${{steps.branch_name.outputs.current_branch}}");
console.log(`Branched DB ID: ${dbId}`);
return dbId;

ensure_db_branch:
name: Ensure the DB branch exists and is up to date
needs: setup
if: |
github.event_name == 'pull_request' && (
github.event.action == 'synchronize'
|| github.event.action == 'opened'
|| github.event.action == 'reopened')
runs-on: ubuntu-latest
steps:
- name: Create DB branch
id: create_db_branch
run: |
RESPONSE=$(curl -L -X POST \
-H "Authorization: Bearer ${{ secrets.TURSO_API_TOKEN }}" \
-H "Content-Type: application/json" \
-d '{"name": "${{ needs.setup.outputs.branch_id }}", "group": "${{ secrets.TURSO_GROUP_NAME }}", "seed": {"type": "database", "name": "${{ secrets.TURSO_DATABASE_NAME }}"} }' \
"https://api.turso.tech/v1/organizations/${{ secrets.TURSO_ORGANIZATION_NAME }}/databases")

if [ $? -ne 0 ]; then
echo "Unable to create database, attempting to fetch the existing database instead"
RESPONSE=$(curl -L 'https://api.turso.tech/v1/organizations/${{ secrets.TURSO_ORGANIZATION_NAME }}/databases/${{ needs.setup.outputs.branch_id }}' \
-H 'Authorization: Bearer ${{ secrets.TURSO_API_TOKEN }}')

if [ $? -ne 0 ]; then
echo "Could not create or fetch database"
exit 1
fi
fi

HOSTNAME=$(echo $RESPONSE | jq -r '.database.Hostname')
if [ -z "$HOSTNAME" ]; then
echo "Hostname not found in response"
exit 1
fi

echo "hostname=$HOSTNAME" >> $GITHUB_OUTPUT
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }} # This is dangerous without the first access check
- name: Run migrations
env:
DATABASE_URL: "libsql://${{ steps.create_db_branch.hostname }}"
TURSO_AUTH_TOKEN: ${{ secrets.TURSO_API_TOKEN }}
run: npm install && npm run db:generate && npm run db:migrate
delete_db_branch:
name: Delete DB branch and migrate prod
needs: setup
if: |
github.event_name == 'pull_request' &&
github.event.action == 'closed'
runs-on: ubuntu-latest
steps:
- name: Delete DB branch
run: |
curl -L -X DELETE 'https://api.turso.tech/v1/organizations/${{ secrets.TURSO_ORGANIZATION_NAME }}/databases/${{ needs.setup.outputs.branch_id }}' \
-H 'Authorization: Bearer ${{ secrets.TURSO_API_TOKEN }}'
# Migrate prod DB, hopefully faster than the vercel build, or at least before anyone tries to open the website
- name: Checkout
if: github.event.pull_request.merged == true
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }} # This is dangerous without the first access check
- name: Apply migrations to production
if: github.event.pull_request.merged == true
env:
DATABASE_URL: "libsql://${{ secrets.TURSO_DATABASE_NAME }}"
TURSO_AUTH_TOKEN: ${{ secrets.TURSO_API_TOKEN }}
run: npm install && npm run db:generate && npm run db:migrate
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ yarn-error.log*

# typescript
*.tsbuildinfo

# local db
local.db*
db.sqlite
15 changes: 15 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { type Config } from "drizzle-kit";
import "dotenv/config";

import { env } from "~/env";

export default {
schema: "./src/server/db/schema.ts",
out: "./src/server/db/migrations",
dialect: "turso",
dbCredentials: {
url: env.DATABASE_URL,
authToken: env.TURSO_AUTH_TOKEN,
},
tablesFilter: ["mcc-gadgets_*"],
} satisfies Config;
Loading