init
This commit is contained in:
87
README.md
Normal file
87
README.md
Normal file
@@ -0,0 +1,87 @@
|
||||
# go-migrate
|
||||
|
||||
A PostgreSQL schema migration tool that generates and applies migrations by comparing your desired schema (SQL files) with the current database state.
|
||||
|
||||
## Features
|
||||
|
||||
- **Schema comparison**: Compare your desired schema against the live database
|
||||
- **Migration generation**: Automatically generate SQL migration statements
|
||||
- **Migration application**: Apply migrations directly to the database
|
||||
- **Type support**: Handles tables, columns, indexes, foreign keys, enums, and constraints
|
||||
|
||||
## Installation
|
||||
|
||||
### As a Library
|
||||
|
||||
```bash
|
||||
go get git.petrovv.com/go-migrate
|
||||
```
|
||||
|
||||
### CLI Tool
|
||||
|
||||
```bash
|
||||
go install git.petrovv.com/go-migrate/cmd/go-migrate@latest
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Library
|
||||
|
||||
```go
|
||||
import "git.petrovv.com/go-migrate"
|
||||
|
||||
ctx := context.Background()
|
||||
db, _ := pgxpool.New(ctx, "postgres://user:pass@localhost:5432/db")
|
||||
|
||||
// Generate migrations
|
||||
migrations, err := migrate.GetMigrations(ctx, db, "schema/")
|
||||
for _, m := range migrations {
|
||||
fmt.Println(m)
|
||||
}
|
||||
|
||||
// Or use LoadDesiredSchema and CompareSchemas for more control
|
||||
desired, _ := migrate.LoadDesiredSchema("schema/")
|
||||
current, _ := migrate.GetCurrentSchema(ctx, db)
|
||||
migrations = migrate.CompareSchemas(current, desired)
|
||||
```
|
||||
|
||||
### CLI
|
||||
|
||||
```bash
|
||||
# Generate migration SQL
|
||||
go-migrate -cmd=generate -dir=schema
|
||||
|
||||
# Apply migrations
|
||||
go-migrate -cmd=apply -dir=schema
|
||||
```
|
||||
|
||||
**Flags:**
|
||||
- `-dir`: Directory containing schema SQL files (default: `schema`)
|
||||
- `-cmd`: `generate` or `apply`
|
||||
|
||||
## Schema Files
|
||||
|
||||
Place your desired schema SQL files in a directory (e.g., `schema/`). Files are loaded alphabetically.
|
||||
|
||||
Example `schema/01_users.sql`:
|
||||
```sql
|
||||
CREATE TABLE users (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
email TEXT NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
CREATE INDEX idx_users_name ON users (name);
|
||||
```
|
||||
|
||||
## Environment Variables
|
||||
|
||||
For the CLI, database connection can be configured via environment variables or `.env` file:
|
||||
|
||||
```
|
||||
DB_USER=postgres
|
||||
DB_PASS=password
|
||||
DB_HOST=localhost
|
||||
DB_PORT=5432
|
||||
DB_NAME=mydb
|
||||
```
|
||||
Reference in New Issue
Block a user