├── .github └── workflows │ └── ci.yml ├── LICENSE ├── NOTICE.txt ├── README.md ├── go.mod ├── go.sum ├── renovate.json └── sqlcaller ├── driver.go └── driver_test.go /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | name: CI 4 | on: 5 | pull_request: 6 | push: 7 | branches: 8 | - main 9 | jobs: 10 | lint: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v3 14 | - name: lint 15 | uses: reviewdog/action-golangci-lint@v2 16 | with: 17 | level: info 18 | build: 19 | services: 20 | mysql: 21 | image: 'mysql:8.0' 22 | env: 23 | MYSQL_ALLOW_EMPTY_PASSWORD: yes 24 | MYSQL_DATABASE: app 25 | ports: 26 | - '3306/tcp' 27 | postgres: 28 | image: 'postgres:12' 29 | env: 30 | POSTGRES_PASSWORD: kogaidan 31 | POSTGRES_USER: dankogai 32 | ports: 33 | - '5432/tcp' 34 | strategy: 35 | matrix: 36 | go_version: 37 | - 1.14.x 38 | - 1.15.x 39 | - 1.16.x 40 | - 1.17.x 41 | - 1.18.x 42 | os: 43 | - ubuntu-latest 44 | runs-on: ${{ matrix.os }} 45 | steps: 46 | - uses: actions/checkout@v3 47 | - uses: actions/setup-go@v3.1.0 48 | with: 49 | go-version: ${{ matrix.go_version }} 50 | - uses: actions/cache@v3 51 | with: 52 | path: ~/go/pkg/mod 53 | key: ${{ runner.os }}-go-${{ matrix.go_version }}-${{ hashFiles('**/go.sum') }} 54 | restore-keys: | 55 | ${{ runner.os }}-go-${{ matrix.go_version }} 56 | - name: test 57 | run: go test ./... 58 | env: 59 | MYSQL_DSN: "root@tcp(127.0.0.1:${{ job.services.mysql.ports['3306'] }})/app" 60 | PG_DSN: "postgres://dankogai:kogaidan@127.0.0.1:${{ job.services.postgres.ports['5432'] }}/dankogai?sslmode=disable" 61 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2020 aereal 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /NOTICE.txt: -------------------------------------------------------------------------------- 1 | AWS X-Ray SDK for Go 2 | Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![CI](https://github.com/aereal/go-sql-caller-annotation/workflows/CI/badge.svg?branch=main) 2 | [![PkgGoDev](https://pkg.go.dev/badge/aereal/go-sql-caller-annotation)](https://pkg.go.dev/github.com/aereal/go-sql-caller-annotation) 3 | 4 | # go-sql-caller-annotation 5 | 6 | Provides an new `sql.*DB` connection that injects caller information to the query 7 | 8 | ```sh 9 | go get github.com/aereal/go-sql-caller-annotation 10 | ``` 11 | 12 | ## Usage 13 | 14 | ```go 15 | package main 16 | 17 | import ( 18 | "github.com/aereal/go-sql-caller-annotation/sqlcaller" 19 | ) 20 | 21 | func main() { 22 | db, _ := sqlcaller.WithAnnotation("mysql", "..." /* DSN */) 23 | db.Exec("SELECT version()") // runs `/* main.main (/path/to/file.go:9) */ SELECT version()` query 24 | } 25 | ``` 26 | 27 | ## License 28 | 29 | See LICENSE file. 30 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/aereal/go-sql-caller-annotation 2 | 3 | go 1.15 4 | 5 | require ( 6 | github.com/go-sql-driver/mysql v1.6.0 7 | github.com/lib/pq v1.10.9 8 | ) 9 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= 2 | github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= 3 | github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= 4 | github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= 5 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "extends": [ 4 | "github>aereal/renovate-config" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /sqlcaller/driver.go: -------------------------------------------------------------------------------- 1 | package sqlcaller 2 | 3 | import ( 4 | "context" 5 | "database/sql" 6 | "database/sql/driver" 7 | "fmt" 8 | "runtime" 9 | "sync" 10 | ) 11 | 12 | var ( 13 | muInitializedDrivers sync.Mutex 14 | initializedDrivers = map[string]bool{} 15 | driverSuffix = ":annotator" 16 | ) 17 | 18 | func injectCaller(query string) string { 19 | pc, file, line, _ := runtime.Caller(1) 20 | fn := runtime.FuncForPC(pc) 21 | return fmt.Sprintf("/* %s (%s:%d) */ %s", fn.Name(), file, line, query) 22 | } 23 | 24 | // WithAnnotation creates wrapped sql.*DB connection that injects caller information to the query. 25 | func WithAnnotation(driver, dsn string) (*sql.DB, error) { 26 | if err := initDriver(driver, dsn); err != nil { 27 | return nil, err 28 | } 29 | return sql.Open(driver+driverSuffix, dsn) 30 | } 31 | 32 | // mostly copied by aws-xray-sdk-go (https://github.com/aws/aws-xray-sdk-go/blob/master/xray/sql_context.go) 33 | 34 | func initDriver(driver, dsn string) error { 35 | muInitializedDrivers.Lock() 36 | defer muInitializedDrivers.Unlock() 37 | 38 | if _, ok := initializedDrivers[driver]; ok { 39 | return nil 40 | } 41 | 42 | db, err := sql.Open(driver, dsn) 43 | if err != nil { 44 | return err 45 | } 46 | sql.Register(driver+driverSuffix, &driverDriver{Driver: db.Driver(), baseName: driver}) 47 | initializedDrivers[driver] = true 48 | db.Close() 49 | 50 | return nil 51 | } 52 | 53 | type driverDriver struct { 54 | driver.Driver 55 | baseName string 56 | } 57 | 58 | func (d *driverDriver) Open(dsn string) (driver.Conn, error) { 59 | rawConn, err := d.Driver.Open(dsn) 60 | if err != nil { 61 | return nil, err 62 | } 63 | 64 | return &driverConn{Conn: rawConn}, nil 65 | } 66 | 67 | type driverConn struct { 68 | driver.Conn 69 | } 70 | 71 | var _ interface { 72 | driver.Conn 73 | driver.ConnPrepareContext 74 | driver.ConnBeginTx 75 | driver.Pinger 76 | driver.Execer 77 | driver.ExecerContext 78 | driver.Queryer 79 | driver.QueryerContext 80 | driver.SessionResetter 81 | driver.NamedValueChecker 82 | } = &driverConn{} 83 | 84 | func (c *driverConn) CheckNamedValue(nv *driver.NamedValue) (err error) { 85 | if checker, ok := c.Conn.(driver.NamedValueChecker); ok { 86 | return checker.CheckNamedValue(nv) 87 | } 88 | nv.Value, err = driver.DefaultParameterConverter.ConvertValue(nv.Value) 89 | return 90 | } 91 | 92 | func (c *driverConn) ResetSession(ctx context.Context) error { 93 | resetter, ok := c.Conn.(driver.SessionResetter) 94 | if !ok { 95 | return driver.ErrSkip 96 | } 97 | return resetter.ResetSession(ctx) 98 | } 99 | 100 | func (c *driverConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) { 101 | beginner, ok := c.Conn.(driver.ConnBeginTx) 102 | if !ok { 103 | return nil, driver.ErrSkip 104 | } 105 | return beginner.BeginTx(ctx, opts) 106 | } 107 | 108 | func (c *driverConn) Ping(ctx context.Context) error { 109 | pinger, ok := c.Conn.(driver.Pinger) 110 | if !ok { 111 | return driver.ErrSkip 112 | } 113 | return pinger.Ping(ctx) 114 | } 115 | 116 | func (c *driverConn) Prepare(query string) (driver.Stmt, error) { 117 | return c.Conn.Prepare(injectCaller(query)) 118 | } 119 | 120 | func (c *driverConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { 121 | var ( 122 | stmt driver.Stmt 123 | err error 124 | ) 125 | if connCtx, ok := c.Conn.(driver.ConnPrepareContext); ok { 126 | stmt, err = connCtx.PrepareContext(ctx, injectCaller(query)) 127 | } else { 128 | stmt, err = c.Conn.Prepare(query) 129 | if err == nil { 130 | select { 131 | default: 132 | case <-ctx.Done(): 133 | stmt.Close() 134 | return nil, ctx.Err() 135 | } 136 | } 137 | } 138 | if err != nil { 139 | return nil, err 140 | } 141 | return &driverStmt{ 142 | Stmt: stmt, 143 | conn: c, 144 | }, nil 145 | } 146 | 147 | func (c *driverConn) Exec(query string, args []driver.Value) (driver.Result, error) { 148 | execer, ok := c.Conn.(driver.Execer) 149 | if !ok { 150 | return nil, driver.ErrSkip 151 | } 152 | return execer.Exec(injectCaller(query), args) 153 | } 154 | 155 | func (c *driverConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { 156 | execer, ok := c.Conn.(driver.ExecerContext) 157 | if !ok { 158 | return nil, driver.ErrSkip 159 | } 160 | return execer.ExecContext(ctx, injectCaller(query), args) 161 | } 162 | 163 | func (c *driverConn) Query(query string, args []driver.Value) (driver.Rows, error) { 164 | queryer, ok := c.Conn.(driver.Queryer) 165 | if !ok { 166 | return nil, driver.ErrSkip 167 | } 168 | return queryer.Query(injectCaller(query), args) 169 | } 170 | 171 | func (c *driverConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { 172 | queryer, ok := c.Conn.(driver.QueryerContext) 173 | if !ok { 174 | return nil, driver.ErrSkip 175 | } 176 | return queryer.QueryContext(ctx, injectCaller(query), args) 177 | } 178 | 179 | type driverStmt struct { 180 | driver.Stmt 181 | conn *driverConn 182 | } 183 | 184 | var _ interface { 185 | driver.Stmt 186 | driver.StmtExecContext 187 | driver.StmtQueryContext 188 | driver.ColumnConverter 189 | driver.NamedValueChecker 190 | } = &driverStmt{} 191 | 192 | func (s *driverStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) { 193 | execer, ok := s.Stmt.(driver.StmtExecContext) 194 | if !ok { 195 | return nil, driver.ErrSkip 196 | } 197 | return execer.ExecContext(ctx, args) 198 | } 199 | 200 | func (s *driverStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { 201 | queryer, ok := s.Stmt.(driver.StmtQueryContext) 202 | if !ok { 203 | return nil, driver.ErrSkip 204 | } 205 | return queryer.QueryContext(ctx, args) 206 | } 207 | 208 | func (s *driverStmt) ColumnConverter(idx int) driver.ValueConverter { 209 | if conv, ok := s.Stmt.(driver.ColumnConverter); ok { 210 | return conv.ColumnConverter(idx) 211 | } 212 | return driver.DefaultParameterConverter 213 | } 214 | 215 | func (s *driverStmt) CheckNamedValue(nv *driver.NamedValue) (err error) { 216 | if checker, ok := s.Stmt.(driver.NamedValueChecker); ok { 217 | return checker.CheckNamedValue(nv) 218 | } 219 | if checker, ok := s.conn.Conn.(driver.NamedValueChecker); ok { 220 | return checker.CheckNamedValue(nv) 221 | } 222 | nv.Value, err = driver.DefaultParameterConverter.ConvertValue(nv.Value) 223 | return 224 | } 225 | -------------------------------------------------------------------------------- /sqlcaller/driver_test.go: -------------------------------------------------------------------------------- 1 | package sqlcaller 2 | 3 | import ( 4 | "context" 5 | "database/sql/driver" 6 | "os" 7 | "testing" 8 | "time" 9 | 10 | _ "github.com/go-sql-driver/mysql" 11 | _ "github.com/lib/pq" 12 | ) 13 | 14 | func TestAdopt(t *testing.T) { 15 | type args struct { 16 | driver string 17 | dsn string 18 | } 19 | type testCase struct { 20 | name string 21 | args args 22 | wantErr bool 23 | } 24 | tests := []testCase{} 25 | if dsn := os.Getenv("MYSQL_DSN"); dsn != "" { 26 | tests = append(tests, testCase{ 27 | name: "mysql", 28 | args: args{ 29 | driver: "mysql", 30 | dsn: dsn, 31 | }, 32 | wantErr: false, 33 | }) 34 | } 35 | if dsn := os.Getenv("PG_DSN"); dsn != "" { 36 | tests = append(tests, testCase{ 37 | name: "postgres", 38 | args: args{ 39 | driver: "postgres", 40 | dsn: dsn, 41 | }, 42 | wantErr: false, 43 | }) 44 | } 45 | if len(tests) == 0 { 46 | t.Fatal("no test cases found") 47 | } 48 | for _, tt := range tests { 49 | t.Run(tt.name, func(t *testing.T) { 50 | deadline := time.Now().Add(time.Second * 30) 51 | ctx, cancel := context.WithDeadline(context.Background(), deadline) 52 | defer cancel() 53 | 54 | db, err := WithAnnotation(tt.args.driver, tt.args.dsn) 55 | if (err != nil) != tt.wantErr { 56 | t.Errorf("Adopt() error = %+v, wantErr %+v", err, tt.wantErr) 57 | return 58 | } 59 | 60 | interval := time.Millisecond * 200 61 | for { 62 | err := db.PingContext(ctx) 63 | if err == nil { 64 | break 65 | } 66 | if err == driver.ErrBadConn { 67 | nextTick := time.Now().Add(interval) 68 | if nextTick.After(deadline) { // whether nextTick overs deadline 69 | t.Error("PingContext failed") 70 | } 71 | time.Sleep(interval) 72 | interval = time.Duration(2 * float64(interval)) 73 | continue 74 | } 75 | if err != nil { 76 | t.Errorf("PingContext error = %+v", err) 77 | return 78 | } 79 | } 80 | 81 | if _, err := db.ExecContext(ctx, "select version()"); err != nil { 82 | t.Errorf("ExecContext error = %+v", err) 83 | return 84 | } 85 | }) 86 | } 87 | } 88 | --------------------------------------------------------------------------------