Skip to content

Commit

Permalink
some refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sredny buitrago authored and sredny buitrago committed Nov 23, 2024
1 parent 44e8d11 commit 6b993ff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
6 changes: 3 additions & 3 deletions pumps/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ func (c *SQLPump) WriteUptimeData(data []interface{}) {
c.log.Debug("Purged ", len(data), " records...")
}

func (c *SQLPump) buildIndexName(baseIndexName string, tableName string) string {
return fmt.Sprintf("%s_%s", tableName, baseIndexName)
func (c *SQLPump) buildIndexName(indexBaseName, tableName string) string {
return fmt.Sprintf("%s_%s", tableName, indexBaseName)
}

func (c *SQLPump) ensureIndex(tableName string, background bool) error {
Expand All @@ -380,7 +380,7 @@ func (c *SQLPump) ensureIndex(tableName string, background bool) error {
}

createIndexFn := func(indexBaseName, columns string) error {
indexName := c.buildIndexName(indexBaseName, columns)
indexName := c.buildIndexName(indexBaseName, tableName)
option := ""
if c.dbType == "postgres" {
option = "CONCURRENTLY"
Expand Down
36 changes: 31 additions & 5 deletions pumps/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func TestDecodeRequestAndDecodeResponseSQL(t *testing.T) {
assert.False(t, newPump.GetDecodedResponse())
}

func TestEnsureIndexSQL1(t *testing.T) {
func TestEnsureIndexSQL(t *testing.T) {
//nolint:govet
tcs := []struct {
testName string
Expand All @@ -383,8 +383,8 @@ func TestEnsureIndexSQL1(t *testing.T) {
cfg["connection_string"] = ""
pmp.log = log.WithField("prefix", "sql-pump")
err := pmp.Init(cfg)

assert.Nil(t, err)

if err := pmp.ensureTable(tableName); err != nil {
return nil
}
Expand All @@ -406,7 +406,6 @@ func TestEnsureIndexSQL1(t *testing.T) {
pmp.log = log.WithField("prefix", "sql-pump")
err := pmp.Init(cfg)
assert.Nil(t, err)
pmp.log = log.WithField("prefix", "sql-aggregate-pump")

pmp.backgroundIndexCreated = make(chan bool, 1)
if err := pmp.ensureTable(tableName); err != nil {
Expand All @@ -415,7 +414,7 @@ func TestEnsureIndexSQL1(t *testing.T) {

return &pmp
},
givenTableName: "test2",
givenTableName: "test",
givenRunInBackground: true,
expectedErr: nil,
shouldHaveIndex: true,
Expand All @@ -438,7 +437,9 @@ func TestEnsureIndexSQL1(t *testing.T) {
// wait for the background index creation to finish
<-pmp.backgroundIndexCreated
} else {
indexName := pmp.buildIndexName("idx_apiid", tc.givenTableName)
indexToUse := indexes[0]
t.Logf("\n Sent: %v --%v \n", indexToUse.baseName, tc.givenTableName)
indexName := pmp.buildIndexName(indexToUse.baseName, tc.givenTableName)
hasIndex := pmp.db.Table(tc.givenTableName).Migrator().HasIndex(tc.givenTableName, indexName)
assert.Equal(t, tc.shouldHaveIndex, hasIndex)
}
Expand All @@ -448,3 +449,28 @@ func TestEnsureIndexSQL1(t *testing.T) {
})
}
}

func TestBuildIndexName(t *testing.T) {
tests := []struct {
indexBaseName string
tableName string
expected string
}{
{"idx_responsecode", "users", "users_idx_responsecode"},
{"idx_apikey", "transactions", "transactions_idx_apikey"},
{"idx_timestamp", "logs", "logs_idx_timestamp"},
{"idx_apiid", "api_calls", "api_calls_idx_apiid"},
{"idx_orgid", "organizations", "organizations_idx_orgid"},
}

c := &SQLPump{} // Create an instance of SQLPump.

for _, tt := range tests {
t.Run(tt.indexBaseName+"_"+tt.tableName, func(t *testing.T) {
result := c.buildIndexName(tt.indexBaseName, tt.tableName)
if result != tt.expected {
t.Errorf("buildIndexName(%s, %s) = %s; want %s", tt.indexBaseName, tt.tableName, result, tt.expected)
}
})
}
}

0 comments on commit 6b993ff

Please sign in to comment.