From 01057659cfae148039dec804bc08c9ef62a195dc Mon Sep 17 00:00:00 2001 From: gandaldf Date: Sun, 28 Nov 2021 15:23:37 +0100 Subject: [PATCH 1/2] Upgrade GORM version --- gormstore/README.md | 14 +-- gormstore/go.mod | 8 +- gormstore/go.sum | 197 +++++++++++++++++++++++++++++++++--- gormstore/gormstore.go | 114 ++++++++++++--------- gormstore/gormstore_test.go | 173 ++++++++++++++++++++----------- 5 files changed, 376 insertions(+), 130 deletions(-) diff --git a/gormstore/README.md b/gormstore/README.md index 83b4d20..5645561 100644 --- a/gormstore/README.md +++ b/gormstore/README.md @@ -32,18 +32,20 @@ import ( "log" "net/http" - "github.com/alexedwards/scs/v2" "github.com/alexedwards/scs/gormstore" - "github.com/jinzhu/gorm" - - _ "github.com/jinzhu/gorm/dialects/postgres" + "github.com/alexedwards/scs/v2" + "gorm.io/driver/postgres" + "gorm.io/gorm" ) var sessionManager *scs.SessionManager func main() { - // Establish connection to your store (PostgreSQL here). - db, err := gorm.Open("postgres", "postgres://user:pass@localhost/db") + // Establish connection to your store. + db, err := gorm.Open(postgres.Open("postgres://username:password@host/dbname", &gorm.Config{})) // PostgreSQL + //db, err := gorm.Open(sqlserver.Open("sqlserver://username:password@host?database=dbname", &gorm.Config{})) // MSSQL + //db, err := gorm.Open(mysql.Open(username:password@tcp(host)/dbname?parseTime=true", &gorm.Config{})) // MySQL + //db, err := gorm.Open(sqlite.Open("sqlite3_database.db"), &gorm.Config{})) // SQLite3 if err != nil { log.Fatal(err) } diff --git a/gormstore/go.mod b/gormstore/go.mod index b242d5c..27e1f88 100644 --- a/gormstore/go.mod +++ b/gormstore/go.mod @@ -2,4 +2,10 @@ module github.com/alexedwards/scs/gormstore go 1.12 -require github.com/jinzhu/gorm v1.9.12 +require ( + gorm.io/driver/mysql v1.2.0 + gorm.io/driver/postgres v1.2.2 + gorm.io/driver/sqlite v1.2.6 + gorm.io/driver/sqlserver v1.2.1 + gorm.io/gorm v1.22.3 +) diff --git a/gormstore/go.sum b/gormstore/go.sum index ef8ae68..a78e918 100644 --- a/gormstore/go.sum +++ b/gormstore/go.sum @@ -1,24 +1,191 @@ -github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= -github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= -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/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= +github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/denisenkom/go-mssqldb v0.11.0 h1:9rHa233rhdOyrz2GcP9NM+gi2psgJZ4GWDpL/7ND8HI= +github.com/denisenkom/go-mssqldb v0.11.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= +github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= +github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/jinzhu/gorm v1.9.12 h1:Drgk1clyWT9t9ERbzHza6Mj/8FY/CqMyVzOiHviMo6Q= -github.com/jinzhu/gorm v1.9.12/go.mod h1:vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZT/Qs= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0= +github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= +github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= +github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8= +github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= +github.com/jackc/pgconn v0.0.0-20190420214824-7e0022ef6ba3/go.mod h1:jkELnwuX+w9qN5YIfX0fl88Ehu4XC3keFuOJJk9pcnA= +github.com/jackc/pgconn v0.0.0-20190824142844-760dd75542eb/go.mod h1:lLjNuW/+OfW9/pnVKPazfWOgNfH2aPem8YQ7ilXGvJE= +github.com/jackc/pgconn v0.0.0-20190831204454-2fabfa3c18b7/go.mod h1:ZJKsE/KZfsUgOEh9hBm+xYTstcNHg7UPMVJqRfQxq4s= +github.com/jackc/pgconn v1.8.0/go.mod h1:1C2Pb36bGIP9QHGBYCjnyhqu7Rv3sGshaQUvmfGIB/o= +github.com/jackc/pgconn v1.9.0/go.mod h1:YctiPyvzfU11JFxoXokUOOKQXQmDMoJL9vJzHH8/2JY= +github.com/jackc/pgconn v1.9.1-0.20210724152538-d89c8390a530/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI= +github.com/jackc/pgconn v1.10.0 h1:4EYhlDVEMsJ30nNj0mmgwIUXoq7e9sMJrVC2ED6QlCU= +github.com/jackc/pgconn v1.10.0/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI= +github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= +github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8= +github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE= +github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd/go.mod h1:hrBW0Enj2AZTNpt/7Y5rr2xe/9Mn757Wtb2xeBzPv2c= +github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65/go.mod h1:5R2h2EEX+qri8jOWMbJCtaPWkrrNc7OHwsp2TCqp7ak= +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/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A= +github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78= +github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA= +github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg= +github.com/jackc/pgproto3/v2 v2.0.0-rc3/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= +github.com/jackc/pgproto3/v2 v2.0.0-rc3.0.20190831210041-4c03ce451f29/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= +github.com/jackc/pgproto3/v2 v2.0.6/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= +github.com/jackc/pgproto3/v2 v2.1.1 h1:7PQ/4gLoqnl87ZxL7xjO0DR5gYuviDCZxQJsUlFW1eI= +github.com/jackc/pgproto3/v2 v2.1.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= +github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg= +github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= +github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg= +github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc= +github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw= +github.com/jackc/pgtype v1.8.1-0.20210724151600-32e20a603178/go.mod h1:C516IlIV9NKqfsMCXTdChteoXmwgUceqaLfjg2e3NlM= +github.com/jackc/pgtype v1.8.1 h1:9k0IXtdJXHJbyAWQgbWr1lU+MEhPXZz6RIXxfR5oxXs= +github.com/jackc/pgtype v1.8.1/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4= +github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y= +github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM= +github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc= +github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs= +github.com/jackc/pgx/v4 v4.13.0 h1:JCjhT5vmhMAf/YwBHLvrBn4OGdIQBiFG6ym8Zmdx570= +github.com/jackc/pgx/v4 v4.13.0/go.mod h1:9P4X524sErlaxj0XSGZk7s+LD0eOyu1ZDUrrpznYDF0= +github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= +github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= 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.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= -github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4= -github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw= -github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= +github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI= +github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= +github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA= +github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= +github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= +github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= +github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= +github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= +github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= +go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= +go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= +go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= +go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= +golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20190823170909-c4a336ef6a2f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/mysql v1.2.0 h1:l8+9VwjjyzEkw0PNPBOr2JHhLOGVk7XEnl5hk42bcvs= +gorm.io/driver/mysql v1.2.0/go.mod h1:4RQmTg4okPghdt+kbe6e1bTXIQp7Ny1NnBn/3Z6ghjk= +gorm.io/driver/postgres v1.2.2 h1:Ka9W6feOU+rPM9m007eYLMD4QoZuYGBnQ3Jp0faGSwg= +gorm.io/driver/postgres v1.2.2/go.mod h1:Ik3tK+a3FMp8ORZl29v4b3M0RsgXsaeMXh9s9eVMXco= +gorm.io/driver/sqlite v1.2.6 h1:SStaH/b+280M7C8vXeZLz/zo9cLQmIGwwj3cSj7p6l4= +gorm.io/driver/sqlite v1.2.6/go.mod h1:gyoX0vHiiwi0g49tv+x2E7l8ksauLK0U/gShcdUsjWY= +gorm.io/driver/sqlserver v1.2.1 h1:KhGOjvPX7JZ5hPyQICTJfMuTz88zgJ2lk9bWiHVNHd8= +gorm.io/driver/sqlserver v1.2.1/go.mod h1:nixq0OB3iLXZDiPv6JSOjWuPgpyaRpOIIevYtA4Ulb4= +gorm.io/gorm v1.22.2/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= +gorm.io/gorm v1.22.3 h1:/JS6z+GStEQvJNW3t1FTwJwG/gZ+A7crFdRqtvG5ehA= +gorm.io/gorm v1.22.3/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= diff --git a/gormstore/gormstore.go b/gormstore/gormstore.go index bef8ed6..37712de 100644 --- a/gormstore/gormstore.go +++ b/gormstore/gormstore.go @@ -1,10 +1,11 @@ package gormstore import ( + "errors" "log" "time" - "github.com/jinzhu/gorm" + "gorm.io/gorm" ) // GORMStore represents the session store. @@ -14,7 +15,7 @@ type GORMStore struct { } type session struct { - Token string `gorm:"primary_key;type:varchar(100)"` + Token string `gorm:"primary_key;type:varchar(43)"` Data []byte Expiry time.Time `gorm:"index"` } @@ -30,81 +31,100 @@ func New(db *gorm.DB) (*GORMStore, error) { // background cleanup goroutine. Setting it to 0 prevents the cleanup goroutine // from running (i.e. expired sessions will not be removed). func NewWithCleanupInterval(db *gorm.DB, cleanupInterval time.Duration) (*GORMStore, error) { - p := &GORMStore{db: db} - if err := p.migrate(); err != nil { + g := &GORMStore{db: db} + if err := g.migrate(); err != nil { return nil, err } if cleanupInterval > 0 { - go p.startCleanup(cleanupInterval) + go g.startCleanup(cleanupInterval) } - return p, nil + return g, nil } -// Find returns the data for a given session token from the PostgresStore instance. +// Find returns the data for a given session token from the GORMStore instance. // If the session token is not found or is expired, the returned exists flag will // be set to false. -func (p *GORMStore) Find(token string) ([]byte, bool, error) { - row := &session{} - sess := p.db.First(row, "token = ? AND expiry >= ?", token, time.Now()) - if sess.RecordNotFound() { +func (g *GORMStore) Find(token string) (b []byte, exists bool, err error) { + s := &session{} + row := g.db.First(s, "token = ? AND expiry >= ?", token, time.Now()) + if errors.Is(row.Error, gorm.ErrRecordNotFound) { return nil, false, nil - } else if errs := sess.GetErrors(); len(errs) > 0 { - return nil, false, errs[0] + } else if row.Error != nil { + return nil, false, row.Error } - if row == nil { - - } - return row.Data, true, nil + return s.Data, true, nil } -// Commit adds a session token and data to the PostgresStore instance with the +// Commit adds a session token and data to the GORMStore instance with the // given expiry time. If the session token already exists, then the data and expiry // time are updated. -func (p *GORMStore) Commit(token string, b []byte, expiry time.Time) error { - row := &session{} - sess := p.db.Where(session{Token: token}).Assign(session{Data: b, Expiry: expiry}).FirstOrCreate(&row) - if errs := sess.GetErrors(); len(errs) > 0 { - return errs[0] +func (g *GORMStore) Commit(token string, b []byte, expiry time.Time) error { + s := &session{} + row := g.db.Where(session{Token: token}).Assign(session{Data: b, Expiry: expiry}).FirstOrCreate(s) + if row.Error != nil { + return row.Error } return nil } -// Delete removes a session token and corresponding data from the PostgresStore +// Delete removes a session token and corresponding data from the GORMStore // instance. -func (p *GORMStore) Delete(token string) error { - sess := p.db.Delete(&session{}, "token = ?", token) - if errs := sess.GetErrors(); len(errs) > 0 { - return errs[0] +func (g *GORMStore) Delete(token string) error { + row := g.db.Delete(&session{}, "token = ?", token) + if row.Error != nil { + return row.Error } return nil } -func (p *GORMStore) migrate() error { +// All returns a map containing the token and data for all active (i.e. +// not expired) sessions in the GORMStore instance. +func (g *GORMStore) All() (map[string][]byte, error) { + rows, err := g.db.Find(&[]session{}, "expiry >= ?", time.Now()).Rows() + if err != nil { + return nil, err + } + defer rows.Close() + ss := make(map[string][]byte) + for rows.Next() { + s := &session{} + err := g.db.ScanRows(rows, s) + if err != nil { + return nil, err + } + ss[s.Token] = s.Data + } + err = rows.Err() + if err != nil { + return nil, err + } + return ss, nil +} + +func (g *GORMStore) migrate() error { var tableOptions string - // Set table options for MySQL database dialect - if p.db.Dialect().GetName() == "mysql" { + // Set table options for MySQL database dialect. + if g.db.Dialector.Name() == "mysql" { tableOptions = "ENGINE=InnoDB CHARSET=utf8mb4" } - - sess := p.db.Set("gorm:table_options", tableOptions). - AutoMigrate(&session{}) - if errs := sess.GetErrors(); len(errs) > 0 { - return errs[0] + err := g.db.Set("gorm:table_options", tableOptions).AutoMigrate(&session{}) + if err != nil { + return err } return nil } -func (p *GORMStore) startCleanup(interval time.Duration) { - p.stopCleanup = make(chan bool) +func (g *GORMStore) startCleanup(interval time.Duration) { + g.stopCleanup = make(chan bool) ticker := time.NewTicker(interval) for { select { case <-ticker.C: - err := p.deleteExpired() + err := g.deleteExpired() if err != nil { log.Println(err) } - case <-p.stopCleanup: + case <-g.stopCleanup: ticker.Stop() return } @@ -121,16 +141,16 @@ func (p *GORMStore) startCleanup(interval time.Duration) { // scenario, the cleanup goroutine (which will run forever) will prevent the // GORMStore object from being garbage collected even after the test function // has finished. You can prevent this by manually calling StopCleanup. -func (p *GORMStore) StopCleanup() { - if p.stopCleanup != nil { - p.stopCleanup <- true +func (g *GORMStore) StopCleanup() { + if g.stopCleanup != nil { + g.stopCleanup <- true } } -func (p *GORMStore) deleteExpired() error { - sess := p.db.Delete(&session{}, "expiry < ?", time.Now()) - if errs := sess.GetErrors(); len(errs) > 0 { - return errs[0] +func (g *GORMStore) deleteExpired() error { + row := g.db.Delete(&session{}, "expiry < ?", time.Now()) + if row.Error != nil { + return row.Error } return nil } diff --git a/gormstore/gormstore_test.go b/gormstore/gormstore_test.go index c175251..d608df1 100644 --- a/gormstore/gormstore_test.go +++ b/gormstore/gormstore_test.go @@ -7,59 +7,82 @@ import ( "testing" "time" - "github.com/jinzhu/gorm" - - _ "github.com/jinzhu/gorm/dialects/mysql" - _ "github.com/jinzhu/gorm/dialects/postgres" - _ "github.com/jinzhu/gorm/dialects/sqlite" + "gorm.io/driver/mysql" + "gorm.io/driver/postgres" + "gorm.io/driver/sqlite" + "gorm.io/driver/sqlserver" + "gorm.io/gorm" ) func initWithCleanupInterval(t *testing.T, cleanupInterval time.Duration) (*GORMStore, *gorm.DB) { + var db *gorm.DB + var err error + dialect := os.Getenv("SCS_GORM_TEST_DIALECT") - var dsn string switch dialect { - case "postgres": - dsn = os.Getenv("SCS_POSTGRES_TEST_DSN") - case "mysql": - dsn = os.Getenv("SCS_MYSQL_TEST_DSN") default: dialect = "sqlite3" fallthrough + case "postgres": + dsn := os.Getenv("SCS_POSTGRES_TEST_DSN") + db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{}) + case "mssql": + dsn := os.Getenv("SCS_MSSQL_TEST_DSN") + db, err = gorm.Open(sqlserver.Open(dsn), &gorm.Config{}) + case "mysql": + dsn := os.Getenv("SCS_MYSQL_TEST_DSN") + db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}) case "sqlite3": - dsn = os.Getenv("./testSQL3lite.db") + dsn := os.Getenv("./testSQL3lite.db") + db, err = gorm.Open(sqlite.Open(dsn), &gorm.Config{}) } - - db, err := gorm.Open(dialect, dsn) if err != nil { t.Fatal(err) } - if err = db.DB().Ping(); err != nil { - db.Close() + + sqlDB, err := db.DB() + if err != nil { + sqlDB.Close() t.Fatal(err) } - if _, err := db.DB().Exec("DROP TABLE IF EXISTS sessions"); err != nil { + if sqlDB.Ping(); err != nil { + sqlDB.Close() t.Fatal(err) } - p, err := NewWithCleanupInterval(db, cleanupInterval) + if dialect == "mssql" { + if err := db.Exec("IF OBJECT_ID('sessions', 'U') IS NOT NULL DROP TABLE sessions").Error; err != nil { + t.Fatal(err) + } + } else { + if err := db.Exec("DROP TABLE IF EXISTS sessions").Error; err != nil { + t.Fatal(err) + } + } + + g, err := NewWithCleanupInterval(db, cleanupInterval) if err != nil { - db.Close() + sqlDB.Close() t.Fatal(err) } - return p, db + return g, db } func TestFind(t *testing.T) { - p, db := initWithCleanupInterval(t, 0) - defer db.Close() + g, db := initWithCleanupInterval(t, 0) + sqlDB, err := db.DB() + if err != nil { + t.Fatal(err) + } + defer sqlDB.Close() - sess := db.Create(&session{Token: "session_token", Data: []byte("encoded_data"), Expiry: time.Now().Add(1 * time.Minute)}) - if errs := sess.GetErrors(); len(errs) != 0 { - t.Fatal(errs[0]) + row := db.Create(&session{Token: "session_token", Data: []byte("encoded_data"), Expiry: time.Now().Add(1 * time.Minute)}) + if row.Error != nil { + t.Fatal(err) } - b, found, err := p.Find("session_token") + b, found, err := g.Find("session_token") if err != nil { t.Fatal(err) } @@ -72,10 +95,14 @@ func TestFind(t *testing.T) { } func TestFindMissing(t *testing.T) { - p, db := initWithCleanupInterval(t, 0) - defer db.Close() + g, db := initWithCleanupInterval(t, 0) + sqlDB, err := db.DB() + if err != nil { + t.Fatal(err) + } + defer sqlDB.Close() - _, found, err := p.Find("missing_session_token") + _, found, err := g.Find("missing_session_token") if err != nil { t.Fatalf("got %v: expected %v", err, nil) } @@ -85,15 +112,19 @@ func TestFindMissing(t *testing.T) { } func TestSaveNew(t *testing.T) { - p, db := initWithCleanupInterval(t, 0) - defer db.Close() + g, db := initWithCleanupInterval(t, 0) + sqlDB, err := db.DB() + if err != nil { + t.Fatal(err) + } + defer sqlDB.Close() - err := p.Commit("session_token", []byte("encoded_data"), time.Now().Add(time.Minute)) + err = g.Commit("session_token", []byte("encoded_data"), time.Now().Add(time.Minute)) if err != nil { t.Fatal(err) } - row := db.DB().QueryRow("SELECT data FROM sessions WHERE token = 'session_token'") + row := sqlDB.QueryRow("SELECT data FROM sessions WHERE token = 'session_token'") var data []byte err = row.Scan(&data) if err != nil { @@ -105,22 +136,26 @@ func TestSaveNew(t *testing.T) { } func TestSaveUpdated(t *testing.T) { - p, db := initWithCleanupInterval(t, 0) - defer db.Close() + g, db := initWithCleanupInterval(t, 0) + sqlDB, err := db.DB() + if err != nil { + t.Fatal(err) + } + defer sqlDB.Close() - sess := db.Create(&session{Token: "session_token", Data: []byte("encoded_data"), Expiry: time.Now().Add(1 * time.Minute)}) - if errs := sess.GetErrors(); len(errs) != 0 { - t.Fatal(errs[0]) + row1 := db.Create(&session{Token: "session_token", Data: []byte("encoded_data"), Expiry: time.Now().Add(1 * time.Minute)}) + if row1.Error != nil { + t.Fatal(row1.Error) } - err := p.Commit("session_token", []byte("new_encoded_data"), time.Now().Add(time.Minute)) + err = g.Commit("session_token", []byte("new_encoded_data"), time.Now().Add(time.Minute)) if err != nil { t.Fatal(err) } - row := db.DB().QueryRow("SELECT data FROM sessions WHERE token = 'session_token'") + row2 := sqlDB.QueryRow("SELECT data FROM sessions WHERE token = 'session_token'") var data []byte - err = row.Scan(&data) + err = row2.Scan(&data) if err != nil { t.Fatal(err) } @@ -130,41 +165,49 @@ func TestSaveUpdated(t *testing.T) { } func TestExpiry(t *testing.T) { - p, db := initWithCleanupInterval(t, 0) - defer db.Close() + g, db := initWithCleanupInterval(t, 0) + sqlDB, err := db.DB() + if err != nil { + t.Fatal(err) + } + defer sqlDB.Close() - err := p.Commit("session_token", []byte("encoded_data"), time.Now().Add(100*time.Millisecond)) + err = g.Commit("session_token", []byte("encoded_data"), time.Now().Add(1*time.Second)) if err != nil { t.Fatal(err) } - _, found, _ := p.Find("session_token") + _, found, _ := g.Find("session_token") if found != true { t.Fatalf("got %v: expected %v", found, true) } - time.Sleep(100 * time.Millisecond) - _, found, _ = p.Find("session_token") + time.Sleep(2 * time.Second) + _, found, _ = g.Find("session_token") if found != false { t.Fatalf("got %v: expected %v", found, false) } } func TestDelete(t *testing.T) { - p, db := initWithCleanupInterval(t, 0) - defer db.Close() + g, db := initWithCleanupInterval(t, 0) + sqlDB, err := db.DB() + if err != nil { + t.Fatal(err) + } + defer sqlDB.Close() - err := p.Commit("session_token", []byte("encoded_data"), time.Now().Add(1*time.Minute)) + err = g.Commit("session_token", []byte("encoded_data"), time.Now().Add(1*time.Minute)) if err != nil { t.Fatal(err) } - err = p.Delete("session_token") + err = g.Delete("session_token") if err != nil { t.Fatal(err) } - row := db.DB().QueryRow("SELECT COUNT(*) FROM sessions WHERE token = 'session_token'") + row := sqlDB.QueryRow("SELECT COUNT(*) FROM sessions WHERE token = 'session_token'") var count int err = row.Scan(&count) if err != nil { @@ -176,16 +219,20 @@ func TestDelete(t *testing.T) { } func TestCleanup(t *testing.T) { - p, db := initWithCleanupInterval(t, 200*time.Millisecond) - defer p.StopCleanup() - defer db.Close() + g, db := initWithCleanupInterval(t, 2*time.Second) + defer g.StopCleanup() + sqlDB, err := db.DB() + if err != nil { + t.Fatal(err) + } + defer sqlDB.Close() - err := p.Commit("session_token", []byte("encoded_data"), time.Now().Add(100*time.Millisecond)) + err = g.Commit("session_token", []byte("encoded_data"), time.Now().Add(1*time.Second)) if err != nil { t.Fatal(err) } - row := db.DB().QueryRow("SELECT COUNT(*) FROM sessions WHERE token = 'session_token'") + row := sqlDB.QueryRow("SELECT COUNT(*) FROM sessions WHERE token = 'session_token'") var count int err = row.Scan(&count) if err != nil { @@ -195,8 +242,8 @@ func TestCleanup(t *testing.T) { t.Fatalf("got %d: expected %d", count, 1) } - time.Sleep(300 * time.Millisecond) - row = db.DB().QueryRow("SELECT COUNT(*) FROM sessions WHERE token = 'session_token'") + time.Sleep(3 * time.Second) + row = sqlDB.QueryRow("SELECT COUNT(*) FROM sessions WHERE token = 'session_token'") err = row.Scan(&count) if err != nil { t.Fatal(err) @@ -207,10 +254,14 @@ func TestCleanup(t *testing.T) { } func TestStopNilCleanup(t *testing.T) { - p, db := initWithCleanupInterval(t, 0) - defer db.Close() + g, db := initWithCleanupInterval(t, 0) + sqlDB, err := db.DB() + if err != nil { + t.Fatal(err) + } + defer sqlDB.Close() time.Sleep(100 * time.Millisecond) // A send to a nil channel will block forever - p.StopCleanup() + g.StopCleanup() } From 3fa2cbf6e06551fe45c6c52cc372830a828bcd31 Mon Sep 17 00:00:00 2001 From: gandaldf Date: Sun, 28 Nov 2021 15:32:39 +0100 Subject: [PATCH 2/2] Update dsn in documentation --- mysqlstore/README.md | 2 +- pgxstore/README.md | 2 +- postgresstore/README.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mysqlstore/README.md b/mysqlstore/README.md index 97d7660..b642325 100644 --- a/mysqlstore/README.md +++ b/mysqlstore/README.md @@ -39,7 +39,7 @@ var sessionManager *scs.SessionManager func main() { // Establish connection to MySQL. - db, err := sql.Open("mysql", "user:pass@/db?parseTime=true") + db, err := sql.Open("mysql", "username:password@tcp(host)/dbname?parseTime=true") if err != nil { log.Fatal(err) } diff --git a/pgxstore/README.md b/pgxstore/README.md index 10b59a3..f6634d0 100644 --- a/pgxstore/README.md +++ b/pgxstore/README.md @@ -38,7 +38,7 @@ var sessionManager *scs.SessionManager func main() { // Establish connection pool to PostgreSQL. - pool, err := pgxpool.Connect(context.Background(), "postgres://user:pass@localhost/db") + pool, err := pgxpool.Connect(context.Background(), "postgres://username:password@host/dbname") if err != nil { log.Fatal(err) } diff --git a/postgresstore/README.md b/postgresstore/README.md index a6c4735..f834207 100644 --- a/postgresstore/README.md +++ b/postgresstore/README.md @@ -39,7 +39,7 @@ var sessionManager *scs.SessionManager func main() { // Establish connection to PostgreSQL. - db, err := sql.Open("postgres", "postgres://user:pass@localhost/db") + db, err := sql.Open("postgres", "postgres://username:password@host/dbname") if err != nil { log.Fatal(err) }