Skip to content

Commit

Permalink
Production Migration CI (#8)
Browse files Browse the repository at this point in the history
* setup action

* interactive: false

* turbo

* testing complete

* readme

* test migrate ci

* fix

* exit code

* fix postgres version
  • Loading branch information
owens1127 authored Oct 6, 2024
1 parent 551709b commit 9503750
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 31 deletions.
12 changes: 12 additions & 0 deletions .github/actions/bun/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: "Bun setup"
description: "Sets up bun and installs dependencies"

runs:
using: composite
steps:
- uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.27

- shell: bash
run: bun install --frozen-lockfile
7 changes: 0 additions & 7 deletions .github/actions/format/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,5 @@ description: "Lint the project using Prettier"
runs:
using: composite
steps:
- uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.27

- shell: bash
run: bun install --frozen-lockfile

- shell: bash
run: bun format
7 changes: 0 additions & 7 deletions .github/actions/lint/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ description: "Lint the project using ESLint"
runs:
using: composite
steps:
- uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.27

- shell: bash
run: bun install --frozen-lockfile

- shell: bash
run: |
bun lint
Expand Down
7 changes: 0 additions & 7 deletions .github/actions/typecheck/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,5 @@ description: "Type-check the project using TSC"
runs:
using: composite
steps:
- uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.27

- shell: bash
run: bun install --frozen-lockfile

- shell: bash
run: bun typecheck
2 changes: 2 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Closes [story](<!--Copy Trello card link here-->)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] CI/CD changes (changing github actions or deployment scripts)
- [ ] Database migration
- [ ] Environment variable change
- [ ] New package(s)
- [ ] New dependencies
- [ ] Global style changes
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/apply-migrations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Apply Database Migrations

on:
push:
branches:
- main

jobs:
migrate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/bun

- name: Apply Migrations
env:
DATABASE_PRISMA_URL: ${{ secrets.DATABASE_PRISMA_URL }}
run: bun db:migrate:prod
34 changes: 34 additions & 0 deletions .github/workflows/check-migrations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Check Database Migrations

on:
pull_request:

jobs:
check-migrations:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:15
env:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: db
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: 1.1.27

- name: Install dependencies
run: bun install --frozen-lockfile

- name: Check migrations
run: bun db:migrate:check postgres://user:password@localhost:5432/db
3 changes: 3 additions & 0 deletions .github/workflows/static-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/bun
- uses: ./.github/actions/typecheck

format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/bun
- uses: ./.github/actions/format

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/bun
- uses: ./.github/actions/lint
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"yoavbls.pretty-ts-errors",
"bradlc.vscode-tailwindcss"
"bradlc.vscode-tailwindcss",
"Prisma.prisma"
]
}
4 changes: 2 additions & 2 deletions apps/db/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ POSTGRES_PASSWORD="password"
POSTGRES_DATABASE="good-dog"
POSTGRES_PORT=5432

DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:${POSTGRES_PORT}/${POSTGRES_DATABASE}"
DATABASE_PRISMA_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:${POSTGRES_PORT}/${POSTGRES_DATABASE}"


# Windows Version:
Expand All @@ -15,4 +15,4 @@ DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:${POS
# POSTGRES_DATABASE="good-dog"
# POSTGRES_PORT=5432

# DATABASE_URL="postgresql://user:password@localhost:5432/good-dog"
# DATABASE_PRISMA_URL="postgresql://user:password@localhost:5432/good-dog"
61 changes: 61 additions & 0 deletions apps/db/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Database Package

This package contains all the database-related stuff for the project. We use the Prisma ORM to manage the database schema and migrations.

## Table of Contents

- [Database Package](#database-package)
- [Table of Contents](#table-of-contents)
- [Setup](#setup)
- [Installation](#installation)
- [Migrations](#migrations)
- [Creating/Applying Migrations](#creatingapplying-migrations)
- [Prisma Studio](#prisma-studio)

## Setup

### Installation

1. Generate Prisma binaries and typescript types:

```sh
bun db:generate
```

2. Start the database:

```sh
bun db:up
```

3. Push changes to the schema to your database

```sh
bun db:push
```

## Migrations

### Creating/Applying Migrations

To create a new migration, follow these steps:

1. Make changes to your Prisma schema file located at [`apps/db/prisma/schema.prisma`](./prisma/schema.prisma).

2. Generate a new migration:

```sh
bun db:migrate
```

3. Follow the prompts to name your migration.

### Prisma Studio

Prisma Studio is a visual editor for your database. To open Prisma Studio, run:

```sh
bun db:studio
```

This will open a web interface where you can view and edit your database records.
7 changes: 5 additions & 2 deletions apps/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
"studio": "prisma studio",
"generate": "prisma generate",
"migrate": "prisma migrate dev",
"env": "cp .env.example .env",
"seed": "prisma db seed"
"migrate:reset": "prisma migrate reset",
"migrate:prod": "prisma migrate deploy",
"migrate:check": "bun prisma migrate diff --exit-code --from-migrations ./prisma/migrations --to-schema-datamodel ./prisma/schema.prisma --shadow-database-url",
"seed": "prisma db seed",
"env": "cp .env.example .env"
},
"dependencies": {
"@prisma/client": "5.19.1"
Expand Down
2 changes: 1 addition & 1 deletion apps/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ generator client {

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
url = env("DATABASE_PRISMA_URL")
}

model User {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"db:push": "turbo run -F @good-dog/db push",
"db:generate": "turbo run -F @good-dog/db generate",
"db:migrate": "turbo run -F @good-dog/db migrate",
"db:migrate:reset": "turbo run -F @good-dog/db migrate:reset",
"db:migrate:prod": "turbo run -F @good-dog/db migrate:prod",
"db:migrate:check": "turbo run -F @good-dog/db migrate:check --",
"db:studio": "turbo run -F @good-dog/db studio",
"db:seed": "turbo run -F @good-dog/db seed",
"__DEVELOPMENT_______": "",
Expand Down
17 changes: 13 additions & 4 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@
"cache": false,
"interactive": true
},
"migrate:reset": {
"cache": false,
"interactive": false
},
"migrate:prod": {
"cache": false,
"interactive": false
},
"migrate:check": {
"cache": false,
"interactive": false
},
"studio": {
"cache": false,
"interactive": true
Expand Down Expand Up @@ -81,11 +93,8 @@
"globalEnv": ["SKIP_ENV_VALIDATION"],
"globalPassThroughEnv": [
"NODE_ENV",
"CI",
"VERCEL",
"VERCEL_ENV",
"VERCEL_URL",
"npm_lifecycle_event",
"USER"
"DATABASE_PRISMA_URL"
]
}

0 comments on commit 9503750

Please sign in to comment.