<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Nicole White</title>
    <description>The latest articles on Forem by Nicole White (@nicolewhite-ab).</description>
    <link>https://forem.com/nicolewhite-ab</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1169865%2F975da321-7039-449a-8063-6616ee510f4c.png</url>
      <title>Forem: Nicole White</title>
      <link>https://forem.com/nicolewhite-ab</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nicolewhite-ab"/>
    <language>en</language>
    <item>
      <title>Validate database schema changes in your CI pipeline before deploying to production</title>
      <dc:creator>Nicole White</dc:creator>
      <pubDate>Tue, 03 Oct 2023 16:00:00 +0000</pubDate>
      <link>https://forem.com/nicolewhite-ab/validate-database-schema-changes-in-your-ci-pipeline-before-deploying-to-production-1bfh</link>
      <guid>https://forem.com/nicolewhite-ab/validate-database-schema-changes-in-your-ci-pipeline-before-deploying-to-production-1bfh</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;At &lt;a href="https://www.autoblocks.ai/"&gt;Autoblocks&lt;/a&gt;, we use &lt;a href="https://neon.tech/"&gt;Neon&lt;/a&gt; as the serverless Postgres provider for our transactional data. Because we are constantly iterating on our product based on user feedback, we make frequent, non-negligible changes to our database schema to best serve evolving business needs. Some of these migrations are not only schema modifications (adding a new table, column, etc.) but also data modifications, where we write scripts to e.g. backfill columns, change the schema of a JSON column, make destructive changes without downtime, etc.&lt;/p&gt;

&lt;p&gt;We always test these kinds of modifications locally, but we appreciate the extra peace of mind we get from doing a dry run on our production database. Before Neon, building this using just RDS (the relational database service from AWS) would have required a substantial engineering investment. With &lt;a href="https://neon.tech/docs/introduction/branching"&gt;Neon branching&lt;/a&gt;, though, it’s shockingly easy to spin up and tear down copies of your production database in a Continuous Integration (CI) environment.&lt;/p&gt;

&lt;p&gt;At Autoblocks, we use GitHub Actions, and we’ve open-sourced the actions we wrote to achieve this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/autoblocksai/neon-actions"&gt;&lt;strong&gt;Explore the repository&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Branches
&lt;/h2&gt;

&lt;p&gt;While the actions support creating a Neon branch for every commit, we’ve opted only to create a Neon branch when there are migrations to run. In our case, this is when our &lt;a href="https://www.prisma.io/"&gt;Prisma&lt;/a&gt; migrations folder has been modified. Our migrations are located in &lt;code&gt;packages/db/prisma/migrations&lt;/code&gt; and the &lt;code&gt;package.json&lt;/code&gt; file in the &lt;code&gt;packages/db&lt;/code&gt; subfolder in our monorepo contains scripts for running migrations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "migrate:dev": "prisma migrate dev",
  "migrate:prod": "prisma migrate deploy"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GitHub Actions does support &lt;a href="https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onpushpull_requestpull_request_targetpathspaths-ignore"&gt;path filtering&lt;/a&gt;, but only at the workflow level. We use &lt;a href="https://github.com/dorny/paths-filter"&gt;dorny/paths-filter&lt;/a&gt; since it allows us to add job-level path filters. Below is an abbreviated example of our main CI workflow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: CI

on:
  push:
    branches-ignore:
      - main

