dbx

module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 16, 2025 License: Apache-2.0

README

dbx

[!WARNING] This project is in beta. The API is subject to changes and may break.

Go Reference Go Report Card test Ask DeepWiki

dbx is a database schema migration library for Go that lets you manage database schemas using Go code instead of SQL.

Features

  • Database inspection: Introspect existing database schemas
  • Schema comparison: Compare schemas and generate migration statements
  • Built on database/sql: Works with standard Go database drivers
  • Automatic SQL generation: Automatically generate SQL statements for schema changes

Usage Examples

Database Inspection

Introspect an existing database schema:

import (
	_ "github.com/lib/pq"
	"github.com/swiftcarrot/dbx/postgresql"
	"github.com/swiftcarrot/dbx/schema"
)

db, err := sql.Open("postgres", "postgres://postgres:postgres@localhost:5432/dbx_test?sslmode=disable")
pg := postgresql.New()
source, err := pg.Inspect(db)
Schema Definition and Comparison

Define a target schema and compare with current schema:

target := schema.NewSchema()
target.CreateTable("user", func(t *schema.Table) {
	t.Column("name", &schema.TextType{}, schema.NotNull)
	t.Index("users_name_idx", []string{"name"})
})

changes, err := schema.Diff(source, target)
Applying Schema Changes

Generate and execute SQL from schema changes:

for _, change := range changes {
	sql := pg.GenerateSQL(change)
	_, err := db.Exec(sql)
}

Supported Dialects

PostgreSQL
import (
	_ "github.com/lib/pq"
	"github.com/swiftcarrot/dbx/postgresql"
)

pg := postgresql.New()
MySQL
import (
	_ "github.com/go-sql-driver/mysql"
	"github.com/swiftcarrot/dbx/mysql"
)

my := mysql.New()
SQLite
import (
	_ "github.com/mattn/go-sqlite3"
	"github.com/swiftcarrot/dbx/sqlite"
)

s := sqlite.New()

For other dialect support, feel free to create an issue.

Documentation and Support

The official dbx documentation is currently in development. In the meantime, you can:

License

This project is licensed under the Apache License - see the LICENSE file for details.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL