litemigrate

package module
v0.0.0-...-ddd7e6b Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 7 Imported by: 0

README

litemigrate

A simple SQLite migration library for Go.

Installation

go get github.com/joeychilson/litemigrate

Example

package main

import (
	"context"
	"database/sql"
	"fmt"
	"log"

	"github.com/joeychilson/litemigrate"
)

func main() {
	ctx := context.Background()

	// Create the migrations slice.
	migrations := litemigrate.Migrations{
		{
			Version:     1,
			Description: "create users table",
			Up: func(tx *sql.Tx) error {
				_, err := tx.Exec(`
					CREATE TABLE IF NOT EXISTS users (
						id INTEGER PRIMARY KEY AUTOINCREMENT,
						name TEXT NOT NULL
					);
				`)
				return err
			},
			Down: func(tx *sql.Tx) error {
				_, err := tx.Exec("DROP TABLE IF EXISTS users;")
				return err
			},
		},
		{
			Version:     2,
			Description: "add email column to users table",
			Up: func(tx *sql.Tx) error {
				_, err := tx.Exec("ALTER TABLE users ADD COLUMN email TEXT;")
				return err
			},
			Down: func(tx *sql.Tx) error {
				_, err := tx.Exec("ALTER TABLE users DROP COLUMN email;")
				return err
			},
		},
	}

	// Create a new database instance.
	db, err := litemigrate.New("test.db", &migrations)
	if err != nil {
		log.Fatalf("failed to create database instance: %v", err)
	}

	// Migrate up to the latest version.
	err = db.MigrateUp(ctx)
	if err != nil {
		log.Fatalf("failed to migrate up: %v", err)
	}

	// Migrate down to the previous version.
	err = db.MigrateDown(ctx, 1)
	if err != nil {
		log.Fatalf("failed to migrate down: %v", err)
	}

	// Get the current version of the database.
	version, err := db.CurrentVersion(ctx)
	if err != nil {
		log.Fatalf("failed to get current version: %v", err)
	}
	fmt.Printf("current database version: %d\n", version)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database struct {
	// contains filtered or unexported fields
}

Database represents a database connection and migration data.

func New

func New(dsn string, migrations *Migrations) (*Database, error)

New creates a new database instance with a DSN string and migrations.

func NewWithConn

func NewWithConn(conn *sql.DB, migrations *Migrations) *Database

NewWithConn creates a new database instance with a database connection and migrations.

func (*Database) Close

func (db *Database) Close() error

Close closes the database connection.

func (*Database) CurrentVersion

func (db *Database) CurrentVersion(ctx context.Context) (uint, error)

CurrentVersion returns the current version of the database.

func (*Database) MigrateDown

func (db *Database) MigrateDown(ctx context.Context, amount int) error

MigrateDown migrates the database down by the specified amount.

func (*Database) MigrateUp

func (db *Database) MigrateUp(ctx context.Context) error

MigrateUp migrates the database up to the current version (highest version).

func (*Database) SetMigrationTable

func (db *Database) SetMigrationTable(table string) *Database

SetMigrationTable sets the name of the migration table.

type Migration

type Migration struct {
	Version     uint
	Description string
	Up          func(tx *sql.Tx) error
	Down        func(tx *sql.Tx) error
}

Migration represents a database migration with a version, description, up and down functions.

type Migrations

type Migrations []Migration

Migrations is a slice of Migration.

Jump to

Keyboard shortcuts

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