70 lines
1.4 KiB
Markdown
70 lines
1.4 KiB
Markdown
|
|
# Database Migrations with Goose
|
||
|
|
|
||
|
|
This document explains how to use goose for database migrations in the CMC Sales Go application.
|
||
|
|
|
||
|
|
## Setup
|
||
|
|
|
||
|
|
1. **Install goose**:
|
||
|
|
```bash
|
||
|
|
make install
|
||
|
|
```
|
||
|
|
|
||
|
|
2. **Configure database connection**:
|
||
|
|
```bash
|
||
|
|
cp goose.env.example goose.env
|
||
|
|
# Edit goose.env with your database credentials
|
||
|
|
```
|
||
|
|
|
||
|
|
## Migration Commands
|
||
|
|
|
||
|
|
### Run Migrations
|
||
|
|
```bash
|
||
|
|
# Run all pending migrations
|
||
|
|
make migrate
|
||
|
|
|
||
|
|
# Check migration status
|
||
|
|
make migrate-status
|
||
|
|
```
|
||
|
|
|
||
|
|
### Rollback Migrations
|
||
|
|
```bash
|
||
|
|
# Rollback the last migration
|
||
|
|
make migrate-down
|
||
|
|
```
|
||
|
|
|
||
|
|
### Create New Migrations
|
||
|
|
```bash
|
||
|
|
# 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.sql`
|
||
|
|
- `002_add_new_feature.sql`
|
||
|
|
|
||
|
|
Each migration file contains:
|
||
|
|
```sql
|
||
|
|
-- +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
|
||
|
|
|
||
|
|
1. **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-status` to check current state
|
||
|
|
- Migrations are atomic - if one fails, none are applied
|
||
|
|
- Each migration should be reversible with a corresponding Down section
|