Skip to content
Permalink
Browse files

feat: switch to mysql

  • Loading branch information...
moul committed Mar 14, 2019
1 parent e41c2b4 commit 6ac64670e694cd1fbc899a41343b5563fa9ad393
Showing with 186 additions and 151 deletions.
  1. +3 −2 .circleci/config.yml
  2. +2 −0 .dockerignore
  3. +5 −4 Dockerfile
  4. +29 −8 Makefile
  5. +17 −5 docker-compose.yml
  6. +1 −1 entity/entity.go
  7. +102 −105 entity/entity.pb.go
  8. +8 −8 entity/entity.proto
  9. +3 −4 go.mod
  10. +3 −2 go.sum
  11. +2 −2 sql/cmd_sql.go
  12. +5 −4 sql/sql.go
  13. +1 −1 test/Dockerfile
  14. +5 −5 test/docker-compose.yml
@@ -50,8 +50,9 @@ jobs:
- setup_remote_docker:
docker_layer_caching: true
- run: docker build -t pathwar/pathwar .
- run: make docker.integration
- run: make docker.integration
- run: docker build -t pathwar/test ./test
- run: make integration.run
- run: make integration.run # yes, again
- run: docker-compose logs

workflows:
@@ -1,3 +1,5 @@
.circleci/
test/
.git/
*~
.#*
@@ -1,16 +1,17 @@
# builder
FROM golang:1.11-alpine as builder
FROM golang:1.12-alpine as builder
RUN apk --no-cache --update add nodejs-npm make gcc g++ musl-dev openssl-dev git
ENV GO111MODULE=on
COPY go.* /go/src/pathwar.pw/
ENV GO111MODULE=on GOPROXY=https://goproxy.io
COPY go.mod go.sum /go/src/pathwar.pw/
WORKDIR /go/src/pathwar.pw
RUN go get .
COPY . .
RUN touch .proto.generated && make install

# runtime
FROM alpine:3.8
RUN apk --no-cache --update add openssl
RUN apk --no-cache --update add openssl wget bash
RUN wget https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh && chmod +x wait-for-it.sh
COPY --from=builder /go/bin/pathwar.pw /bin/pathwar.pw
ENTRYPOINT ["/bin/pathwar.pw"]
CMD ["server"]
@@ -39,14 +39,34 @@ help:
sed 's/^/ $(HELP_MSG_PREFIX)make /'

.PHONY: run
run: $(BIN)
run: $(BIN) mysql.up
$(BIN) server $(RUN_OPTS)

.PHONY: install
install: $(BIN)
$(BIN): .proto.generated $(PWCTL_OUT_FILES) $(OUR_SOURCES)
go install -v

.PHONY: mysql.up
mysql.up:
docker-compose up -d mysql
@echo "Waiting for mysql to be ready..."
@while ! mysqladmin ping -h127.0.0.1 -P3306 --silent; do sleep 1; done
@echo "Done."

.PHONY: mysql.down
mysql.down:
docker-compose stop mysql || true
docker-compose rm -f -v mysql || true

.PHONY: mysql.shell
mysql.shell:
mysql -h127.0.0.1 -P3306 -uroot -puns3cur3 pathwar

.PHONY: mysql.dump
mysql.dump:
mysqldump -h127.0.0.1 -P3306 -uroot -puns3cur3 pathwar

.PHONY: clean
clean:
rm -f $(GENERATED_FILES) .proto.generated
@@ -93,13 +113,14 @@ test: .proto.generated

.PHONY: docker.build
docker.build:
docker build -t pathwar/pathwar .

.PHONY: docker.integration
docker.integration:
docker-compose -f ./test/docker-compose.yml up -d server
docker-compose -f ./test/docker-compose.yml run client
docker-compose -f ./test/docker-compose.yml down
docker build -t pathwar/pathwar:latest .
docker build -t pathwar/pathwar:test ./test

.PHONY: integration.run
integration.run:
docker-compose -f ./docker-compose.yml -f ./test/docker-compose.yml up -d --no-build server
docker-compose -f ./docker-compose.yml -f ./test/docker-compose.yml run --no-deps client
docker-compose -f ./docker-compose.yml -f ./test/docker-compose.yml down

.PHONY: integration
integration:
@@ -2,10 +2,22 @@ version: '2.3'

services:
server:
&server
image: pathwar/pathwar
command: server
#depends_on:
# - mysql
image: pathwar/pathwar:latest
build: .
entrypoint: /bin/bash -c
command:
- ./wait-for-it.sh mysql:3306 -- /bin/pathwar.pw server --sql-config="root:uns3cur3@tcp(mysql:3306)/pathwar?charset=utf8"
depends_on:
- mysql
ports:
- 8000

mysql:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: uns3cur3
MYSQL_DATABASE: pathwar
@@ -2,7 +2,6 @@ package entity

