Skip to content

Commit

Permalink
More guardrails around ShouldDeleteFromName (#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Aug 15, 2024
1 parent 060ed44 commit 9178501
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
23 changes: 16 additions & 7 deletions lib/destination/ddl/expiry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,30 @@ import (
"strconv"
"strings"
"time"

"github.com/artie-labs/transfer/lib/config/constants"
)

func ShouldDeleteFromName(name string) bool {
nameParts := strings.Split(name, "_")
if len(nameParts) < 2 {
parts := strings.Split(strings.ToLower(name), constants.ArtiePrefix)
if len(parts) != 2 {
return false
}

suffixParts := strings.Split(parts[1], "_")
if len(suffixParts) != 3 {
return false
}

unixString := nameParts[len(nameParts)-1]
unix, err := strconv.Atoi(unixString)
unix, err := strconv.Atoi(suffixParts[2])
if err != nil {
slog.Error("Failed to parse unix string", slog.Any("err", err), slog.String("tableName", name), slog.String("unixString", unixString))
slog.Error("Failed to parse unix string",
slog.Any("err", err),
slog.String("tableName", name),
slog.String("tsString", suffixParts[2]),
)
return false
}

ts := time.Unix(int64(unix), 0)
return time.Now().UTC().After(ts)
return time.Now().UTC().After(time.Unix(int64(unix), 0))
}
42 changes: 28 additions & 14 deletions lib/destination/ddl/expiry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,43 @@ package ddl

import (
"fmt"
"strings"
"testing"
"time"

"github.com/artie-labs/transfer/lib/config/constants"

"github.com/stretchr/testify/assert"
)

func TestShouldDeleteFromName(t *testing.T) {
tblsToNotDelete := []string{
"table", "table_", "table_abcdef9",
fmt.Sprintf("future_table_%d", time.Now().Add(1*time.Hour).Unix()),
}

for _, tblToNotDelete := range tblsToNotDelete {
assert.False(t, ShouldDeleteFromName(tblToNotDelete), tblToNotDelete)
}
{
// Tables to not drop
tablesToNotDrop := []string{
"foo",
"transactions",
fmt.Sprintf("future_tbl___artie_suffix_%d", time.Now().Add(constants.TemporaryTableTTL).Unix()),
fmt.Sprintf("future_tbl___notartie_%d", time.Now().Add(-1*time.Hour).Unix()),
}

tblsToDelete := []string{
fmt.Sprintf("expired_table_%d", time.Now().Add(-1*time.Hour).Unix()),
fmt.Sprintf("expired_tbl__artie_%d", time.Now().Add(-1*time.Hour).Unix()),
fmt.Sprintf("expired_%d", time.Now().Add(-1*time.Hour).Unix()),
for _, tblToNotDelete := range tablesToNotDrop {
assert.False(t, ShouldDeleteFromName(strings.ToLower(tblToNotDelete)), tblToNotDelete)
assert.False(t, ShouldDeleteFromName(strings.ToUpper(tblToNotDelete)), tblToNotDelete)
assert.False(t, ShouldDeleteFromName(tblToNotDelete), tblToNotDelete)
}
}
{
// Tables that are eligible to be dropped
tablesToDrop := []string{
"transactions___ARTIE_48GJC_1723663043",
fmt.Sprintf("expired_tbl_%s_suffix_%d", constants.ArtiePrefix, time.Now().Add(-1*constants.TemporaryTableTTL).Unix()),
fmt.Sprintf("artie_%s_suffix_%d", constants.ArtiePrefix, time.Now().Add(-1*constants.TemporaryTableTTL).Unix()),
}

for _, tblToDelete := range tblsToDelete {
assert.True(t, ShouldDeleteFromName(tblToDelete), tblToDelete)
for _, tblToDelete := range tablesToDrop {
assert.True(t, ShouldDeleteFromName(strings.ToLower(tblToDelete)), tblToDelete)
assert.True(t, ShouldDeleteFromName(strings.ToUpper(tblToDelete)), tblToDelete)
assert.True(t, ShouldDeleteFromName(tblToDelete), tblToDelete)
}
}
}

0 comments on commit 9178501

Please sign in to comment.