Simple MySQL migrations in Go

gomigrator

This package contains a very small MySQL migration solution for go written in 1 file.
The aim of this package is to be very small but highly functional.

Roadmap

  1. Create error variables
  2. Add testing
  3. Up/Down functionality
  4. Whatever issue/idea comes up...

Migration file structure

The migrations are regular SQL files containing sql statements.
The file names need to be structured in the following way:

<version>_<name>.sql

Where the version is always an integer, incremented from the last version.
The name is the name of the migration and preferably describes it.
The name may not contain an underscore (_) character.

Example folder structure:

  • migrations/
    • 1_create-user-table.sql
    • 2_update-email-column.sql

Example migration

Here is a very short migration example, just straight forward SQL like we all know it:

CREATE TABLE user (
  id    BINARY(16)   NOT NULL,
  email VARCHAR(255) NOT NULL,
);

ALTER TABLE user ADD CONSTRAINT unique_email UNIQUE(email);
ALTER TABLE user ADD CONSTRAINT pk_user PRIMARY KEY(id);

Usage in an example API

Here is an example of a main file in an API, the essence is shown in the if cfg.Mode == config.MODE_MIGRATE part:

package main

import (
  // Golog is a private logging package
  "github.com/reefercoding/golog"
  // This package
  "github.com/reefercoding/gomigrator"

  // Example internal packages
  "github.com/reefercoding/example-gomigrator/internal/config"
  "github.com/reefercoding/example-gomigrator/internal/server"
  "github.com/reefercoding/example-gomigrator/internal/storage"
)

func main() {
  // Example program config generation
  cfg := config.New()
  // Create *sql.DB
  db, err := storage.NewMysql(&cfg.MySQLConfig)
  if err != nil {
    golog.Fatalf("could not connect to mysql: %w", err)
  }
  if cfg.Mode == config.MODE_MIGRATE {
    // Stub logger if you like
    gomigrator.InfoLogger = golog.Infof
    err := gomigrator.Migrate(db, cfg.MigratorDir)
    if err != nil {
      golog.Fatalf("error occured during gomigrator execution: %w", err)
    }
  }
  if cfg.Mode == config.MODE_API {
    s := server.New(cfg, db)
    golog.Infof("starting http server on address...")
    forever := make(chan bool)
    go func() {
      if err := http.ListenAndServe(cfg.ListenAddress, s.Router); err != nil {
        golog.Fatalf("http server crashed: %w", err)
      }
    }()
    golog.Infof("successfully started http server, listening on: %s", cfg.ListenAddress)
    <-forever
  }
  golog.Infof("exiting application")
}
Similar Resources

db-recovery is a tool for recovering MySQL data.

db-recovery is a tool for recovering MySQL data. It is used in scenarios where the database has no backup or binlog. It can parse data files and redo/undo logs to recover data.

Nov 17, 2022

一个使 mysql,pgsql 数据库表自动生成 go struct 的工具

db2go 一个使 mysql、pgsql 数据库表自动生成 go struct 的工具 快速使用 将项目放入到GOPATH/src目录下

Nov 25, 2022

🐳 A most popular sql audit platform for mysql

🐳 A most popular sql audit platform for mysql

🐳 A most popular sql audit platform for mysql

Jan 6, 2023

Dumpling is a fast, easy-to-use tool written by Go for dumping data from the database(MySQL, TiDB...) to local/cloud(S3, GCP...) in multifarious formats(SQL, CSV...).

🥟 Dumpling Dumpling is a tool and a Go library for creating SQL dump from a MySQL-compatible database. It is intended to replace mysqldump and mydump

Nov 9, 2022

Vitess is a database clustering system for horizontal scaling of MySQL.

Vitess Vitess is a database clustering system for horizontal scaling of MySQL through generalized sharding. By encapsulating shard-routing logic, Vite

Jan 4, 2023

Gaea is a mysql proxy, it's developed by xiaomi b2c-dev team.

Gaea is a mysql proxy, it's developed by xiaomi b2c-dev team.

简介 Gaea是小米中国区电商研发部研发的基于mysql协议的数据库中间件,目前在小米商城大陆和海外得到广泛使用,包括订单、社区、活动等多个业务。Gaea支持分库分表、sql路由、读写分离等基本特性,更多详细功能可以参照下面的功能列表。其中分库分表方案兼容了mycat和kingshard两个项目的路

Dec 30, 2022

Bifrost ---- 面向生产环境的 MySQL 同步到Redis,MongoDB,ClickHouse,MySQL等服务的异构中间件

Bifrost ---- 面向生产环境的 MySQL 同步到Redis,MongoDB,ClickHouse,MySQL等服务的异构中间件

Bifrost ---- 面向生产环境的 MySQL 同步到Redis,ClickHouse等服务的异构中间件 English 漫威里的彩虹桥可以将 雷神 送到 阿斯加德 和 地球 而这个 Bifrost 可以将 你 MySQL 里的数据 全量 , 实时的同步到 : Redis MongoDB Cl

Dec 30, 2022

go mysql driver, support distributed transaction

Go-MySQL-Driver A MySQL-Driver for Go's database/sql package Features Requirements Installation Usage DSN (Data Source Name) Password Protocol Address

Jul 23, 2022
Database migrations. CLI and Golang library.

migrate Database migrations written in Go. Use as CLI or import as library. Migrate reads migrations from sources and applies them in correct order to

Jan 9, 2023
Database migrations written in Go

migrate Database migrations written in Go. Use as CLI or import as library. Migrate reads migrations from sources and applies them in correct order to

Oct 3, 2021
mysql to mysql 轻量级多线程的库表数据同步

goMysqlSync golang mysql to mysql 轻量级多线程库表级数据同步 测试运行 设置当前binlog位置并且开始运行 go run main.go -position mysql-bin.000001 1 1619431429 查询当前binlog位置,参数n为秒数,查询结

Nov 15, 2022
Simple key-value store on top of SQLite or MySQL

KV Work in progress, not ready for prime time. A simple key/value store on top of SQLite or MySQL (Go port of GitHub's KV). Aims to be 100% compatible

Dec 3, 2022
a powerful mysql toolset with Go
a powerful mysql toolset with Go

go-mysql A pure go library to handle MySQL network protocol and replication. Call for Committer/Maintainer Sorry that I have no enough time to maintai

Dec 28, 2022
Sync MySQL data into elasticsearch
Sync MySQL data into elasticsearch

go-mysql-elasticsearch is a service syncing your MySQL data into Elasticsearch automatically. It uses mysqldump to fetch the origin data at first, the

Dec 30, 2022
A high-performance MySQL proxy

kingshard 中文主页 Overview kingshard is a high-performance proxy for MySQL powered by Go. Just like other mysql proxies, you can use it to split the read

Dec 30, 2022
Golang MySql binary log replication listener

Go MySql binary log replication listener Pure Go Implementation of MySQL replication protocol. This allow you to receive event like insert, update, de

Oct 25, 2022
MySQL replication topology management and HA
MySQL replication topology management and HA

orchestrator [Documentation] orchestrator is a MySQL high availability and replication management tool, runs as a service and provides command line ac

Jan 4, 2023
Vitess is a database clustering system for horizontal scaling of MySQL.

Vitess Vitess is a database clustering system for horizontal scaling of MySQL through generalized sharding. By encapsulating shard-routing logic, Vite

Jan 3, 2023