Skip to content

Commit

Permalink
Support source/replica connections (#10)
Browse files Browse the repository at this point in the history
* Support source/replica connections

* update workflow

* update workflow

* fix workflow

* fix test

* use MustOpen

* update rel

* refactor test

* added example

* fix import

* update docker compose

* fix condition

* Revert "fix condition"

This reverts commit c418dcc.

* Bump REL v0.28.0

* tidy
  • Loading branch information
Fs02 committed Oct 29, 2021
1 parent eed7878 commit f04a000
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 12 deletions.
21 changes: 19 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ on:
branches: [ main ]

jobs:
test:
name: Test
mysql:
name: MySQL
strategy:
matrix:
mysql: [5, 8]
Expand Down Expand Up @@ -42,3 +42,20 @@ jobs:
- name: Codecov
uses: codecov/codecov-action@v1
if: matrix.mysql == 8

primaryreplica:
name: Primary Replica
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Start mysql containers
run: docker-compose -f "docker-compose.yml" up -d --build
- name: Test
env:
TEST_PRIMARY_REPLICA: 'true'
run: |
sleep 15
go test -race ./...
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,35 @@ func main() {
}
```

## Example Replication (Source/Replica)

```go
package main

import (
"context"

_ "github.com/go-sql-driver/mysql"
"github.com/go-rel/primaryreplica"
"github.com/go-rel/mysql"
"github.com/go-rel/rel"
)

func main() {
// open mysql connection.
// note: `clientFoundRows=true` is required for update and delete to works correctly.
adapter := primaryreplica.New(
mysql.MustOpen("root@(source:23306)/rel_test?charset=utf8&parseTime=True&loc=Local"),
mysql.MustOpen("root@(replica:23307)/rel_test?charset=utf8&parseTime=True&loc=Local"),
)
defer adapter.Close()

// initialize REL's repo.
repo := rel.New(adapter)
repo.Ping(context.TODO())
}
```

## Supported Driver

- github.com/go-sql-driver/mysql
Expand Down
42 changes: 42 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
version: '2.1'

services:
mariadb-master:
image: docker.io/bitnami/mariadb:10.6
ports:
- '23306:3306'
environment:
- MARIADB_REPLICATION_MODE=master
- MARIADB_REPLICATION_USER=repl_user
- MARIADB_USER=rel
- MARIADB_PASSWORD=rel
- MARIADB_DATABASE=rel_test
- MARIADB_ROOT_PASSWORD=rel
- ALLOW_EMPTY_PASSWORD=yes
healthcheck:
test: ['CMD', '/opt/bitnami/scripts/mariadb/healthcheck.sh']
interval: 15s
timeout: 5s
retries: 6

mariadb-slave:
image: docker.io/bitnami/mariadb:10.6
ports:
- '23307:3306'
depends_on:
- mariadb-master
environment:
- MARIADB_REPLICATION_MODE=slave
- MARIADB_REPLICATION_USER=repl_user
- MARIADB_USER=rel
- MARIADB_PASSWORD=rel
- MARIADB_DATABASE=rel_test
- MARIADB_MASTER_HOST=mariadb-master
- MARIADB_MASTER_PORT_NUMBER=3306
- MARIADB_MASTER_ROOT_PASSWORD=rel
- ALLOW_EMPTY_PASSWORD=yes
healthcheck:
test: ['CMD', '/opt/bitnami/scripts/mariadb/healthcheck.sh']
interval: 15s
timeout: 5s
retries: 6
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module github.com/go-rel/mysql
go 1.17

require (
github.com/go-rel/rel v0.27.0
github.com/go-rel/primaryreplica v0.1.0
github.com/go-rel/rel v0.28.0
github.com/go-rel/sql v0.5.0
github.com/go-sql-driver/mysql v1.6.0
github.com/stretchr/testify v1.7.0
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/go-rel/rel v0.27.0 h1:Z9NOScAMwu54sxXmL/gET5+qKkisTS95MIk6uH/+r9Y=
github.com/go-rel/primaryreplica v0.1.0 h1:ARt8QHa55Tmz7hJYtGunz4B3THQiFDOJb0yZBgnrzx4=
github.com/go-rel/primaryreplica v0.1.0/go.mod h1:j/b9RmL4gdnuQMh4UPBeE1h6RpQCtiUIC9QHIvgjIs8=
github.com/go-rel/rel v0.27.0/go.mod h1:zaIYPmM3AfJrh0xBmm7KoVKRgTNvr0cgZfcJ88gVA2U=
github.com/go-rel/rel v0.28.0 h1:gRcQjNbwuFL35RxeHFMKSy3a/xV+WbtkA8wP24dWPEA=
github.com/go-rel/rel v0.28.0/go.mod h1:zaIYPmM3AfJrh0xBmm7KoVKRgTNvr0cgZfcJ88gVA2U=
github.com/go-rel/sql v0.5.0 h1:+TVS9JvEl06Q8rswwuWlY6VZ+gwSBX9um+vKuZ9gsyY=
github.com/go-rel/sql v0.5.0/go.mod h1:2YwenlIaHpTqdD/KPVYG7Y5Ub1+sn1winlC8TrighRU=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
Expand Down
10 changes: 10 additions & 0 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ func Open(dsn string) (rel.Adapter, error) {
return New(database), err
}

// MustOpen mysql connection using dsn.
func MustOpen(dsn string) rel.Adapter {
var (
adapter, err = Open(dsn)
)

check(err)
return adapter
}

func incrementFunc(adapter sql.SQL) int {
var (
variable string
Expand Down
40 changes: 32 additions & 8 deletions mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"testing"

"github.com/go-rel/primaryreplica"
"github.com/go-rel/rel"
"github.com/go-rel/rel/adapter/specs"
_ "github.com/go-sql-driver/mysql"
Expand All @@ -19,16 +20,10 @@ func dsn() string {
return os.Getenv("MYSQL_DATABASE") + "?charset=utf8&parseTime=True&loc=Local"
}

return "root@tcp(localhost:3306)/rel_test?charset=utf8&parseTime=True&loc=Local"
return "rel:rel@tcp(localhost:23306)/rel_test?charset=utf8&parseTime=True&loc=Local"
}

func TestAdapter_specs(t *testing.T) {
adapter, err := Open(dsn())
assert.Nil(t, err)
defer adapter.Close()

repo := rel.New(adapter)

func AdapterSpecs(t *testing.T, repo rel.Repository) {
// Prepare tables
teardown := specs.Setup(t, repo)
defer teardown()
Expand Down Expand Up @@ -96,6 +91,35 @@ func TestAdapter_specs(t *testing.T) {
specs.ForeignKeyConstraintOnUpdate(t, repo)
}

func TestAdapter_specs(t *testing.T) {
if os.Getenv("TEST_PRIMARY_REPLICA") == "true" {
t.Log("Skipping single node specs")
return
}

adapter := MustOpen(dsn())
defer adapter.Close()

repo := rel.New(adapter)
AdapterSpecs(t, repo)
}

func TestAdapter_PrimaryReplica_specs(t *testing.T) {
if os.Getenv("TEST_PRIMARY_REPLICA") != "true" {
t.Log("Skipping primary replica specs")
return
}

adapter := primaryreplica.New(
MustOpen("rel:rel@tcp(localhost:23306)/rel_test?charset=utf8&parseTime=True&loc=Local"),
MustOpen("rel:rel@tcp(localhost:23307)/rel_test?charset=utf8&parseTime=True&loc=Local"),
)
defer adapter.Close()

repo := rel.New(adapter)
AdapterSpecs(t, repo)
}

func TestAdapter_Open(t *testing.T) {
// with parameter
assert.NotPanics(t, func() {
Expand Down

0 comments on commit f04a000

Please sign in to comment.