flyway

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2025 License: Apache-2.0 Imports: 10 Imported by: 1

README ¶

flyway

License Language version Go report Godoc codecov

📋 Requirements

  • Go 1.22+

🚀 Install

go get github.com/libgox/flyway

NOTICE

If you are using MySQL and need to execute multiple SQL statements, make sure to add ?multiStatements=true to your connection string. This is required to allow the execution of multiple SQL statements in a single query.

💡 Usage

Initialization Sample DB & Migrator
	db, err := sql.Open("sqlite3", ":memory:")
	if err != nil {
		panic(err)
	}

	defer db.Close()

	migrator, err := flyway.NewMigrator(db, &flyway.MigratorConfig{
		DbType: flyway.DbTypeSqlite,
		User:   "admin",
	})
	if err != nil {
		panic(err)
	}
Auto Migrate db/migration path

If you want to specify the migration path, you can use MigrateFromPath method.

	err = migrator.Migrate()
	if err != nil {
		panic(err)
	}
Migrate Manually
	schemas := []flyway.Schema{
		{
			Version:     1,
			Description: "Create users table",
			Script:      "V1__Create_users.sql",
			Sql:         `CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);`,
		},
		{
			Version:     2,
			Description: "Add email column",
			Script:      "V2__Add_email.sql",
			Sql:         `ALTER TABLE users ADD COLUMN email TEXT;`,
		},
	}

	err = migrator.MigrateBySchemas(schemas)
	if err != nil {
		panic(err)
	}
}

Documentation ¶

Index ¶

Constants ¶

View Source
const LOCK_MAGIC_NUM = (0x46 << 40) |
	(0x6C << 32) |
	(0x79 << 24) |
	(0x77 << 16) |
	(0x61 << 8) |

	0x79 // y

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type Database ¶ added in v0.1.1

type Database interface {
	CreateSchemaHistoryTable() (sql.Result, error)
	RecordMigration(installedRank int, version string, description string, script string, checksum int32, user string, executionTime int) (sql.Result, error)
	IsVersionMigrated(version string) *sql.Row
	AcquireLock() error
	ReleaseLock() error
}

type DbType ¶

type DbType string
const (
	DbTypeMySQL    DbType = "mysql"
	DbTypePostgres DbType = "postgres"
	DbTypeSqlite   DbType = "sqlite3"
)

type Migrator ¶

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

func NewMigrator ¶

func NewMigrator(db *sql.DB, config *MigratorConfig) (*Migrator, error)

func (*Migrator) Migrate ¶

func (m *Migrator) Migrate() error

Migrate applies the schema in db/migration folder

func (*Migrator) MigrateBySchemas ¶

func (m *Migrator) MigrateBySchemas(schemas []*Schema) error

MigrateBySchemas applies the schema migrations

func (*Migrator) MigrateFromDir ¶

func (m *Migrator) MigrateFromDir(dir string) error

MigrateFromDir applies the schema in specified directory

type MigratorConfig ¶

type MigratorConfig struct {
	DbType DbType
	User   string
}

type MysqlDatabase ¶ added in v0.1.1

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

func (*MysqlDatabase) AcquireLock ¶ added in v0.1.1

func (m *MysqlDatabase) AcquireLock() error

func (*MysqlDatabase) CreateSchemaHistoryTable ¶ added in v0.1.1

func (m *MysqlDatabase) CreateSchemaHistoryTable() (sql.Result, error)

func (*MysqlDatabase) IsVersionMigrated ¶ added in v0.1.1

func (m *MysqlDatabase) IsVersionMigrated(version string) *sql.Row

func (*MysqlDatabase) RecordMigration ¶ added in v0.1.1

func (m *MysqlDatabase) RecordMigration(installedRank int, version string, description string, script string, checksum int32, user string, executionTime int) (sql.Result, error)

func (*MysqlDatabase) ReleaseLock ¶ added in v0.1.1

func (m *MysqlDatabase) ReleaseLock() error

type PostgresDatabase ¶ added in v0.1.1

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

func (*PostgresDatabase) AcquireLock ¶ added in v0.1.1

func (p *PostgresDatabase) AcquireLock() error

func (*PostgresDatabase) CreateSchemaHistoryTable ¶ added in v0.1.1

func (p *PostgresDatabase) CreateSchemaHistoryTable() (sql.Result, error)

func (*PostgresDatabase) IsVersionMigrated ¶ added in v0.1.1

func (p *PostgresDatabase) IsVersionMigrated(version string) *sql.Row

func (*PostgresDatabase) RecordMigration ¶ added in v0.1.1

func (p *PostgresDatabase) RecordMigration(installedRank int, version string, description string, script string, checksum int32, user string, executionTime int) (sql.Result, error)

func (*PostgresDatabase) ReleaseLock ¶ added in v0.1.1

func (p *PostgresDatabase) ReleaseLock() error

type Schema ¶

type Schema struct {
	InstalledRank int
	Version       string
	Description   string
	Script        string
	Sql           string
}

type SqliteDatabase ¶ added in v0.1.1

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

func (*SqliteDatabase) AcquireLock ¶ added in v0.1.1

func (s *SqliteDatabase) AcquireLock() error

func (*SqliteDatabase) CreateSchemaHistoryTable ¶ added in v0.1.1

func (s *SqliteDatabase) CreateSchemaHistoryTable() (sql.Result, error)

func (*SqliteDatabase) IsVersionMigrated ¶ added in v0.1.1

func (s *SqliteDatabase) IsVersionMigrated(version string) *sql.Row

func (*SqliteDatabase) RecordMigration ¶ added in v0.1.1

func (s *SqliteDatabase) RecordMigration(installedRank int, version string, description string, script string, checksum int32, user string, executionTime int) (sql.Result, error)

func (*SqliteDatabase) ReleaseLock ¶ added in v0.1.1

func (s *SqliteDatabase) ReleaseLock() error

Jump to

Keyboard shortcuts

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