Skip to content

Commit

Permalink
Setup CI for database (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciabad authored Oct 24, 2023
1 parent 4b705f0 commit 0fa9ca0
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 37 deletions.
18 changes: 13 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ GOOGLE_CLIENT_SECRET=toBeChanged
BASE_URL=http://localhost:3000
NEXTAUTH_URL=http://localhost:3000

# Optional, false by default
USE_LOCAL_DB=false

# Planetscale DB connection settings
DATABASE_HOST=toBeChanged
DATABASE_NAME=toBeChanged

# Prod DB
# DATABASE_LABEL=prod
# DATABASE_USERNAME=toBeChanged
# DATABASE_PASSWORD=toBeChanged

# Stage DB
DATABASE_LABEL=stage
DATABASE_USERNAME=toBeChanged
DATABASE_PASSWORD=toBeChanged
DATABASE_NAME=toBeChanged

# Local DB
# DATABASE_LABEL=local
# USE_LOCAL_DB=true

# AWS S3 connection settings
AWS_ACCESS_KEY_ID=toBeChanged
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ jobs:
- name: 📦 Install dependencies
run: pnpm install

- name: 🆕 Check if migrations are up to date
run: pnpm run db:generate

- name: 🚨 Fail if new migrations are generated
uses: NathanielHill/fail-if-changes@master

- name: 🔦 Run lint
run: pnpm lint --fix

Expand Down
32 changes: 32 additions & 0 deletions .github/workflows/comment-if-db-migrations.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Check if db migrations are needed

on:
pull_request:
branches: [main]
paths:
- 'drizzle/**'

jobs:
comment-if-db-migrations:
permissions:
pull-requests: write
contents: read

runs-on: ubuntu-latest
steps:
- name: Comment PR - db migrated
uses: thollander/actions-comment-pull-request@main
with:
message: |
> [!NOTE]
> This PR introduces changes to the database.
**Remember to:**
1. Run the migrations in [stage database branch](https://app.planetscale.com/descobreix-begur-app/descobreix-begur-app/stage) with:
```bash
pnpm db:push
```
1. Create a [deploy request in Planetscale](https://app.planetscale.com/descobreix-begur-app/descobreix-begur-app/deploy-requests/)
1. Merge the deploy request from Planetscale just before merging the PR.
comment_tag: execution
22 changes: 13 additions & 9 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import path from 'node:path'
dotenv.config({ path: path.resolve(process.cwd(), '.env.local') })
dotenv.config({ path: path.resolve(process.cwd(), '.env') })

if (process.env.USE_LOCAL_DB !== 'true') {
if (
!process.env.DATABASE_HOST ||
!process.env.DATABASE_USERNAME ||
!process.env.DATABASE_PASSWORD ||
!process.env.DATABASE_NAME
) {
throw new Error('Missing environment variables for database')
}
if (
process.env.USE_LOCAL_DB !== 'true' &&
process.env.SKIP_ENV_VALIDATION !== 'true'
) {
if (!process.env.DATABASE_HOST)
throw new Error('Missing environment variable DATABASE_HOST')
if (!process.env.DATABASE_NAME)
throw new Error('Missing environment variable DATABASE_NAME')
if (!process.env.DATABASE_USERNAME)
throw new Error('Missing environment variable DATABASE_USERNAME')
if (!process.env.DATABASE_PASSWORD)
throw new Error('Missing environment variable DATABASE_PASSWORD')
}

export default {
schema: './src/server/db/schema/*',
out: './drizzle',
Expand Down
1 change: 1 addition & 0 deletions drizzle/0003_colorful_fat_cobra.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `place` ADD `mainImage` varchar(1024);
285 changes: 285 additions & 0 deletions drizzle/meta/0003_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
{
"version": "5",
"dialect": "mysql",
"id": "aea0cf0a-32d1-453e-b0e7-2419b3070b31",
"prevId": "4083d305-aa61-41e2-9e44-89f0efa632c2",
"tables": {
"place": {
"name": "place",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": false,
"notNull": true,
"autoincrement": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"mainImage": {
"name": "mainImage",
"type": "varchar(1024)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"place_id": {
"name": "place_id",
"columns": [
"id"
]
}
},
"uniqueConstraints": {}
},
"account": {
"name": "account",
"columns": {
"userId": {
"name": "userId",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"type": {
"name": "type",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"provider": {
"name": "provider",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"providerAccountId": {
"name": "providerAccountId",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"refresh_token": {
"name": "refresh_token",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"access_token": {
"name": "access_token",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"expires_at": {
"name": "expires_at",
"type": "int",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"token_type": {
"name": "token_type",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"scope": {
"name": "scope",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"id_token": {
"name": "id_token",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"session_state": {
"name": "session_state",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"account_provider_providerAccountId": {
"name": "account_provider_providerAccountId",
"columns": [
"provider",
"providerAccountId"
]
}
},
"uniqueConstraints": {}
},
"session": {
"name": "session",
"columns": {
"sessionToken": {
"name": "sessionToken",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"userId": {
"name": "userId",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"expires": {
"name": "expires",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"session_sessionToken": {
"name": "session_sessionToken",
"columns": [
"sessionToken"
]
}
},
"uniqueConstraints": {}
},
"user": {
"name": "user",
"columns": {
"id": {
"name": "id",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"hashedPassword": {
"name": "hashedPassword",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"email": {
"name": "email",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"emailVerified": {
"name": "emailVerified",
"type": "timestamp(3)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"image": {
"name": "image",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"user_id": {
"name": "user_id",
"columns": [
"id"
]
}
},
"uniqueConstraints": {
"user_email_unique": {
"name": "user_email_unique",
"columns": [
"email"
]
}
}
},
"verificationToken": {
"name": "verificationToken",
"columns": {
"identifier": {
"name": "identifier",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"token": {
"name": "token",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"expires": {
"name": "expires",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"verificationToken_identifier_token": {
"name": "verificationToken_identifier_token",
"columns": [
"identifier",
"token"
]
}
},
"uniqueConstraints": {}
}
},
"schemas": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
Loading

0 comments on commit 0fa9ca0

Please sign in to comment.