diff --git a/go.mod b/go.mod index bf3ae706c8..2dbc85b4b4 100644 --- a/go.mod +++ b/go.mod @@ -12,13 +12,11 @@ require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/gin-contrib/gzip v0.0.1 github.com/gin-gonic/gin v1.5.0 - github.com/go-sql-driver/mysql v1.5.0 + github.com/go-sql-driver/mysql v1.6.0 github.com/goccy/go-graphviz v0.0.5 github.com/google/pprof v0.0.0-20200407044318-7d83b28da2e9 github.com/google/uuid v1.0.0 github.com/gtank/cryptopasta v0.0.0-20170601214702-1f550f6f2f69 - github.com/hypnoglow/gormzap v0.3.0 - github.com/jinzhu/gorm v1.9.12 github.com/joho/godotenv v1.3.0 github.com/joomcode/errorx v1.0.1 github.com/oleiade/reflections v1.0.1 @@ -40,7 +38,11 @@ require ( go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738 go.uber.org/atomic v1.6.0 go.uber.org/fx v1.10.0 - go.uber.org/zap v1.15.0 + go.uber.org/zap v1.16.0 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c google.golang.org/grpc v1.25.1 + gorm.io/driver/mysql v1.0.6 + gorm.io/driver/sqlite v1.1.4 + gorm.io/gorm v1.21.9 + moul.io/zapgorm2 v1.1.0 ) diff --git a/go.sum b/go.sum index 0775541b70..9c262e1771 100644 --- a/go.sum +++ b/go.sum @@ -53,16 +53,12 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do 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/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM= -github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4 h1:qk/FSDDxo05wdJH28W+p5yivv7LuLYLRXPPD8KQCtZs= github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y= -github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8= github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= @@ -105,9 +101,8 @@ github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3yg github.com/go-playground/overalls v0.0.0-20180201144345-22ec1a223b7c/go.mod h1:UqxAgEOt89sCiXlrc/ycnx00LVvUO/eS8tMUkWX4R7w= github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM= github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY= -github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= -github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= -github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +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/goccy/go-graphviz v0.0.5 h1:qcjgvNiYbLyfLAq9LvyYBJ7sNMbQh9w4FoAzBDrYhYw= github.com/goccy/go-graphviz v0.0.5/go.mod h1:wXVsXxmyMQU6TN3zGRttjNn3h+iCAS7xQFC6TlNvLhk= @@ -116,8 +111,6 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= -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/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= @@ -153,18 +146,15 @@ github.com/grpc-ecosystem/grpc-gateway v1.12.1 h1:zCy2xE9ablevUOrUZc3Dl72Dt+ya2F github.com/grpc-ecosystem/grpc-gateway v1.12.1/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c= github.com/gtank/cryptopasta v0.0.0-20170601214702-1f550f6f2f69 h1:7xsUJsB2NrdcttQPa7JLEaGzvdbk7KvfrjgHZXOQRo0= github.com/gtank/cryptopasta v0.0.0-20170601214702-1f550f6f2f69/go.mod h1:YLEMZOtU+AZ7dhN9T/IpGhXVGly2bvkJQ+zxj3WeVQo= -github.com/hypnoglow/gormzap v0.3.0 h1:zqVHEcLN2snfwLFf2Dtam0mV+XN9s+NvcZ7XcnKHyZU= -github.com/hypnoglow/gormzap v0.3.0/go.mod h1:5Wom8B7Jl2oK0Im9hs6KQ+Kl92w4Y7gKCrj66rhyvw0= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 h1:UDMh68UUwekSh5iP2OMhRRZJiiBccgV7axzUG8vi56c= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= -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/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 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M= -github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI= +github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo= @@ -190,8 +180,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8= github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw= -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/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq0y3QnmWAArdw9PqbmotexnWx/FU8= @@ -203,8 +191,8 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg= github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= -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/mattn/go-sqlite3 v1.14.5 h1:1IdxlwTNazvbKJQSxoJ5/9ECbEeaTTyeU7sEAZ5KKTQ= +github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -358,19 +346,18 @@ go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEaUSmT1ysygQC7qYo7sG4= go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= -go.uber.org/zap v1.8.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= 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.12.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= go.uber.org/zap v1.15.0 h1:ZZCA22JRF2gQE5FoNmhmrf7jeJJ2uhqDUNRYKm8dvmM= go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc= +go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM= +go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 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-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= @@ -502,9 +489,18 @@ gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo= gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/mysql v1.0.6 h1:mA0XRPjIKi4bkE9nv+NKs6qj6QWOchqUSdWOcpd3x1E= +gorm.io/driver/mysql v1.0.6/go.mod h1:KdrTanmfLPPyAOeYGyG+UpDys7/7eeWT1zCq+oekYnU= +gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM= +gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw= +gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw= +gorm.io/gorm v1.21.9 h1:INieZtn4P2Pw6xPJ8MzT0G4WUOsHq3RhfuDF1M6GW0E= +gorm.io/gorm v1.21.9/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= +moul.io/zapgorm2 v1.1.0 h1:qwAlMBYf+qJkJ7PAzJl4oCe6eS6QGiKAXUPeis0+RBE= +moul.io/zapgorm2 v1.1.0/go.mod h1:emRfKjNqSzVj5lcgasBdovIXY1jSOwFz2GQZn1Rddks= sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs= sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= diff --git a/pkg/apiserver/clusterinfo/host.go b/pkg/apiserver/clusterinfo/host.go index 995d9ef922..61c4484a22 100644 --- a/pkg/apiserver/clusterinfo/host.go +++ b/pkg/apiserver/clusterinfo/host.go @@ -16,10 +16,10 @@ package clusterinfo import ( "sort" - "github.com/jinzhu/gorm" "github.com/pingcap/log" "github.com/thoas/go-funk" "go.uber.org/zap" + "gorm.io/gorm" "github.com/pingcap/tidb-dashboard/pkg/apiserver/clusterinfo/hostinfo" "github.com/pingcap/tidb-dashboard/pkg/utils/topology" diff --git a/pkg/apiserver/clusterinfo/hostinfo/cluster_config.go b/pkg/apiserver/clusterinfo/hostinfo/cluster_config.go index 037c39f11a..2bf4dd7f4f 100644 --- a/pkg/apiserver/clusterinfo/hostinfo/cluster_config.go +++ b/pkg/apiserver/clusterinfo/hostinfo/cluster_config.go @@ -16,7 +16,7 @@ package hostinfo import ( "strings" - "github.com/jinzhu/gorm" + "gorm.io/gorm" "github.com/pingcap/tidb-dashboard/pkg/utils/host" ) diff --git a/pkg/apiserver/clusterinfo/hostinfo/cluster_hardware.go b/pkg/apiserver/clusterinfo/hostinfo/cluster_hardware.go index 5b981ba8c4..da34f15ad4 100644 --- a/pkg/apiserver/clusterinfo/hostinfo/cluster_hardware.go +++ b/pkg/apiserver/clusterinfo/hostinfo/cluster_hardware.go @@ -18,7 +18,7 @@ import ( "encoding/json" "strings" - "github.com/jinzhu/gorm" + "gorm.io/gorm" "github.com/pingcap/tidb-dashboard/pkg/utils/host" ) diff --git a/pkg/apiserver/clusterinfo/hostinfo/cluster_load.go b/pkg/apiserver/clusterinfo/hostinfo/cluster_load.go index 5d7f1762b2..2e8bec7b10 100644 --- a/pkg/apiserver/clusterinfo/hostinfo/cluster_load.go +++ b/pkg/apiserver/clusterinfo/hostinfo/cluster_load.go @@ -17,7 +17,7 @@ import ( "bytes" "encoding/json" - "github.com/jinzhu/gorm" + "gorm.io/gorm" "github.com/pingcap/tidb-dashboard/pkg/utils/host" ) diff --git a/pkg/apiserver/clusterinfo/statistics.go b/pkg/apiserver/clusterinfo/statistics.go index f04d6575f9..958c032a2b 100644 --- a/pkg/apiserver/clusterinfo/statistics.go +++ b/pkg/apiserver/clusterinfo/statistics.go @@ -17,8 +17,8 @@ import ( "fmt" "sort" - "github.com/jinzhu/gorm" "github.com/thoas/go-funk" + "gorm.io/gorm" "github.com/pingcap/tidb-dashboard/pkg/apiserver/clusterinfo/hostinfo" "github.com/pingcap/tidb-dashboard/pkg/utils/topology" diff --git a/pkg/apiserver/configuration/service.go b/pkg/apiserver/configuration/service.go index 30ae481b3c..d04d116ea3 100644 --- a/pkg/apiserver/configuration/service.go +++ b/pkg/apiserver/configuration/service.go @@ -20,10 +20,10 @@ import ( "fmt" "sort" - "github.com/jinzhu/gorm" "github.com/joomcode/errorx" "go.etcd.io/etcd/clientv3" "go.uber.org/fx" + "gorm.io/gorm" "github.com/pingcap/tidb-dashboard/pkg/apiserver/utils" "github.com/pingcap/tidb-dashboard/pkg/config" diff --git a/pkg/apiserver/diagnose/compare.go b/pkg/apiserver/diagnose/compare.go index 8435afb90e..7e691f910c 100644 --- a/pkg/apiserver/diagnose/compare.go +++ b/pkg/apiserver/diagnose/compare.go @@ -10,8 +10,8 @@ import ( "sync" "sync/atomic" - "github.com/jinzhu/gorm" "github.com/pingcap/errors" + "gorm.io/gorm" "github.com/pingcap/tidb-dashboard/pkg/dbstore" ) @@ -50,7 +50,7 @@ func GetCompareReportTablesForDisplay(startTime1, endTime1, startTime2, endTime2 go func() { // Get compare tables - tables3, errRows3 = getCompareTables(startTime2, endTime2, db.New(), sqliteDB, reportID, &progress, &totalTableCount) + tables3, errRows3 = getCompareTables(startTime2, endTime2, db.Session(&gorm.Session{NewDB: true}), sqliteDB, reportID, &progress, &totalTableCount) errRows = append(errRows, errRows3...) wg.Done() }() diff --git a/pkg/apiserver/diagnose/diagnose.go b/pkg/apiserver/diagnose/diagnose.go index 481a4d8a75..666e0fc066 100644 --- a/pkg/apiserver/diagnose/diagnose.go +++ b/pkg/apiserver/diagnose/diagnose.go @@ -134,7 +134,7 @@ func (s *Service) genReportHandler(c *gin.Context) { db := utils.TakeTiDBConnection(c) go func() { - defer db.Close() + defer utils.CloseTiDBConnection(db) //nolint:errcheck var tables []*TableDef if compareStartTime == nil || compareEndTime == nil { @@ -241,7 +241,7 @@ func (s *Service) genDiagnosisHandler(c *gin.Context) { } db := utils.TakeTiDBConnection(c) - defer db.Close() + defer utils.CloseTiDBConnection(db) //nolint:errcheck table, err := GetDiagnoseReport(startTime.Format(timeLayout), endTime.Format(timeLayout), db, rules) if err != nil { tableErr := TableRowDef{Values: []string{CategoryDiagnose, "diagnose", err.Error()}} diff --git a/pkg/apiserver/diagnose/inspection.go b/pkg/apiserver/diagnose/inspection.go index 36ef706314..8e3a498f73 100644 --- a/pkg/apiserver/diagnose/inspection.go +++ b/pkg/apiserver/diagnose/inspection.go @@ -6,7 +6,7 @@ import ( "strconv" "strings" - "github.com/jinzhu/gorm" + "gorm.io/gorm" ) type clusterInspection struct { @@ -422,7 +422,7 @@ func (s *queryTotal) appendRow(row []string) error { } func (c *clusterInspection) queryBigQueryInSlowLog() (string, error) { - sql := fmt.Sprintf(`select count(*) from + sql := fmt.Sprintf(`select count(*) from (select sum(Process_time) as sum_process_time, digest from information_schema.CLUSTER_SLOW_QUERY @@ -430,7 +430,7 @@ func (c *clusterInspection) queryBigQueryInSlowLog() (string, error) { AND time < '%s' AND Is_internal = false group by digest) AS t1 - where t1.digest NOT IN + where t1.digest NOT IN (select digest from information_schema.CLUSTER_SLOW_QUERY where time >= '%s' @@ -450,7 +450,7 @@ func (c *clusterInspection) queryBigQueryInSlowLog() (string, error) { if count == 0 { return "", nil } - return fmt.Sprintf(`select * from + return fmt.Sprintf(`select * from (select count(*), min(time), sum(query_time) AS sum_query_time, @@ -468,7 +468,7 @@ func (c *clusterInspection) queryBigQueryInSlowLog() (string, error) { and time < '%s' and Is_internal = false group by digest) AS t1 - where t1.digest NOT IN + where t1.digest NOT IN (select digest from information_schema.CLUSTER_SLOW_QUERY where time >= '%s' diff --git a/pkg/apiserver/diagnose/model.go b/pkg/apiserver/diagnose/model.go index ea31cd3bc7..156c1312ce 100644 --- a/pkg/apiserver/diagnose/model.go +++ b/pkg/apiserver/diagnose/model.go @@ -37,8 +37,7 @@ func (Report) TableName() string { } func autoMigrate(db *dbstore.DB) error { - return db.AutoMigrate(&Report{}). - Error + return db.AutoMigrate(&Report{}) } func NewReport(db *dbstore.DB, startTime, endTime time.Time, compareStartTime, compareEndTime *time.Time) (string, error) { diff --git a/pkg/apiserver/diagnose/query.go b/pkg/apiserver/diagnose/query.go index 4ea750613e..4632f5132c 100644 --- a/pkg/apiserver/diagnose/query.go +++ b/pkg/apiserver/diagnose/query.go @@ -20,7 +20,7 @@ import ( "strconv" "strings" - "github.com/jinzhu/gorm" + "gorm.io/gorm" ) type rowQuery interface { diff --git a/pkg/apiserver/diagnose/report.go b/pkg/apiserver/diagnose/report.go index 40179f1261..5e987055f8 100644 --- a/pkg/apiserver/diagnose/report.go +++ b/pkg/apiserver/diagnose/report.go @@ -23,7 +23,7 @@ import ( "sync" "sync/atomic" - "github.com/jinzhu/gorm" + "gorm.io/gorm" "github.com/pingcap/tidb-dashboard/pkg/dbstore" ) @@ -753,7 +753,7 @@ func GetPDConfigInfo(startTime, endTime string, db *gorm.DB) (TableDef, error) { func GetPDConfigChangeInfo(startTime, endTime string, db *gorm.DB) (TableDef, error) { sql := fmt.Sprintf(`select t1.* from (select min(time) as time,type,value from metrics_schema.pd_scheduler_config where time>='%[1]s' and time<'%[2]s' group by type,value order by type) as t1 join - (select type, count(distinct value) as count from metrics_schema.pd_scheduler_config where time>='%[1]s' and time<'%[2]s' group by type order by count desc) as t2 + (select type, count(distinct value) as count from metrics_schema.pd_scheduler_config where time>='%[1]s' and time<'%[2]s' group by type order by count desc) as t2 where t1.type=t2.type and t2.count > 1 order by t2.count desc, t1.time;`, startTime, endTime) table := TableDef{ @@ -796,7 +796,7 @@ func GetTiDBGCConfigInfo(startTime, endTime string, db *gorm.DB) (TableDef, erro func GetTiDBGCConfigChangeInfo(startTime, endTime string, db *gorm.DB) (TableDef, error) { sql := fmt.Sprintf(`select t1.* from (select min(time) as time,type,value from metrics_schema.tidb_gc_config where time>='%[1]s' and time<'%[2]s' and value > 0 group by type,value order by type) as t1 join - (select type, count(distinct value) as count from metrics_schema.tidb_gc_config where time>='%[1]s' and time<'%[2]s' and value > 0 group by type order by count desc) as t2 + (select type, count(distinct value) as count from metrics_schema.tidb_gc_config where time>='%[1]s' and time<'%[2]s' and value > 0 group by type order by count desc) as t2 where t1.type=t2.type and t2.count>1 order by t2.count desc, t1.time;`, startTime, endTime) table := TableDef{ @@ -877,7 +877,7 @@ func GetTiKVRocksDBConfigInfo(startTime, endTime string, db *gorm.DB) (TableDef, func GetTiKVRocksDBConfigChangeInfo(startTime, endTime string, db *gorm.DB) (TableDef, error) { sql := fmt.Sprintf(`select t1.* from (select min(time) as time,concat(name,' , ',cf) as name,instance,value from metrics_schema.tikv_config_rocksdb where time>='%[1]s' and time<'%[2]s' group by name,cf,instance,value order by name) as t1 join - (select concat(name,' , ',cf) as name,instance, count(distinct value) as count from metrics_schema.tikv_config_rocksdb where time>='%[1]s' and time<'%[2]s' group by name,cf,instance order by count desc) as t2 + (select concat(name,' , ',cf) as name,instance, count(distinct value) as count from metrics_schema.tikv_config_rocksdb where time>='%[1]s' and time<'%[2]s' group by name,cf,instance order by count desc) as t2 where t1.name=t2.name and t1.instance = t2.instance and t2.count>1 order by t1.name,instance, t2.count desc, t1.time;`, startTime, endTime) table := TableDef{ @@ -959,7 +959,7 @@ func GetTiKVRaftStoreConfigInfo(startTime, endTime string, db *gorm.DB) (TableDe func GetTiKVRaftStoreConfigChangeInfo(startTime, endTime string, db *gorm.DB) (TableDef, error) { sql := fmt.Sprintf(`select t1.* from (select min(time) as time,name,instance,value from metrics_schema.tikv_config_raftstore where time>='%[1]s' and time<'%[2]s' group by name,instance,value order by name) as t1 join - (select name,instance, count(distinct value) as count from metrics_schema.tikv_config_raftstore where time>='%[1]s' and time<'%[2]s' group by name,instance order by count desc) as t2 + (select name,instance, count(distinct value) as count from metrics_schema.tikv_config_raftstore where time>='%[1]s' and time<'%[2]s' group by name,instance order by count desc) as t2 where t1.name=t2.name and t1.instance = t2.instance and t2.count>1 order by t1.name,instance,t2.count desc, t1.time;`, startTime, endTime) table := TableDef{ @@ -1718,7 +1718,7 @@ func getAvgMaxMinCPUUsage(startTime, endTime string, db *gorm.DB) (*TableRowDef, func getAvgMaxMinMemoryUsage(startTime, endTime string, db *gorm.DB) (*TableRowDef, error) { condition := fmt.Sprintf("where time >= '%s' and time < '%s' ", startTime, endTime) - sql := fmt.Sprintf(`select 'node_mem_usage','', 100*(1-t1.avg_value/t2.total),100*(1-t1.min_value/t2.total), 100*(1-t1.max_value/t2.total) from + sql := fmt.Sprintf(`select 'node_mem_usage','', 100*(1-t1.avg_value/t2.total),100*(1-t1.min_value/t2.total), 100*(1-t1.max_value/t2.total) from (select avg(value) as avg_value,max(value) as max_value,min(value) as min_value from metrics_schema.node_memory_available %[1]s) as t1 join (select max(value) as total from metrics_schema.node_total_memory %[1]s) as t2;`, condition) rows, err := querySQL(db, sql) @@ -1728,7 +1728,7 @@ func getAvgMaxMinMemoryUsage(startTime, endTime string, db *gorm.DB) (*TableRowD if len(rows) == 0 { return nil, nil } - sql = fmt.Sprintf(`select 'node_mem_usage',t1.instance, 100*(1-t1.avg_value/t2.total) as avg_value, 100*(1-t1.min_value/t2.total), 100*(1-t1.max_value/t2.total) from + sql = fmt.Sprintf(`select 'node_mem_usage',t1.instance, 100*(1-t1.avg_value/t2.total) as avg_value, 100*(1-t1.min_value/t2.total), 100*(1-t1.max_value/t2.total) from (select instance, avg(value) as avg_value,max(value) as max_value,min(value) as min_value from metrics_schema.node_memory_available %[1]s GROUP BY instance) as t1 join (select instance, max(value) as total from metrics_schema.node_total_memory %[1]s GROUP BY instance) as t2 where t1.instance = t2.instance order by avg_value desc;`, condition) subRows, err := querySQL(db, sql) diff --git a/pkg/apiserver/diagnose/report_test.go b/pkg/apiserver/diagnose/report_test.go index fd8e08d664..ff186a850e 100644 --- a/pkg/apiserver/diagnose/report_test.go +++ b/pkg/apiserver/diagnose/report_test.go @@ -18,8 +18,9 @@ import ( "strings" "testing" - "github.com/jinzhu/gorm" . "github.com/pingcap/check" + "gorm.io/driver/mysql" + "gorm.io/gorm" ) func TestT(t *testing.T) { @@ -46,10 +47,8 @@ type testReportSuite struct{} //} func (t *testReportSuite) TestGetTable(c *C) { - //cli, err := gorm.Open("mysql", "root:@tcp(127.0.0.1:4000)/test?charset=utf8&parseTime=True&loc=Local") - cli, err := gorm.Open("mysql", "root:@tcp(172.16.5.40:4009)/test?charset=utf8&parseTime=True&loc=Local") + cli, err := gorm.Open(mysql.Open("root:@tcp(172.16.5.40:4009)/test?charset=utf8&parseTime=True&loc=Local")) c.Assert(err, IsNil) - defer cli.Close() startTime := "2020-03-25 23:00:00" endTime := "2020-03-25 23:05:00" @@ -61,9 +60,8 @@ func (t *testReportSuite) TestGetTable(c *C) { } func (t *testReportSuite) TestGetCompareTable(c *C) { - cli, err := gorm.Open("mysql", "root:@tcp(172.16.5.40:4009)/test?charset=utf8&parseTime=True&loc=Local") + cli, err := gorm.Open(mysql.Open("root:@tcp(172.16.5.40:4009)/test?charset=utf8&parseTime=True&loc=Local")) c.Assert(err, IsNil) - defer cli.Close() //startTime1 := "2020-03-12 20:17:00" //endTime1 := "2020-03-12 20:39:00" @@ -84,9 +82,8 @@ func (t *testReportSuite) TestGetCompareTable(c *C) { } func (t *testReportSuite) TestInspection(c *C) { - cli, err := gorm.Open("mysql", "root:@tcp(172.16.5.40:4009)/test?charset=utf8&parseTime=True&loc=Local") + cli, err := gorm.Open(mysql.Open("root:@tcp(172.16.5.40:4009)/test?charset=utf8&parseTime=True&loc=Local")) c.Assert(err, IsNil) - defer cli.Close() // affect by big query join startTime1 := "2020-03-08 01:36:00" diff --git a/pkg/apiserver/info/info.go b/pkg/apiserver/info/info.go index 689f4411ef..67bf1ad2d5 100644 --- a/pkg/apiserver/info/info.go +++ b/pkg/apiserver/info/info.go @@ -93,25 +93,27 @@ func (s *Service) whoamiHandler(c *gin.Context) { c.JSON(http.StatusOK, resp) } -type DatabaseResponse = []string - // @ID infoListDatabases // @Summary List all databases -// @Success 200 {object} DatabaseResponse +// @Success 200 {object} []string // @Router /info/databases [get] // @Security JwtAuth // @Failure 401 {object} utils.APIError "Unauthorized failure" func (s *Service) databasesHandler(c *gin.Context) { + type databaseSchemas struct { + Databases string `gorm:"column:Databases"` + } + var result []databaseSchemas db := utils.GetTiDBConnection(c) - var result DatabaseResponse - err := db.Raw("show databases").Pluck("Databases", &result).Error + err := db.Raw("SHOW DATABASES").Scan(&result).Error if err != nil { _ = c.Error(err) return } - for i, v := range result { - result[i] = strings.ToLower(v) + strs := []string{} + for _, v := range result { + strs = append(strs, strings.ToLower(v.Databases)) } - sort.Strings(result) - c.JSON(http.StatusOK, result) + sort.Strings(strs) + c.JSON(http.StatusOK, strs) } diff --git a/pkg/apiserver/logsearch/models.go b/pkg/apiserver/logsearch/models.go index de16f10c67..bf5238f90a 100644 --- a/pkg/apiserver/logsearch/models.go +++ b/pkg/apiserver/logsearch/models.go @@ -150,10 +150,7 @@ func (PreviewModel) TableName() string { } func autoMigrate(db *dbstore.DB) error { - return db.AutoMigrate(&TaskModel{}). - AutoMigrate(&TaskGroupModel{}). - AutoMigrate(&PreviewModel{}). - Error + return db.AutoMigrate(&TaskModel{}, &TaskGroupModel{}, &PreviewModel{}) } func cleanupAllTasks(db *dbstore.DB) { diff --git a/pkg/apiserver/profiling/model.go b/pkg/apiserver/profiling/model.go index f03e97b860..5025084e76 100644 --- a/pkg/apiserver/profiling/model.go +++ b/pkg/apiserver/profiling/model.go @@ -61,9 +61,7 @@ func (TaskGroupModel) TableName() string { } func autoMigrate(db *dbstore.DB) error { - return db.AutoMigrate(&TaskModel{}). - AutoMigrate(&TaskGroupModel{}). - Error + return db.AutoMigrate(&TaskModel{}, &TaskGroupModel{}) } // Task is the unit to fetch profiling information. diff --git a/pkg/apiserver/queryeditor/service.go b/pkg/apiserver/queryeditor/service.go index 382e5d63b4..e30227510a 100644 --- a/pkg/apiserver/queryeditor/service.go +++ b/pkg/apiserver/queryeditor/service.go @@ -141,7 +141,11 @@ func (s *Service) runHandler(c *gin.Context) { defer cancel() startTime := time.Now() - colNames, rows, err := executeStatements(ctx, utils.GetTiDBConnection(c).DB(), req.Statements) + sqlDB, err := utils.GetTiDBConnection(c).DB() + if err != nil { + panic(err) + } + colNames, rows, err := executeStatements(ctx, sqlDB, req.Statements) elapsedTime := time.Since(startTime) if err != nil { diff --git a/pkg/apiserver/slowquery/queries.go b/pkg/apiserver/slowquery/queries.go index 54a2467e48..b1b57dd09b 100644 --- a/pkg/apiserver/slowquery/queries.go +++ b/pkg/apiserver/slowquery/queries.go @@ -16,7 +16,7 @@ package slowquery import ( "strings" - "github.com/jinzhu/gorm" + "gorm.io/gorm" ) const ( @@ -27,7 +27,7 @@ type GetListRequest struct { BeginTime int `json:"begin_time" form:"begin_time"` EndTime int `json:"end_time" form:"end_time"` DB []string `json:"db" form:"db"` - Limit uint `json:"limit" form:"limit"` + Limit int `json:"limit" form:"limit"` Text string `json:"text" form:"text"` OrderBy string `json:"orderBy" form:"orderBy"` IsDesc bool `json:"desc" form:"desc"` diff --git a/pkg/apiserver/statement/config.go b/pkg/apiserver/statement/config.go index 9b6ee2a246..409a221d88 100644 --- a/pkg/apiserver/statement/config.go +++ b/pkg/apiserver/statement/config.go @@ -24,61 +24,56 @@ import ( ) // incoming configuration field should have the gorm tag `column` used to specify global variables -// sql will be built like this, gorm:"column:some_global_var" -> @@GLOBAL.some_global_var as some_global_var -func buildConfigQuerySQL(config interface{}) string { - var configType reflect.Type - if reflect.ValueOf(config).Kind() == reflect.Ptr { - configType = reflect.TypeOf(config).Elem() - } else { - configType = reflect.TypeOf(config) - } - - stmts := []string{} - fNum := configType.NumField() - for i := 0; i < fNum; i++ { - f := configType.Field(i) +// sql will be built like this, +// struct { FieldName `gorm:"column:some_global_var"` } -> @@GLOBAL.some_global_var AS some_global_var +func buildGlobalConfigProjectionSelectSQL(config interface{}) string { + str := buildStringByStructField(config, func(f reflect.StructField) (string, bool) { gormTag, ok := f.Tag.Lookup("gorm") if !ok { - continue + return "", false } column := utils.GetGormColumnName(gormTag) - stmts = append(stmts, fmt.Sprintf("@@GLOBAL.%s AS %s", column, column)) - } + return fmt.Sprintf("@@GLOBAL.%s AS %s", column, column), true + }, ", ") + return "SELECT " + str //nolint:gosec +} - // skip `SQL string formatting (gosec)` lint - return "SELECT " + strings.Join(stmts, ", ") // nolints +// sql will be built like this, +// struct { FieldName `gorm:"column:some_global_var"` } -> @@GLOBAL.some_global_var = @FieldName +// `allowedFields` means only allowed fields can be kept in built SQL. +func buildGlobalConfigNamedArgsUpdateSQL(config interface{}, allowedFields ...string) string { + str := buildStringByStructField(config, func(f reflect.StructField) (string, bool) { + // extract fields on demand + if len(allowedFields) != 0 && !funk.ContainsString(allowedFields, f.Name) { + return "", false + } + + gormTag, ok := f.Tag.Lookup("gorm") + if !ok { + return "", false + } + column := utils.GetGormColumnName(gormTag) + return fmt.Sprintf("@@GLOBAL.%s = @%s", column, f.Name), true + }, ", ") + return "SET " + str //nolint:gosec } -// sql will be built like this, gorm:"column:some_global_var" -> @@GLOBAL.some_global_var = some_global_var_value -func buildConfigUpdateSQL(config interface{}, extract ...string) string { - var configType reflect.Type - var configValue reflect.Value - if reflect.ValueOf(config).Kind() == reflect.Ptr { - configType = reflect.TypeOf(config).Elem() - configValue = reflect.ValueOf(config).Elem() +func buildStringByStructField(i interface{}, buildFunc func(f reflect.StructField) (string, bool), sep string) string { + var t reflect.Type + if reflect.ValueOf(i).Kind() == reflect.Ptr { + t = reflect.TypeOf(i).Elem() } else { - configType = reflect.TypeOf(config) - configValue = reflect.ValueOf(config) + t = reflect.TypeOf(i) } - stmts := []string{} - fNum := configType.NumField() + strs := []string{} + fNum := t.NumField() for i := 0; i < fNum; i++ { - f := configType.Field(i) - // extract fields on demand - if len(extract) != 0 && !funk.ContainsString(extract, f.Name) { - continue - } - gormTag, ok := f.Tag.Lookup("gorm") + str, ok := buildFunc(t.Field(i)) if !ok { continue } - - val := configValue.Field(i) - column := utils.GetGormColumnName(gormTag) - stmts = append(stmts, fmt.Sprintf("@@GLOBAL.%s = %v", column, val)) + strs = append(strs, str) } - - // skip `SQL string formatting (gosec)` lint - return "SET " + strings.Join(stmts, ", ") // nolints + return strings.Join(strs, sep) } diff --git a/pkg/apiserver/statement/config_test.go b/pkg/apiserver/statement/config_test.go index dd0db3a8f3..3ef6edf203 100644 --- a/pkg/apiserver/statement/config_test.go +++ b/pkg/apiserver/statement/config_test.go @@ -33,14 +33,14 @@ type testConfig struct { RefreshInterval int `json:"refresh_interval" gorm:"column:tidb_stmt_summary_refresh_interval"` } -func (t *testConfigSuite) Test_buildConfigQuerySQL_struct_success(c *C) { +func (t *testConfigSuite) Test_buildGlobalConfigProjectionSelectSQL_struct_success(c *C) { testConfigStmt := "SELECT @@GLOBAL.tidb_enable_stmt_summary AS tidb_enable_stmt_summary, @@GLOBAL.tidb_stmt_summary_refresh_interval AS tidb_stmt_summary_refresh_interval" - c.Assert(buildConfigQuerySQL(testConfig{}), Equals, testConfigStmt) + c.Assert(buildGlobalConfigProjectionSelectSQL(testConfig{}), Equals, testConfigStmt) } -func (t *testConfigSuite) Test_buildConfigQuerySQL_ptr_success(c *C) { +func (t *testConfigSuite) Test_buildGlobalConfigProjectionSelectSQL_ptr_success(c *C) { testConfigStmt := "SELECT @@GLOBAL.tidb_enable_stmt_summary AS tidb_enable_stmt_summary, @@GLOBAL.tidb_stmt_summary_refresh_interval AS tidb_stmt_summary_refresh_interval" - c.Assert(buildConfigQuerySQL(&testConfig{}), Equals, testConfigStmt) + c.Assert(buildGlobalConfigProjectionSelectSQL(&testConfig{}), Equals, testConfigStmt) } type testConfig2 struct { @@ -48,27 +48,27 @@ type testConfig2 struct { RefreshInterval int `json:"refresh_interval"` } -func (t *testConfigSuite) Test_buildConfigQuerySQL_without_gorm_tag(c *C) { +func (t *testConfigSuite) Test_buildGlobalConfigProjectionSelectSQL_without_gorm_tag(c *C) { testConfigStmt := "SELECT @@GLOBAL.tidb_enable_stmt_summary AS tidb_enable_stmt_summary" - c.Assert(buildConfigQuerySQL(&testConfig2{}), Equals, testConfigStmt) + c.Assert(buildGlobalConfigProjectionSelectSQL(&testConfig2{}), Equals, testConfigStmt) } -func (t *testConfigSuite) Test_buildConfigUpdateSQL_struct_success(c *C) { - testConfigStmt := "SET @@GLOBAL.tidb_enable_stmt_summary = true, @@GLOBAL.tidb_stmt_summary_refresh_interval = 1800" - c.Assert(buildConfigUpdateSQL(testConfig{Enable: true, RefreshInterval: 1800}), Equals, testConfigStmt) +func (t *testConfigSuite) Test_buildGlobalConfigNamedArgsUpdateSQL_struct_success(c *C) { + testConfigStmt := "SET @@GLOBAL.tidb_enable_stmt_summary = @Enable, @@GLOBAL.tidb_stmt_summary_refresh_interval = @RefreshInterval" + c.Assert(buildGlobalConfigNamedArgsUpdateSQL(testConfig{Enable: true, RefreshInterval: 1800}), Equals, testConfigStmt) } -func (t *testConfigSuite) Test_buildConfigUpdateSQL_ptr_success(c *C) { - testConfigStmt := "SET @@GLOBAL.tidb_enable_stmt_summary = true, @@GLOBAL.tidb_stmt_summary_refresh_interval = 1800" - c.Assert(buildConfigUpdateSQL(&testConfig{Enable: true, RefreshInterval: 1800}), Equals, testConfigStmt) +func (t *testConfigSuite) Test_buildGlobalConfigNamedArgsUpdateSQL_ptr_success(c *C) { + testConfigStmt := "SET @@GLOBAL.tidb_enable_stmt_summary = @Enable, @@GLOBAL.tidb_stmt_summary_refresh_interval = @RefreshInterval" + c.Assert(buildGlobalConfigNamedArgsUpdateSQL(&testConfig{Enable: true, RefreshInterval: 1800}), Equals, testConfigStmt) } -func (t *testConfigSuite) Test_buildConfigUpdateSQL_without_gorm_tag(c *C) { - testConfigStmt := "SET @@GLOBAL.tidb_enable_stmt_summary = true" - c.Assert(buildConfigUpdateSQL(&testConfig2{Enable: true, RefreshInterval: 1800}), Equals, testConfigStmt) +func (t *testConfigSuite) Test_buildGlobalConfigNamedArgsUpdateSQL_without_gorm_tag(c *C) { + testConfigStmt := "SET @@GLOBAL.tidb_enable_stmt_summary = @Enable" + c.Assert(buildGlobalConfigNamedArgsUpdateSQL(&testConfig2{Enable: true, RefreshInterval: 1800}), Equals, testConfigStmt) } -func (t *testConfigSuite) Test_buildConfigUpdateSQL_extract_fields(c *C) { - testConfigStmt := "SET @@GLOBAL.tidb_enable_stmt_summary = true" - c.Assert(buildConfigUpdateSQL(&testConfig{Enable: true, RefreshInterval: 1800}, "Enable"), Equals, testConfigStmt) +func (t *testConfigSuite) Test_buildGlobalConfigNamedArgsUpdateSQL_extract_fields(c *C) { + testConfigStmt := "SET @@GLOBAL.tidb_enable_stmt_summary = @Enable" + c.Assert(buildGlobalConfigNamedArgsUpdateSQL(&testConfig{Enable: true, RefreshInterval: 1800}, "Enable"), Equals, testConfigStmt) } diff --git a/pkg/apiserver/statement/models.go b/pkg/apiserver/statement/models.go index 4c99bd8e7a..821cf65c3e 100644 --- a/pkg/apiserver/statement/models.go +++ b/pkg/apiserver/statement/models.go @@ -16,7 +16,8 @@ package statement import ( "strings" - "github.com/jinzhu/gorm" + "gorm.io/gorm" + "gorm.io/gorm/schema" "github.com/pingcap/tidb-dashboard/pkg/apiserver/utils" ) @@ -126,7 +127,7 @@ func extractSchemasFromTableNames(tableNames string) string { return strings.Join(keys, ", ") } -func (m *Model) AfterFind() error { +func (m *Model) AfterFind(db *gorm.DB) error { if len(m.AggTableNames) > 0 { m.RelatedSchemas = extractSchemasFromTableNames(m.AggTableNames) } @@ -141,12 +142,14 @@ type Field struct { Aggregation string } +var gormDefaultNamingStrategy = schema.NamingStrategy{} + func getFieldsAndTags() (stmtFields []Field) { fields := utils.GetFieldsAndTags(Model{}, []string{"related", "agg", "json"}) for _, f := range fields { sf := Field{ - ColumnName: gorm.ToColumnName(f.Name), + ColumnName: gormDefaultNamingStrategy.ColumnName("", f.Name), JSONName: f.Tags["json"], Related: []string{}, Aggregation: f.Tags["agg"], diff --git a/pkg/apiserver/statement/queries.go b/pkg/apiserver/statement/queries.go index 9b430d9573..d9a4d3d12d 100644 --- a/pkg/apiserver/statement/queries.go +++ b/pkg/apiserver/statement/queries.go @@ -18,7 +18,7 @@ import ( "regexp" "strings" - "github.com/jinzhu/gorm" + "gorm.io/gorm" ) const ( diff --git a/pkg/apiserver/statement/service.go b/pkg/apiserver/statement/service.go index 6d1aeb2351..9dacfc9938 100644 --- a/pkg/apiserver/statement/service.go +++ b/pkg/apiserver/statement/service.go @@ -89,7 +89,7 @@ type EditableConfig struct { func (s *Service) configHandler(c *gin.Context) { db := utils.GetTiDBConnection(c) cfg := &EditableConfig{} - err := db.Raw(buildConfigQuerySQL(cfg)).Find(cfg).Error + err := db.Raw(buildGlobalConfigProjectionSelectSQL(cfg)).Find(cfg).Error if err != nil { _ = c.Error(err) return @@ -104,19 +104,20 @@ func (s *Service) configHandler(c *gin.Context) { // @Security JwtAuth // @Failure 401 {object} utils.APIError "Unauthorized failure" func (s *Service) modifyConfigHandler(c *gin.Context) { - var err error var config EditableConfig - if err = c.ShouldBindJSON(&config); err != nil { + if err := c.ShouldBindJSON(&config); err != nil { utils.MakeInvalidRequestErrorFromError(c, err) return } db := utils.GetTiDBConnection(c) + var sqlWithNamedArgument string if !config.Enable { - err = db.Exec(buildConfigUpdateSQL(&config, "Enable")).Error + sqlWithNamedArgument = buildGlobalConfigNamedArgsUpdateSQL(&config, "Enable") } else { - err = db.Exec(buildConfigUpdateSQL(&config)).Error + sqlWithNamedArgument = buildGlobalConfigNamedArgsUpdateSQL(&config) } + err := db.Exec(sqlWithNamedArgument, &config).Error if err != nil { _ = c.Error(err) return diff --git a/pkg/apiserver/user/auth.go b/pkg/apiserver/user/auth.go index 05b6d6f653..f014de3d54 100644 --- a/pkg/apiserver/user/auth.go +++ b/pkg/apiserver/user/auth.go @@ -224,7 +224,7 @@ func (s *AuthService) authSQLForm(f *authenticateForm) (*utils.SessionUser, erro // tidb.ErrTiDBAuthFailed return nil, err } - defer db.Close() //nolint:errcheck + defer utils.CloseTiDBConnection(db) //nolint:errcheck return &utils.SessionUser{ HasTiDBAuth: true, diff --git a/pkg/apiserver/utils/gorm.go b/pkg/apiserver/utils/gorm.go index c9f31e4c79..4aa9815eb9 100644 --- a/pkg/apiserver/utils/gorm.go +++ b/pkg/apiserver/utils/gorm.go @@ -13,10 +13,11 @@ package utils -import "strings" +import "gorm.io/gorm/schema" func GetGormColumnName(gormStr string) string { - // TODO: use go-gorm/gorm/schema ParseTagSetting. Prerequisite: Upgrade to the latest version - columnName := strings.Split(gormStr, ":")[1] - return columnName + gormStrMap := schema.ParseTagSetting(gormStr, ";") + // The key will be converted to uppercase in: + // https://github.com/go-gorm/gorm/blob/master/schema/utils.go#L33 + return gormStrMap["COLUMN"] } diff --git a/pkg/apiserver/utils/gorm_test.go b/pkg/apiserver/utils/gorm_test.go new file mode 100644 index 0000000000..db586b9b30 --- /dev/null +++ b/pkg/apiserver/utils/gorm_test.go @@ -0,0 +1,35 @@ +// Copyright 2021 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package utils + +import ( + "testing" + + . "github.com/pingcap/check" +) + +func TestT(t *testing.T) { + CustomVerboseFlag = true + TestingT(t) +} + +var _ = Suite(&testGormSuite{}) + +type testGormSuite struct{} + +func (t *testGormSuite) Test_GetGormColumnName(c *C) { + c.Assert(GetGormColumnName(`column:db`), Equals, `db`) + c.Assert(GetGormColumnName(`primaryKey;index`), Equals, ``) + c.Assert(GetGormColumnName(`column:db;primaryKey;index`), Equals, `db`) +} diff --git a/pkg/apiserver/utils/tidb_conn.go b/pkg/apiserver/utils/tidb_conn.go index c2fc0277ad..d71217acbc 100644 --- a/pkg/apiserver/utils/tidb_conn.go +++ b/pkg/apiserver/utils/tidb_conn.go @@ -17,8 +17,8 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/jinzhu/gorm" "github.com/joomcode/errorx" + "gorm.io/gorm" "github.com/pingcap/tidb-dashboard/pkg/tidb" ) @@ -71,7 +71,7 @@ func MWConnectTiDB(tidbClient *tidb.Client) gin.HandlerFunc { if dbInContext != nil { dbInContext2 := dbInContext.(*gorm.DB) if dbInContext2 != nil { - _ = dbInContext2.Close() + _ = CloseTiDBConnection(dbInContext2) } } }() @@ -92,6 +92,14 @@ func TakeTiDBConnection(c *gin.Context) *gorm.DB { return db } +func CloseTiDBConnection(db *gorm.DB) error { + sqlDB, err := db.DB() + if err != nil { + return err + } + return sqlDB.Close() +} + // GetTiDBConnection gets the TiDB connection stored in the gin context by `MWConnectTiDB` middleware. // // The connection will be closed automatically after all handlers are finished. Thus you must not use it outside diff --git a/pkg/dbstore/dbstore.go b/pkg/dbstore/dbstore.go index 41d89d24a1..6f4a45518a 100644 --- a/pkg/dbstore/dbstore.go +++ b/pkg/dbstore/dbstore.go @@ -18,15 +18,15 @@ import ( "os" "path" - "github.com/hypnoglow/gormzap" - "github.com/jinzhu/gorm" + "gorm.io/driver/sqlite" + "gorm.io/gorm" + "moul.io/zapgorm2" - // Sqlite3 driver used by gorm - _ "github.com/jinzhu/gorm/dialects/sqlite" "github.com/pingcap/log" "go.uber.org/fx" "go.uber.org/zap" + "github.com/pingcap/tidb-dashboard/pkg/apiserver/utils" "github.com/pingcap/tidb-dashboard/pkg/config" ) @@ -42,18 +42,19 @@ func NewDBStore(lc fx.Lifecycle, config *config.Config) (*DB, error) { p := path.Join(config.DataDir, "dashboard.sqlite.db") log.Info("Dashboard initializing local storage file", zap.String("path", p)) - gormDB, err := gorm.Open("sqlite3", p) + gormDB, err := gorm.Open(sqlite.Open(p), &gorm.Config{ + Logger: zapgorm2.New(log.L()), + }) if err != nil { log.Error("Failed to open Dashboard storage file", zap.Error(err)) return nil, err } db := &DB{gormDB} - db.SetLogger(gormzap.New(log.L(), gormzap.WithLevel(zap.WarnLevel))) lc.Append(fx.Hook{ OnStop: func(context.Context) error { - return db.Close() + return utils.CloseTiDBConnection(db.DB) }, }) diff --git a/pkg/keyvisual/storage/model.go b/pkg/keyvisual/storage/model.go index 08442ff7a7..9111a58748 100644 --- a/pkg/keyvisual/storage/model.go +++ b/pkg/keyvisual/storage/model.go @@ -70,10 +70,10 @@ func (a *AxisModel) Delete(db *dbstore.DB) error { // If the table `AxisModel` exists, return true, nil // or create table `AxisModel` func CreateTableAxisModelIfNotExists(db *dbstore.DB) (bool, error) { - if db.HasTable(&AxisModel{}) { + if db.Migrator().HasTable(&AxisModel{}) { return true, nil } - return false, db.CreateTable(&AxisModel{}).Error + return false, db.Migrator().CreateTable(&AxisModel{}) } func ClearTableAxisModel(db *dbstore.DB) error { diff --git a/pkg/keyvisual/storage/model_test.go b/pkg/keyvisual/storage/model_test.go index 1c3a60681d..48fbe0ebb3 100644 --- a/pkg/keyvisual/storage/model_test.go +++ b/pkg/keyvisual/storage/model_test.go @@ -18,8 +18,9 @@ import ( "testing" "time" - "github.com/jinzhu/gorm" . "github.com/pingcap/check" + "gorm.io/driver/sqlite" + "gorm.io/gorm" "github.com/pingcap/tidb-dashboard/pkg/dbstore" "github.com/pingcap/tidb-dashboard/pkg/keyvisual/matrix" @@ -38,17 +39,13 @@ type testDbstoreSuite struct { func (t *testDbstoreSuite) SetUpTest(c *C) { t.dir = c.MkDir() - gormDB, err := gorm.Open("sqlite3", path.Join(t.dir, "test.sqlite.db")) + gormDB, err := gorm.Open(sqlite.Open(path.Join(t.dir, "test.sqlite.db"))) if err != nil { c.Errorf("Open %s error: %v", path.Join(t.dir, "test.sqlite.db"), err) } t.db = &dbstore.DB{DB: gormDB} } -func (t *testDbstoreSuite) TearDownTest(c *C) { - _ = t.db.Close() -} - func (t *testDbstoreSuite) TestCreateTableAxisModelIfNotExists(c *C) { isExist, err := CreateTableAxisModelIfNotExists(t.db) c.Assert(isExist, Equals, false) @@ -71,7 +68,7 @@ func (t *testDbstoreSuite) TestClearTableAxisModel(c *C) { if err != nil { c.Fatalf("AxisModel Insert error: %v", err) } - var count int + var count int64 err = t.db.Table(tableAxisModelName).Count(&count).Error if err != nil { @@ -121,7 +118,7 @@ func (t *testDbstoreSuite) TestAxisModelFunc(c *C) { err = axisModel.Delete(t.db) c.Assert(err, IsNil) - var count int + var count int64 err = t.db.Table(tableAxisModelName).Count(&count).Error if err != nil { c.Fatalf("Count table AxisModel error: %v", err) @@ -155,7 +152,7 @@ func (t *testDbstoreSuite) TestAxisModelsFindAndDelete(c *C) { } } - var count int + var count int64 err = t.db.Table(tableAxisModelName).Count(&count).Error if err != nil { c.Fatalf("Count table AxisModel error: %v", err) diff --git a/pkg/tidb/client.go b/pkg/tidb/client.go index 0cc731d926..f17731e65b 100644 --- a/pkg/tidb/client.go +++ b/pkg/tidb/client.go @@ -12,14 +12,12 @@ import ( "github.com/VividCortex/mysqlerr" "github.com/go-sql-driver/mysql" - "github.com/jinzhu/gorm" "github.com/pingcap/log" "go.etcd.io/etcd/clientv3" "go.uber.org/fx" "go.uber.org/zap" - - // MySQL driver used by gorm - _ "github.com/jinzhu/gorm/dialects/mysql" + mysqlDriver "gorm.io/driver/mysql" + "gorm.io/gorm" "github.com/pingcap/tidb-dashboard/pkg/config" "github.com/pingcap/tidb-dashboard/pkg/httpc" @@ -139,7 +137,7 @@ func (c *Client) OpenSQLConn(user string, pass string) (*gorm.DB, error) { dsnConfig.TLSConfig = c.sqlAPITLSKey dsn := dsnConfig.FormatDSN() - db, err := gorm.Open("mysql", dsn) + db, err := gorm.Open(mysqlDriver.Open(dsn)) if err != nil { if _, ok := err.(*net.OpError); ok || err == driver.ErrBadConn { if strings.HasPrefix(addr, "0.0.0.0:") { diff --git a/pkg/utils/sys_schema.go b/pkg/utils/sys_schema.go index c3d9ba032d..9ac4a13b49 100644 --- a/pkg/utils/sys_schema.go +++ b/pkg/utils/sys_schema.go @@ -19,8 +19,8 @@ import ( "time" "github.com/ReneKroon/ttlcache/v2" - "github.com/jinzhu/gorm" "go.uber.org/fx" + "gorm.io/gorm" ) const (