Support a separate database for Web UI tables
This commit is contained in:
1 parent
a2f8e59d9b
commit
f464dc0af9
38 files changed
+880
-302
No files matched your search
+83
-13
@@ -88,6 +88,8 @@ type itDatabase struct {
|
||||
server itServer
|
||||
name string
|
||||
prefix string
|
||||
webui string
|
||||
webuiPrefix string
|
||||
appUser string
|
||||
appPassword string
|
||||
dnsUser string
|
||||
@@ -98,17 +100,24 @@ type itDatabase struct {
|
||||
// InnoDB on older servers only refreshes its lock metadata after 100 ms of idle time.
|
||||
const itLockPollInterval = 150 * time.Millisecond
|
||||
|
||||
func (d *itDatabase) table(suffix string) (string, string) {
|
||||
if suffix == "records" {
|
||||
return d.name, d.prefix + suffix
|
||||
}
|
||||
return d.webui, d.webuiPrefix + suffix
|
||||
}
|
||||
|
||||
func (d *itDatabase) rowLockWaiting(ctx context.Context, suffix string) (bool, error) {
|
||||
table := d.prefix + suffix
|
||||
schema, table := d.table(suffix)
|
||||
var waiting bool
|
||||
err := d.root.QueryRowContext(ctx, "SELECT EXISTS (SELECT 1 FROM performance_schema.data_lock_waits w "+
|
||||
"JOIN performance_schema.data_locks l ON l.ENGINE = w.ENGINE AND l.ENGINE_LOCK_ID = w.REQUESTING_ENGINE_LOCK_ID "+
|
||||
"WHERE l.OBJECT_SCHEMA = ? AND l.OBJECT_NAME = ?)", d.name, table).Scan(&waiting)
|
||||
"WHERE l.OBJECT_SCHEMA = ? AND l.OBJECT_NAME = ?)", schema, table).Scan(&waiting)
|
||||
if mysqlErrNumber(err) == 1146 {
|
||||
// MySQL 5.7 and MariaDB expose the same wait through InnoDB metadata.
|
||||
err = d.root.QueryRowContext(ctx, "SELECT EXISTS (SELECT 1 FROM information_schema.INNODB_LOCK_WAITS w "+
|
||||
"JOIN information_schema.INNODB_LOCKS l ON l.LOCK_ID = w.REQUESTED_LOCK_ID "+
|
||||
"WHERE l.LOCK_TABLE = ?)", "`"+d.name+"`.`"+table+"`").Scan(&waiting)
|
||||
"WHERE l.LOCK_TABLE = ?)", qualifiedIdent(schema, table)).Scan(&waiting)
|
||||
}
|
||||
return waiting, err
|
||||
}
|
||||
@@ -116,7 +125,7 @@ func (d *itDatabase) rowLockWaiting(ctx context.Context, suffix string) (bool, e
|
||||
func (d *itDatabase) hasCheckConstraint(t *testing.T, suffix string) bool {
|
||||
t.Helper()
|
||||
var tableName, ddl string
|
||||
if err := d.root.QueryRow("SHOW CREATE TABLE `"+d.prefix+suffix+"`").Scan(&tableName, &ddl); err != nil {
|
||||
if err := d.root.QueryRow("SHOW CREATE TABLE "+qualifiedIdent(d.table(suffix))).Scan(&tableName, &ddl); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// MySQL 5.7 parses CHECK constraints and omits them from the table.
|
||||
@@ -124,9 +133,27 @@ func (d *itDatabase) hasCheckConstraint(t *testing.T, suffix string) bool {
|
||||
}
|
||||
|
||||
type itOptions struct {
|
||||
prefix string
|
||||
recordDDL string
|
||||
noRecords bool
|
||||
prefix string
|
||||
recordDDL string
|
||||
noRecords bool
|
||||
layout string
|
||||
webuiPrefix *string
|
||||
webuiGrants []string
|
||||
}
|
||||
|
||||
const (
|
||||
itLayoutShared = "shared"
|
||||
itLayoutSeparate = "separate"
|
||||
)
|
||||
|
||||
func itLayout(opts itOptions) string {
|
||||
if opts.layout != "" {
|
||||
return opts.layout
|
||||
}
|
||||
if os.Getenv("IT_WEBUI_LAYOUT") == itLayoutSeparate {
|
||||
return itLayoutSeparate
|
||||
}
|
||||
return itLayoutShared
|
||||
}
|
||||
|
||||
func newITDatabase(t *testing.T, opts itOptions) *itDatabase {
|
||||
@@ -137,11 +164,19 @@ func newITDatabase(t *testing.T, opts itOptions) *itDatabase {
|
||||
server: srv,
|
||||
name: "it_" + id,
|
||||
prefix: opts.prefix,
|
||||
webui: "it_" + id,
|
||||
webuiPrefix: opts.prefix,
|
||||
appUser: "it_" + id + "_app",
|
||||
appPassword: itRandom(12),
|
||||
dnsUser: "it_" + id + "_dns",
|
||||
dnsPassword: itRandom(12),
|
||||
}
|
||||
if itLayout(opts) == itLayoutSeparate {
|
||||
d.webui = "it_" + id + "_webui"
|
||||
}
|
||||
if opts.webuiPrefix != nil {
|
||||
d.webuiPrefix = *opts.webuiPrefix
|
||||
}
|
||||
admin := srv.open(t, "root", srv.rootPassword, "")
|
||||
ctx := context.Background()
|
||||
exec := func(operation, query string) {
|
||||
@@ -151,7 +186,11 @@ func newITDatabase(t *testing.T, opts itOptions) *itDatabase {
|
||||
}
|
||||
}
|
||||
exec("create database", "CREATE DATABASE `"+d.name+"` DEFAULT CHARSET = utf8mb4")
|
||||
if d.webui != d.name {
|
||||
exec("create webui database", "CREATE DATABASE `"+d.webui+"` DEFAULT CHARSET = utf8mb4")
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_, _ = admin.ExecContext(ctx, "DROP DATABASE IF EXISTS `"+d.webui+"`")
|
||||
_, _ = admin.ExecContext(ctx, "DROP DATABASE IF EXISTS `"+d.name+"`")
|
||||
_, _ = admin.ExecContext(ctx, "DROP USER IF EXISTS '"+d.appUser+"'@'%'")
|
||||
_, _ = admin.ExecContext(ctx, "DROP USER IF EXISTS '"+d.dnsUser+"'@'%'")
|
||||
@@ -166,10 +205,13 @@ func newITDatabase(t *testing.T, opts itOptions) *itDatabase {
|
||||
exec("create application user", "CREATE USER '"+d.appUser+"'@'%' IDENTIFIED BY '"+d.appPassword+"'")
|
||||
exec("create dns user", "CREATE USER '"+d.dnsUser+"'@'%' IDENTIFIED BY '"+d.dnsPassword+"'")
|
||||
for _, g := range itAppGrants {
|
||||
if !g.create && opts.noRecords {
|
||||
if !g.create && opts.noRecords || g.create && opts.webuiGrants != nil {
|
||||
continue
|
||||
}
|
||||
exec("grant application privileges on "+g.table, fmt.Sprintf("GRANT %s ON `%s`.`%s%s` TO '%s'@'%%'", g.privs, d.name, d.prefix, g.table, d.appUser))
|
||||
exec("grant application privileges on "+g.table, fmt.Sprintf("GRANT %s ON %s TO '%s'@'%%'", g.privs, qualifiedIdent(d.table(g.table)), d.appUser))
|
||||
}
|
||||
for _, grant := range opts.webuiGrants {
|
||||
exec("grant application privileges on the webui database", fmt.Sprintf("GRANT %s ON `%s`.* TO '%s'@'%%'", grant, d.webui, d.appUser))
|
||||
}
|
||||
if !opts.noRecords {
|
||||
exec("grant dns read privileges", fmt.Sprintf("GRANT SELECT ON `%s`.`%srecords` TO '%s'@'%%'", d.name, d.prefix, d.dnsUser))
|
||||
@@ -198,7 +240,7 @@ func TestIntegrationSetupErrorRedactsSQL(t *testing.T) {
|
||||
}
|
||||
|
||||
func (d *itDatabase) env() map[string]string {
|
||||
return map[string]string{
|
||||
env := map[string]string{
|
||||
"DB_HOST": d.server.host,
|
||||
"DB_PORT": strconv.Itoa(d.server.port),
|
||||
"DB_USER": d.appUser,
|
||||
@@ -207,6 +249,13 @@ func (d *itDatabase) env() map[string]string {
|
||||
"DB_TABLE_PREFIX": d.prefix,
|
||||
"LOG_LEVEL": "ERROR",
|
||||
}
|
||||
if d.webui != d.name {
|
||||
env["DB_WEBUI_NAME"] = d.webui
|
||||
}
|
||||
if d.webuiPrefix != d.prefix {
|
||||
env["DB_WEBUI_TABLE_PREFIX"] = d.webuiPrefix
|
||||
}
|
||||
return env
|
||||
}
|
||||
|
||||
func (d *itDatabase) config(t *testing.T) Config {
|
||||
@@ -221,13 +270,34 @@ func (d *itDatabase) config(t *testing.T) Config {
|
||||
|
||||
func (d *itDatabase) tables(t *testing.T) tableNames {
|
||||
t.Helper()
|
||||
tn, err := newTableNames(d.prefix)
|
||||
tn, err := d.tableNames()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return tn
|
||||
}
|
||||
|
||||
func (d *itDatabase) tableNames() (tableNames, error) {
|
||||
return newTableNames(tableLayout{recordsSchema: d.name, recordsPrefix: d.prefix, webuiSchema: d.webui, webuiPrefix: d.webuiPrefix})
|
||||
}
|
||||
|
||||
func (d *itDatabase) migrations(t *testing.T) []migration {
|
||||
t.Helper()
|
||||
ms, err := loadMigrations(migrationFiles, d.webui, d.webuiPrefix)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
|
||||
func (d *itDatabase) sql(query string) string {
|
||||
tn, err := d.tableNames()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return tn.sql(query)
|
||||
}
|
||||
|
||||
func (d *itDatabase) openApp(t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
db, err := openDatabase(d.config(t), discardLogger())
|
||||
@@ -262,7 +332,7 @@ func (d *itDatabase) migrated(t *testing.T) (*sql.DB, tableNames) {
|
||||
func (d *itDatabase) count(t *testing.T, query string, args ...any) int {
|
||||
t.Helper()
|
||||
var n int
|
||||
if err := d.root.QueryRow(query, args...).Scan(&n); err != nil {
|
||||
if err := d.root.QueryRow(d.sql(query), args...).Scan(&n); err != nil {
|
||||
t.Fatalf("%s: %v", query, err)
|
||||
}
|
||||
return n
|
||||
@@ -270,7 +340,7 @@ func (d *itDatabase) count(t *testing.T, query string, args ...any) int {
|
||||
|
||||
func (d *itDatabase) rootExec(t *testing.T, query string, args ...any) sql.Result {
|
||||
t.Helper()
|
||||
res, err := d.root.Exec(query, args...)
|
||||
res, err := d.root.Exec(d.sql(query), args...)
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", query, err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user