sbsqlm

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

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

Go to latest
Published: Aug 3, 2025 License: MIT Imports: 12 Imported by: 0

README

sbsqlm

import "github.com/barbell-math/smoothbrain-sqlmigrate"

A very simple library that provides a way to perform SQL migrations using pgxDB.

Index

Variables

var (
    MalformedMigrationFileErr = errors.New("Malformed migration file")
    MigrationSequenceErr      = errors.New("Malformed migration sequence")
    MissingSqlMigrationErr    = errors.New(
        "A postOp did not have an associated sql migration file",
    )
    UnknownMigrationErr = errors.New(
        "A migration requested from the database was not found",
    )
)

func Load

func Load(fs embed.FS, dir string, postOps map[Migration]PostMigrationOp) error

Loads a set of migration files from the supplied directory of the supplied embedded file system. All sql migration files are expected to have an integer for the file name and end with a sql file extension. Migration files are expected to be strictly increasing with no missing numbers.

postOps define code that will be run after the corresponding sql migration has been run, as defined by the sql file number. Post ops are not required.

An embedded file system is used to encourage all migrations to be baked into the executable so that deploying any application will not also require deploying a dir containing all the migration files.

func Run

func Run(ctxt context.Context, p *pgxpool.Pool) error

Runs all migrations that need to be run. This will run all migrations that have a status of false in the database and will run any additional migrations that have been added. All migrations will be run in the increasing order and if an error is encountered all further migrations will not be run.

All migrations will be run inside a transaction. If any migration fails the entire transaction will be rolled back, there will be no changes to the database, and any migrations that ran successfully before the failed migration will need to be re-run.

func Status

func Status(ctxt context.Context, p *pgxpool.Pool) ([]queries.SmoothbrainSqlmigrateVersioning, error)

Returns the current status of all migrations. This result will include the status of all operations that are not yet listed in the database.

type Migration

type Migration int

type Migrations

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

func (*Migrations) Load
func (m *Migrations) Load(sqlFiles embed.FS, dir string, postOps map[Migration]PostMigrationOp) error

Loads a set of migration files from the supplied directory of the supplied embedded file system. All sql migration files are expected to have an integer for the file name and end with a sql file extension. Migration files are expected to be strictly increasing with no missing numbers.

postOps define code that will be run after the corresponding sql migration has been run, as defined by the sql file number. Post ops are not required.

An embedded file system is used to encourage all migrations to be baked into the executable so that deploying any application will not also require deploying a dir containing all the migration files.

func (*Migrations) Run
func (m *Migrations) Run(ctxt context.Context, p *pgxpool.Pool) error

Runs all migrations that need to be run. This will run all migrations that have a status of false in the database and will run any additional migrations that have been added. All migrations will be run in the increasing order and if an error is encountered all further migrations will not be run.

All migrations will be run inside a transaction. If any migration fails the entire transaction will be rolled back, there will be no changes to the database, and any migrations that ran successfully before the failed migration will need to be re-run.

func (*Migrations) Status
func (m *Migrations) Status(ctxt context.Context, p *pgxpool.Pool) ([]queries.SmoothbrainSqlmigrateVersioning, error)

Returns the current status of all migrations. This result will include the status of all operations that are not yet listed in the database.

type PostMigrationOp

The function signature for any golang operations that should be performed after the sql migration has been executed.

type PostMigrationOp func(ctxt context.Context, p pgx.Tx) error

Generated by gomarkdoc

Helpful Developer Cmds

To build the build system:

go build -o ./bs/bs ./bs

The build system can then be used as usual:

./bs/bs --help
./bs/bs buildbs # Builds the build system!

Documentation

Overview

A very simple library that provides a way to perform SQL migrations using pgxDB.

Index

Constants

This section is empty.

Variables

View Source
var (
	MalformedMigrationFileErr = errors.New("Malformed migration file")
	MigrationSequenceErr      = errors.New("Malformed migration sequence")
	MissingSqlMigrationErr    = errors.New(
		"A postOp did not have an associated sql migration file",
	)
	UnknownMigrationErr = errors.New(
		"A migration requested from the database was not found",
	)
)

Functions

func Load

func Load(fs embed.FS, dir string, postOps map[Migration]PostMigrationOp) error

Loads a set of migration files from the supplied directory of the supplied embedded file system. All sql migration files are expected to have an integer for the file name and end with a sql file extension. Migration files are expected to be strictly increasing with no missing numbers.

postOps define code that will be run after the corresponding sql migration has been run, as defined by the sql file number. Post ops are not required.

An embedded file system is used to encourage all migrations to be baked into the executable so that deploying any application will not also require deploying a dir containing all the migration files.

func Run

func Run(ctxt context.Context, p *pgxpool.Pool) error

Runs all migrations that need to be run. This will run all migrations that have a status of false in the database and will run any additional migrations that have been added. All migrations will be run in the increasing order and if an error is encountered all further migrations will not be run.

All migrations will be run inside a transaction. If any migration fails the entire transaction will be rolled back, there will be no changes to the database, and any migrations that ran successfully before the failed migration will need to be re-run.

func Status

Returns the current status of all migrations. This result will include the status of all operations that are not yet listed in the database.

Types

type Migration

type Migration int

type Migrations

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

func (*Migrations) Load

func (m *Migrations) Load(
	sqlFiles embed.FS,
	dir string,
	postOps map[Migration]PostMigrationOp,
) error

Loads a set of migration files from the supplied directory of the supplied embedded file system. All sql migration files are expected to have an integer for the file name and end with a sql file extension. Migration files are expected to be strictly increasing with no missing numbers.

postOps define code that will be run after the corresponding sql migration has been run, as defined by the sql file number. Post ops are not required.

An embedded file system is used to encourage all migrations to be baked into the executable so that deploying any application will not also require deploying a dir containing all the migration files.

func (*Migrations) Run

func (m *Migrations) Run(ctxt context.Context, p *pgxpool.Pool) error

Runs all migrations that need to be run. This will run all migrations that have a status of false in the database and will run any additional migrations that have been added. All migrations will be run in the increasing order and if an error is encountered all further migrations will not be run.

All migrations will be run inside a transaction. If any migration fails the entire transaction will be rolled back, there will be no changes to the database, and any migrations that ran successfully before the failed migration will need to be re-run.

func (*Migrations) Status

Returns the current status of all migrations. This result will include the status of all operations that are not yet listed in the database.

type PostMigrationOp

type PostMigrationOp func(ctxt context.Context, p pgx.Tx) error

The function signature for any golang operations that should be performed after the sql migration has been executed.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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