common/schema: make enabled/disabled columns configurable

This commit is contained in:
Vincent Bernat
2023-01-19 18:47:20 +01:00
parent a8e05548a4
commit 72d51d0512
13 changed files with 193 additions and 24 deletions

View File

@@ -6,14 +6,47 @@
// the subsystem that will use it.
package schema
import (
"fmt"
"golang.org/x/exp/slices"
)
// Component represents the schema compomenent.
type Component struct {
c Configuration
Schema
}
// New creates a new schema component.
func New() (*Component, error) {
func New(config Configuration) (*Component, error) {
schema := flows()
for _, k1 := range config.Enabled {
for _, k2 := range config.Disabled {
if k1 == k2 {
return nil, fmt.Errorf("column %q contained in both EnabledColumns and DisabledColumns", k1)
}
}
}
for _, k := range config.Enabled {
if column, ok := schema.LookupColumnByKey(k); ok {
column.Disabled = false
}
}
for _, k := range config.Disabled {
if column, ok := schema.LookupColumnByKey(k); ok {
if column.NoDisable {
return nil, fmt.Errorf("column %q cannot be disabled", k)
}
if slices.Contains(schema.clickHousePrimaryKeys, k) {
return nil, fmt.Errorf("column %q cannot be disabled (primary key)", k)
}
column.Disabled = true
}
}
return &Component{
Schema: flows(),
c: config,
Schema: schema,
}, nil
}