# HG changeset patch # User Sascha L. Teichmann # Date 1546871674 -3600 # Node ID 897d4d8316ad6b69ac918cc7cc8ce953f3469f25 # Parent d0830ebb3a23eeeff103c94b961c40ee82233c15 Import configuration: Made extra attributes updatable, too. diff -r d0830ebb3a23 -r 897d4d8316ad pkg/controllers/importconfig.go --- a/pkg/controllers/importconfig.go Mon Jan 07 13:30:29 2019 +0100 +++ b/pkg/controllers/importconfig.go Mon Jan 07 15:34:34 2019 +0100 @@ -14,6 +14,7 @@ package controllers import ( + "context" "database/sql" "errors" "fmt" @@ -61,6 +62,10 @@ SELECT true FROM waterway.import_configuration WHERE id = $1` + deleteImportConfiguationAttributesSQL = ` +DELETE FROM waterway.import_configuration_attributes +WHERE id = $1` + deleteImportConfiguationSQL = ` DELETE FROM waterway.import_configuration WHERE id = $1` @@ -149,6 +154,15 @@ return } + if importConfig.Attributes != nil { + if _, err = tx.ExecContext(ctx, deleteImportConfiguationAttributesSQL, id); err != nil { + return + } + if err = storeConfigAttributes(ctx, tx, id, importConfig.Attributes); err != nil { + return + } + } + scheduler.UnbindByID(id) if cron.Valid { @@ -258,6 +272,10 @@ return } + if _, err = tx.ExecContext(ctx, deleteImportConfiguationAttributesSQL, id); err != nil { + return + } + if _, err = tx.ExecContext(ctx, deleteImportConfiguationSQL, id); err != nil { return } @@ -279,6 +297,36 @@ return } +func storeConfigAttributes( + ctx context.Context, + tx *sql.Tx, + id int64, + attrs imports.ConfigAttributes, +) error { + if len(attrs) == 0 { + return nil + } + attrStmt, err := tx.PrepareContext(ctx, insertImportConfigurationAttributeSQL) + if err != nil { + return err + } + defer attrStmt.Close() + // Sort to make it deterministic + keys := make([]string, len(attrs)) + i := 0 + for key := range attrs { + keys[i] = key + i++ + } + sort.Strings(keys) + for _, key := range keys { + if _, err := attrStmt.ExecContext(ctx, id, key, attrs[key]); err != nil { + return err + } + } + return nil +} + func addImportConfig( input interface{}, req *http.Request, @@ -322,27 +370,8 @@ } // Store extra attributes - if attrs := importConfig.Attributes; len(attrs) > 0 { - var attrStmt *sql.Stmt - attrStmt, err = tx.PrepareContext(ctx, insertImportConfigurationAttributeSQL) - if err != nil { - return - } - defer attrStmt.Close() - // Sort to make it deterministic - keys := make([]string, 0, len(attrs)) - for key, value := range importConfig.Attributes { - // Ignore nil values. - if value != nil { - keys = append(keys, key) - } - } - sort.Strings(keys) - for _, key := range keys { - if _, err = attrStmt.ExecContext(ctx, id, key, *attrs[key]); err != nil { - return - } - } + if err = storeConfigAttributes(ctx, tx, id, importConfig.Attributes); err != nil { + return } // Need to start a scheduler job right away? diff -r d0830ebb3a23 -r 897d4d8316ad pkg/imports/config.go --- a/pkg/imports/config.go Mon Jan 07 13:30:29 2019 +0100 +++ b/pkg/imports/config.go Mon Jan 07 15:34:34 2019 +0100 @@ -34,7 +34,7 @@ // ConfigAttributes is a map of optional key/value attributes // of an import configuration. - ConfigAttributes map[string]*string + ConfigAttributes map[string]string // Config is JSON serialized form of a import configuration. Config struct { @@ -79,10 +79,8 @@ if ca == nil { return "", false } - if value, found := ca[key]; found && value != nil { - return *value, true - } - return "", false + value, found := ca[key] + return value, found } // UnmarshalJSON checks if the incoming string @@ -169,16 +167,16 @@ return err } defer rows.Close() - var attributes map[string]*string + var attributes map[string]string for rows.Next() { var k, v string if err = rows.Scan(&k, &v); err != nil { return err } if attributes == nil { - attributes = map[string]*string{} + attributes = map[string]string{} } - attributes[k] = &v + attributes[k] = v } if err = rows.Err(); err != nil { return err