migration

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package migration contains an implementation of a simple database migration mechanism that allows semi-automatic transitions between various database versions as well as building the latest version of the database from scratch.

Please look into README.md with further instructions how to use it.

Index

Constants

View Source
const (
	// DriverUnsupportedErr error message indicating unsupported DB driver
	DriverUnsupportedErr = "%v driver is not supported"
)

Variables

This section is empty.

Functions

func DowngradeTable added in v1.4.0

func DowngradeTable(tx *sql.Tx, tableName, oldTableDefinition string, columns []string) error

DowngradeTable downgrades table to oldTableDefinition. Set columns to the list of new columns if you're removing any columns

func InitDBSchema added in v1.4.0

func InitDBSchema(db *sql.DB, schema Schema) error

InitDBSchema ensures that a given database schema exists. If it already exists, no changes will be made to the database, otherwise it will attempt to create the schema.

func InitInfoTable

func InitInfoTable(db *sql.DB, schema Schema) error

InitInfoTable ensures that the migration information table is created. If it already exists, no changes will be made to the database. Otherwise, a new migration information table will be created and initialized.

func SetDBVersion

func SetDBVersion(
	db *sql.DB,
	dbDriver types.DBDriver,
	dbSchema Schema,
	targetVer Version,
	migrations []Migration,
) error

SetDBVersion attempts to get the database into the specified target version using available migration steps.

func UpdateTableData added in v1.4.0

func UpdateTableData(tx *sql.Tx, table, query string, args ...interface{}) error

UpdateTableData updates data and checks rows affected

func UpgradeTable added in v1.4.0

func UpgradeTable(tx *sql.Tx, tableName, newTableDefinition string) error

UpgradeTable runs ALTER TABLE based on the given definition

Types

type Migration

type Migration struct {
	StepUp   Step
	StepDown Step
}

Migration type describes a single Migration.

func NewUpdateTableMigration

func NewUpdateTableMigration(tableName, previousSchema string, previousColumns []string, newSchema string) Migration

NewUpdateTableMigration generates a migration which changes tables schema and copies data (should work in most of cases like adding a field, altering it and so on) Set previousColumns to the list of previous columns if you're changing any columns

type Schema added in v1.4.0

type Schema string

Schema represents the used schema of the database.

type Step

type Step func(tx *sql.Tx, driver types.DBDriver) error

Step represents an action performed to either increase or decrease the migration version of the database.

type Version

type Version uint

Version represents a version of the database.

func GetDBVersion

func GetDBVersion(db *sql.DB, schema Schema) (Version, error)

GetDBVersion reads the current version of the database from the migration info table.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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