All Projects → go-gormigrate → Gormigrate

go-gormigrate / Gormigrate

Licence: mit
Minimalistic database migration helper for Gorm ORM

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Gormigrate

Gen
Converts a database into gorm structs and RESTful api
Stars: ✭ 825 (+43.73%)
Mutual labels:  gorm, databases
Migrate
Database migrations. CLI and Golang library.
Stars: ✭ 7,712 (+1243.55%)
Mutual labels:  migrations, databases
upscheme
Database migrations and schema updates made easy
Stars: ✭ 737 (+28.4%)
Mutual labels:  schema, migrations
Migrate
Database migrations. CLI and Golang library.
Stars: ✭ 2,315 (+303.31%)
Mutual labels:  migrations, databases
Mongo.migration
On-the-fly migrations with MongoDB C# Driver
Stars: ✭ 99 (-82.75%)
Mutual labels:  schema, migrations
Goose
A database migration tool. Supports SQL migrations and Go functions.
Stars: ✭ 2,112 (+267.94%)
Mutual labels:  schema, migrations
krab
Krab is a migration and automation tool for PostgreSQL based on HCL syntax
Stars: ✭ 15 (-97.39%)
Mutual labels:  schema, migrations
Sleekdb
Pure PHP NoSQL database with no dependency. Flat file, JSON based document database.
Stars: ✭ 450 (-21.6%)
Mutual labels:  schema
Zero downtime migrations
Zero downtime migrations with ActiveRecord 3+ and PostgreSQL
Stars: ✭ 513 (-10.63%)
Mutual labels:  migrations
Sqlflow
Brings SQL and AI together.
Stars: ✭ 4,412 (+668.64%)
Mutual labels:  databases
Phinx
PHP Database Migrations for Everyone
Stars: ✭ 4,245 (+639.55%)
Mutual labels:  migrations
Etlalchemy
Extract, Transform, Load: Any SQL Database in 4 lines of Code.
Stars: ✭ 460 (-19.86%)
Mutual labels:  migrations
Ragtime
Database-independent migration library
Stars: ✭ 519 (-9.58%)
Mutual labels:  migrations
Plank
A tool for generating immutable model objects
Stars: ✭ 449 (-21.78%)
Mutual labels:  schema
Superstruct
A simple and composable way to validate data in JavaScript (and TypeScript).
Stars: ✭ 5,604 (+876.31%)
Mutual labels:  schema
Go Gin Example
An example of gin
Stars: ✭ 4,992 (+769.69%)
Mutual labels:  gorm
Cs Video Courses
List of Computer Science courses with video lectures.
Stars: ✭ 27,209 (+4640.24%)
Mutual labels:  databases
Irisadminapi
iris 框架的后台api项目
Stars: ✭ 544 (-5.23%)
Mutual labels:  gorm
Schm
Composable schemas for JavaScript and Node.js
Stars: ✭ 498 (-13.24%)
Mutual labels:  schema
Libpqxx
The official C++ client API for PostgreSQL.
Stars: ✭ 492 (-14.29%)
Mutual labels:  databases

Gormigrate

PkgGoDev Build status

Gormigrate is a minimalistic migration helper for Gorm. Gorm already has useful migrate functions, just misses proper schema versioning and migration rollback support.

IMPORTANT: If you need support to Gorm v1 (which uses github.com/jinzhu/gorm as its import path), please import Gormigrate by using the gopkg.in/gormigrate.v1 import path.

The current Gorm version (v2) is supported by using the github.com/go-gormigrate/gormigrate/v2 import path as described in the documentation below.

Supported databases

It supports any of the databases Gorm supports:

  • PostgreSQL
  • MySQL
  • SQLite
  • Microsoft SQL Server

Usage

package main

import (
	"log"

	"github.com/go-gormigrate/gormigrate/v2"
	"gorm.io/gorm"
	_ "github.com/jinzhu/gorm/dialects/sqlite"
)

func main() {
	db, err := gorm.Open("sqlite3", "mydb.sqlite3")
	if err != nil {
		log.Fatal(err)
	}

	db.LogMode(true)

	m := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{
		// 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")
			},
		},
	})

	if err = m.Migrate(); err != nil {
		log.Fatalf("Could not migrate: %v", err)
	}
	log.Printf("Migration did run successfully")
}

Having a separated function for initializing the schema

If you have a lot of migrations, it can be a pain to run all them, as example, when you are deploying a new instance of the app, in a clean database. To prevent this, you can set a function that will run if no migration was run before (in a new clean database). Remember to create everything here, all tables, foreign keys and what more you need in your app.

type Person struct {
	gorm.Model
	Name string
	Age int
}

type Pet struct {
	gorm.Model
	Name     string
	PersonID int
}

m := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{
    // you migrations here
})

m.InitSchema(func(tx *gorm.DB) error {
	err := tx.AutoMigrate(
		&Person{},
		&Pet{},
		// all other tables of you app
	)
	if err != nil {
		return err
	}

	if err := tx.Exec("ALTER TABLE pets ADD CONSTRAINT fk_pets_people FOREIGN KEY (person_id) REFERENCES people (id)").Error; err != nil {
		return err
	}
	// all other foreign keys...
	return nil
})

Options

This is the options struct, in case you don't want the defaults:

type Options struct {
	// TableName is the migration table.
	TableName string
	// IDColumnName is the name of column where the migration id will be stored.
	IDColumnName string
	// IDColumnSize is the length of the migration id column
	IDColumnSize int
	// UseTransaction makes Gormigrate execute migrations inside a single transaction.
	// Keep in mind that not all databases support DDL commands inside transactions.
	UseTransaction bool
	// ValidateUnknownMigrations will cause migrate to fail if there's unknown migration
	// IDs in the database
	ValidateUnknownMigrations bool
}

Who is Gormigrate for?

Gormigrate was born to be a simple and minimalistic migration tool for small projects that uses Gorm. You may want to take a look at more advanced solutions like golang-migrate/migrate if you plan to scale.

Be aware that Gormigrate has no builtin lock mechanism, so if you're running it automatically and have a distributed setup (i.e. more than one executable running at the same time), you might want to use a distributed lock/mutex mechanism to prevent race conditions while running migrations.

Contributing

To run tests, first copy .sample.env as sample.env and edit the connection string of the database you want to run tests against. Then, run tests like below:

# running tests for PostgreSQL
go test -tags postgresql

# running test for MySQL
go test -tags mysql

# running tests for SQLite
go test -tags sqlite

# running tests for SQL Server
go test -tags sqlserver

# running test for multiple databases at once
go test -tags 'sqlite postgresql mysql'

Or altenatively, you could use Docker to easily run tests on all databases at once. To do that, make sure Docker is installed and running in your machine and then run:

task docker
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].