Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema Migration POC #6

Merged
merged 4 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions .bundle/config

This file was deleted.

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.env
bin
!bin/README.md
bundle
12 changes: 0 additions & 12 deletions Gemfile

This file was deleted.

80 changes: 0 additions & 80 deletions Gemfile.lock

This file was deleted.

12 changes: 4 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ export DOCKER_REGISTRY ?= docker.pkg.github.com/fs02/go-todo-backend
export DEPLOY ?= api

all: build start
bundle:
bundle install --path bundle
db-create: bundle
bundle exec rake db:create
db-migrate: bundle
bundle exec rake db:migrate
db-rollback: bundle
bundle exec rake db:rollback
db-migrate:
export $$(cat .env | grep -v ^\# | xargs) && go run cmd/db/main.go migrate
db-rollback:
export $$(cat .env | grep -v ^\# | xargs) && go run cmd/db/main.go rollback
gen:
go generate ./...
build: gen
Expand Down
23 changes: 0 additions & 23 deletions Rakefile

This file was deleted.

60 changes: 60 additions & 0 deletions cmd/db/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"context"
"fmt"
"os"

"github.com/Fs02/go-todo-backend/db/migrations"
"github.com/Fs02/rel"
"github.com/Fs02/rel/adapter/postgres"
"github.com/Fs02/rel/migrator"
_ "github.com/lib/pq"
"go.uber.org/zap"
)

var (
logger, _ = zap.NewProduction(zap.Fields(zap.String("type", "main")))
shutdowns []func() error
)

func main() {
var (
ctx = context.Background()
dsn = fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
os.Getenv("POSTGRESQL_USERNAME"),
os.Getenv("POSTGRESQL_PASSWORD"),
os.Getenv("POSTGRESQL_HOST"),
os.Getenv("POSTGRESQL_PORT"),
os.Getenv("POSTGRESQL_DATABASE"))
)

adapter, err := postgres.Open(dsn)
if err != nil {
logger.Fatal(err.Error(), zap.Error(err))
}

var (
op string
repo = rel.New(adapter)
m = migrator.New(repo)
)

// There will be a command line like go test for this in the future.
m.Register(20202806225100, migrations.MigrateCreateTodos, migrations.RollbackCreateTodos)
m.Register(20203006230600, migrations.MigrateCreateScores, migrations.RollbackCreateScores)
m.Register(20203006230700, migrations.MigrateCreatePoints, migrations.RollbackCreatePoints)

if len(os.Args) > 1 {
op = os.Args[1]
}

switch op {
case "migrate", "up":
m.Migrate(ctx)
case "rollback", "down":
m.Rollback(ctx)
default:
logger.Fatal("command not recognized", zap.String("command", op))
}
}
4 changes: 1 addition & 3 deletions db/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# db

Contains file required for building db schema. `schema.rb` is useful when your project already running for a long time and running migration file one by one takes a lot of time, which then you can just load the schema directly which is faster.

The reason I'm using ruby based solution is because there's no golang based solution that can generate schema and allows working on multiple branch with different not yet merged migration without having to rollback and migrate when switching branch.
Contains file required for building db schema.
20 changes: 0 additions & 20 deletions db/config.yml

This file was deleted.

13 changes: 0 additions & 13 deletions db/migrate/20202806225100_create_todos.rb

This file was deleted.

9 changes: 0 additions & 9 deletions db/migrate/20203006230600_create_scores.rb

This file was deleted.

12 changes: 0 additions & 12 deletions db/migrate/20203006230700_create_points.rb

This file was deleted.

24 changes: 24 additions & 0 deletions db/migrations/20202806225100_create_todos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package migrations

import (
"github.com/Fs02/rel"
)

// MigrateCreateTodos definition
func MigrateCreateTodos(schema *rel.Schema) {
schema.CreateTable("todos", func(t *rel.Table) {
t.ID("id")
t.DateTime("created_at")
t.DateTime("updated_at")
t.String("title")
t.Bool("completed")
t.Int("order")
})

schema.CreateIndex("todos", "order", []string{"order"})
}

// RollbackCreateTodos definition
func RollbackCreateTodos(schema *rel.Schema) {
schema.DropTable("todos")
}
20 changes: 20 additions & 0 deletions db/migrations/20203006230600_create_scores.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package migrations

import (
"github.com/Fs02/rel"
)

// MigrateCreateScores definition
func MigrateCreateScores(schema *rel.Schema) {
schema.CreateTable("scores", func(t *rel.Table) {
t.ID("id")
t.DateTime("created_at")
t.DateTime("updated_at")
t.Int("total_point")
})
}

// RollbackCreateScores definition
func RollbackCreateScores(schema *rel.Schema) {
schema.DropTable("scores")
}
24 changes: 24 additions & 0 deletions db/migrations/20203006230700_create_points.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package migrations

import (
"github.com/Fs02/rel"
)

// MigrateCreatePoints definition
func MigrateCreatePoints(schema *rel.Schema) {
schema.CreateTable("points", func(t *rel.Table) {
t.ID("id")
t.DateTime("created_at")
t.DateTime("updated_at")
t.Int("name")
t.Int("count")
t.Int("score_id", rel.Unsigned(true))

t.ForeignKey("score_id", "scores", "id")
})
}

// RollbackCreatePoints definition
func RollbackCreatePoints(schema *rel.Schema) {
schema.DropTable("points")
}
42 changes: 0 additions & 42 deletions db/schema.rb

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/Fs02/go-todo-backend
go 1.14

require (
github.com/Fs02/rel v0.5.0
github.com/Fs02/rel v0.6.1-0.20200903172418-3af9e733867d
github.com/azer/snakecase v1.0.0 // indirect
github.com/go-chi/chi v3.3.2+incompatible
github.com/goware/cors v1.1.1
Expand Down