Skip to content

Commit

Permalink
Add integration tests for specific RDBMS savepointers
Browse files Browse the repository at this point in the history
  • Loading branch information
dhui committed Jan 4, 2019
1 parent 8d944c9 commit cf78653
Show file tree
Hide file tree
Showing 5 changed files with 325 additions and 1 deletion.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ module github.com/dhui/satomic

require (
github.com/DATA-DOG/go-sqlmock v1.3.0
github.com/denisenkom/go-mssqldb v0.0.0-20181014144952-4e0d7dc8888f
github.com/dhui/dhrand v0.1.0
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/dhui/dktest v0.1.0
github.com/go-sql-driver/mysql v1.4.1
github.com/jmoiron/sqlx v1.2.0
github.com/lib/pq v1.0.0
github.com/mattn/go-sqlite3 v1.10.0
google.golang.org/appengine v1.3.0 // indirect
)
70 changes: 70 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,27 +1,97 @@
cloud.google.com/go v0.26.0 h1:e0WKqKTd5BnrG8aKH3J3h+QvEIQtSUcf2n5UZ5ZgLtQ=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
github.com/DATA-DOG/go-sqlmock v1.3.0 h1:ljjRxlddjfChBJdFKJs5LuCwCWPLaC1UZLwAo3PBBMk=
github.com/DATA-DOG/go-sqlmock v1.3.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/Microsoft/go-winio v0.4.11 h1:zoIOcVf0xPN1tnMVbTtEdI+P8OofVk3NObnwOQ6nK2Q=
github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
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/denisenkom/go-mssqldb v0.0.0-20181014144952-4e0d7dc8888f h1:WH0w/R4Yoey+04HhFxqZ6VX6I0d7RMyw5aXQ9UTvQPs=
github.com/denisenkom/go-mssqldb v0.0.0-20181014144952-4e0d7dc8888f/go.mod h1:xN/JuLBIz4bjkxNmByTiV1IbhfnYb6oo99phBn4Eqhc=
github.com/dhui/dhrand v0.1.0 h1:C8ES1tf4bO37VmLmjdcOR03nYVQDJuF26mMyUITz4NY=
github.com/dhui/dhrand v0.1.0/go.mod h1:tK3W7Gn/9L6SqpDX5QmS0gX55BjYjUZbJsavMWxj5A0=
github.com/dhui/dktest v0.1.0 h1:XeZFhKJ8CnpI9yfNIOsz/jvOcqQ/M/Qy/X3KZ5Wa0GI=
github.com/dhui/dktest v0.1.0/go.mod h1:cyzIUfGsBEbZ6BT7tnXqAShHSXCZhSNmFl70sZ7c1yc=
github.com/docker/distribution v2.7.0+incompatible h1:neUDAlf3wX6Ml4HdqTrbcOHXtfRN0TFIwt6YFL7N9RU=
github.com/docker/distribution v2.7.0+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/docker v0.7.3-0.20190103212154-2b7e084dc98b h1:Y0C03XhDDcak1Ow6em58mBJmUJjxaMfB5sFttITXE0Q=
github.com/docker/docker v0.7.3-0.20190103212154-2b7e084dc98b/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
github.com/docker/go-units v0.3.3 h1:Xk8S3Xj5sLGlG5g67hJmYMmUgXv5N4PhkjJHHqrwnTk=
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/gogo/protobuf v1.2.0 h1:xU6/SpYbvkNYiptHJYEDRseDLvYE7wSqhYYNy0QSUzI=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2 h1:Pgr17XVTNXAk3q/r4CpKzC5xBM/qW1uVLV+IhRZpIIk=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/4=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/opencontainers/go-digest v1.0.0-rc1 h1:WzifXhOVOEOuFYOJAW6aQqW0TooG2iki3E3Ii+WN7gQ=
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/opencontainers/image-spec v1.0.1 h1:JMemWkRwHx4Zj+fVxWoMCFm/8sYGGrUVojFA6h/TRcI=
github.com/opencontainers/image-spec v1.0.1/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3 h1:eH6Eip3UpmR+yM/qI9Ijluzb1bNv/cAU/n+6l8tRSis=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190102155601-82a175fd1598 h1:S8GOgffXV1X3fpVG442QRfWOt0iFl79eHJ7OPt725bo=
golang.org/x/sys v0.0.0-20190102155601-82a175fd1598/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
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-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.3.0 h1:FBSsiFRMz3LBeXIomRnVzrQwSDj4ibvcRexLG0LZGQk=
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/grpc v1.17.0 h1:TRJYBgMclJvGYn2rIMjj+h9KtMt5r1Ij7ODVRIZkwhk=
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
85 changes: 85 additions & 0 deletions savepointers/mssql/mssql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package mssql_test

