Skip to content

afkdevs/go-schema

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go-Schema

Go Go Report Card codecov GoDoc Go Version License

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.

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

Supported Databases

Currently, schema is tested and optimized for:

  • PostgreSQL
  • MySQL / MariaDB
  • SQLite (TODO)

Installation

go get github.com/afkdevs/go-schema

Integration Example (with goose)

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.

Documentation

For detailed documentation, please refer to the GoDoc page.

Contributing

Contributions are welcome! Please read the contributing guidelines and submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

🛠️ A lightweight, expressive Go library for defining and applying SQL schema (DDL) programmatically.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Contributors 2

  •  
  •