godb

package module
v0.2.4-fork.0 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2025 License: MIT Imports: 9 Imported by: 0

README

Fork Notice

  • This repository is a fork of github.com/mickamy/godb.
  • Module path has been changed to github.com/KiraboshiSys/godb for long-term internal use.

🐘 godb

A lightweight database management CLI/Library.

godb helps you manage your database with simple commands like create, migrate, drop, and more — inspired by tools like Rails’ db:* commands.


✨ Features

  • ✅ Interactive godb init to scaffold .godb.yaml
  • ✅ Easily create and drop databases
  • ✅ Run migrations using migrate or generate migrate
  • ✅ Supports MySQL and PostgreSQL
  • ✅ CLI and YAML-based — no magic involved

📦 Installation

# Install godb into your project
go get -tool github.com/KiraboshiSys/godb/cmd/godb@latest

# or install it globally
go install github.com/KiraboshiSys/godb/cmd/godb@latest

⚙️ Set-up

Initialize a project with:

godb init

This will walk you through DB config like:

# .godb.yaml
database:
  driver: postgres
  host: localhost
  port: 5432
  user: godb
  password: password
  name: godb

migrations:
  dir: migrations
  ext: sql
  seq: false

🚀 Usage

Create a database
godb create
Drop a database
godb drop
Run migrations
godb migrate

This uses the migrate binary under the hood (with fallback to go tool migrate if available).

Rollback migrations
godb rollback --step=1

This also uses the migrate binary under the hood.


🛠 Generate migration file

godb g migration create_users

This forwards to migrate create to create a new migration file.

migrations/000001_create_users.up.sql
migrations/000001_create_users.down.sql

📚 Using as a Library

You can also use godb as a Go package to manage your database programmatically:

go get github.com/KiraboshiSys/godb@latest
1. Load the config
import (
  "log"

  "github.com/KiraboshiSys/godb/config"
)

cfg, err := config.Load()
if err != nil {
  log.Fatal("failed to load config:", err)
}
2. Create a database
import (
  "errors"
  "log"

  "github.com/KiraboshiSys/godb"
)

err := godb.Create(cfg)
if errors.Is(err, godb.ErrCreateDatabaseExists) {
  log.Println("database already exists, skipping.")
} else if err != nil {
  log.Fatal("failed to create database:", err)
}
3. Drop a database
import (
  "log"

  "github.com/KiraboshiSys/godb"
)

if err := godb.Drop(cfg, false); err != nil {
  log.Fatal("failed to drop database:", err)
}

The second argument is a flag to terminate all the connections to the database before dropping it.

4. Run migrations
import (
  "errors"
  "log"

  "github.com/KiraboshiSys/godb"
)

err := godb.Migrate(cfg)
if errors.Is(err, godb.ErrMigrateNoChange) {
  log.Println"no new migrations to apply.")
} else if err != nil {
  log.Fatal("migration failed:", err)
}

err = godb.Rollback(cfg, 1)
if errors.Is(err, godb.ErrMigrateNoChange) {
  log.Println("no migrations to rollback.")
} else if err != nil {
  log.Fatal("rollback failed:", err)
}

You can use this to integrate database set-up into your own tooling, tests, or set-up scripts.


🧪 Supported drivers

  • ✅ MySQL
  • ✅ PostgreSQL

📄 License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCreateDatabaseExists = errors.New("database already exists")
)
View Source
var (
	ErrMigrateNoChange = errors.New("no change")
)

Functions

func Create

func Create(cfg config.Config) error

func Drop

func Drop(cfg config.Config, force bool) error

func Migrate

func Migrate(cfg config.Config) error

func Reset

func Reset(cfg config.Config, force bool) error

func Rollback

func Rollback(cfg config.Config, step int) error

Types

This section is empty.

Jump to

Keyboard shortcuts

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