Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# makemigrations

A **Go-first** database migration tool with a Django-style workflow. Define your schema in YAML, generate type-safe Go migration files, and run them with a compiled binary that embeds every migration your project has ever had.
A **Go-first** database migration tool with a Django-style workflow. Define your schema in YAML, generate type-safe Go migration files, and run them in-process — no Go toolchain required at runtime, no compiled binary to ship.

## ✨ Why Go Migrations?

- 🔒 **Type-safe**: Migrations are Go code — caught by the compiler, not at runtime
- 📦 **Self-contained binary**: One compiled binary knows all migrations, their dependencies, and their SQL
- 🔒 **Type-safe at edit time**: Migrations are real Go files — caught by your IDE and `go vet`
- **No build step**: Migrations are interpreted in-process via [yaegi](https://github.com/traefik/yaegi); no `go build`, no temporary binary, no GOWORK juggling
- 🗄️ **Database-agnostic schema**: Write YAML once, deploy to PostgreSQL, MySQL, SQLite, or SQL Server
- 🔀 **DAG-based ordering**: Migrations form a dependency graph so parallel branches merge cleanly
- 🔄 **Auto change detection**: Diff YAML schemas, generate only what changed
- ⚠️ **Safe destructive ops**: Field removals, table drops, and renames require explicit review
- 🔧 **Zero runtime dependency**: The compiled binary has no runtime dependency on makemigrations itself
- 🛠 **Optional fallback**: The generated `migrations/` directory is still a buildable Go module, so you can `go build` it for IDE checks or as an escape hatch

---

Expand All @@ -34,8 +34,8 @@ This creates:
```
your-project/
└── migrations/
├── main.go ← compiled binary entry point
└── go.mod ← dedicated migrations module
├── main.go ← optional fallback entry point (`go build` still works)
└── go.mod ← dedicated migrations module (used by your IDE / gopls)
```

### 3. Define your schema
Expand Down Expand Up @@ -99,7 +99,7 @@ export DATABASE_URL="postgresql://user:pass@localhost/mydb"
makemigrations migrate up
```

`makemigrations migrate` compiles the migration binary automatically and runs it with the correct Go workspace settings. No manual `go build` required.
`makemigrations migrate` interprets the migration files in-process using yaegi and runs the embedded migration App. No `go build`, no temporary binary.

---

Expand Down Expand Up @@ -130,7 +130,7 @@ makemigrations migrate status

## 📋 migrate Subcommands

`makemigrations migrate` compiles and runs the migration binary. All arguments are forwarded:
`makemigrations migrate` interprets the migration files in-process via yaegi and runs the embedded App. All arguments are forwarded:

```bash
makemigrations migrate up # apply all pending
Expand All @@ -141,7 +141,6 @@ makemigrations migrate status # show applied / pending
makemigrations migrate showsql # print SQL without running it
makemigrations migrate fake 0001_initial # mark applied without running SQL
makemigrations migrate dag # show migration dependency graph
makemigrations migrate --verbose up # show build output
```

---
Expand Down Expand Up @@ -242,14 +241,14 @@ makemigrations migrate-to-go --dir migrations/

### Database connection

The compiled binary reads connection details from `migrations/main.go`. The generated file uses `DATABASE_URL` and `DB_TYPE`:
`makemigrations migrate` reads connection details from `DATABASE_URL` and `DB_TYPE`:

```bash
export DATABASE_URL="postgresql://user:pass@localhost/mydb"
export DB_TYPE=postgresql # optional, defaults to postgresql
```

`DB_HOST`, `DB_PORT`, `DB_USER` etc. can be added by editing `migrations/main.go` — see the [Manual Build Guide](docs/manual-migration-build.md#running-the-binary).
If you prefer the optional fallback path of compiling `migrations/` into a standalone binary, edit `migrations/main.go` to read additional vars (`DB_HOST`, `DB_PORT`, `DB_USER`, …) — see the [Manual Build Guide](docs/manual-migration-build.md#running-the-binary).

### Configuration file

Expand Down Expand Up @@ -279,14 +278,15 @@ See the [Configuration Guide](docs/configuration.md) for complete options.
- **[Schema Format Guide](docs/schema-format.md)** — complete YAML schema reference
- **[Configuration Guide](docs/configuration.md)**
- **[Manual Build Guide](docs/manual-migration-build.md)** — GOWORK/GOTOOLCHAIN details for CI/CD
- **[Extending the yaegi Symbol Map](docs/extending-yaegi-symbols.md)** — let interpreted migrations import third-party packages

### Command Reference

| Command | Description |
|---------|-------------|
| **[init](docs/commands/init.md)** | Bootstrap the `migrations/` directory |
| **[makemigrations](docs/commands/makemigrations.md)** | Generate `.go` migration files from YAML schema |
| **[migrate](docs/commands/migrate.md)** | Build and run the compiled migration binary |
| **[migrate](docs/commands/migrate.md)** | Run migrations in-process via the yaegi interpreter |
| **[migrate-to-go](docs/commands/migrate_to_go.md)** | Convert existing Goose SQL migrations to Go |
| [struct2schema](docs/commands/struct2schema.md) | Generate YAML schemas from Go structs |
| [dump_sql](docs/commands/dump_sql.md) | Preview generated SQL from schemas |
Expand Down
40 changes: 40 additions & 0 deletions cmd/drivers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
MIT License

# Copyright (c) 2025 OcomSoft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package cmd

// Blank-imports for SQL drivers used by the migrate command. Under the
// previous compile-flow each user added the driver they needed to their
// own migrations/main.go; with yaegi the migrations run in the CLI's
// process, so the CLI itself must register the drivers. lib/pq is already
// registered transitively by internal/providers/postgresql.
//
// The mapping from migrate.Config.DatabaseType to driver name is in
// migrate/app.go (driverName): "mysql"/"tidb" → mysql, "sqlserver" →
// sqlserver, "sqlite" → sqlite3, anything else → postgres.
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
_ "github.com/microsoft/go-mssqldb"
)
11 changes: 7 additions & 4 deletions cmd/go_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ Initialization complete. No existing schema found.
To generate your first migration:
makemigrations makemigrations --name "initial"

Then build and run:
cd %s && go mod tidy && go build -o migrate .
./migrate up
`, migrationsDir)
Then run:
makemigrations migrate up

Migrations are interpreted in-process — no Go toolchain required at runtime.
The generated %s/main.go and %s/go.mod remain available so you can still
'go build' the migrations module for IDE type-checking or as a fallback.
`, migrationsDir, migrationsDir)
}

return nil
Expand Down
Loading
Loading