jobs:
  changes:
    runs-on: ubuntu-latest
    outputs:
      migrations: ${{ steps.changes.outputs.migrations }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - uses: dorny/paths-filter@v2
        id: changes
        with:
          filters: |
            migrations:
              - 'packages/db/prisma/migrations/**'

  validate-migrations:
    needs: changes
    if: needs.changes.outputs.migrations == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      # Setup node, install dependencies, etc.

      - name: Create Neon branch
        uses: autoblocksai/neon-actions/create-branch@v1
        id: neon
        with:
          api-key: ${{ secrets.NEON_API_KEY }}
          project-id: ${{ vars.NEON_PROJECT_ID }}

      - name: Run DB migrations
        run: npm run migrate:prod
        working-directory: packages/db
        env:
          DATABASE_URL: postgres://${{ secrets.PG_USERNAME }}:${{ secrets.PG_PASSWORD }}@${{ steps.neon.outputs.direct-host }}:5432/neondb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, when I push a commit to a GitHub branch that modifies the &lt;code&gt;packages/db/prisma/migrations&lt;/code&gt; folder, we will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a Neon branch from the latest state of our production database,&lt;/li&gt;
&lt;li&gt;Run our database migrations against that branch, and&lt;/li&gt;
&lt;li&gt;Add a pass / fail commit status to the pull request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Neon branch name will be the GitHub branch name plus the first seven characters of the commit SHA: &lt;code&gt;{branchName}-{commitSha}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example, I created a GitHub branch called &lt;code&gt;add-new-table&lt;/code&gt;, modified the migrations folder, then pushed three commits:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7eOwv98j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0y7nog9x630ocrk74gyr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7eOwv98j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0y7nog9x630ocrk74gyr.png" alt="Image description" width="800" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On each run of the CI workflow, a new Neon branch was created, and then our migrations were run against that branch. Below is an example of the console output from the &lt;code&gt;autoblocksai/neon-actions/create-branch&lt;/code&gt; action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-&amp;gt; Run autoblocksai/neon-actions/create-branch@v1
Creating new branch with name 'add-new-table-d105eb3'
Created new branch:
{
  "branch": {
    "id": "br-empty-voice-99968957",
    "project_id": "early-silence-295820",
    "parent_id": "br-smelly-top-392940",
    "name": "add-new-table-d105eb3",
    "current_state": "init"
  },
  "endpoints": [
    {
      "id": "ep-bold-glade-26143287",
      "host": "ep-bold-glade-26143287.us-east-1.aws.neon.tech",
      "current_state": "init"
    }
  ]
}
Notice: Your Neon branch is at &amp;lt;https://console.neon.tech/app/projects/early-silence-295820/branches/br-empty-voice-99968957&amp;gt;
Wating for branch br-empty-voice-99968957 to be ready
Retrying in 0.25 seconds (5 retries left)
Branch br-empty-voice-99968957 is ready!
Wating for endpoint ep-bold-glade-26143287 to be ready
Endpoint ep-bold-glade-26143287 is ready!
Direct host: ep-bold-glade-26143287.us-east-1.aws.neon.tech
Pooled host: ep-bold-glade-26143287-pooler.us-east-1.aws.neon.tech
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll notice that we have two hosts to choose from, either the &lt;code&gt;direct&lt;/code&gt; host or the &lt;code&gt;pooled&lt;/code&gt; host. These are available as outputs from the action as &lt;code&gt;direct-host&lt;/code&gt; and &lt;code&gt;pooled-host&lt;/code&gt;, respectively. We’re using the direct connection for migrations since Prisma requires it, but you’ll likely want to use the &lt;a href="https://neon.tech/docs/connect/connection-pooling"&gt;pooled connection&lt;/a&gt; for any other type of connection, like from a web application or smoke test suite.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deleting Branches
&lt;/h2&gt;

&lt;p&gt;When a GitHub branch is deleted, either directly or from merging a pull request, we want to clean up all Neon branches created from that branch. To do so, we just run the &lt;code&gt;autoblocksai/neon-actions/delete-branches&lt;/code&gt; action on the &lt;code&gt;delete&lt;/code&gt; event:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: Delete Neon Branches

on: delete

jobs:
  delete-neon-branches:
    if: github.event.ref_type == 'branch'
    runs-on: ubuntu-latest
    steps:
      - name: Delete Neon branches
        uses: autoblocksai/neon-actions/delete-branches@v1
        with:
          api-key: ${{ secrets.NEON_API_KEY }}
          project-id: ${{ vars.NEON_PROJECT_ID }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will delete all of the Neon branches whose names start with the name of the GitHub branch that was just deleted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-&amp;gt; Run autoblocksai/neon-actions/delete-branches@v1
Found 3 branches:
  add-new-table-d105eb3 (br-empty-voice-99968957)
  add-new-table-9c62e5b (br-autumn-silence-26803864)
  add-new-table-a58c909 (br-jolly-rice-40072094)
Deleting branches with prefix 'add-new-table-':
  deleting add-new-table-d105eb3 (br-empty-voice-99968957)
  deleting add-new-table-9c62e5b (br-autumn-silence-26803864)
  deleting add-new-table-a58c909 (br-jolly-rice-40072094)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  More Examples
&lt;/h2&gt;

&lt;p&gt;While we only use Neon branches to validate our migrations, you can also use them in other contexts, such as smoke tests or Vercel preview environments. In these cases, you’d likely want to drop the path filtering and then use the pooled host in subsequent steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: CI

on: push

jobs:
  ci:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup node
        uses: actions/setup-node@v3

      - name: Install dependencies
        run: npm ci

      - name: Create neon branch
        id: neon
        uses: autoblocksai/neon-actions/create-branch@v1
        with:
          api-key: ${{ secrets.NEON_API_KEY }}
          project-id: ${{ vars.NEON_PROJECT_ID }}

      - name: Run prisma migrations
        run: npx prisma migrate deploy
        env:
          DATABASE_URL: postgres://${{ secrets.PG_USERNAME }}:${{ secrets.PG_PASSWORD }}@${{ steps.neon.outputs.direct-host }}:5432/neondb

      - name: Run smoke tests
        run: npm run test:smoke
        env:
          DATABASE_URL: postgres://${{ secrets.PG_USERNAME }}:${{ secrets.PG_PASSWORD }}@${{ steps.neon.outputs.pooled-host }}:5432/neondb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/autoblocksai/neon-actions"&gt;See the docs for more details&lt;br&gt;
&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>cicd</category>
      <category>ci</category>
      <category>neon</category>
      <category>githubactions</category>
    </item>
  </channel>
</rss>
