Database Migration Management
Create, run, and validate database migrations using goose.
Usage
/migrate create add_user_preferences- Create new migration/migrate up- Run pending migrations/migrate down- Rollback last migration/migrate status- Show migration status/migrate validate- Validate migrations and GORM models
Commands
create [name]
Create a new migration file.
- Generate migration file
```bash
goose -dir migrations create $1 sql
```
- Open the created file and add template:
```sql
-- +goose Up
-- +goose StatementBegin
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
-- +goose StatementEnd
```
- Remind about migration rules:
- Always include reversible DOWN migration
- Add indexes for columns in WHERE/JOIN clauses
- Use appropriate column types
- Never modify existing migrations
up
Run all pending migrations.
- Ensure binary is current
```bash
go install ./...
```
- Check current status
```bash
veloria migrate status
```
- Run migrations
```bash
veloria migrate up
```
- Verify success
```bash
veloria migrate status
```
down
Rollback the last migration.
- Show current status to confirm which migration will be rolled back
- Run rollback
```bash
veloria migrate down
```
- Verify rollback by checking status again
status
Show current migration status.
veloria migrate status
Display:
- Applied migrations with timestamps
- Pending migrations
- Current database version
validate
Validate migrations and check GORM model alignment.
- List all migration files
```bash
ls -la migrations/*.sql
```
- Read each migration and check for:
- Both UP and DOWN sections present
- StatementBegin/StatementEnd blocks
- No destructive operations without confirmation
- Proper index creation
- Read GORM models in
internal/repo/:
plugin.go- Plugin modeltheme.go- Theme modelcore.go- Core model- Check for any models in other files
- Compare schema
- Verify model fields match migration columns
- Check for missing indexes on foreign keys
- Identify any mismatches
- Report findings
```
## Migration Validation Report
### Migrations
| File | UP | DOWN | Status |
|------|----|----- |--------|
### Model Alignment
| Model | Table | Issues |
|-------|-------|--------|
### Recommendations
- [specific issues found]
```
Migration Best Practices
DO
- Create new migrations for all schema changes
- Include reversible DOWN migrations
- Add indexes for frequently queried columns
- Use transactions for multi-statement migrations
- Test rollback before committing
DON'T
- Modify existing migrations (create new ones instead)
- Use TEXT for small string fields (use VARCHAR with limit)
- Forget indexes on foreign key columns
- Skip DOWN migration for "simple" changes
Index Guidelines
Add indexes for:
- Foreign key columns (
user_id,plugin_id) - Columns in WHERE clauses (
slug,status) - Columns in ORDER BY (
created_at,active_installs) - Composite indexes for common query patterns
Example:
-- +goose Up
CREATE INDEX idx_searches_user_status ON searches(user_id, status);
-- +goose Down
DROP INDEX idx_searches_user_status;
Existing Tables
Current schema includes:
users- User accounts with OAuthplugins- WordPress plugin metadatathemes- WordPress theme metadatacores- WordPress core releasessearches- Search history and resultsoauth_identities- OAuth provider credentialssessions- User sessionslargest_repo_files- File size statistics
Review existing migrations in migrations/ before creating new ones.