Skip to content

Commit

Permalink
chore(examples): add example simple migration
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Dec 12, 2022
1 parent 8cf19b2 commit 515bf1e
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ node_modules
Thumbs.db

/gobblr
/examples/simple/migrations/simple
14 changes: 13 additions & 1 deletion .gitpod.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
tasks:
- name: Build migration program
init: |
cd ./examples/simple/migrations
go build .
command: |
exit 0
- name: MySQL
env:
DB_CONNECTION: "root:password@tcp(localhost:3306)/gobblr"
DB_TYPE: mysql
command: |
docker run \
-e MYSQL_ROOT_PASSWORD=password \
-e MYSQL_DATABASE=gobblr \
--rm \
-p 3306:3306 \
--name mysql \
mysql
mysql &
./examples/simple/migrations/simple
- name: MongoDB
command: |
Expand Down
16 changes: 16 additions & 0 deletions examples/simple/data/001-users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"meta": {
"createdKey": "created_at",
"updatedKey": "updated_at"
},
"data": [
{
"registered": true,
"confirmed": true,
"username": "test1",
"email_address": "test@test.com",
"name": "Test Testington",
"password": "13905934cb5503269ca5613b72a8aa8a:57a1846d12716308be489ec25f2a017e22aba88d591f5c5184e046cb42ec921f1ac5f5249b212efe5c8a644d45d7c63d3ee075cce79ba3a663c731971a56f4c3"
}
]
}
13 changes: 13 additions & 0 deletions examples/simple/migrations/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM golang AS builder
WORKDIR /app
ADD . .
ENV CGO_ENABLED=0
ENV GOOS=linux
RUN go build -o /app/app
ENTRYPOINT /app/app

FROM alpine
RUN apk --no-cache add ca-certificates
WORKDIR /app
COPY --from=builder /app/app /app
ENTRYPOINT [ "/app/app" ]
15 changes: 15 additions & 0 deletions examples/simple/migrations/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module simple

go 1.19

require (
github.com/cenkalti/backoff/v4 v4.2.0
gorm.io/driver/mysql v1.4.4
gorm.io/gorm v1.24.2
)

require (
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
)
14 changes: 14 additions & 0 deletions examples/simple/migrations/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/cenkalti/backoff/v4 v4.2.0 h1:HN5dHm3WBOgndBH6E8V0q2jIYIR3s9yglV8k/+MN3u4=
github.com/cenkalti/backoff/v4 v4.2.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
gorm.io/driver/mysql v1.4.4 h1:MX0K9Qvy0Na4o7qSC/YI7XxqUw5KDw01umqgID+svdQ=
gorm.io/driver/mysql v1.4.4/go.mod h1:BCg8cKI+R0j/rZRQxeKis/forqRwRSYOR8OM3Wo6hOM=
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.24.2 h1:9wR6CFD+G8nOusLdvkZelOEhpJVwwHzpQOUM+REd6U0=
gorm.io/gorm v1.24.2/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
72 changes: 72 additions & 0 deletions examples/simple/migrations/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// The SQL databases need to have the tables created before
// we can do anything. Normally, this would be your own migration
// package's responsibility, but this simulates it for our
// purposes

package main

import (
"fmt"
"os"

"github.com/cenkalti/backoff/v4"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

type User struct {
gorm.Model
Registered bool
Confirmed bool
Username string
EmailAddress string
Name string
Password string
}

func execute(dialector gorm.Dialector) error {
db, err := gorm.Open(dialector, &gorm.Config{})
if err != nil {
return err
}

if err := db.AutoMigrate(&User{}); err != nil {
return err
}

return nil
}

func main() {
connection := os.Getenv("DB_CONNECTION")
dbType := os.Getenv("DB_TYPE")

var dialector gorm.Dialector
switch dbType {
case "mysql":
dialector = mysql.Open(connection)
default:
panic(fmt.Errorf("unknown db type: %s", dbType))
}

tries := 0

err := backoff.Retry(
func() error {
if tries > 0 {
fmt.Println("Retrying database connection...")
}
err := execute(dialector)

tries += 1

return err
},
backoff.NewExponentialBackOff(),
)
if err != nil {
panic(err)
}

fmt.Println("Migration completed")
}
6 changes: 6 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go 1.19

use (
.
./examples/simple/migrations
)
6 changes: 6 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b h1:tvrvnPFcdzp294diPnrdZZZ8XUt2Tyj7svb7X52iDuU=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=

0 comments on commit 515bf1e

Please sign in to comment.