Skip to content

Commit 7e78a8a

Browse files
author
James Cor
committed
testing
1 parent 7b4c45d commit 7e78a8a

File tree

4 files changed

+69
-54
lines changed

4 files changed

+69
-54
lines changed

enginetest/enginetests.go

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import (
3939
"github.com/dolthub/go-mysql-server/sql/expression"
4040
"github.com/dolthub/go-mysql-server/sql/expression/function/aggregation"
4141
"github.com/dolthub/go-mysql-server/sql/mysql_db"
42-
"github.com/dolthub/go-mysql-server/sql/mysql_db/serial"
4342
"github.com/dolthub/go-mysql-server/sql/parse"
4443
"github.com/dolthub/go-mysql-server/sql/plan"
4544
"github.com/dolthub/go-mysql-server/sql/transform"
@@ -1014,7 +1013,7 @@ func TestUserPrivileges(t *testing.T, h Harness) {
10141013
Address: "localhost",
10151014
})
10161015
engine.Analyzer.Catalog.MySQLDb.AddRootAccount()
1017-
engine.Analyzer.Catalog.MySQLDb.CanPersist = true
1016+
engine.Analyzer.Catalog.MySQLDb.SetPersister(&mysql_db.NoopPersister{})
10181017

10191018
for _, statement := range script.SetUpScript {
10201019
if sh, ok := harness.(SkippingHarness); ok {
@@ -1074,7 +1073,7 @@ func TestUserPrivileges(t *testing.T, h Harness) {
10741073
defer engine.Close()
10751074

10761075
engine.Analyzer.Catalog.MySQLDb.AddRootAccount()
1077-
engine.Analyzer.Catalog.MySQLDb.CanPersist = true
1076+
engine.Analyzer.Catalog.MySQLDb.SetPersister(mysql_db.NoopPersister{})
10781077
rootCtx := harness.NewContextWithClient(sql.Client{
10791078
User: "root",
10801079
Address: "localhost",
@@ -1175,7 +1174,7 @@ func TestUserAuthentication(t *testing.T, h Harness) {
11751174
engine := mustNewEngine(t, harness)
11761175
defer engine.Close()
11771176
engine.Analyzer.Catalog.MySQLDb.AddRootAccount()
1178-
engine.Analyzer.Catalog.MySQLDb.CanPersist = true
1177+
engine.Analyzer.Catalog.MySQLDb.SetPersister(&mysql_db.NoopPersister{})
11791178
if script.SetUpFunc != nil {
11801179
script.SetUpFunc(ctx, t, engine)
11811180
}
@@ -5401,47 +5400,47 @@ func TestPrivilegePersistence(t *testing.T, h Harness) {
54015400
engine := mustNewEngine(t, harness)
54025401
defer engine.Close()
54035402
engine.Analyzer.Catalog.MySQLDb.AddRootAccount()
5403+
engine.Analyzer.Catalog.MySQLDb.SetPersister(&mysql_db.NoopPersister{})
54045404
ctx := NewContextWithClient(harness, sql.Client{
54055405
User: "root",
54065406
Address: "localhost",
54075407
})
5408-
engine.Analyzer.Catalog.MySQLDb.CanPersist = true
54095408

54105409
var users []*mysql_db.User
54115410
var roles []*mysql_db.RoleEdge
5412-
engine.Analyzer.Catalog.MySQLDb.SetPersistCallback(
5413-
func(ctx *sql.Context, buf []byte) error {
5414-
// erase everything from users and roles
5415-
users = make([]*mysql_db.User, 0)
5416-
roles = make([]*mysql_db.RoleEdge, 0)
5417-
5418-
// Deserialize the flatbuffer
5419-
serialMySQLDb := serial.GetRootAsMySQLDb(buf, 0)
5420-
5421-
// Fill in users
5422-
for i := 0; i < serialMySQLDb.UserLength(); i++ {
5423-
serialUser := new(serial.User)
5424-
if !serialMySQLDb.User(serialUser, i) {
5425-
continue
5426-
}
5427-
user := mysql_db.LoadUser(serialUser)
5428-
users = append(users, user)
5429-
}
5430-
5431-
// Fill in roles
5432-
for i := 0; i < serialMySQLDb.RoleEdgesLength(); i++ {
5433-
serialRoleEdge := new(serial.RoleEdge)
5434-
if !serialMySQLDb.RoleEdges(serialRoleEdge, i) {
5435-
continue
5436-
}
5437-
role := mysql_db.LoadRoleEdge(serialRoleEdge)
5438-
roles = append(roles, role)
5439-
}
5440-
return nil
5441-
},
5442-
)
54435411

5444-
engine.Analyzer.Catalog.MySQLDb.CanPersist = true
5412+
// TODO: do I need this?
5413+
//engine.Analyzer.Catalog.MySQLDb.SetPersistCallback(
5414+
// func(ctx *sql.Context, buf []byte) error {
5415+
// // erase everything from users and roles
5416+
// users = make([]*mysql_db.User, 0)
5417+
// roles = make([]*mysql_db.RoleEdge, 0)
5418+
//
5419+
// // Deserialize the flatbuffer
5420+
// serialMySQLDb := serial.GetRootAsMySQLDb(buf, 0)
5421+
//
5422+
// // Fill in users
5423+
// for i := 0; i < serialMySQLDb.UserLength(); i++ {
5424+
// serialUser := new(serial.User)
5425+
// if !serialMySQLDb.User(serialUser, i) {
5426+
// continue
5427+
// }
5428+
// user := mysql_db.LoadUser(serialUser)
5429+
// users = append(users, user)
5430+
// }
5431+
//
5432+
// // Fill in roles
5433+
// for i := 0; i < serialMySQLDb.RoleEdgesLength(); i++ {
5434+
// serialRoleEdge := new(serial.RoleEdge)
5435+
// if !serialMySQLDb.RoleEdges(serialRoleEdge, i) {
5436+
// continue
5437+
// }
5438+
// role := mysql_db.LoadRoleEdge(serialRoleEdge)
5439+
// roles = append(roles, role)
5440+
// }
5441+
// return nil
5442+
// },
5443+
//)
54455444

54465445
RunQueryWithContext(t, engine, harness, ctx, "CREATE USER tester@localhost")
54475446
// If the user exists in []*mysql_db.User, then it must be NOT nil.

sql/mysql_db/mysql_db.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,30 @@ import (
3030
"github.com/dolthub/go-mysql-server/sql/mysql_db/serial"
3131
)
3232

33-
// PersistCallback represents the callback that will be called when the Grant Tables have been updated and need to be
34-
// persisted.
35-
type PersistCallback func(ctx *sql.Context, data []byte) error
33+
// MySQLDbPersistence is used to determine the behavior of how certain tables in MySQLDb will be persisted.
34+
type MySQLDbPersistence interface {
35+
CanPersist() bool
36+
Persist(ctx *sql.Context, data []byte) error
37+
}
38+
39+
// NoopPersister is used when nothing in mysql db should be persisted
40+
type NoopPersister struct{}
41+
42+
var _ MySQLDbPersistence = NoopPersister{}
43+
44+
// CanPersist implements the MySQLDbPersistence interface
45+
func (p NoopPersister) CanPersist() bool {
46+
return true
47+
}
48+
49+
// Persist implements the MySQLDbPersistence interface
50+
func (p NoopPersister) Persist(ctx *sql.Context, data []byte) error {
51+
return nil
52+
}
3653

3754
// MySQLDb are the collection of tables that are in the MySQL database
3855
type MySQLDb struct {
39-
Enabled bool
40-
CanPersist bool
56+
Enabled bool
4157

4258
user *mysqlTable
4359
role_edges *mysqlTable
@@ -51,7 +67,7 @@ type MySQLDb struct {
5167
//default_roles *mysqlTable
5268
//password_history *mysqlTable
5369

54-
persistFunc PersistCallback
70+
persister MySQLDbPersistence
5571
}
5672

5773
var _ sql.Database = (*MySQLDb)(nil)
@@ -137,9 +153,9 @@ func (t *MySQLDb) LoadData(ctx *sql.Context, buf []byte) error {
137153
return nil
138154
}
139155

140-
// SetPersistCallback sets the callback to be used when the MySQL Db tables have been updated and need to be persisted.
141-
func (t *MySQLDb) SetPersistCallback(persistFunc PersistCallback) {
142-
t.persistFunc = persistFunc
156+
// SetPersister sets the custom persister to be used when the MySQL Db tables have been updated and need to be persisted.
157+
func (t *MySQLDb) SetPersister(persister MySQLDbPersistence) {
158+
t.persister = persister
143159
}
144160

145161
// AddRootAccount adds the root account to the list of accounts.
@@ -332,13 +348,13 @@ func (t *MySQLDb) Negotiate(c *mysql.Conn, user string, addr net.Addr) (mysql.Ge
332348
return nil, fmt.Errorf(`the only user login interface currently supported is "mysql_native_password"`)
333349
}
334350

351+
// CanPersist calls the persister's CanPersist method
352+
func (t *MySQLDb) CanPersist() bool {
353+
return t.persister.CanPersist()
354+
}
355+
335356
// Persist passes along all changes to the integrator.
336357
func (t *MySQLDb) Persist(ctx *sql.Context) error {
337-
// Do nothing if persist function is nil
338-
if t.persistFunc == nil {
339-
return nil
340-
}
341-
342358
// Extract all user entries from table, and sort
343359
userEntries := t.user.data.ToSlice(ctx)
344360
users := make([]*User, len(userEntries))
@@ -388,7 +404,7 @@ func (t *MySQLDb) Persist(ctx *sql.Context) error {
388404
b.Finish(mysqlDbOffset)
389405

390406
// Persist data
391-
return t.persistFunc(ctx, b.FinishedBytes())
407+
return t.persister.Persist(ctx, b.FinishedBytes())
392408
}
393409

394410
// UserTable returns the "user" table.

sql/plan/create_role.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (n *CreateRole) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error)
108108
}
109109

110110
// Check if you can even persist in the first place
111-
if !mysqlDb.CanPersist {
111+
if !mysqlDb.CanPersist() {
112112
return nil, fmt.Errorf("no privilege file specified, to persist users/grants run with --privilege-file=<file_path>")
113113
}
114114

sql/plan/create_user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (n *CreateUser) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error)
101101
return nil, sql.ErrDatabaseNotFound.New("mysql")
102102
}
103103
// Check if you can even persist in the first place
104-
if !mysqlDb.CanPersist {
104+
if !mysqlDb.CanPersist() {
105105
return nil, fmt.Errorf("no privilege file specified, to persist users/grants run with --privilege-file=<file_path>")
106106
}
107107
userTableData := mysqlDb.UserTable().Data()

0 commit comments

Comments
 (0)