Skip to content
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

More guardrails around ShouldDeleteFromName #842

Merged
merged 6 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
16 changes: 13 additions & 3 deletions lib/destination/ddl/expiry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ import (
"strconv"
"strings"
"time"

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

func ShouldDeleteFromName(name string) bool {
// We expect the table name to be in the format of `tableName__artie_suffix_unix`
if !strings.Contains(strings.ToLower(name), constants.ArtiePrefix) {
return false
}

nameParts := strings.Split(name, "_")
if len(nameParts) < 2 {
if len(nameParts) < 6 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 6?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's just how many are in a temp table

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the minimum

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could put it back to 2 if it's confusing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, because right after the table name there are 3 underscores in a row, so 2 of the 6 parts are empty. Maybe it would be more clear to just split the part that comes after the constants.ArtiePrefix and check for 2 parts there? Because otherwise if the table name itself has any underscores in it, we could get a false positive here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danafallon Want to take another look?

slog.Warn("Table does not have enough parts to it, but contains __artie in the table name",
slog.String("tableName", name),
slog.Int("parts", len(nameParts)),
)
return false
}

Expand All @@ -20,6 +31,5 @@ func ShouldDeleteFromName(name string) bool {
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("expired_tbl__artie_suffix_%d", time.Now().Add(constants.TemporaryTableTTL).Unix()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call this future_tbl__... for clarity?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup!

fmt.Sprintf("expired_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("tableName_%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)
}
}
}