Replies: 1 comment
|
The key rule for production migrations: never use Recommended approach: golang-migrate as a Kubernetes init container golang-migrate is the most widely used migration tool in the Go ecosystem. It works with SQL migration files (versioned Step 1: Migration files Step 2: Run migrations as an init container in your Deployment # deployment.yaml
spec:
initContainers:
- name: migrate
image: migrate/migrate:v4 # or your own image with migrate binary
args:
- "-path=/migrations"
- "-database=postgres://$(DB_USER):$(DB_PASSWORD)@$(DB_HOST):5432/$(DB_NAME)?sslmode=disable"
- "up"
env:
- name: DB_HOST
valueFrom:
secretKeyRef:
name: db-credentials
key: host
# ... other env vars
volumeMounts:
- name: migrations
mountPath: /migrations
containers:
- name: app
image: your-app:latest
# ... app container config
volumes:
- name: migrations
configMap:
name: db-migrationsThis ensures migrations always run before your app starts, and the pod won't become Ready if migrations fail. Alternative: migrate in application startup (for simpler setups) import "github.com/golang-migrate/migrate/v4"
import _ "github.com/golang-migrate/migrate/v4/database/postgres"
import _ "github.com/golang-migrate/migrate/v4/source/file"
func runMigrations(dsn string) error {
m, err := migrate.New("file://migrations", dsn)
if err != nil {
return err
}
if err := m.Up(); err != nil && err != migrate.ErrNoChange {
return err
}
return nil
}Other options:
General safety practices:
|
Uh oh!
There was an error while loading. Please reload this page.
Hi everyone,
I’m working on a Go project using GORM as the ORM. My app and database are both running inside a Kubernetes cluster (PostgreSQL as DB). I’m looking for advice on how to safely migrate the database in production.
Thanks in advance
All reactions