func All() []interface{} {
return []interface{}{
Achievement{},
Coupon{},
Event{},
Hypervisor{},
@@ -19,5 +18,6 @@ func All() []interface{} {
UserSession{},
User{},
WhoswhoAttempt{},
Achievement{},
}
}

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -36,10 +36,10 @@ message TeamMember {
//

User user = 200 [(gogoproto.moretags) = "gorm:\"foreignkey:UserID\""];
string user_id = 201 [(gogoproto.customname) = "UserID", (gogoproto.moretags) = "sql:\"type:string not null REFERENCES user(id)\" gorm:\"index\""];
string user_id = 201 [(gogoproto.customname) = "UserID", (gogoproto.moretags) = "sql:\"not null\" gorm:\"index\""];

TournamentTeam tournament_team = 202 [(gogoproto.moretags) = "gorm:\"foreignkey:TournamentTeamID\""];
string tournament_team_id = 203 [(gogoproto.customname) = "TournamentTeamID", (gogoproto.moretags) = "sql:\"type:string not null REFERENCES tournament_team(id)\" gorm:\"index\""];
string tournament_team_id = 203 [(gogoproto.customname) = "TournamentTeamID", (gogoproto.moretags) = "sql:\"not null\" gorm:\"index\""];
}

// User definces a User Account / Profile
@@ -142,7 +142,7 @@ message TournamentTeam {
//

Tournament tournament = 200 [(gogoproto.moretags) = "gorm:\"foreignkey:TournamentID\""];
string tournament_id = 201 [(gogoproto.customname) = "TournamentID", (gogoproto.moretags) = "sql:\"type:string not null REFERENCES tournament(id)\" gorm:\"index\""];
string tournament_id = 201 [(gogoproto.customname) = "TournamentID", (gogoproto.moretags) = "sql:\"not null\" gorm:\"index\""];
}

message WhoswhoAttempt {
@@ -169,7 +169,7 @@ message LevelSubscription {
//

TournamentTeam tournament_team = 200 [(gogoproto.moretags) = "gorm:\"foreignkey:TournamentTeamID\""];
string tournament_team_id = 201 [(gogoproto.customname) = "TournamentTeamID", (gogoproto.moretags) = "sql:\"type:string not null REFERENCES tournament_team(id)\" gorm:\"index\""];
string tournament_team_id = 201 [(gogoproto.customname) = "TournamentTeamID", (gogoproto.moretags) = "sql:\"not null\" gorm:\"index\""];
}

message ShopItem {
@@ -184,7 +184,7 @@ message ShopItem {
//

TournamentTeam tournament_team = 200 [(gogoproto.moretags) = "gorm:\"foreignkey:TournamentTeamID\""];
string tournament_team_id = 201 [(gogoproto.customname) = "TournamentTeamID", (gogoproto.moretags) = "sql:\"type:string not null REFERENCES tournament_team(id)\" gorm:\"index\""];
string tournament_team_id = 201 [(gogoproto.customname) = "TournamentTeamID", (gogoproto.moretags) = "sql:\"not null\" gorm:\"index\""];
}

message LevelFlavor {
@@ -210,7 +210,7 @@ message LevelInstance {
// associations
//
Hypervisor hypervisor = 200 [(gogoproto.moretags) = "gorm:\"foreignkey:HypervisorID\""];
string hypervisor_id = 201 [(gogoproto.customname) = "HypervisorID", (gogoproto.moretags) = "sql:\"type:string not null REFERENCES hypervisor(id)\" gorm:\"index\""];
string hypervisor_id = 201 [(gogoproto.customname) = "HypervisorID", (gogoproto.moretags) = "sql:\"not null\" gorm:\"index\""];
}

message Hypervisor {
@@ -261,7 +261,7 @@ message Coupon {
// associations
//
TeamMember team_member = 200 [(gogoproto.moretags) = "gorm:\"foreignkey:TeamMemberID\""];
string team_member_id = 201 [(gogoproto.customname) = "TeamMemberID", (gogoproto.moretags) = "sql:\"type:string REFERENCES team_member(id)\" gorm:\"index\""];
string team_member_id = 201 [(gogoproto.customname) = "TeamMemberID", (gogoproto.moretags) = "gorm:\"index\""];
}

message Achievement {
@@ -308,7 +308,7 @@ message Achievement {
//

TeamMember team_member = 200 [(gogoproto.moretags) = "gorm:\"foreignkey:TeamMemberID\""];
string team_member_id = 201 [(gogoproto.customname) = "TeamMemberID", (gogoproto.moretags) = "sql:\"type:string not null REFERENCES team_member(id)\" gorm:\"index\""];
string team_member_id = 201 [(gogoproto.customname) = "TeamMemberID", (gogoproto.moretags) = "sql:\"not null\" gorm:\"index\""];
}

message Dump {
7 go.mod
@@ -11,20 +11,19 @@ require (
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.3.3 // indirect
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/go-sql-driver/mysql v1.4.1
github.com/gobuffalo/packr/v2 v2.0.0-rc.15
github.com/gogo/gateway v1.0.0
github.com/gogo/protobuf v1.2.0
github.com/golang/protobuf v1.2.0
github.com/google/go-cmp v0.2.0 // indirect
github.com/gorilla/mux v1.7.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190104160321-4832df01553a
github.com/grpc-ecosystem/grpc-gateway v1.6.4
github.com/jinzhu/gorm v1.9.2
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect
github.com/jinzhu/now v0.0.0-20181116074157-8ec929ed50c3 // indirect
github.com/lib/pq v1.0.0 // indirect
github.com/mattn/go-sqlite3 v1.10.0
github.com/mattn/go-sqlite3 v1.10.0 // indirect
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/pkg/errors v0.8.1
@@ -40,6 +39,6 @@ require (
google.golang.org/appengine v1.2.0 // indirect
google.golang.org/genproto v0.0.0-20190108161440-ae2f86662275
google.golang.org/grpc v1.17.0
gotest.tools v2.2.0+incompatible // indirect
gotest.tools v0.0.0-20181223230014-1083505acf35 // indirect
moul.io/zapgorm v0.0.0-20181220094309-2149c5ded6fc
)
5 go.sum
@@ -453,6 +453,7 @@ golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c h1:fqgJT0MGcGpPgpWU7VRdRjuArfcOvC4AoJmILihzhDg=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180810170437-e96c4e24768d/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181003024731-2f84ea8ef872/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
@@ -501,8 +502,8 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gotest.tools v2.2.0+incompatible h1:y0IMTfclpMdsdIbr6uwmJn5/WZ7vFuObxDMdrylFM3A=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools v0.0.0-20181223230014-1083505acf35 h1:zpdCK+REwbk+rqjJmHhiCN6iBIigrZ39glqSF0P3KF0=
gotest.tools v0.0.0-20181223230014-1083505acf35/go.mod h1:R//lfYlUuTOTfblYI3lGoAAAebUdzjvbmQsuB7Ykd90=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
moul.io/zapgorm v0.0.0-20181220094309-2149c5ded6fc h1:Ddo1Fa0ruGmrNMWVOzEfbMcp3yy3XrspnmQMJ4o7mfg=
moul.io/zapgorm v0.0.0-20181220094309-2149c5ded6fc/go.mod h1:5XkuYCrMvJcscMD3+T4QGDaWsCZiql8k2Pv5iNhTB/g=
@@ -9,7 +9,7 @@ import (
)

type Options struct {
Path string `mapstructure:"path"`
Config string `mapstructure:"config"`
}

func Commands() cli.Commands {
@@ -28,7 +28,7 @@ type sqlCommand struct{ opts Options }

func (cmd *sqlCommand) LoadDefaultOptions() error { return viper.Unmarshal(&cmd.opts) }
func (cmd *sqlCommand) ParseFlags(flags *pflag.FlagSet) {
flags.StringVarP(&cmd.opts.Path, "sql-path", "", "/tmp/pathwar.db", "SQL db path")
flags.StringVarP(&cmd.opts.Config, "sql-config", "", "root:uns3cur3@tcp(127.0.0.1:3306)/pathwar?charset=utf8", "SQL connection config")
if err := viper.BindPFlags(flags); err != nil {
zap.L().Warn("failed to bind viper flags", zap.Error(err))
}
@@ -6,15 +6,16 @@ import (
"os"

"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3" // required by gorm
//_ "github.com/mattn/go-sqlite3" // required by gorm
_ "github.com/go-sql-driver/mysql" // required by gorm
"go.uber.org/zap"
"moul.io/zapgorm"

"pathwar.pw/entity"
)

func FromOpts(opts *Options) (*gorm.DB, error) {
db, err := gorm.Open("sqlite3", opts.Path)
db, err := gorm.Open("mysql", opts.Config)
if err != nil {
return nil, err
}
@@ -30,10 +31,10 @@ func FromOpts(opts *Options) (*gorm.DB, error) {
db.BlockGlobalUpdate(true)
db.SingularTable(true)
db.LogMode(true)
if err := db.Exec("PRAGMA foreign_keys = ON").Error; err != nil {
if err := db.AutoMigrate(entity.All()...).Error; err != nil {
return nil, err
}
if err := db.AutoMigrate(entity.All()...).Error; err != nil {
if err := db.Model(entity.Achievement{}).AddForeignKey("team_member_id", "team_member(id)", "RESTRICT", "RESTRICT").Error; err != nil {
return nil, err
}
// FIXME: use gormigrate
@@ -3,6 +3,6 @@ RUN apk --no-cache add curl openssl python3 make bash jq \
&& pip3 install -U pip setuptools httpie \
&& http --version \
&& jq --version
RUN wget https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh && chmod +x wait-for-it.sh
ENTRYPOINT ["/bin/bash", "-c"]
CMD ["sleep 5; ./integration.sh"]
COPY . .
@@ -1,15 +1,15 @@
version: '2.3'

services:
server:
extends:
file: ../docker-compose.yml
service: server

client:
image: pathwar/pathwar:test
build: .
environment:
TARGET: http://server:8000
command:
- ./wait-for-it.sh server:8000 -- ./integration.sh
depends_on:
- server
links:
- server
tty: true

0 comments on commit 6ac6467

Please sign in to comment.
You can’t perform that action at this time.