https://github.com/golang-migrate/migrate/
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.
Below is an example of a .go file of a migration.
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
}
}
}
go build -buildmode=plugin -o /your/migrations/directory[id]_add_ten_example_tables.up.so [id]_add_ten_example_tables.up.go
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.
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.