Skip to content

Migrate from custom migration implementation to golang-migrate#24

Merged
nutcas3 merged 7 commits intomainfrom
migration
Apr 29, 2026
Merged

Migrate from custom migration implementation to golang-migrate#24
nutcas3 merged 7 commits intomainfrom
migration

Conversation

@nutcas3
Copy link
Copy Markdown
Owner

@nutcas3 nutcas3 commented Apr 29, 2026

This PR replaces the custom database migration implementation with the industry-standard golang-migrate library, providing production-ready features like up/down migrations, database locking, and rollback capabilities.

Changes

Migration Infrastructure

  • Installed golang-migrate CLI tool and added library dependency
  • Created down migration files for all 7 existing migrations
  • Renamed migration files from sequential format (001_xxx.sql) to timestamp format (20240429000001_xxx.up.sql)
  • Updated cmd/migrate/main.go to use golang-migrate library instead of custom implementation
  • Added support for up, down, and version actions with step-based migration control
  • Removed internal/infrastructure/database/migrate.go (custom implementation)

Build & Automation

  • Updated Makefile with new migration targets: migrate-up, migrate-down, migrate-version
  • Added migration step to CI/CD pipeline (.github/workflows/ci.yml) to run migrations before tests
  • Migrations now run automatically in CI to ensure schema consistency

Documentation

  • Updated README.md with dedicated Database Migrations section
  • Added usage examples for golang-migrate CLI, Makefile targets, and custom migration tool
  • Documented migration naming convention and creation workflow

Benefits

  • Production-ready migration tool with database locking to prevent race conditions
  • Full rollback capability with down migrations for safe schema changes
  • Standard tooling with widespread community support and documentation
  • Better CI/CD integration with automatic migration execution
  • Support for timestamp-based versioning to avoid conflicts in team development

Migration Commands

# Apply all pending migrations
./bin/migrate -dir ./migrations -action up

# Rollback one migration
./bin/migrate -dir ./migrations -action down -steps 1

# Check current migration version
./bin/migrate -dir ./migrations -action version

# Using Makefile
make migrate-up
make migrate-down
make migrate-version

Testing

All existing migrations have been converted and tested. The CI/CD pipeline now runs migrations automatically before executing tests to ensure database schema is up-to-date.

nutcas3 added 7 commits April 29, 2026 02:58
…grate

Update davecgh/go-spew from v1.1.1 to v1.1.2-0.20180830191138-d8f796af33cc and pmezard/go-difflib from v1.0.0 to v1.0.1-0.20181226105442-5d4384ee4fb2 for latest upstream fixes. Upgrade go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp from v0.60.0 to v0.61.0. Add github.com/golang-migrate/migrate/v4 v4.19.1 as indirect dependency for database migration support.
… with separate up/down files for bidirectional schema changes

Rename migration files from sequential numbering (001_*.sql) to golang-migrate timestamp format (20240429000001_*.up.sql) for version ordering. Add corresponding .down.sql files for each migration containing DROP statements in reverse dependency order with trigger/function cleanup before table drops. Maintain original schema definitions in .up.sql files unchanged.
…for bidirectional migration support and improved version control

Remove custom Migrate function and schema_migrations table management from database/migrate.go. Replace with golang-migrate/migrate/v4 library in cmd/migrate/main.go supporting up/down/version actions with optional step count. Add database URL construction from config and migrate instance creation with file source. Implement action switch handling Up/Down/Steps/Version operations
…with step control for bidirectional schema management

Replace single migrate target with migrate-up/migrate-down/migrate-version targets supporting golang-migrate actions. Add migrate-down with -steps 1 flag for single-step rollback and migrate-version for current schema version display. Update .PHONY declarations and dev-setup to use migrate-up. Retain legacy migrate target as alias to migrate-up for backward compatibility.
…ensure database schema is up-to-date

Add migrate tool build step and database migration execution to CI workflow before running tests. Build migrate binary to bin/migrate from cmd/migrate and execute migrations with up action using test database credentials (localhost:5432/betting_test). Configure DATABASE_* environment variables for migration connection and set migrations directory to ./migrations.
…d remove golangci-lint v2 config options

Downgrade golangci-lint from v1.58.0 to v1.55.2 in CI workflow for compatibility. Correct gosec installation path from securecodewarrior/gosec to securego/gosec in security workflow. Remove go version specification and default: none directive from .golangci.yml as these are not supported in golangci-lint v1 configuration format.
…nstead of manual installation

Replace manual Go setup and gosec installation steps with securego/gosec@master GitHub Action. Pass exclude-dir argument directly to action with args parameter to maintain tests directory exclusion while reducing workflow complexity.
@nutcas3 nutcas3 merged commit 52b138d into main Apr 29, 2026
3 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant