Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Podgornyy committed Jun 27, 2020
0 parents commit dd13cf9
Show file tree
Hide file tree
Showing 26 changed files with 4,398 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage.out
tmp.out
profile.out
40 changes: 40 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
language: go

matrix:
fast_finish: true
include:
- go: 1.11.x
env: GO111MODULE=on
- go: 1.12.x
env: GO111MODULE=on
- go: 1.13.x
- go: 1.13.x
env:
- TESTTAGS=nomsgpack
- go: 1.14.x
- go: 1.14.x
env:
- TESTTAGS=nomsgpack
- go: master

git:
depth: 10

before_install:
- if [[ "${GO111MODULE}" = "on" ]]; then mkdir "${HOME}/go"; export GOPATH="${HOME}/go"; fi

install:
- if [[ "${GO111MODULE}" = "on" ]]; then go mod download; fi
- if [[ "${GO111MODULE}" = "on" ]]; then export PATH="${GOPATH}/bin:${GOROOT}/bin:${PATH}"; fi
- if [[ "${GO111MODULE}" = "on" ]]; then make tools; fi

go_import_path: github.com/larapulse/migrator

script:
- make vet
- make fmt-check
- make misspell-check
- make test

after_success:
- bash <(curl -s https://codecov.io/bash)
25 changes: 25 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
MIT License
-----------

Copyright (c) 2020 Sergey Podgornyy (https://podgornyy.rocks)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
68 changes: 68 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
GOFMT ?= gofmt "-s"
PACKAGES ?= $(shell go list ./...)
GOFILES := $(shell find . -name "*.go")
TESTTAGS ?= ""

.PHONY: test
test:
echo "mode: count" > coverage.out
for d in $(PACKAGES); do \
go test -timeout 30s -tags $(TESTTAGS) -v -covermode=count -coverprofile=profile.out $$d > tmp.out; \
cat tmp.out; \
if grep -q "^--- FAIL" tmp.out; then \
rm tmp.out; \
exit 1; \
elif grep -q "build failed" tmp.out; then \
rm tmp.out; \
exit 1; \
elif grep -q "setup failed" tmp.out; then \
rm tmp.out; \
exit 1; \
fi; \
if [ -f profile.out ]; then \
cat profile.out | grep -v "mode:" >> coverage.out; \
rm profile.out; \
fi; \
done

.PHONY: fmt
fmt:
$(GOFMT) -w $(GOFILES)

.PHONY: fmt-check
fmt-check:
@diff=$$($(GOFMT) -d $(GOFILES)); \
if [ -n "$$diff" ]; then \
echo "Please run 'make fmt' and commit the result:"; \
echo "$${diff}"; \
exit 1; \
fi;

vet:
go vet $(PACKAGES)

.PHONY: lint
lint:
@hash golint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
go get -u golang.org/x/lint/golint; \
fi
for PKG in $(PACKAGES); do golint -set_exit_status $$PKG || exit 1; done;

.PHONY: misspell-check
misspell-check:
@hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
go get -u github.com/client9/misspell/cmd/misspell; \
fi
misspell -error $(GOFILES)

.PHONY: misspell
misspell:
@hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
go get -u github.com/client9/misspell/cmd/misspell; \
fi
misspell -w $(GOFILES)

.PHONY: tools
tools:
go install golang.org/x/lint/golint; \
go install github.com/client9/misspell/cmd/misspell;
217 changes: 217 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
# MySQL database migrator

<img align="right" width="159px" src="./logo.png">

[![Build Status](https://travis-ci.org/larapulse/migrator.svg)](https://travis-ci.org/larapulse/migrator)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE.md)
[![codecov](https://codecov.io/gh/larapulse/migrator/branch/master/graph/badge.svg)](https://codecov.io/gh/larapulse/migrator)
[![Go Report Card](https://goreportcard.com/badge/github.com/larapulse/migrator)](https://goreportcard.com/report/github.com/larapulse/migrator)
[![GoDoc](https://godoc.org/github.com/larapulse/migrator?status.svg)](https://pkg.go.dev/github.com/larapulse/migrator?tab=doc)
[![Release](https://img.shields.io/github/release/larapulse/migrator.svg)](https://github.com/larapulse/migrator/releases)
[![TODOs](https://badgen.net/https/api.tickgit.com/badgen/github.com/larapulse/migrator)](https://www.tickgit.com/browse?repo=github.com/larapulse/migrator)

MySQL database migrator designed to run migrations to your features and manage database schema update with intuitive go code. It is compatible with latest MySQL v8.

## Installation

To install `migrator` package, you need to install Go and set your Go workspace first.

1. The first need [Go](https://golang.org/) installed (**version 1.13+ is required**), then you can use the below Go command to install `migrator`.

```sh
$ go get -u github.com/larapulse/migrator
```

2. Import it in your code:

```go
import "github.com/larapulse/migrator"
```

## Quick start

Initialize migrator with migration entries:

```go
var migrations = []migrator.Migration{
{
Name: "19700101_0001_create_posts_table",
Up: func() migrator.Schema {
var s migrator.Schema
posts := migrator.Table{Name: "posts"}

posts.UniqueID("id")
posts.Column("title", migrator.String{Precision: 64})
posts.Column("content", migrator.Text{})
posts.Timestamps()

s.CreateTable(posts)

return s
},
Down: func() migrator.Schema {
var s migrator.Schema

s.DropTable("posts")

return s
},
},
{
Name: "19700101_0002_create_comments_table",
Up: func() migrator.Schema {
var s migrator.Schema
comments := migrator.Table{Name: "comments"}

comments.UniqueID("id")
comments.UUID("post_id", "", false)
comments.Column("title", migrator.String{Precision: 64})
comments.Column("content", migrator.Text{})
comments.Timestamps()

comments.Foreign("post_id", "id", "posts", "RESTRICT", "RESTRICT")

s.CreateTable(comments)

return s
},
Down: func() migrator.Schema {
var s migrator.Schema

s.DropTable("comments")

return s
},
},
}

m := migrator.Migrator{Pool: migrations}
migrated, err = m.Migrate(db)

if err != nil {
fmt.Errorf("Could not migrate: %v", err)
os.Exit(1)
}

if len(migrated) == 0 {
fmt.Println("Nothing were migrated.")
}

for _, m := range migrated {
fmt.Println("Migration: "+m+" was migrated ✅")
}

fmt.Println("Migration did run successfully")
```

After first migration run, `migrations` table will be created:

```
+----+-------------------------------------+-------+---------------------+
| id | name | batch | applied_at |
+----+-------------------------------------+-------+---------------------+
| 1 | 19700101_0001_create_posts_table | 1 | 2020-06-27 00:00:00 |
| 2 | 19700101_0002_create_comments_table | 1 | 2020-06-27 00:00:00 |
+----+-------------------------------------+-------+---------------------+
```

If you want to use another name for migration table, change it `Migrator` before running migrations:

```go
m := migrator.Migrator{TableName: "_my_app_migrations"}
```

### Transactional migration

In case you have multiple commands within one migration and you want to be sure it is migrated properly, you might enable transactional execution per migration:

```go
var migration = migrator.Migration{
Name: "19700101_0001_create_posts_and_users_tables",
Up: func() migrator.Schema {
var s migrator.Schema
posts := migrator.Table{Name: "posts"}
posts.UniqueID("id")
posts.Timestamps()

users := migrator.Table{Name: "users"}
users.UniqueID("id")
users.Timestamps()

s.CreateTable(posts)
s.CreateTable(users)

return s
},
Down: func() migrator.Schema {
var s migrator.Schema

s.DropTable("users")
s.DropTable("posts")

return s
},
Transaction: true,
}
```

### Rollback and revert

In case you need to revert your deploy and DB, you can revert last migrated batch:

```go
m := migrator.Migrator{Pool: migrations}
reverted, err := m.Rollback(db)

if err != nil {
fmt.Errorf("Could not roll back migrations: %v", err)
os.Exit(1)
}

if len(reverted) == 0 {
fmt.Println("Nothing were rolled back.")
}

for _, m := range reverted {
fmt.Println("Migration: "+m+" was rolled back ✅")
}
```

To revert all migrated items back, you have to call `Revert()` on your `migrator`:

```go
m := migrator.Migrator{Pool: migrations}
reverted, err := m.Revert(db)
```

## Customize queries

You may add any column definition to the database on your own, just be sure you implement `columnType` interface:

```go
type customType string

func (ct customType) buildRow() string {
return string(ct)
}

posts := migrator.Table{Name: "posts"}
posts.UniqueID("id")
posts.Column("data", customType("json not null"))
posts.Timestamps()
```

Same logic is for adding custom commands to the Schema to be migrated or reverted, just be sure you implement `command` interface:

```go
type customCommand string

func (cc customCommand) toSQL() string {
return string(cc)
}

var s migrator.Schema

c := customCommand("DROP PROCEDURE abc")
s.CustomCommand(c)
```
Loading

0 comments on commit dd13cf9

Please sign in to comment.