migo

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2022 License: MIT Imports: 7 Imported by: 0

README

Migo

Migo is a tool to safely migrate your migrations from gorm-goose to Gormigrate

Example
package main

import (
	"gorm.io/driver/sqlite"
	"github.com/CloudInn/migo"
	"gorm.io/gorm"
)


func main() {
	db, _ := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})

	migrations := migo.Migrations{
		// create persons table
		{
			ID: "201608301400",
			Migrate: func(tx *gorm.DB) error {
				// it's a good pratice to copy the struct inside the function,
				// so side effects are prevented if the original struct changes during the time
				type Person struct {
					gorm.Model
					Name string
				}
				return tx.AutoMigrate(&Person{})
			},
			Rollback: func(tx *gorm.DB) error {
				return tx.Migrator().DropTable("people")
			},
		},
		// add age column to persons
		{
			ID: "201608301415",
			Migrate: func(tx *gorm.DB) error {
				// when table already exists, it just adds fields as columns
				type Person struct {
					Age int
				}
				return tx.AutoMigrate(&Person{})
			},
			Rollback: func(tx *gorm.DB) error {
				return tx.Migrator().DropColumn("people", "age")
			},
		},
		// add pets table
		{
			ID: "201608301430",
			Migrate: func(tx *gorm.DB) error {
				type Pet struct {
					gorm.Model
					Name     string
					PersonID int
				}
				return tx.AutoMigrate(&Pet{})
			},
			Rollback: func(tx *gorm.DB) error {
				return tx.Migrator().DropTable("pets")
			},
		},
	}

	err := migo.Run(dbClient, migrations, "up", *migo.DefaultOptions.WithPgSchema("<YOUR PGSCHEMA>"))
	if err != nil {
		log.Fatalln(err)
	}

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewID

func NewID() string

func Run

func Run(db *gorm.DB, migrations Migrations, command string, options *Options) error

Types

type Migrations

type Migrations []*gormigrate.Migration

type Options

type Options struct {
	gormigrate.Options
	PgSchema string
}
var DefaultOptions *Options = &Options{
	Options: *gormigrate.DefaultOptions,
}

func (Options) WithPgSchema

func (o Options) WithPgSchema(pgschema string) *Options

Jump to

Keyboard shortcuts

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