Skip to content

Database migrations. CLI and Golang library.

License

Notifications You must be signed in to change notification settings

nikoskarakostas/migrate

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

migrate CLI

Original Repo

https://github.com/golang-migrate/migrate/

About this fork

I wanted to run either sql migrations or execute a golang binary file as a migration step applying changes to the database. This addition utilizes the plugin package in order to do so. Changes are currently at the support_binary_migrations branch.

Example usage

Below is an example of a .go file of a migration.

example file: [id]_add_ten_example_tables.up.go

package main // must be main

import (
    "database/sql"
    "fmt"
)

var (
	CONN         *sql.Conn  // mandatory
	DB           *sql.DB    // mandatory
	TargetSchema *string    // mandatory
	Err          *error     // mandatory
)

func Migration() { // must be Migration [this gets called]
    
    // We do whatever we want here

    // for example,
	for i := 0; i < 10; i++ {
		_, err := DB.Exec(fmt.Sprintf(`
			CREATE TABLE %s.example_%d (
				id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
				field1 VARCHAR(30) NOT NULL,
				field2 VARCHAR(30) NOT NULL
			);
		`, *TargetSchema, i))   // You must use the TargetSchema in order to target it. Otherwise, the default schema is targeted.
		if err != nil {
			*Err = err          // This is how we catch errors
			return              // This is how we catch errors
		}
	}

}

Build this and add to your migrations dir:

go build -buildmode=plugin -o /your/migrations/directory[id]_add_ten_example_tables.up.so [id]_add_ten_example_tables.up.go

notes:

Databases supported Sources supported
mysql file
s3
  • File extension of the built file must be .so
  • Changes must be contained in the Migration function. This is the one that will be executed. Follow the structure of the example above.
  • CONN, DB, Err, TargetSchema declarations are mandatory. These are accessed by the package and get to point to package's memory addresses.

Basic Functionality

In a nutshell, we read the bytes of the binary file, fetch them, create a temporary file, write the feched data to it and call the plugin package in order to execute the binary's Migration func.

About

Database migrations. CLI and Golang library.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 98.2%
  • Makefile 1.1%
  • Other 0.7%