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
.
- 📊 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
Currently, schema
is tested and optimized for:
- PostgreSQL
- MySQL / MariaDB
- SQLite (TODO)
go get github.com/afkdevs/go-schema
package migrations
import (
"context"
"database/sql"
"github.com/afkdevs/go-schema"
"github.com/pressly/goose/v3"
)
func init() {
goose.AddMigrationContext(upCreateUsersTable, downCreateUsersTable)
}
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 downCreateUsersTable(ctx context.Context, tx *sql.Tx) error {
return schema.Drop(ctx, tx, "users")
}
For more examples, check out the examples directory.
For detailed documentation, please refer to the GoDoc page.
Contributions are welcome! Please read the contributing guidelines and submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.