generic

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2022 License: MIT Imports: 11 Imported by: 0

README

Generic Go Methods Driver

  • Runs pre-registered Golang methods that receive no parameters and return error on failure.
  • Stores migration version details in auto-generated table db_migrations of database that user should provide in driver initialization url. The url should be same as the database connection string, but the schema will be generic. and the real schema (database type) should be provided in migrations_db_type query parameter (see example below) This parameter will be stripped of before the driver will attempt to connect to the database. Currently only postgres schema is supported.

Usage in Go

import "github.com/jfrog/go-dbmigrate/migrate"

// Import your migration methods package so that they are registered and available for the GoMethods driver.
// There is no need to import the GoMethods driver explicitly, as it should already be imported by your migration methods package.
import _ "my_go_methods_migrator"

// use synchronous versions of migration functions ...
allErrors, ok := migrate.UpSync("generic://user@host:port/database?migrations_db_type=postgres", "./path")
if !ok {
  fmt.Println("Oh no ...")
  // do sth with allErrors slice
}

// use the asynchronous version of migration functions ...
pipe := migrate.NewPipe()
go migrate.Up(pipe, "generic://user@host:port/database?migrations_db_type=postgres", "./path")
// pipe is basically just a channel
// write your own channel listener. see writePipe() in main.go as an example.

Migration files format

The migration files should have an ".gom" extension and contain a list of registered methods names.

Migration methods should satisfy the following:

  • They should be exported (their name should start with a capital letter)
  • Their type should be func () error

Recommended (but not required) naming conventions for migration methods:

  • Prefix with V : for example V001 for version 1.
  • Suffix with "_up" or "_down" for up and down migrations correspondingly.

001_first_release.up.gom

V001_some_migration_operation_up
V001_some_other_operation_up
...

001_first_release.down.gom

V001_some_other_operation_down
V001_some_migration_operation_down
...

Methods registration

For a detailed example see: sample_migrator.go

package my_go_methods_migrator

import (
  _ "github.com/jfrog/go-dbmigrate/driver/generic"
  "github.com/jfrog/go-dbmigrate/driver/mongodb/gomethods"
)

// common boilerplate
type MyGoMethodsMigrator struct {
}

func init() {
	gomethods.RegisterMethodsReceiverForDriver("generic", &MyGoMethodsMigrator{})
}


// Here goes the application-specific migration logic
func (r *MyGoMethodsMigrator) V001_some_migration_operation_up() error {
  // do something
  return nil
}

func (r *MyGoMethodsMigrator) V001_some_migration_operation_down() error {
  // revert some_migration_operation_up from above
  return nil
}

Authors

Documentation

Index

Constants

View Source
const DRIVER_NAME = "generic"

Variables

This section is empty.

Functions

This section is empty.

Types

type Driver

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

func (*Driver) Close

func (p *Driver) Close() error

func (*Driver) FilenameExtension

func (driver *Driver) FilenameExtension() string

func (*Driver) Initialize

func (driver *Driver) Initialize(url string, initOptions ...func(driver.Driver)) error

func (*Driver) Invoke

func (driver *Driver) Invoke(methodName string) error

func (*Driver) MethodsReceiver

func (d *Driver) MethodsReceiver() interface{}

func (*Driver) Migrate

func (driver *Driver) Migrate(f file.File, pipe chan interface{})

func (*Driver) SetMethodsReceiver

func (d *Driver) SetMethodsReceiver(r interface{}) error

func (*Driver) Unlock added in v1.6.0

func (p *Driver) Unlock() error

func (*Driver) Validate

func (driver *Driver) Validate(methodName string) error

func (*Driver) Version

func (driver *Driver) Version() (uint64, error)

type MethodsReceiver

type MethodsReceiver interface {
}

type UnregisteredMethodsReceiverError

type UnregisteredMethodsReceiverError string

func (UnregisteredMethodsReceiverError) Error

type WrongMethodsReceiverTypeError

type WrongMethodsReceiverTypeError string

func (WrongMethodsReceiverTypeError) Error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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