---
title: "Migrating JSONB columns in Go"
description: "When a JSONB column's shape changes, a custom sql.Scanner and driver.Valuer on the Go column type can read both the old and new formats while always writing the new one. incident.io used this to move conditions from a flat array to an array of condition groups, which was needed to support OR logic."
url: "https://swe.quest/articles/migrating-jsonb-columns-in-go-042317bcb9572fbf"
canonical: "https://incident.io/blog/migrating-jsonb-columns-in-go"
source: "incident.io"
published: "2024-07-11"
difficulty: "medium"
kind: "How-to"
reading_minutes: 4
tags: ["schema-migrations","go","postgres"]
---

# Migrating JSONB columns in Go

A post on [incident.io](https://swe.quest/sources/incident-io.md), published 2024-07-11, 4 min read, How-to. Original: [https://incident.io/blog/migrating-jsonb-columns-in-go](https://incident.io/blog/migrating-jsonb-columns-in-go)

## Summary

When a JSONB column's shape changes, a custom sql.Scanner and driver.Valuer on the Go column type can read both the old and new formats while always writing the new one. incident.io used this to move conditions from a flat array to an array of condition groups, which was needed to support OR logic. Old flat arrays are wrapped in a single group on read, so backend and frontend work could proceed before the data migration finished. The backfill then reads and rewrites rows in batches of 1,000 via a GORM upsert (ON CONFLICT DO UPDATE), tracked by a temporary indexed conditions\_migrated column, after which the compatibility scanner is removed.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Schema migrations](https://swe.quest/tags/schema-migrations.md) (Data & databases)
- [Go](https://swe.quest/tags/go.md) (Languages)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)

## Prerequisites

- Go database/sql interfaces ([easy posts on Go](https://swe.quest/tags/go.md?difficulty=easy))
- PostgreSQL JSONB ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- GORM basics

## Keywords

JSONB, sql.Scanner, driver.Valuer, GORM, PostgreSQL

## Related posts

- [SQL or NoSQL? Why not use both (with PostgreSQL)?](https://swe.quest/articles/sql-or-nosql-why-not-use-both-with-postgresql-98c239e2ff7e761c.md): Supabase · 2022-11-24 · Medium · 12 min read · PostgreSQL, SQL, Databases
  PostgreSQL's JSONB columns let an app store nested, variable-length data (here, a daily food, water and exercise log) as a single row instead of spreading it across several normalized tables, while keeping SQL available for analysis.
- [Migrating from Fauna to Supabase](https://swe.quest/articles/migrating-from-fauna-to-supabase-eec2a4cdabcfd906.md): Supabase · 2025-03-21 · Medium · 6 min read · Schema migrations, PostgreSQL, Databases, JavaScript
  With Fauna sunsetting by the end of May 2025, a two-phase migration to Supabase keeps applications online.
- [What's New in PostgreSQL 14?](https://swe.quest/articles/what-s-new-in-postgresql-14-d679a59b3961e203.md): Render · 2021-10-05 · Medium · 4 min read · PostgreSQL, SQL
  PostgreSQL 14 adds jsonb subscripting, which lets queries read, filter and update nested JSONB values with JavaScript-like bracket syntax instead of path operators such as #\>.
- [pg\_jsonschema: JSON Schema support for Postgres](https://swe.quest/articles/pg-jsonschema-json-schema-support-for-postgres-489ffe5b7545ddca.md): Supabase · 2022-08-19 · Medium · 4 min read · PostgreSQL, Databases
  pg\_jsonschema is a Postgres extension that validates \`json\` and \`jsonb\` values against a JSON Schema through two functions, \`json\_matches\_schema\` and \`jsonb\_matches\_schema\`.
- [Top 3 features in Postgres 17](https://swe.quest/articles/top-3-features-in-postgres-17-b0d086972cf3cc60.md): Neon · 2024-08-30 · Medium · 5 min read · PostgreSQL, SQL
  Postgres 17 adds RETURNING support to MERGE (with a merge\_action() function that reports whether each row was inserted, updated or deleted), SQL/JSON functions including JSON\_TABLE for turning JSON into relational rows, and performance work.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
