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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Contributing to \[Schema]
# Contributing to \[Migris]

Thank you for considering contributing to this Go library! We welcome contributions of all kinds—bug reports, feature requests, documentation updates, tests, and code improvements.

Expand Down
53 changes: 29 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,49 +1,54 @@
COVERAGE_FILE := coverage.out
COVERAGE_HTML := coverage.html
MIN_COVERAGE := 80

FORMAT ?= dots

# Format code
.PHONY: fmt
fmt:
fmt: # Format code
@echo "Formatting code..."
@go fmt ./...

# Lint code
.PHONY: lint
lint:
lint: # Lint code
@echo "Linting code..."
@golangci-lint run --timeout 5m

# Install dependencies and tools
.PHONY: install
install:
install: # Install dependencies
@echo "Installing dependencies..."
@go mod download
@go mod tidy

# Run tests
install-tools: # Install tools
@echo "Installing tools..."
@go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest
@go install gotest.tools/gotestsum@latest

.PHONY: test
test:
test: # Run tests
@echo "Running tests..."
go test -v -cover -race ./... -coverprofile=$(COVERAGE_FILE) -coverpkg=./...
@gotestsum --format=$(FORMAT) -- ./...

.PHONY: testcov
testcov: # Run tests with coverage
@echo "Running tests with coverage..."
@gotestsum --format=$(FORMAT) -- -coverprofile=$(COVERAGE_FILE) ./...
@echo "Total coverage is: " $(shell go tool cover -func=$(COVERAGE_FILE) | grep total | awk '{print $$3}')

.PHONY: coverage
coverage:
@echo "Generating test coverage report..."
.PHONY: testcov-html
testcov-html: testcov # Generate coverage HTML report
@go tool cover -html=$(COVERAGE_FILE) -o $(COVERAGE_HTML)
@go tool cover -func=$(COVERAGE_FILE) | tee coverage.txt
@echo "Coverage HTML report generated: $(COVERAGE_HTML)"
@open $(COVERAGE_HTML)

