gosmm

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2023 License: MIT Imports: 11 Imported by: 0

README

GoSMM

Golang Simple Migration Manager

GoDoc GitHub release

Overview

GoSMM is a simple yet powerful SQL migration manager written in Go. This library enables you to manage SQL database migrations for multiple database drivers including Postgres, MySQL, and SQLite3.

The GoSMM package allows you to perform tasks like:

  • Checking migration integrity.
  • Executing migration files in order.
  • Creating migration history tables.

Note: Please write your migration files in SQL format.

By emphasizing SQL-based migration files, GoSMM aims to provide a straightforward and consistent approach to database migration tasks.

Installation

To get started, you can install GoSMM using go get:

```bash
go get github.com/k1e1n04/gosmm
```

Usage

Configuration
```go
config := gosmm.DBConfig{
    Driver:        os.Getenv("DB_DRIVER"),
    Host:          os.Getenv("DB_HOST"),
    Port:          os.Getenv("DB_PORT"),
    User:          os.Getenv("DB_USER"),
    Password:      os.Getenv("DB_PASSWORD"),
    DBName:        os.Getenv("DB_NAME"),
}
```
Fields:
  • Driver: Database driver ("postgres", "mysql", or "sqlite3").
  • Host: Hostname of your database.
  • Port: Port number for the database.
  • User: Username for the database.
  • Password: Password for the database.
  • DBName: The name of the database.
Performing Migrations

To perform migrations, use the Migrate function:

```go
db, err := gosmm.ConnectDB(config)
if err != nil {
    log.Fatalf("Connection failed: %v", err)
}
err := gosmm.Migrate(db, os.Getenv("MIGRATIONS_DIR"))
if err != nil {
    log.Fatalf("Migration failed: %v", err)
}
err := gosmm.CloseDB(db)
if err != nil {
    log.Fatalf("Connection failed: %v", err)
}
```

This will:

  1. Connect to the database.
  2. Validate the DB configuration.
  3. Check migration integrity.
  4. Execute pending migrations.
  5. Record migration history.
  6. Close the database connection.

Migration History Table

GoSMM will create a migration history table in your database to keep track of which migrations have been executed. The table will be named gosmm_migration_history and will have the following schema:

Column Name Data Type Description
installed_rank int The rank of the migration.
filename TEXT The name of the migration script.
installed_on TIMESTAMP The timestamp when the migration was installed.
execution_time int The time it took to execute the migration.
success BOOLEAN Whether the migration was successful or not.

How to Contribute

Contributions are welcome! Feel free to submit a pull request on GitHub.

License

This project is licensed under the MIT License.

Disclaimer

Please make sure to back up your database before running migrations. The maintainers are not responsible for any data loss.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseDB added in v1.1.0

func CloseDB(db *sql.DB) error

CloseDB closes the database connection

func ConnectDB

func ConnectDB(config DBConfig) (*sql.DB, error)

ConnectDB connects to the database based on the given DBConfig

func Migrate

func Migrate(db *sql.DB, migrationsDir string) error

Migrate executes the SQL migrations in the given directory

Types

type DBConfig

type DBConfig struct {
	Driver   string
	Host     string
	Port     int
	User     string
	Password string
	DBName   string
}

DBConfig holds the database configuration information

Jump to

Keyboard shortcuts

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