-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
341a57a
commit eaba126
Showing
15 changed files
with
381 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FROM golang:1.21 | ||
|
||
WORKDIR /root | ||
COPY . /root | ||
|
||
RUN go get ./... | ||
|
||
ENTRYPOINT go test github.com/pg-sharding/gorm-spqr/tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# gorm-spqr | ||
Tool for testing compatibility of SPQR and gorm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package controllers | ||
|
||
import "github.com/pg-sharding/gorm-spqr/models" | ||
|
||
func GetAllPersons() []models.Person { | ||
var res []models.Person | ||
models.DB.Find(&res) | ||
|
||
return res | ||
} | ||
|
||
func GetPerson(id uint32) (*models.Person, error) { | ||
var person models.Person | ||
if err := models.DB.Where("id = ?", id).First(&person).Error; err != nil { | ||
return nil, err | ||
} | ||
return &person, nil | ||
} | ||
|
||
func WritePerson(person *models.Person) error { | ||
tx := models.DB.Create(person) | ||
return tx.Error | ||
} | ||
|
||
func UpdatePerson(person *models.Person) error { | ||
var current models.Person | ||
if err := models.DB.Where("id = ?", person.ID).First(¤t).Error; err != nil { | ||
return err | ||
} | ||
models.DB.Save(&person) | ||
return nil | ||
} | ||
|
||
func DeletePerson(id uint32) error { | ||
var person models.Person | ||
if err := models.DB.Where("id = ?", id).First(&person).Error; err != nil { | ||
return err | ||
} | ||
|
||
models.DB.Delete(&person) | ||
return nil | ||
} | ||
|
||
func WritePeople(people []*models.Person) error { | ||
tx := models.DB.Create(people) | ||
return tx.Error | ||
} | ||
|
||
func GetPeople(from uint32, to uint32) ([]*models.Person, error) { | ||
people := make([]*models.Person, 0) | ||
if err := models.DB.Where("id >= ? AND id <= ?", from, to).Find(&people).Error; err != nil { | ||
return nil, err | ||
} | ||
return people, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module github.com/pg-sharding/gorm-spqr | ||
|
||
go 1.21 | ||
|
||
require ( | ||
github.com/jackc/pgx/v5 v5.4.3 | ||
gorm.io/driver/postgres v1.5.4 | ||
gorm.io/gorm v1.25.5 | ||
gotest.tools/v3 v3.5.1 | ||
) | ||
|
||
require ( | ||
github.com/google/go-cmp v0.5.9 // indirect | ||
github.com/jackc/pgpassfile v1.0.0 // indirect | ||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect | ||
github.com/jinzhu/inflection v1.0.0 // indirect | ||
github.com/jinzhu/now v1.1.5 // indirect | ||
golang.org/x/crypto v0.14.0 // indirect | ||
golang.org/x/text v0.13.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= | ||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= | ||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= | ||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= | ||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= | ||
github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY= | ||
github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= | ||
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.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= | ||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= | ||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= | ||
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= | ||
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= | ||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gorm.io/driver/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo= | ||
gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0= | ||
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls= | ||
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= | ||
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU= | ||
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package models | ||
|
||
type Person struct { | ||
ID uint32 `json:"id" gorm:"primary_key"` | ||
FirstName string `json:"first_name"` | ||
LastName string `json:"last_name"` | ||
Email string `json:"email"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package models | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/jackc/pgx/v5" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
"os" | ||
) | ||
|
||
var DB *gorm.DB | ||
|
||
func SetupSharding() { | ||
dsn := fmt.Sprintf( | ||
"host=%s user=%s dbname=%s port=%s %s", | ||
os.Getenv("DB_HOST"), | ||
"spqr-console", | ||
"spqr-console", | ||
os.Getenv("DB_PORT"), | ||
os.Getenv("EXTRA_PARAMS"), | ||
) | ||
conn, err := pgx.Connect(context.Background(), dsn) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to connect to database: %s", err)) | ||
} | ||
defer func() { | ||
_ = conn.Close(context.Background()) | ||
}() | ||
|
||
_, err = conn.Exec(context.Background(), "CREATE DISTRIBUTION ds1 COLUMN TYPES integer;") | ||
if err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "could not setup sharding: %s\n", err) | ||
} | ||
_, err = conn.Exec(context.Background(), "CREATE SHARDING RULE r1 COLUMNS id FOR DISTRIBUTION ds1;") | ||
if err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "could not setup sharding: %s\n", err) | ||
} | ||
_, err = conn.Exec(context.Background(), "CREATE KEY RANGE krid1 FROM 1 TO 100 ROUTE TO sh1 FOR DISTRIBUTION ds1;") | ||
if err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "could not setup sharding: %s\n", err) | ||
} | ||
_, err = conn.Exec(context.Background(), "CREATE KEY RANGE krid2 FROM 100 TO 200 ROUTE TO sh2 FOR DISTRIBUTION ds1;") | ||
if err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "could not setup sharding: %s\n", err) | ||
} | ||
_, err = conn.Exec(context.Background(), "ALTER DISTRIBUTION ds1 ATTACH RELATION people DISTRIBUTION KEY id;") | ||
if err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "could not setup sharding: %s\n", err) | ||
} | ||
} | ||
|
||
func ConnectDatabase() { | ||
dsn := fmt.Sprintf( | ||
"host=%s user=%s password=%s dbname=%s port=%s TimeZone=UTC %s", | ||
os.Getenv("DB_HOST"), | ||
os.Getenv("DB_USER"), | ||
os.Getenv("DB_PASSWORD"), | ||
os.Getenv("DB_NAME"), | ||
os.Getenv("DB_PORT"), | ||
os.Getenv("EXTRA_PARAMS"), | ||
) | ||
database, err := gorm.Open(postgres.New(postgres.Config{ | ||
DSN: dsn, | ||
PreferSimpleProtocol: true, | ||
}), &gorm.Config{}) | ||
|
||
if err != nil { | ||
panic("Failed to connect to database") | ||
} | ||
|
||
if err = database.AutoMigrate(&Person{}); err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "could not migrate: %s", err) | ||
} | ||
|
||
DB = database | ||
} |
Oops, something went wrong.