.PHONY: coverage-check
coverage-check:
@COVERAGE=$$(go tool cover -func=$(COVERAGE_FILE) | grep total | awk '{print $$3}' | sed 's/%//'); \
RESULT=$$(echo "$$COVERAGE < $(MIN_COVERAGE)" | bc); \
if [ "$$RESULT" -eq 1 ]; then \
echo "Coverage is below $(MIN_COVERAGE)%: $$COVERAGE%"; \
exit 1; \
else \
echo "Coverage is sufficient: $$COVERAGE%"; \
fi
.PHONY: help
help:
@echo "Available commands:"
@echo " fmt - Format code"
@echo " lint - Lint code"
@echo " install - Install dependencies"
@echo " install-tools- Install development tools"
@echo " test - Run tests"
@echo " testcov - Run tests with coverage"
@echo " testcov-html - Generate coverage HTML report"
@echo " help - Show this help message"
152 changes: 104 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,74 +1,130 @@
# Go-Schema
[![Go](https://github.com/afkdevs/go-schema/actions/workflows/ci.yml/badge.svg)](https://github.com/afkdevs/go-schema/actions/workflows/ci.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/afkdevs/go-schema)](https://goreportcard.com/report/github.com/afkdevs/go-schema)
[![codecov](https://codecov.io/gh/afkdevs/go-schema/graph/badge.svg?token=7tbSVRaD4b)](https://codecov.io/gh/afkdevs/go-schema)
[![GoDoc](https://pkg.go.dev/badge/github.com/afkdevs/go-schema)](https://pkg.go.dev/github.com/afkdevs/go-schema)
[![Go Version](https://img.shields.io/github/go-mod/go-version/afkdevs/go-schema)](https://golang.org/doc/devel/release.html)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
# Migris

`Go-Schema` is a simple Go library for building and running SQL schema (DDL) code in a clean, readable, and migration-friendly way. Inspired by Laravel's Schema Builder, it helps you easily create or change database tables—and works well with tools like [`goose`](https://github.com/pressly/goose).
**Migris** is a database migration library for Go, inspired by Laravel's migrations.
It combines the power of [pressly/goose](https://github.com/pressly/goose) with a fluent schema builder, making migrations easy to write, run, and maintain.

## Features
## Features

- 📊 Programmatic table and column definitions
- 🗃️ Support for common data types and constraints
- ⚙️ Auto-generates `CREATE TABLE`, `ALTER TABLE`, index and foreign key SQL
- 🔀 Designed to work with database transactions
- 🧪 Built-in types and functions make migration code clear and testable
- 🔍 Provides helper functions to get list tables, columns, and indexes
- 📦 Migration management (`up`, `down`, `reset`, `status`, `create`)
- 🏗️ Fluent schema builder (similar to Laravel migrations)
- 🗄️ Supports PostgreSQL, MySQL, and MariaDB
- 🔄 Transaction-based migrations
- 🛠️ Integration with Go projects (no external CLI required)

## Supported Databases
## 🚀 Installation

Currently, `schema` is tested and optimized for:
```bash
go get -u github.com/akfaiz/migris
```

* PostgreSQL
* MySQL / MariaDB
* SQLite (TODO)
## 📚 Usage

## Installation
### 1. Create a Migration

```bash
go get github.com/afkdevs/go-schema
```
Migrations are defined in Go files using the schema builder:

## Integration Example (with goose)
```go
package migrations

import (
"context"
"github.com/akfaiz/migris"
"github.com/akfaiz/migris/schema"
)

func init() {
migris.AddMigrationContext(upCreateUsersTable, downCreateUsersTable)
}

func upCreateUsersTable(c *schema.Context) error {
return schema.Create(c, "users", func(table *schema.Blueprint) {
table.ID()
table.String("name")
table.String("email")
table.Timestamp("email_verified_at").Nullable()
table.String("password")
table.Timestamps()
})
}

func downCreateUsersTable(c *schema.Context) error {
return schema.DropIfExists(c, "users")
}
```

This creates a `users` table with common fields.

### 2. Run Migrations

You can manage migrations directly from Go code:

```go
package migrate

import (
"database/sql"
"fmt"

"github.com/afkdevs/go-schema"
"github.com/pressly/goose/v3"
"github.com/akfaiz/migris"
_ "migrations" // Import migrations
_ "github.com/lib/pq" // PostgreSQL driver
)

func init() {
goose.AddMigrationContext(upCreateUsersTable, downCreateUsersTable)
func Up() error {
m, err := newMigrate()
if err != nil {
return err
}
return m.Up()
}

func upCreateUsersTable(ctx context.Context, tx *sql.Tx) error {
return schema.Create(ctx, tx, "users", func(table *schema.Blueprint) {
table.ID()
table.String("name")
table.String("email")
table.Timestamp("email_verified_at").Nullable()
table.String("password")
table.Timestamps()
})
func Create(name string) error {
m, err := newMigrate()
if err != nil {
return err
}
return m.Create(name)
}

func downCreateUsersTable(ctx context.Context, tx *sql.Tx) error {
return schema.Drop(ctx, tx, "users")
func Reset() error {
m, err := newMigrate()
if err != nil {
return err
}
return m.Reset()
}

func Down() error {
m, err := newMigrate()
if err != nil {
return err
}
return m.Down()
}

func Status() error {
m, err := newMigrate()
if err != nil {
return err
}
return m.Status()
}

func newMigrate() (*migris.Migrate, error) {
dsn := "postgres://user:pass@localhost:5432/mydb?sslmode=disable"
db, err := sql.Open("postgres", dsn)
if err != nil {
return nil, fmt.Errorf("failed to open database: %w", err)
}
return migris.New("postgres", migris.WithDB(db), migris.WithMigrationDir("migrations")), nil
}
```
For more examples, check out the [examples](examples/basic) directory.

## Documentation
For detailed documentation, please refer to the [GoDoc](https://pkg.go.dev/github.com/afkdevs/go-schema) page.
## 📖 Roadmap

- [ ] Add SQLite support
- [ ] CLI wrapper for quick usage

## Contributing
Contributions are welcome! Please read the [contributing guidelines](CONTRIBUTING.md) and submit a pull request.
## 📄 License

## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
MIT License.
See [LICENSE](./LICENSE) for details.
Loading