From 097c8eefe3281327f4ccf4271204553d66f402d5 Mon Sep 17 00:00:00 2001 From: kai Date: Mon, 11 Jul 2022 12:52:08 +0100 Subject: [PATCH] Validate table Name property matches key in plugin's TableMap. Closes #355 --- cache/query_cache.go | 2 ++ plugin/plugin.go | 3 +++ plugin/plugin_validate.go | 14 ++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/cache/query_cache.go b/cache/query_cache.go index f04fbea0..23ac3537 100644 --- a/cache/query_cache.go +++ b/cache/query_cache.go @@ -304,6 +304,8 @@ func (c *QueryCache) getKeyColumnsForTable(table string) map[string]*proto.KeyCo for _, k := range append(tableSchema.ListCallKeyColumnList, tableSchema.GetCallKeyColumnList...) { res[k.Name] = k } + } else { + log.Printf("[WARN] getKeyColumnsForTable found NO SCHEMA FOR %s", table) } return res } diff --git a/plugin/plugin.go b/plugin/plugin.go index a14e8664..329cda23 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -416,6 +416,9 @@ func (p *Plugin) ensureCache() error { } func (p *Plugin) buildSchema() (map[string]*proto.TableSchema, error) { + log.Printf("[TRACE] buildSchema") + defer log.Printf("[TRACE] buildSchema complete") + // the connection property must be set already if p.Connection == nil { return nil, fmt.Errorf("plugin.GetSchema called before setting connection config") diff --git a/plugin/plugin_validate.go b/plugin/plugin_validate.go index 384ac9ab..1edaed6c 100644 --- a/plugin/plugin_validate.go +++ b/plugin/plugin_validate.go @@ -29,6 +29,20 @@ func (p *Plugin) Validate() string { log.Printf("[TRACE] validate DefaultIgnoreConfig") validationErrors = append(validationErrors, p.DefaultIgnoreConfig.Validate(nil)...) + log.Printf("[TRACE] validate table names") + validationErrors = append(validationErrors, p.validateTableNames()...) + log.Printf("[TRACE] plugin has %d validation errors", len(validationErrors)) return strings.Join(validationErrors, "\n") } + +// validate that table names are consistent with their key in the table map +func (p *Plugin) validateTableNames() []string { + var validationErrors []string + for tableName, table := range p.TableMap { + if table.Name != tableName { + validationErrors = append(validationErrors, fmt.Sprintf("table '%s' has inconsistent Name property: '%s'", tableName, table.Name)) + } + } + return validationErrors +}