diff --git a/.github/actions/bun/action.yml b/.github/actions/bun/action.yml new file mode 100644 index 0000000..e7f8088 --- /dev/null +++ b/.github/actions/bun/action.yml @@ -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 diff --git a/.github/actions/format/action.yml b/.github/actions/format/action.yml index 72eeeef..41e9033 100644 --- a/.github/actions/format/action.yml +++ b/.github/actions/format/action.yml @@ -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 diff --git a/.github/actions/lint/action.yml b/.github/actions/lint/action.yml index fc8c51c..f4e13e3 100644 --- a/.github/actions/lint/action.yml +++ b/.github/actions/lint/action.yml @@ -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 diff --git a/.github/actions/typecheck/action.yml b/.github/actions/typecheck/action.yml index 0d6a951..d813e1a 100644 --- a/.github/actions/typecheck/action.yml +++ b/.github/actions/typecheck/action.yml @@ -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 diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 16c4904..fdcdd1a 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -20,6 +20,8 @@ Closes [story]() - [ ] 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 diff --git a/.github/workflows/apply-migrations.yml b/.github/workflows/apply-migrations.yml new file mode 100644 index 0000000..676a704 --- /dev/null +++ b/.github/workflows/apply-migrations.yml @@ -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 diff --git a/.github/workflows/check-migrations.yml b/.github/workflows/check-migrations.yml new file mode 100644 index 0000000..2b931ce --- /dev/null +++ b/.github/workflows/check-migrations.yml @@ -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 diff --git a/.github/workflows/static-checks.yml b/.github/workflows/static-checks.yml index cda0707..0cc5bfa 100644 --- a/.github/workflows/static-checks.yml +++ b/.github/workflows/static-checks.yml @@ -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 diff --git a/.vscode/extensions.json b/.vscode/extensions.json index fd682ae..b5c7f1f 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -3,6 +3,7 @@ "dbaeumer.vscode-eslint", "esbenp.prettier-vscode", "yoavbls.pretty-ts-errors", - "bradlc.vscode-tailwindcss" + "bradlc.vscode-tailwindcss", + "Prisma.prisma" ] } diff --git a/apps/db/.env.example b/apps/db/.env.example index 7af0140..4348d42 100644 --- a/apps/db/.env.example +++ b/apps/db/.env.example @@ -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: @@ -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" \ No newline at end of file +# DATABASE_PRISMA_URL="postgresql://user:password@localhost:5432/good-dog" \ No newline at end of file diff --git a/apps/db/README.md b/apps/db/README.md new file mode 100644 index 0000000..19ff65b --- /dev/null +++ b/apps/db/README.md @@ -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. diff --git a/apps/db/package.json b/apps/db/package.json index 409a36c..c0b7a54 100644 --- a/apps/db/package.json +++ b/apps/db/package.json @@ -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" diff --git a/apps/db/prisma/schema.prisma b/apps/db/prisma/schema.prisma index 738a466..be5edcc 100644 --- a/apps/db/prisma/schema.prisma +++ b/apps/db/prisma/schema.prisma @@ -11,7 +11,7 @@ generator client { datasource db { provider = "postgresql" - url = env("DATABASE_URL") + url = env("DATABASE_PRISMA_URL") } model User { diff --git a/package.json b/package.json index d9c0790..4474666 100644 --- a/package.json +++ b/package.json @@ -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_______": "", diff --git a/turbo.json b/turbo.json index f1f776b..62d8a7a 100644 --- a/turbo.json +++ b/turbo.json @@ -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 @@ -81,11 +93,8 @@ "globalEnv": ["SKIP_ENV_VALIDATION"], "globalPassThroughEnv": [ "NODE_ENV", - "CI", - "VERCEL", "VERCEL_ENV", "VERCEL_URL", - "npm_lifecycle_event", - "USER" + "DATABASE_PRISMA_URL" ] }