Skip to content

Commit

Permalink
added test for ensure index in SQL pump
Browse files Browse the repository at this point in the history
  • Loading branch information
sredny buitrago authored and sredny buitrago committed Nov 21, 2024
1 parent 02be918 commit a372054
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 17 deletions.
34 changes: 17 additions & 17 deletions pumps/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,23 +199,6 @@ func (c *SQLPump) Init(conf interface{}) error {
return nil
}

// ensureTable creates the table if it doesn't exist
func (c *SQLPump) ensureTable(tableName string) error {
if !c.db.Migrator().HasTable(tableName) {
c.db = c.db.Table(tableName)

if err := c.db.Migrator().CreateTable(&analytics.AnalyticsRecord{}); err != nil {
c.log.Error("error creating table", err)
return err
}

if err := c.ensureIndex(tableName, false); err != nil {
return err
}
}
return nil
}

func (c *SQLPump) WriteData(ctx context.Context, data []interface{}) error {
c.log.Debug("Attempting to write ", len(data), " records...")

Expand Down Expand Up @@ -434,3 +417,20 @@ func (c *SQLPump) ensureIndex(tableName string, background bool) error {
}
return nil
}

// ensureTable creates the table if it doesn't exist
func (c *SQLPump) ensureTable(tableName string) error {
if !c.db.Migrator().HasTable(tableName) {
c.db = c.db.Table(tableName)

if err := c.db.Migrator().CreateTable(&analytics.AnalyticsRecord{}); err != nil {
c.log.Error("error creating table", err)
return err
}

if err := c.ensureIndex(tableName, false); err != nil {
return err
}
}
return nil
}
81 changes: 81 additions & 0 deletions pumps/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,84 @@ func TestDecodeRequestAndDecodeResponseSQL(t *testing.T) {
assert.False(t, newPump.GetDecodedRequest())
assert.False(t, newPump.GetDecodedResponse())
}

func TestEnsureIndexSQL(t *testing.T) {
//nolint:govet
tcs := []struct {
testName string
givenTableName string
expectedErr error
pmpSetupFn func(tableName string) *SQLPump
givenRunInBackground bool
shouldHaveIndex bool
}{
{
testName: "index created correctly, not background",
pmpSetupFn: func(tableName string) *SQLPump {
pmp := SQLPump{}
cfg := make(map[string]interface{})
cfg["type"] = "sqlite"
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
}

return &pmp
},
givenTableName: "test",
givenRunInBackground: false,
expectedErr: nil,
shouldHaveIndex: true,
},
{
testName: "index created correctly, background",
pmpSetupFn: func(tableName string) *SQLPump {
cfg := make(map[string]interface{})
pmp := SQLPump{}
cfg["type"] = "sqlite"
cfg["connection_string"] = ""
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 {
return nil
}

return &pmp
},
givenTableName: "test2",
givenRunInBackground: true,
expectedErr: nil,
shouldHaveIndex: true,
},
}

for _, tc := range tcs {
t.Run(tc.testName, func(t *testing.T) {
pmp := tc.pmpSetupFn(tc.givenTableName)
assert.NotNil(t, pmp)

actualErr := pmp.ensureIndex(tc.givenTableName, tc.givenRunInBackground)

if actualErr == nil {
if tc.givenRunInBackground {
// wait for the background index creation to finish
<-pmp.backgroundIndexCreated
} else {
indexName := tc.givenTableName + "_idx_apiid"
hasIndex := pmp.db.Table(tc.givenTableName).Migrator().HasIndex(tc.givenTableName, indexName)
assert.Equal(t, tc.shouldHaveIndex, hasIndex)
}
} else {
assert.Equal(t, tc.expectedErr.Error(), actualErr.Error())
}
})
}
}

0 comments on commit a372054

Please sign in to comment.