Skip to content

Commit

Permalink
isolate index build logic
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 913eb63 commit 44e8d11
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
30 changes: 17 additions & 13 deletions pumps/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ var (
SQLPrefix = "SQL-pump"
SQLDefaultENV = PUMPS_ENV_PREFIX + "_SQL" + PUMPS_ENV_META_PREFIX
SQLDefaultQueryBatchSize = 1000

indexes = []struct {
baseName string
columns string
}{
{"idx_responsecode", "responsecode"},
{"idx_apikey", "apikey"},
{"idx_timestamp", "timestamp"},
{"idx_apiid", "apiid"},
{"idx_orgid", "orgid"},
{"idx_oauthid", "oauthid"},
}
)

func (c *SQLPump) New() Pump {
Expand Down Expand Up @@ -358,25 +370,17 @@ func (c *SQLPump) WriteUptimeData(data []interface{}) {
c.log.Debug("Purged ", len(data), " records...")
}

func (c *SQLPump) buildIndexName(baseIndexName string, tableName string) string {

Check failure on line 373 in pumps/sql.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
return fmt.Sprintf("%s_%s", tableName, baseIndexName)
}

func (c *SQLPump) ensureIndex(tableName string, background bool) error {
if !c.db.Migrator().HasTable(tableName) {
return errors.New("cannot create indexes as table doesn't exist: " + tableName)
}

indexes := []struct {
baseName string
columns string
}{
{"idx_responsecode", "responsecode"},
{"idx_apikey", "apikey"},
{"idx_timestamp", "timestamp"},
{"idx_apiid", "apiid"},
{"idx_orgid", "orgid"},
{"idx_oauthid", "oauthid"},
}

createIndexFn := func(indexBaseName, columns string) error {
indexName := tableName + "_" + indexBaseName
indexName := c.buildIndexName(indexBaseName, columns)
option := ""
if c.dbType == "postgres" {
option = "CONCURRENTLY"
Expand Down
8 changes: 6 additions & 2 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 TestEnsureIndexSQL(t *testing.T) {
func TestEnsureIndexSQL1(t *testing.T) {
//nolint:govet
tcs := []struct {
testName string
Expand Down Expand Up @@ -425,6 +425,10 @@ func TestEnsureIndexSQL(t *testing.T) {
for _, tc := range tcs {
t.Run(tc.testName, func(t *testing.T) {
pmp := tc.pmpSetupFn(tc.givenTableName)
defer func() {
pmp.db.Migrator().DropTable(tc.givenTableName)

Check failure on line 429 in pumps/sql_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `(gorm.io/gorm.Migrator).DropTable` is not checked (errcheck)
}()

assert.NotNil(t, pmp)

actualErr := pmp.ensureIndex(tc.givenTableName, tc.givenRunInBackground)
Expand All @@ -434,7 +438,7 @@ func TestEnsureIndexSQL(t *testing.T) {
// wait for the background index creation to finish
<-pmp.backgroundIndexCreated
} else {
indexName := tc.givenTableName + "_idx_apiid"
indexName := pmp.buildIndexName("idx_apiid", tc.givenTableName)
hasIndex := pmp.db.Table(tc.givenTableName).Migrator().HasIndex(tc.givenTableName, indexName)
assert.Equal(t, tc.shouldHaveIndex, hasIndex)
}
Expand Down

0 comments on commit 44e8d11

Please sign in to comment.