Skip to content

Commit b38077e

Browse files
Roland Bracewell Shoemakerjsha
authored andcommitted
Change prefixdb semantics (letsencrypt#2674)
Instead of executing the prefix for every statement only do it when creating the connection. Leaves most of the existing naming conventions alone but updates the relevant comments to reflect setting variables is now connection level instead of statement. Fixes letsencrypt#2673.
1 parent d849f58 commit b38077e

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

prefixdb/db.go

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package prefixdb
33
import "database/sql/driver"
44

55
// New clones a database driver to create a new driver with the property that
6-
// every statement executed will have the given prefix prepended.
7-
// This is useful, for instance, to set statement-level variables like
6+
// every connection created will have a query executed before it is used.
7+
// This is useful, for instance, to set connection-level variables like
88
// max_statement_time and long_query_time.
99
func New(prefix string, underlying driver.Driver) driver.Driver {
1010
return &prefixedDB{
@@ -23,25 +23,13 @@ func (p *prefixedDB) Open(name string) (driver.Conn, error) {
2323
if err != nil {
2424
return nil, err
2525
}
26-
return &prefixedConn{
27-
prefix: p.prefix,
28-
conn: conn,
29-
}, nil
30-
}
31-
32-
type prefixedConn struct {
33-
prefix string
34-
conn driver.Conn
35-
}
36-
37-
func (c *prefixedConn) Prepare(query string) (driver.Stmt, error) {
38-
return c.conn.Prepare(c.prefix + " " + query)
39-
}
40-
41-
func (c *prefixedConn) Close() error {
42-
return c.conn.Close()
43-
}
44-
45-
func (c *prefixedConn) Begin() (driver.Tx, error) {
46-
return c.conn.Begin()
26+
stmt, err := conn.Prepare(p.prefix)
27+
if err != nil {
28+
return nil, err
29+
}
30+
_, err = stmt.Exec(nil)
31+
if err != nil {
32+
return nil, err
33+
}
34+
return conn, nil
4735
}

prefixdb/db_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func TestPrefixing(t *testing.T) {
15-
sql.Register("prefixedmysql", New("SET STATEMENT max_statement_time=0.1 FOR", mysql.MySQLDriver{}))
15+
sql.Register("prefixedmysql", New("SET SESSION max_statement_time=0.1", mysql.MySQLDriver{}))
1616
db, err := sql.Open("prefixedmysql", vars.DBConnSA)
1717
if err != nil {
1818
log.Fatal(err)

sa/database.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func NewDbMapFromConfig(config *mysql.Config, maxOpenConns int) (*gorp.DbMap, er
5959
adjustMySQLConfig(config)
6060

6161
// We always want strict mode. Rather than leaving this up to DB config, we
62-
// prefix each statement with it.
63-
prefix := "SET STATEMENT sql_mode='STRICT_ALL_TABLES' FOR "
62+
// prefix each session with it.
63+
prefix := "SET SESSION sql_mode='STRICT_ALL_TABLES'"
6464

6565
// If a read timeout is set, we set max_statement_time to 95% of that, and
6666
// long_query_time to 80% of that. That way we get logs of queries that are
@@ -72,7 +72,7 @@ func NewDbMapFromConfig(config *mysql.Config, maxOpenConns int) (*gorp.DbMap, er
7272
// Note: in MySQL (which we don't use), max_statement_time is millis.
7373
readTimeout := config.ReadTimeout.Seconds()
7474
prefix = fmt.Sprintf(
75-
"SET STATEMENT max_statement_time=%g, long_query_time=%g, sql_mode='STRICT_ALL_TABLES' FOR ",
75+
"SET SESSION max_statement_time=%g, long_query_time=%g, sql_mode='STRICT_ALL_TABLES'",
7676
readTimeout*0.95, readTimeout*0.80)
7777
}
7878

0 commit comments

Comments
 (0)