Skip to content

Commit

Permalink
add gorm-test local
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaUnisikhin committed Mar 18, 2024
1 parent 341a57a commit eaba126
Show file tree
Hide file tree
Showing 15 changed files with 381 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ jobs:
run: ./run-clang-format/run-clang-format.py -r --clang-format-executable clang-format-11 modules sources stress test third_party

- name: Test
env:
CC: ${{ matrix.compiler }}
CC_FOR_BUILD: ${{ matrix.compiler }}
run: make run_test

- name: Prepare for release
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ run_test_prep: build_asan copy_asan_bin build_dbg copy_dbg_bin build_release cop
run_test:
# change dir, test would not work with absolute path
./cleanup-docker.sh
docker-compose -f ./docker-compose-test.yml up --exit-code-from odyssey odyssey openldapr
docker-compose -f ./docker-compose-test.yml up --exit-code-from odyssey

submit-cov:
mkdir cov-build && cd cov-build
Expand Down
10 changes: 0 additions & 10 deletions docker-compose-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ services:
networks:
od_net:
ipv4_address: 192.168.233.16

gorm-test:
build:
context: https://github.com/pg-sharding/gorm-spqr.git#main
environment:
DB_HOST: "localhost"
DB_PORT: "6432"
DB_USER: "spqr-console"
DB_NAME: "spqr-console"
network_mode: "host"

networks:
od_net:
Expand Down
4 changes: 0 additions & 4 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ RUN curl -fsSL https://get.docker.com -o get-docker.sh && \

COPY ./docker/pg/pg_hba-test.conf /etc/postgresql/14/main/pg_hba.conf

COPY ./docker-compose-test.yml /docker-compose-test.yml

RUN mkdir test_dir
COPY . /test_dir
RUN cd /test_dir && make run_test_prep && cp /test_dir/docker/bin/* /usr/bin/
Expand All @@ -84,8 +82,6 @@ COPY ./docker/gorm /gorm

COPY ./docker/entrypoint.sh /usr/local/bin/entrypoint.sh

RUN touch ./logs.txt

RUN chmod a+x /usr/local/bin/entrypoint.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
14 changes: 5 additions & 9 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ set -ex

setup

# gorm
ody-start
/gorm/test.sh
ody-stop

# # odyssey rule-address test
# /rule-address/test.sh
# if [ $? -eq 1 ]
Expand Down Expand Up @@ -66,15 +71,6 @@ setup
# exit 1
# fi

# gorm
ody-start
/gorm/test.sh
if [ $? -eq 1 ]
then
exit 1
fi
ody-stop

# /usr/bin/odyssey-asan /etc/odyssey/odyssey.conf
# ody-stop

Expand Down
8 changes: 8 additions & 0 deletions docker/gorm/gorm-spqr/Dockerfile
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
2 changes: 2 additions & 0 deletions docker/gorm/gorm-spqr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gorm-spqr
Tool for testing compatibility of SPQR and gorm
55 changes: 55 additions & 0 deletions docker/gorm/gorm-spqr/controllers/person.go
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(&current).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
}
20 changes: 20 additions & 0 deletions docker/gorm/gorm-spqr/go.mod
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
)
36 changes: 36 additions & 0 deletions docker/gorm/gorm-spqr/go.sum
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=
8 changes: 8 additions & 0 deletions docker/gorm/gorm-spqr/models/person.go
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"`
}
77 changes: 77 additions & 0 deletions docker/gorm/gorm-spqr/models/setup.go
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
}
Loading

0 comments on commit eaba126

Please sign in to comment.