From 44e8d11003e39fef8d45f1292cc930fbb9402831 Mon Sep 17 00:00:00 2001 From: sredny buitrago Date: Sat, 23 Nov 2024 10:17:01 -0300 Subject: [PATCH] isolate index build logic --- pumps/sql.go | 30 +++++++++++++++++------------- pumps/sql_test.go | 8 ++++++-- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/pumps/sql.go b/pumps/sql.go index b8b52c339..e9bf3b820 100644 --- a/pumps/sql.go +++ b/pumps/sql.go @@ -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 { @@ -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 { + 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" diff --git a/pumps/sql_test.go b/pumps/sql_test.go index aa428fb2c..1233463f5 100644 --- a/pumps/sql_test.go +++ b/pumps/sql_test.go @@ -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 @@ -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) + }() + assert.NotNil(t, pmp) actualErr := pmp.ensureIndex(tc.givenTableName, tc.givenRunInBackground) @@ -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) }