import (
"database/sql"
"fmt"
"testing"
)

import (
_ "github.com/denisenkom/go-mssqldb"
"github.com/dhui/dktest"
)

import (
"github.com/dhui/satomic/savepointers/mssql"
)

const (
password = "insecurePassword1"
)

var env = map[string]string{
// Developer edition (the default) is free
// License: https://go.microsoft.com/fwlink/?linkid=857698
"ACCEPT_EULA": "Y",
"SA_PASSWORD": password,
}

func readyFunc(c dktest.ContainerInfo) bool {
connStr := fmt.Sprintf("sqlserver://sa:%s@%s:%s", password, c.IP, c.Port)
db, err := sql.Open("sqlserver", connStr)
if err != nil {
return false
}
defer db.Close() // nolint:errcheck
return db.Ping() == nil
}

func TestSavepointerMSSQL(t *testing.T) {
t.Parallel()

// https://www.mysql.com/support/supportedplatforms/database.html
versions := []string{
"microsoft/mssql-server-linux",
}

for _, v := range versions {
v := v
t.Run(v, func(t *testing.T) {
t.Parallel()
dktest.Run(t, v, dktest.Options{Env: env, PortRequired: true, ReadyFunc: readyFunc},
func(t *testing.T, c dktest.ContainerInfo) {
connStr := fmt.Sprintf("sqlserver://sa:%s@%s:%s", password, c.IP, c.Port)
db, err := sql.Open("sqlserver", connStr)
if err != nil {
t.Fatal(err)
}
defer db.Close() // nolint:errcheck
if err := db.Ping(); err != nil {
t.Fatal(err)
}
tx, err := db.Begin()
if err != nil {
t.Fatal("Error starting transaction:", err)
}

savepointer := mssql.Savepointer{}
savepointName1 := `needs to be quoted1 +/'"]` + "`"
savepointName2 := `needs to be quoted2 +/'"]` + "`"
if _, err := tx.Exec(savepointer.Create(savepointName1)); err != nil {
t.Fatal("Error creating savepoint:", err)
}
if _, err := tx.Exec(savepointer.Rollback(savepointName1)); err != nil {
t.Fatal("Error rolling back savepoint:", err)
}
if _, err := tx.Exec(savepointer.Create(savepointName2)); err != nil {
t.Fatal("Error creating savepoint:", err)
}
if _, err := tx.Exec(savepointer.Release(savepointName2)); err != nil {
t.Fatal("Error releasing savepoint:", err)
}
})
})
}
}
87 changes: 87 additions & 0 deletions savepointers/mysql/mysql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package mysql_test

import (
"database/sql"
"fmt"
"testing"
)

import (
"github.com/dhui/dktest"
_ "github.com/go-sql-driver/mysql"
)

import (
"github.com/dhui/satomic/savepointers/mysql"
)

const (
password = "insecurepassword"
db = "public"
)

var env = map[string]string{
"MYSQL_ROOT_PASSWORD": password,
"MYSQL_DATABASE": db,
}

func readyFunc(c dktest.ContainerInfo) bool {
connStr := fmt.Sprintf("root:%s@tcp(%s:%s)/%s", password, c.IP, c.Port, db)
db, err := sql.Open("mysql", connStr)
if err != nil {
return false
}
defer db.Close() // nolint:errcheck
return db.Ping() == nil
}

