in catalog/sql/sql.go [742:807]
func getUpdatedPropsAndUpdateSummary(currentProps iceberg.Properties, removals []string, updates iceberg.Properties) (iceberg.Properties, catalog.PropertiesUpdateSummary, error)
func (c *Catalog) UpdateNamespaceProperties(ctx context.Context, namespace table.Identifier, removals []string, updates iceberg.Properties) (catalog.PropertiesUpdateSummary, error) {
var summary catalog.PropertiesUpdateSummary
currentProps, err := c.LoadNamespaceProperties(ctx, namespace)
if err != nil {
return summary, err
}
_, summary, err = getUpdatedPropsAndUpdateSummary(currentProps, removals, updates)
if err != nil {
return summary, err
}
nsToUpdate := strings.Join(namespace, ".")
return summary, withWriteTx(ctx, c.db, func(ctx context.Context, tx bun.Tx) error {
var m *sqlIcebergNamespaceProps
if len(removals) > 0 {
_, err := tx.NewDelete().Model(m).
Where("catalog_name = ?", c.name).
Where("namespace = ?", nsToUpdate).
Where("property_key in (?)", bun.In(removals)).Exec(ctx)
if err != nil {
return fmt.Errorf("error deleting properties for '%s': %w", namespace, err)
}
}
if len(updates) > 0 {
props := make([]sqlIcebergNamespaceProps, 0, len(updates))
for k, v := range updates {
props = append(props, sqlIcebergNamespaceProps{
CatalogName: c.name,
Namespace: nsToUpdate,
PropertyKey: k,
PropertyValue: sql.NullString{String: v, Valid: true},
})
}
q := tx.NewInsert().Model(&props)
switch {
case c.db.HasFeature(feature.InsertOnConflict):
q = q.On("CONFLICT (catalog_name, namespace, property_key) DO UPDATE").
Set("property_value = EXCLUDED.property_value")
case c.db.HasFeature(feature.InsertOnDuplicateKey):
q = q.On("DUPLICATE KEY UPDATE")
default:
_, err := tx.NewDelete().Model(m).
Where("catalog_name = ?", c.name).
Where("namespace = ?", nsToUpdate).
Where("property_key in (?)", bun.In(slices.Collect(maps.Keys(updates)))).
Exec(ctx)
if err != nil {
return fmt.Errorf("error deleting properties for '%s': %w", namespace, err)
}
}
_, err := q.Exec(ctx)
if err != nil {
return fmt.Errorf("error updating namespace properties for '%s': %w", namespace, err)
}
}
return nil
})
}