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
+34
-21
@@ -16,7 +16,7 @@ import (
|
||||
)
|
||||
|
||||
func TestLoadEmbeddedMigrations(t *testing.T) {
|
||||
ms, err := loadMigrations(migrationFiles, "dns_")
|
||||
ms, err := loadMigrations(migrationFiles, "dns-ui", "dns_")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -24,12 +24,12 @@ func TestLoadEmbeddedMigrations(t *testing.T) {
|
||||
t.Fatalf("got %+v", ms)
|
||||
}
|
||||
if ms[1].version != 2 || ms[1].name != "002_wide_audit_record_id.sql" || !reflect.DeepEqual(ms[1].statements,
|
||||
[]string{"ALTER TABLE `dns_webui_audit` MODIFY COLUMN `record_id` BIGINT NULL"}) {
|
||||
[]string{"ALTER TABLE `dns-ui`.`dns_webui_audit` MODIFY COLUMN `record_id` BIGINT NULL"}) {
|
||||
t.Fatalf("got %+v", ms[1])
|
||||
}
|
||||
wantTables := []string{"`dns_webui_schema`", "`dns_webui_users`", "`dns_webui_sessions`", "`dns_webui_zone_delegations`", "`dns_webui_audit`"}
|
||||
wantTables := []string{"`dns-ui`.`dns_webui_schema`", "`dns-ui`.`dns_webui_users`", "`dns-ui`.`dns_webui_sessions`", "`dns-ui`.`dns_webui_zone_delegations`", "`dns-ui`.`dns_webui_audit`"}
|
||||
for i, stmt := range ms[0].statements {
|
||||
if strings.Contains(stmt, "{prefix}") || strings.Contains(stmt, "coredns_") || strings.Contains(stmt, "--") {
|
||||
if strings.Contains(stmt, "{prefix}") || strings.Contains(stmt, "{database}") || strings.Contains(stmt, "coredns_") || strings.Contains(stmt, "--") {
|
||||
t.Fatalf("statement %d not substituted: %s", i, stmt)
|
||||
}
|
||||
if !strings.Contains(stmt, "CREATE TABLE") || !strings.Contains(stmt, wantTables[i]) {
|
||||
@@ -39,25 +39,34 @@ func TestLoadEmbeddedMigrations(t *testing.T) {
|
||||
t.Fatalf("statement %d: %s", i, stmt)
|
||||
}
|
||||
}
|
||||
if !strings.HasPrefix(ms[0].statements[0], "CREATE TABLE IF NOT EXISTS `dns_webui_schema`") {
|
||||
if !strings.HasPrefix(ms[0].statements[0], "CREATE TABLE IF NOT EXISTS `dns-ui`.`dns_webui_schema`") {
|
||||
t.Fatal(ms[0].statements[0])
|
||||
}
|
||||
if !strings.Contains(ms[0].statements[2], "REFERENCES `dns_webui_users` (`id`)") {
|
||||
if !strings.Contains(ms[0].statements[2], "REFERENCES `dns-ui`.`dns_webui_users` (`id`)") {
|
||||
t.Fatal(ms[0].statements[2])
|
||||
}
|
||||
if _, err := loadMigrations(migrationFiles, "bad-prefix"); err == nil {
|
||||
if _, err := loadMigrations(migrationFiles, "dns-ui", "bad-prefix"); err == nil {
|
||||
t.Fatal("invalid prefix accepted")
|
||||
}
|
||||
for _, name := range []string{"", "bad\nname", strings.Repeat("d", 65)} {
|
||||
if _, err := loadMigrations(migrationFiles, name, "dns_"); err == nil {
|
||||
t.Fatalf("invalid database name %q accepted", name)
|
||||
}
|
||||
}
|
||||
legacy, err := loadMigrations(migrationFiles, "legacy; `dns", "dns_")
|
||||
if err != nil || len(legacy[0].statements) != 5 || !strings.Contains(legacy[0].statements[0], "`legacy; ``dns`.`dns_webui_schema`") {
|
||||
t.Fatalf("legacy migrations: %+v %v", legacy, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadMigrationsFS(t *testing.T) {
|
||||
good := fstest.MapFS{
|
||||
"migrations/002_second.sql": {Data: []byte("CREATE TABLE `{prefix}b` (a INT);")},
|
||||
"migrations/002_second.sql": {Data: []byte("CREATE TABLE {database}.`{prefix}b` (a INT);")},
|
||||
"migrations/001_first.sql": {Data: []byte("-- header\nCREATE TABLE `{prefix}a` (a INT);\nINSERT INTO `{prefix}a` VALUES (1)")},
|
||||
"migrations/README.txt": {Data: []byte("ignored")},
|
||||
"migrations/sub/003_x.sql": {Data: []byte("ignored")},
|
||||
}
|
||||
ms, err := loadMigrations(good, "p_")
|
||||
ms, err := loadMigrations(good, "db", "p_")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -67,6 +76,9 @@ func TestLoadMigrationsFS(t *testing.T) {
|
||||
if !reflect.DeepEqual(ms[0].statements, []string{"CREATE TABLE `p_a` (a INT)", "INSERT INTO `p_a` VALUES (1)"}) {
|
||||
t.Fatalf("statements %q", ms[0].statements)
|
||||
}
|
||||
if !reflect.DeepEqual(ms[1].statements, []string{"CREATE TABLE `db`.`p_b` (a INT)"}) {
|
||||
t.Fatalf("statements %q", ms[1].statements)
|
||||
}
|
||||
bad := []fstest.MapFS{
|
||||
{"migrations/initial.sql": {Data: []byte("SELECT 1;")}},
|
||||
{"migrations/x1_initial.sql": {Data: []byte("SELECT 1;")}},
|
||||
@@ -78,7 +90,7 @@ func TestLoadMigrationsFS(t *testing.T) {
|
||||
{"other/001_a.sql": {Data: []byte("SELECT 1;")}},
|
||||
}
|
||||
for i, fsys := range bad {
|
||||
if _, err := loadMigrations(fsys, "p_"); err == nil {
|
||||
if _, err := loadMigrations(fsys, "db", "p_"); err == nil {
|
||||
t.Errorf("case %d accepted", i)
|
||||
}
|
||||
}
|
||||
@@ -119,13 +131,14 @@ func appSchemaRows(prefix string, change func(rows [][]driver.Value) [][]driver.
|
||||
}
|
||||
|
||||
func (m *migrationDB) handler(q string, args []any) fdbResponse {
|
||||
q = strings.ReplaceAll(q, "`coredns`.`coredns_webui_", "`webui`.`coredns_webui_")
|
||||
if m.failOn != "" && strings.Contains(q, m.failOn) {
|
||||
return fdbResponse{err: m.failErr}
|
||||
}
|
||||
switch {
|
||||
case strings.HasPrefix(q, "INSERT IGNORE INTO `coredns_webui_locks`"):
|
||||
case strings.HasPrefix(q, "INSERT IGNORE INTO `webui`.`coredns_webui_locks`"):
|
||||
return fdbResponse{rowsAffected: 1}
|
||||
case strings.HasPrefix(q, "SELECT `name` FROM `coredns_webui_locks`"):
|
||||
case strings.HasPrefix(q, "SELECT `name` FROM `webui`.`coredns_webui_locks`"):
|
||||
return fdbResponse{rows: fdbRow(args[0])}
|
||||
case strings.Contains(q, "information_schema.TABLES"):
|
||||
return fdbResponse{rows: fdbRow(m.tableCount)}
|
||||
@@ -137,9 +150,9 @@ func (m *migrationDB) handler(q string, args []any) fdbResponse {
|
||||
case strings.HasPrefix(q, "UPDATE"):
|
||||
m.versionWrite = append(m.versionWrite, "update", args[0])
|
||||
return fdbResponse{rowsAffected: m.updateRows}
|
||||
case strings.HasPrefix(q, "SELECT COUNT(*) FROM `coredns_webui_schema`"):
|
||||
case strings.HasPrefix(q, "SELECT COUNT(*) FROM `webui`.`coredns_webui_schema`"):
|
||||
return fdbResponse{rows: fdbRow(m.existsCount)}
|
||||
case strings.HasPrefix(q, "INSERT INTO `coredns_webui_schema`"):
|
||||
case strings.HasPrefix(q, "INSERT INTO `webui`.`coredns_webui_schema`"):
|
||||
m.versionWrite = append(m.versionWrite, "insert", args[0])
|
||||
m.updateRows = 1
|
||||
return fdbResponse{}
|
||||
@@ -207,7 +220,7 @@ func TestSetSchemaVersion(t *testing.T) {
|
||||
func TestApplyMigrations(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tn := fdbTables(t)
|
||||
ms, err := loadMigrations(migrationFiles, "coredns_")
|
||||
ms, err := loadMigrations(migrationFiles, "webui", "coredns_")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -252,7 +265,7 @@ func TestApplyMigrations(t *testing.T) {
|
||||
t.Fatalf("got %v %v", err, m.versionWrite)
|
||||
}
|
||||
|
||||
for _, fail := range []string{"information_schema", "INSERT INTO `coredns_webui_schema`"} {
|
||||
for _, fail := range []string{"information_schema", "INSERT INTO `webui`.`coredns_webui_schema`"} {
|
||||
m = &migrationDB{tableCount: 0, failOn: fail, failErr: errors.New("x")}
|
||||
db, _ = newFakeDB(t, m.handler)
|
||||
if err := applyMigrations(ctx, db, tn, ms, discardLogger()); err == nil {
|
||||
@@ -269,13 +282,13 @@ func TestMigrateDatabase(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
calls := st.Calls()
|
||||
if !strings.HasPrefix(calls[0].query, "INSERT IGNORE INTO `coredns_webui_locks`") || calls[0].args[0] != "coredns_webui_migrate" {
|
||||
if !strings.HasPrefix(calls[0].query, "INSERT IGNORE INTO `webui`.`coredns_webui_locks`") || calls[0].args[0] != "coredns_webui_migrate" {
|
||||
t.Fatalf("initial call %+v", calls[0])
|
||||
}
|
||||
if !strings.HasSuffix(calls[1].query, "FOR UPDATE") || calls[1].args[0] != "coredns_webui_migrate" || st.commits != 1 {
|
||||
t.Fatalf("guard call %+v, commits %d", calls[1], st.commits)
|
||||
}
|
||||
bad := tableNames{prefix: "bad-prefix"}
|
||||
bad := tableNames{webuiSchema: "webui", webuiPrefix: "bad-prefix"}
|
||||
if err := migrateDatabase(ctx, db, bad, discardLogger()); err == nil {
|
||||
t.Fatal("invalid prefix accepted")
|
||||
}
|
||||
@@ -285,7 +298,7 @@ func TestMigrationBootstrapRetriesDeadlock(t *testing.T) {
|
||||
m := &migrationDB{tableCount: 0}
|
||||
seeds := 0
|
||||
db, state := newFakeDB(t, func(q string, args []any) fdbResponse {
|
||||
if strings.HasPrefix(q, "INSERT IGNORE INTO `coredns_webui_locks`") {
|
||||
if strings.HasPrefix(q, "INSERT IGNORE INTO `webui`.`coredns_webui_locks`") {
|
||||
seeds++
|
||||
switch seeds {
|
||||
case 1:
|
||||
@@ -317,10 +330,10 @@ func TestVerifyAppSchema(t *testing.T) {
|
||||
verify = c
|
||||
}
|
||||
}
|
||||
if len(verify.args) != 5 || verify.args[0] != "coredns_webui_schema" || verify.args[4] != "coredns_webui_audit" {
|
||||
if len(verify.args) != 6 || verify.args[0] != "webui" || verify.args[1] != "coredns_webui_schema" || verify.args[5] != "coredns_webui_audit" {
|
||||
t.Fatalf("%+v", verify)
|
||||
}
|
||||
all, err := loadMigrations(migrationFiles, "coredns_")
|
||||
all, err := loadMigrations(migrationFiles, "webui", "coredns_")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user