| 1 |
package tests_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"database/sql" |
| 6 |
"os" |
| 7 |
"reflect" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"gorm.io/driver/mysql" |
| 11 |
"gorm.io/gorm" |
| 12 |
. "gorm.io/gorm/utils/tests" |
| 13 |
) |
| 14 |
|
| 15 |
type wrapperTx struct { |
| 16 |
*sql.Tx |
| 17 |
conn *wrapperConnPool |
| 18 |
} |
| 19 |
|
| 20 |
func (c *wrapperTx) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) { |
| 21 |
c.conn.got = append(c.conn.got, query) |
| 22 |
return c.Tx.PrepareContext(ctx, query) |
| 23 |
} |
| 24 |
|
| 25 |
func (c *wrapperTx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) { |
| 26 |
c.conn.got = append(c.conn.got, query) |
| 27 |
return c.Tx.ExecContext(ctx, query, args...) |
| 28 |
} |
| 29 |
|
| 30 |
func (c *wrapperTx) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) { |
| 31 |
c.conn.got = append(c.conn.got, query) |
| 32 |
return c.Tx.QueryContext(ctx, query, args...) |
| 33 |
} |
| 34 |
|
| 35 |
func (c *wrapperTx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row { |
| 36 |
c.conn.got = append(c.conn.got, query) |
| 37 |
return c.Tx.QueryRowContext(ctx, query, args...) |
| 38 |
} |
| 39 |
|
| 40 |
type wrapperConnPool struct { |
| 41 |
db *sql.DB |
| 42 |
got []string |
| 43 |
expect []string |
| 44 |
} |
| 45 |
|
| 46 |
func (c *wrapperConnPool) Ping() error { |
| 47 |
return c.db.Ping() |
| 48 |
} |
| 49 |
|
| 50 |
// If you use BeginTx returned *sql.Tx as shown below then you can't record queries in a transaction. |
| 51 |
// |
| 52 |
// func (c *wrapperConnPool) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) { |
| 53 |
// return c.db.BeginTx(ctx, opts) |
| 54 |
// } |
| 55 |
// |
| 56 |
// You should use BeginTx returned gorm.Tx which could wrap *sql.Tx then you can record all queries. |
| 57 |
func (c *wrapperConnPool) BeginTx(ctx context.Context, opts *sql.TxOptions) (gorm.ConnPool, error) { |
| 58 |
tx, err := c.db.BeginTx(ctx, opts) |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
return &wrapperTx{Tx: tx, conn: c}, nil |
| 63 |
} |
| 64 |
|
| 65 |
func (c *wrapperConnPool) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) { |
| 66 |
c.got = append(c.got, query) |
| 67 |
return c.db.PrepareContext(ctx, query) |
| 68 |
} |
| 69 |
|
| 70 |
func (c *wrapperConnPool) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) { |
| 71 |
c.got = append(c.got, query) |
| 72 |
return c.db.ExecContext(ctx, query, args...) |
| 73 |
} |
| 74 |
|
| 75 |
func (c *wrapperConnPool) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) { |
| 76 |
c.got = append(c.got, query) |
| 77 |
return c.db.QueryContext(ctx, query, args...) |
| 78 |
} |
| 79 |
|
| 80 |
func (c *wrapperConnPool) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row { |
| 81 |
c.got = append(c.got, query) |
| 82 |
return c.db.QueryRowContext(ctx, query, args...) |
| 83 |
} |
| 84 |
|
| 85 |
func TestConnPoolWrapper(t *testing.T) { |
| 86 |
dialect := os.Getenv("GORM_DIALECT") |
| 87 |
if dialect != "mysql" { |
| 88 |
t.SkipNow() |
| 89 |
} |
| 90 |
|
| 91 |
dbDSN := os.Getenv("GORM_DSN") |
| 92 |
if dbDSN == "" { |
| 93 |
dbDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local" |
| 94 |
} |
| 95 |
nativeDB, err := sql.Open("mysql", dbDSN) |
| 96 |
if err != nil { |
| 97 |
t.Fatalf("Should open db success, but got %v", err) |
| 98 |
} |
| 99 |
|
| 100 |
conn := &wrapperConnPool{ |
| 101 |
db: nativeDB, |
| 102 |
expect: []string{ |
| 103 |
"SELECT VERSION()", |
| 104 |
"INSERT INTO `users` (`created_at`,`updated_at`,`deleted_at`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`) VALUES (?,?,?,?,?,?,?,?,?)", |
| 105 |
"SELECT * FROM `users` WHERE name = ? AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT ?", |
| 106 |
"INSERT INTO `users` (`created_at`,`updated_at`,`deleted_at`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`) VALUES (?,?,?,?,?,?,?,?,?)", |
| 107 |
"SELECT * FROM `users` WHERE name = ? AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT ?", |
| 108 |
"SELECT * FROM `users` WHERE name = ? AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT ?", |
| 109 |
"INSERT INTO `users` (`created_at`,`updated_at`,`deleted_at`,`name`,`age`,`birthday`,`company_id`,`manager_id`,`active`) VALUES (?,?,?,?,?,?,?,?,?)", |
| 110 |
"SELECT * FROM `users` WHERE name = ? AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT ?", |
| 111 |
"SELECT * FROM `users` WHERE name = ? AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT ?", |
| 112 |
}, |
| 113 |
} |
| 114 |
|
| 115 |
defer func() { |
| 116 |
if !reflect.DeepEqual(conn.got, conn.expect) { |
| 117 |
t.Errorf("expect %#v but got %#v", conn.expect, conn.got) |
| 118 |
} |
| 119 |
}() |
| 120 |
|
| 121 |
db, err := gorm.Open(mysql.New(mysql.Config{Conn: conn, DisableWithReturning: true})) |
| 122 |
db.Logger = DB.Logger |
| 123 |
if err != nil { |
| 124 |
t.Fatalf("Should open db success, but got %v", err) |
| 125 |
} |
| 126 |
|
| 127 |
tx := db.Begin() |
| 128 |
user := *GetUser("transaction", Config{}) |
| 129 |
|
| 130 |
if err = tx.Save(&user).Error; err != nil { |
| 131 |
t.Fatalf("No error should raise, but got %v", err) |
| 132 |
} |
| 133 |
|
| 134 |
if err = tx.First(&User{}, "name = ?", "transaction").Error; err != nil { |
| 135 |
t.Fatalf("Should find saved record, but got %v", err) |
| 136 |
} |
| 137 |
|
| 138 |
user1 := *GetUser("transaction1-1", Config{}) |
| 139 |
|
| 140 |
if err = tx.Save(&user1).Error; err != nil { |
| 141 |
t.Fatalf("No error should raise, but got %v", err) |
| 142 |
} |
| 143 |
|
| 144 |
if err = tx.First(&User{}, "name = ?", user1.Name).Error; err != nil { |
| 145 |
t.Fatalf("Should find saved record, but got %v", err) |
| 146 |
} |
| 147 |
|
| 148 |
if sqlTx, ok := tx.Statement.ConnPool.(gorm.TxCommitter); !ok || sqlTx == nil { |
| 149 |
t.Fatalf("Should return the underlying sql.Tx") |
| 150 |
} |
| 151 |
|
| 152 |
tx.Rollback() |
| 153 |
|
| 154 |
if err = db.First(&User{}, "name = ?", "transaction").Error; err == nil { |
| 155 |
t.Fatalf("Should not find record after rollback, but got %v", err) |
| 156 |
} |
| 157 |
|
| 158 |
txDB := db.Where("fake_name = ?", "fake_name") |
| 159 |
tx2 := txDB.Session(&gorm.Session{NewDB: true}).Begin() |
| 160 |
user2 := *GetUser("transaction-2", Config{}) |
| 161 |
if err = tx2.Save(&user2).Error; err != nil { |
| 162 |
t.Fatalf("No error should raise, but got %v", err) |
| 163 |
} |
| 164 |
|
| 165 |
if err = tx2.First(&User{}, "name = ?", "transaction-2").Error; err != nil { |
| 166 |
t.Fatalf("Should find saved record, but got %v", err) |
| 167 |
} |
| 168 |
|
| 169 |
tx2.Commit() |
| 170 |
|
| 171 |
if err = db.First(&User{}, "name = ?", "transaction-2").Error; err != nil { |
| 172 |
t.Fatalf("Should be able to find committed record, but got %v", err) |
| 173 |
} |
| 174 |
} |