func TestSavepointerMySQL(t *testing.T) {
t.Parallel()

// https://www.mysql.com/support/supportedplatforms/database.html
versions := []string{
"mysql:8",
"mysql:5.7",
"mysql:5.6",
"mysql:5.5",
}

for _, v := range versions {
v := v
t.Run(v, func(t *testing.T) {
t.Parallel()
dktest.Run(t, v, dktest.Options{Env: env, PortRequired: true, ReadyFunc: readyFunc},
func(t *testing.T, c dktest.ContainerInfo) {
connStr := fmt.Sprintf("root:%s@tcp(%s:%s)/%s", password, c.IP, c.Port, db)
db, err := sql.Open("mysql", connStr)
if err != nil {
t.Fatal(err)
}
defer db.Close() // nolint:errcheck
if err := db.Ping(); err != nil {
t.Fatal(err)
}
tx, err := db.Begin()
if err != nil {
t.Fatal("Error starting transaction:", err)
}

savepointer := mysql.Savepointer{}
savepointName1 := `needs to be quoted1 +/'"]` + "`"
savepointName2 := `needs to be quoted2 +/'"]` + "`"
if _, err := tx.Exec(savepointer.Create(savepointName1)); err != nil {
t.Fatal("Error creating savepoint:", err)
}
if _, err := tx.Exec(savepointer.Rollback(savepointName1)); err != nil {
t.Fatal("Error rolling back savepoint:", err)
}
if _, err := tx.Exec(savepointer.Create(savepointName2)); err != nil {
t.Fatal("Error creating savepoint:", err)
}
if _, err := tx.Exec(savepointer.Release(savepointName2)); err != nil {
t.Fatal("Error releasing savepoint:", err)
}
})
})
}
}
79 changes: 79 additions & 0 deletions savepointers/postgres/postgres_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package postgres_test

import (
"database/sql"
"fmt"
"testing"
)

import (
"github.com/dhui/dktest"
_ "github.com/lib/pq"
)

import (
"github.com/dhui/satomic/savepointers/postgres"
)

func readyFunc(c dktest.ContainerInfo) bool {
connStr := fmt.Sprintf("host=%s port=%s user=postgres dbname=postgres sslmode=disable", c.IP, c.Port)
db, err := sql.Open("postgres", connStr)
if err != nil {
return false
}
defer db.Close() // nolint:errcheck
return db.Ping() == nil
}

func TestSavepointerPostgres(t *testing.T) {
t.Parallel()

// https://www.postgresql.org/support/versioning/
versions := []string{
"postgres:11-alpine",
"postgres:10.6-alpine",
"postgres:9.6-alpine",
"postgres:9.5-alpine",
"postgres:9.4-alpine",
}

for _, v := range versions {
v := v
t.Run(v, func(t *testing.T) {
t.Parallel()
dktest.Run(t, v, dktest.Options{PortRequired: true, ReadyFunc: readyFunc},
func(t *testing.T, c dktest.ContainerInfo) {
connStr := fmt.Sprintf("host=%s port=%s user=postgres dbname=postgres sslmode=disable",
c.IP, c.Port)
db, err := sql.Open("postgres", connStr)
if err != nil {
t.Fatal(err)
}
defer db.Close() // nolint:errcheck
if err := db.Ping(); err != nil {
t.Fatal(err)
}
tx, err := db.Begin()
if err != nil {
t.Fatal("Error starting transaction:", err)
}

savepointer := postgres.Savepointer{}
savepointName1 := `needs to be quoted1 +/'"]` + "`"
savepointName2 := `needs to be quoted2 +/'"]` + "`"
if _, err := tx.Exec(savepointer.Create(savepointName1)); err != nil {
t.Fatal("Error creating savepoint:", err)
}
if _, err := tx.Exec(savepointer.Rollback(savepointName1)); err != nil {
t.Fatal("Error rolling back savepoint:", err)
}
if _, err := tx.Exec(savepointer.Create(savepointName2)); err != nil {
t.Fatal("Error creating savepoint:", err)
}
if _, err := tx.Exec(savepointer.Release(savepointName2)); err != nil {
t.Fatal("Error releasing savepoint:", err)
}
})
})
}
}

0 comments on commit cf78653

Please sign in to comment.