Skip to content

feat: delete old records on domain update #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,27 @@ func (s *State) UpdateDomain(
domainName string,
nameServers map[string]string,
) error {
logger := logging.GetLogger()
err := s.db.Update(func(txn *badger.Txn) error {
// TODO: find any existing keys for domain and delete
// Delete old records for domain
keyPrefix := []byte(fmt.Sprintf("domain_%s_", domainName))
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
for it.Seek(keyPrefix); it.ValidForPrefix(keyPrefix); it.Next() {
item := it.Item()
k := item.Key()
if err := txn.Delete(k); err != nil {
return err
}
logger.Debug(
fmt.Sprintf(
"deleted record for domain %s with key: %s",
domainName,
k,
),
)
}
// Add new records
for nameServer, ipAddress := range nameServers {
key := fmt.Sprintf(
"domain_%s_nameserver_%s",
Expand All @@ -137,6 +156,14 @@ func (s *State) UpdateDomain(
if err := txn.Set([]byte(key), []byte(ipAddress)); err != nil {
return err
}
logger.Debug(
fmt.Sprintf(
"added record for domain %s: %s: %s",
domainName,
nameServer,
ipAddress,
),
)
}
return nil
})
Expand Down