Support a separate database for Web UI tables

This commit is contained in:
bryanpedini committed 2026-11-21 20:36:03 +01:00
1 parent a2f8e59d9b
commit f464dc0af9
38 files changed
+880 -302

No files matched your search

+76 -23
View File
@@ -22,33 +22,76 @@ import (
)
func TestNewTableNames(t *testing.T) {
tn, err := newTableNames("dns_")
tn, err := newTableNames(tableLayout{recordsSchema: "dns", recordsPrefix: "dns_", webuiSchema: "dns-ui", webuiPrefix: "ui_"})
if err != nil {
t.Fatal(err)
}
want := tableNames{
prefix: "dns_",
records: "`dns_records`",
schema: "`dns_webui_schema`",
users: "`dns_webui_users`",
sessions: "`dns_webui_sessions`",
delegations: "`dns_webui_zone_delegations`",
audit: "`dns_webui_audit`",
locks: "`dns_webui_locks`",
recordsSchema: "dns",
recordsTable: "dns_records",
webuiSchema: "dns-ui",
webuiPrefix: "ui_",
records: "`dns`.`dns_records`",
schema: "`dns-ui`.`ui_webui_schema`",
users: "`dns-ui`.`ui_webui_users`",
sessions: "`dns-ui`.`ui_webui_sessions`",
delegations: "`dns-ui`.`ui_webui_zone_delegations`",
audit: "`dns-ui`.`ui_webui_audit`",
locks: "`dns-ui`.`ui_webui_locks`",
}
if tn != want {
t.Fatalf("got %+v", tn)
}
got := tn.sql("{records} {schema} {users} {sessions} {delegations} {audit} {locks} {other}")
if got != "`dns_records` `dns_webui_schema` `dns_webui_users` `dns_webui_sessions` `dns_webui_zone_delegations` `dns_webui_audit` `dns_webui_locks` {other}" {
if got != "`dns`.`dns_records` `dns-ui`.`ui_webui_schema` `dns-ui`.`ui_webui_users` `dns-ui`.`ui_webui_sessions` `dns-ui`.`ui_webui_zone_delegations` `dns-ui`.`ui_webui_audit` `dns-ui`.`ui_webui_locks` {other}" {
t.Fatalf("sql %s", got)
}
if _, err := newTableNames("bad`prefix"); err == nil {
t.Fatal("invalid prefix accepted")
valid := tableLayout{recordsSchema: "coredns", recordsPrefix: "coredns_", webuiSchema: "coredns", webuiPrefix: "coredns_"}
for _, change := range []func(*tableLayout){
func(l *tableLayout) { l.recordsPrefix = "bad`prefix" },
func(l *tableLayout) { l.webuiPrefix = "bad-prefix" },
func(l *tableLayout) { l.recordsSchema = "" },
func(l *tableLayout) { l.webuiSchema = "" },
func(l *tableLayout) { l.recordsSchema = "bad\x00db" },
func(l *tableLayout) { l.webuiSchema = "bad\ndb" },
func(l *tableLayout) { l.webuiSchema = strings.Repeat("d", 65) },
} {
l := valid
change(&l)
if _, err := newTableNames(l); err == nil {
t.Fatalf("invalid layout accepted: %+v", l)
}
}
empty, err := newTableNames("")
if err != nil || empty.records != "`records`" || empty.migrateLockName() != "webui_migrate" {
t.Fatalf("%+v %v", empty, err)
empty := valid
empty.recordsPrefix, empty.webuiPrefix = "", ""
tn, err = newTableNames(empty)
if err != nil || tn.records != "`coredns`.`records`" || tn.users != "`coredns`.`webui_users`" || tn.migrateLockName() != "webui_migrate" {
t.Fatalf("%+v %v", tn, err)
}
legacy := tableLayout{recordsSchema: "legacy `dns", webuiSchema: "legacy `dns"}
tn, err = newTableNames(legacy)
if err != nil || tn.records != "`legacy ``dns`.`records`" || tn.users != "`legacy ``dns`.`webui_users`" {
t.Fatalf("legacy layout %+v %v", tn, err)
}
}
func TestTableNamesFor(t *testing.T) {
cfg, err := loadConfig(cfgEnv(nil))
if err != nil {
t.Fatal(err)
}
tn, err := tableNamesFor(cfg)
if err != nil || tn.records != "`coredns`.`coredns_records`" || tn.users != "`coredns`.`coredns_webui_users`" || tn.locks != "`coredns`.`coredns_webui_locks`" {
t.Fatalf("shared layout %+v %v", tn, err)
}
cfg, err = loadConfig(cfgEnv(map[string]string{"DB_WEBUI_NAME": "dns_infra", "DB_WEBUI_TABLE_PREFIX": ""}))
if err != nil {
t.Fatal(err)
}
tn, err = tableNamesFor(cfg)
if err != nil || tn.records != "`coredns`.`coredns_records`" || tn.users != "`dns_infra`.`webui_users`" ||
!strings.HasPrefix(tn.zoneLockName("a."), "webui:") || tn.migrateLockName() != "webui_migrate" {
t.Fatalf("separate layout %+v %v", tn, err)
}
}
@@ -62,7 +105,7 @@ func TestLockNames(t *testing.T) {
if got := tn.zoneLockName("example.org."); got != want || len(got) != len("coredns_webui:")+16 {
t.Fatalf("got %s want %s", got, want)
}
long, _ := newTableNames(strings.Repeat("p", 32))
long, _ := newTableNames(tableLayout{recordsSchema: "coredns", recordsPrefix: "coredns_", webuiSchema: "webui", webuiPrefix: strings.Repeat("p", 32)})
if n := len(long.zoneLockName("x.")); n > 64 {
t.Fatalf("lock name too long: %d", n)
}
@@ -91,6 +134,16 @@ func TestMySQLConfig(t *testing.T) {
}
}
func TestMySQLConfigUsesWebUIDatabase(t *testing.T) {
cfg, err := loadConfig(cfgEnv(map[string]string{"DB_WEBUI_NAME": "dns_infra"}))
if err != nil {
t.Fatal(err)
}
if mc := mysqlConfig(cfg); mc.DBName != "dns_infra" || cfg.DBName != "coredns" {
t.Fatalf("connection database %q, records database %q", mc.DBName, cfg.DBName)
}
}
func TestOpenDatabase(t *testing.T) {
cfg, _ := loadConfig(cfgEnv(nil))
var buf bytes.Buffer
@@ -228,7 +281,7 @@ func validColumns() map[string]columnInfo {
}
func TestCheckRecordsColumns(t *testing.T) {
if err := checkRecordsColumns("coredns_records", validColumns()); err != nil {
if err := checkRecordsColumns("coredns", "coredns_records", validColumns()); err != nil {
t.Fatal(err)
}
for _, alt := range []map[string]columnInfo{
@@ -239,17 +292,17 @@ func TestCheckRecordsColumns(t *testing.T) {
{"id": column("int"), "zone": column("longtext"), "name": column("text"), "ttl": column("bigint"),
"content": column("mediumtext"), "record_type": column("mediumtext")},
} {
if err := checkRecordsColumns("coredns_records", alt); err != nil {
if err := checkRecordsColumns("coredns", "coredns_records", alt); err != nil {
t.Fatal(err)
}
}
if err := checkRecordsColumns("coredns_records", map[string]columnInfo{}); !errors.Is(err, errContract) || !strings.Contains(err.Error(), "coredns_records does not exist") {
if err := checkRecordsColumns("coredns", "coredns_records", map[string]columnInfo{}); !errors.Is(err, errContract) || !strings.Contains(err.Error(), "coredns_records does not exist") {
t.Fatalf("got %v", err)
}
for _, col := range []string{"id", "zone", "name", "ttl", "content", "record_type"} {
cols := validColumns()
delete(cols, col)
err := checkRecordsColumns("coredns_records", cols)
err := checkRecordsColumns("coredns", "coredns_records", cols)
if !errors.Is(err, errContract) || !strings.Contains(err.Error(), "has no column "+col) {
t.Fatalf("%s: %v", col, err)
}
@@ -279,7 +332,7 @@ func TestCheckRecordsColumns(t *testing.T) {
} {
cols := validColumns()
cols[c.col] = column(c.typ)
err := checkRecordsColumns("coredns_records", cols)
err := checkRecordsColumns("coredns", "coredns_records", cols)
want := "column " + c.col + " of table coredns_records has incompatible type " + c.typ + ", it must be " + c.requirement
if !errors.Is(err, errContract) || !strings.Contains(err.Error(), want) {
t.Fatalf("%s %s: %v", c.col, c.typ, err)
@@ -287,7 +340,7 @@ func TestCheckRecordsColumns(t *testing.T) {
}
cols := validColumns()
cols["id"] = columnInfo{dataType: "bigint"}
if err := checkRecordsColumns("coredns_records", cols); err == nil || !strings.Contains(err.Error(), "incompatible type bigint,") {
if err := checkRecordsColumns("coredns", "coredns_records", cols); err == nil || !strings.Contains(err.Error(), "incompatible type bigint,") {
t.Fatal(err)
}
}
@@ -301,7 +354,7 @@ func TestVerifyRecordsContract(t *testing.T) {
t.Fatal(err)
}
calls := st.Calls()
if len(calls) != 1 || calls[0].args[0] != "coredns_records" || !strings.Contains(calls[0].query, "TABLE_SCHEMA = DATABASE()") {
if len(calls) != 1 || calls[0].args[0] != "coredns" || calls[0].args[1] != "coredns_records" || !strings.Contains(calls[0].query, "TABLE_SCHEMA = ? AND") {
t.Fatalf("calls %+v", calls)
}
db, _ = newFakeDB(t, fdbRules(fdbRule{"COLUMNS", fdbResponse{rows: [][]driver.Value{{"id", "int", "int", nil}}}}))