migrator

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2024 License: MIT Imports: 10 Imported by: 0

README

Migrator

Migrator is an automatic migration script creation tool for go.

Only works with MySQL, for now...


  • Migrator uses GORM struct tags to parse Go struct fields into MySQL columns.
  • You can check the GORM Official Docs to learn more about these GORM tags.
  • Migrator uses versioned migration database approach. You can use golang-migrate to apply migrations to database

What migrator can do?

Migrator can handle given scenarios and create related migration scripts for you:

[!WARNING] Migrator DELETES unused tables, columns and constraints from database

  • Creating and deleting table
  • Adding, deleting and modifying columns
  • Passing default values for colums. GORM docs
  • Creating, deleting and modifying one-to-one or one-to-many foreign key references One-to-one, One-to-many
  • Creating, deleting and modifying unique keys.
  • Creating, deleting composite unique keys.
  • Creating composite primary keys. GORM docs
  • Renaming primary key

TODO:

  • Creating indexes
  • Renaming columns
  • Type checking for default values
  • General error checking

Example
  • You can add this into your main and run it by go run . -migrate to create migration scripts in the given destination. If -dry-run argument is passed along with the -migrate, migrator prints the migration script and exits.
  • To be able to create foreign key relations, you must pass referenced model into migrator.MigrateAndSave method before the model that has foreign key

main.go

var migrateFlag = flag.Bool("migrate", false, "creates migration scripts")
var dryRun = flag.Bool("dry-run", false, "Print the content of migration instead of creating the file")

func main() {
	flag.Parse()

	// If migrate flag is passed, create migration scripts without starting the server
	if *migrateFlag {
		dsn := fmt.Sprintf("name:password@tcp(host:port)/DBName")
		migrator := migrator.NewMigrator(dsn, "DBName")
		defer migrator.DB.Close()

		migrator.MigrateAndSave(*dryRun, "./database/migrations", // You can pass any destination to save migration scripts
			User{},
			Company{},
			// You can add more models
			...
		)

		// Close the program after migration script created
		os.Exit(0)
	}

	// Other main codes
	...

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Migrator

type Migrator struct {
	DB             *sql.DB
	SchemaName     string
	Relations      []schema.Reference
	CurrentVersion int
}

func NewMigrator

func NewMigrator(dsn, schemaName string) *Migrator

Returns a new migrator instance that is connected to the database by given dsn

func (*Migrator) AddColumnQuery

func (m *Migrator) AddColumnQuery(t schema.Table, c schema.Column) string

func (*Migrator) AddReferenceQuery

func (m *Migrator) AddReferenceQuery(reference schema.Reference) string

func (*Migrator) AddUniqueIndexQuery

func (m *Migrator) AddUniqueIndexQuery(table schema.Table, indexName string, colNames []string) string

func (*Migrator) CreateMigration

func (m *Migrator) CreateMigration(dst []*schema.Table, verbose bool) (string, string)

Compares the current state of the database schema with the given 'dst' schema. Creates and returns the migration script that will bring database to the desired state

func (*Migrator) CreateTableQuery

func (m *Migrator) CreateTableQuery(t *schema.Table) string

func (*Migrator) DescribeTable

func (m *Migrator) DescribeTable(tableName string) []*schema.Column

func (*Migrator) DropColumnQuery

func (m *Migrator) DropColumnQuery(t schema.Table, c schema.Column) string

func (*Migrator) DropReferenceQuery

func (m *Migrator) DropReferenceQuery(reference schema.Reference) string

func (*Migrator) DropTableQuery

func (m *Migrator) DropTableQuery(t *schema.Table) string

func (*Migrator) DropUniqueIndexQuery

func (m *Migrator) DropUniqueIndexQuery(table schema.Table, indexName string, colNames []string) string

func (*Migrator) GetReferences

func (m *Migrator) GetReferences(tableName string) []schema.Reference

func (*Migrator) GetTables

func (m *Migrator) GetTables() []*schema.Table

Parses the current database state into 'schema.Table' struct

func (*Migrator) MigrateAndSave

func (m *Migrator) MigrateAndSave(dryRun bool, saveDirectory string, targetModels ...interface{})

Creates and saves migration script based on the given target models and current state of the database. If dryRun flag is true Migrator prints the migration script and exits

func (*Migrator) ModifyColumnQuery

func (m *Migrator) ModifyColumnQuery(t schema.Table, c schema.Column) string

func (*Migrator) ParseTablesFromStructs

func (m *Migrator) ParseTablesFromStructs(dst ...interface{}) []*schema.Table

Parses given structs into `schema.Table` struct. Given structs must be in order so that referenced table comes before the foreignKey table

func (*Migrator) RenameColumnQuery

func (m *Migrator) RenameColumnQuery(t schema.Table, newCol schema.Column, oldColumn schema.Column) string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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