1.4 KiB
1.4 KiB
Database Migrations with Goose
This document explains how to use goose for database migrations in the CMC Sales Go application.
Setup
-
Install goose:
make install -
Configure database connection:
cp goose.env.example goose.env # Edit goose.env with your database credentials
Migration Commands
Run Migrations
# Run all pending migrations
make migrate
# Check migration status
make migrate-status
Rollback Migrations
# Rollback the last migration
make migrate-down
Create New Migrations
# Create a new migration file
make migrate-create name=add_new_feature
Migration Structure
Migrations are stored in sql/migrations/ and follow this naming convention:
001_add_gmail_fields.sql002_add_new_feature.sql
Each migration file contains:
-- +goose Up
-- Your upgrade SQL here
-- +goose Down
-- Your rollback SQL here
Configuration Files
goose.env- Database connection settings (gitignored)goose.env.example- Template for goose.env
Current Migrations
- 001_add_gmail_fields.sql - Adds Gmail integration fields to emails and email_attachments tables
Tips
- Always test migrations on a backup database first
- Use
make migrate-statusto check current state - Migrations are atomic - if one fails, none are applied
- Each migration should be reversible with a corresponding Down section