Skip to content

Commit 8f9cf68

Browse files
committed
[v0.7] Drop tables before create
Forward-ports: #892
1 parent 0b096ef commit 8f9cf68

File tree

7 files changed

+60
-35
lines changed

7 files changed

+60
-35
lines changed

pkg/sqlcache/informer/indexer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func NewIndexer(ctx context.Context, indexers cache.Indexers, s Store) (*Indexer
8282
dbName := db.Sanitize(s.GetName())
8383

8484
err := s.WithTransaction(ctx, true, func(tx db.TxClient) error {
85+
dropTableQuery := fmt.Sprintf(dropIndicesFmt, dbName)
86+
if _, err := tx.Exec(dropTableQuery); err != nil {
87+
return err
88+
}
8589
createTableQuery := fmt.Sprintf(createTableFmt, dbName)
8690
if _, err := tx.Exec(createTableQuery); err != nil {
8791
return err

pkg/sqlcache/informer/indexer_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func TestNewIndexer(t *testing.T) {
4949
storeName := "someStoreName"
5050

5151
store.EXPECT().GetName().AnyTimes().Return(storeName)
52+
client.EXPECT().Exec(fmt.Sprintf(dropIndicesFmt, storeName)).Return(nil, nil)
5253
client.EXPECT().Exec(fmt.Sprintf(createTableFmt, storeName, storeName)).Return(nil, nil)
5354
client.EXPECT().Exec(fmt.Sprintf(createIndexFmt, storeName, storeName)).Return(nil, nil)
5455
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
@@ -98,6 +99,7 @@ func TestNewIndexer(t *testing.T) {
9899
}
99100
storeName := "someStoreName"
100101
store.EXPECT().GetName().AnyTimes().Return(storeName)
102+
client.EXPECT().Exec(fmt.Sprintf(dropIndicesFmt, storeName)).Return(nil, nil)
101103
client.EXPECT().Exec(fmt.Sprintf(createTableFmt, storeName, storeName)).Return(nil, fmt.Errorf("error"))
102104

103105
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(fmt.Errorf("error")).Do(
@@ -123,6 +125,7 @@ func TestNewIndexer(t *testing.T) {
123125
}
124126
storeName := "someStoreName"
125127
store.EXPECT().GetName().AnyTimes().Return(storeName)
128+
client.EXPECT().Exec(fmt.Sprintf(dropIndicesFmt, storeName)).Return(nil, nil)
126129
client.EXPECT().Exec(fmt.Sprintf(createTableFmt, storeName, storeName)).Return(nil, nil)
127130
client.EXPECT().Exec(fmt.Sprintf(createIndexFmt, storeName, storeName)).Return(nil, fmt.Errorf("error"))
128131

@@ -150,6 +153,7 @@ func TestNewIndexer(t *testing.T) {
150153
}
151154
storeName := "someStoreName"
152155
store.EXPECT().GetName().AnyTimes().Return(storeName)
156+
client.EXPECT().Exec(fmt.Sprintf(dropIndicesFmt, storeName)).Return(nil, nil)
153157
client.EXPECT().Exec(fmt.Sprintf(createTableFmt, storeName, storeName)).Return(nil, nil)
154158
client.EXPECT().Exec(fmt.Sprintf(createIndexFmt, storeName, storeName)).Return(nil, nil)
155159
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(fmt.Errorf("error")).Do(

pkg/sqlcache/informer/informer_test.go

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestNewInformer(t *testing.T) {
4343

4444
// NewStore() from store package logic. This package is only concerned with whether it returns err or not as NewStore
4545
// is tested in depth in its own package.
46-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(3)
46+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(4)
4747
dbClient.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
4848
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
4949
err := f(txClient)
@@ -55,8 +55,7 @@ func TestNewInformer(t *testing.T) {
5555

5656
// NewIndexer() logic (within NewListOptionIndexer(). This test is only concerned with whether it returns err or not as NewIndexer
5757
// is tested in depth in its own indexer_test.go
58-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
59-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
58+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(3)
6059
dbClient.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
6160
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
6261
err := f(txClient)
@@ -67,12 +66,7 @@ func TestNewInformer(t *testing.T) {
6766

6867
// NewListOptionIndexer() logic. This test is only concerned with whether it returns err or not as NewIndexer
6968
// is tested in depth in its own indexer_test.go
70-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
71-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
72-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
73-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
74-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
75-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
69+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(9)
7670
dbClient.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
7771
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
7872
err := f(txClient)
@@ -96,7 +90,7 @@ func TestNewInformer(t *testing.T) {
9690

9791
// NewStore() from store package logic. This package is only concerned with whether it returns err or not as NewStore
9892
// is tested in depth in its own package.
99-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
93+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(2)
10094
dbClient.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(fmt.Errorf("error")).Do(
10195
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
10296
err := f(txClient)
@@ -120,8 +114,7 @@ func TestNewInformer(t *testing.T) {
120114

121115
// NewStore() from store package logic. This package is only concerned with whether it returns err or not as NewStore
122116
// is tested in depth in its own package.
123-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
124-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
117+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(3)
125118
dbClient.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
126119
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
127120
err := f(txClient)
@@ -133,7 +126,7 @@ func TestNewInformer(t *testing.T) {
133126

134127
// NewIndexer() logic (within NewListOptionIndexer(). This test is only concerned with whether it returns err or not as NewIndexer
135128
// is tested in depth in its own indexer_test.go
136-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
129+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(2)
137130
dbClient.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(fmt.Errorf("error")).Do(
138131
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
139132
err := f(txClient)
@@ -157,9 +150,7 @@ func TestNewInformer(t *testing.T) {
157150

158151
// NewStore() from store package logic. This package is only concerned with whether it returns err or not as NewStore
159152
// is tested in depth in its own package.
160-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
161-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
162-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
153+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(4)
163154
dbClient.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
164155
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
165156
err := f(txClient)
@@ -171,8 +162,7 @@ func TestNewInformer(t *testing.T) {
171162

172163
// NewIndexer() logic (within NewListOptionIndexer(). This test is only concerned with whether it returns err or not as NewIndexer
173164
// is tested in depth in its own indexer_test.go
174-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
175-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
165+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(3)
176166
dbClient.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
177167
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
178168
err := f(txClient)
@@ -183,12 +173,7 @@ func TestNewInformer(t *testing.T) {
183173

184174
// NewListOptionIndexer() logic. This test is only concerned with whether it returns err or not as NewIndexer
185175
// is tested in depth in its own indexer_test.go
186-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
187-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
188-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
189-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
190-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
191-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
176+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(9)
192177
dbClient.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(fmt.Errorf("error")).Do(
193178
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
194179
err := f(txClient)
@@ -220,9 +205,7 @@ func TestNewInformer(t *testing.T) {
220205

221206
// NewStore() from store package logic. This package is only concerned with whether it returns err or not as NewStore
222207
// is tested in depth in its own package.
223-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
224-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
225-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
208+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(4)
226209
dbClient.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
227210
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
228211
err := f(txClient)
@@ -234,8 +217,7 @@ func TestNewInformer(t *testing.T) {
234217

235218
// NewIndexer() logic (within NewListOptionIndexer(). This test is only concerned with whether it returns err or not as NewIndexer
236219
// is tested in depth in its own indexer_test.go
237-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
238-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
220+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(3)
239221
dbClient.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
240222
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
241223
err := f(txClient)
@@ -246,12 +228,7 @@ func TestNewInformer(t *testing.T) {
246228

247229
// NewListOptionIndexer() logic. This test is only concerned with whether it returns err or not as NewIndexer
248230
// is tested in depth in its own indexer_test.go
249-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
250-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
251-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
252-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
253-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
254-
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
231+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil).Times(9)
255232
dbClient.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
256233
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
257234
err := f(txClient)

pkg/sqlcache/informer/listoption_indexer.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,21 @@ func NewListOptionIndexer(ctx context.Context, s Store, opts ListOptionIndexerOp
218218
setStatements := make([]string, 0, len(indexedFields))
219219

220220
err = l.WithTransaction(ctx, true, func(tx db.TxClient) error {
221+
dropEventsQuery := fmt.Sprintf(dropEventsFmt, dbName)
222+
if _, err := tx.Exec(dropEventsQuery); err != nil {
223+
return err
224+
}
225+
221226
createEventsTableQuery := fmt.Sprintf(createEventsTableFmt, dbName)
222227
if _, err := tx.Exec(createEventsTableQuery); err != nil {
223228
return err
224229
}
225230

231+
dropFieldsQuery := fmt.Sprintf(dropFieldsFmt, dbName)
232+
if _, err := tx.Exec(dropFieldsQuery); err != nil {
233+
return err
234+
}
235+
226236
createFieldsTableQuery := fmt.Sprintf(createFieldsTableFmt, dbName, dbName, strings.Join(columnDefs, ", "))
227237
if _, err := tx.Exec(createFieldsTableQuery); err != nil {
228238
return err
@@ -249,6 +259,12 @@ func NewListOptionIndexer(ctx context.Context, s Store, opts ListOptionIndexerOp
249259
setStatements = append(setStatements, setStatement)
250260
}
251261
}
262+
263+
dropLabelsQuery := fmt.Sprintf(dropLabelsStmtFmt, dbName)
264+
if _, err := tx.Exec(dropLabelsQuery); err != nil {
265+
return err
266+
}
267+
252268
createLabelsTableQuery := fmt.Sprintf(createLabelsTableFmt, dbName, dbName)
253269
if _, err := tx.Exec(createLabelsTableQuery); err != nil {
254270
return err

pkg/sqlcache/informer/listoption_indexer_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func TestNewListOptionIndexer(t *testing.T) {
127127
store.EXPECT().GetName().Return(id).AnyTimes()
128128
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
129129
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
130+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
130131
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
131132
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
132133
err := f(txClient)
@@ -146,14 +147,18 @@ func TestNewListOptionIndexer(t *testing.T) {
146147
store.EXPECT().RegisterBeforeDropAll(gomock.Any()).AnyTimes()
147148

148149
// create events table
150+
txClient.EXPECT().Exec(fmt.Sprintf(dropEventsFmt, id)).Return(nil, nil)
149151
txClient.EXPECT().Exec(fmt.Sprintf(createEventsTableFmt, id)).Return(nil, nil)
150152
// create field table
153+
txClient.EXPECT().Exec(fmt.Sprintf(dropFieldsFmt, id)).Return(nil, nil)
151154
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsTableFmt, id, id, `"metadata.name" TEXT, "metadata.creationTimestamp" TEXT, "metadata.namespace" TEXT, "something" INT`)).Return(nil, nil)
152155
// create field table indexes
153156
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsIndexFmt, id, "metadata.name", id, "metadata.name")).Return(nil, nil)
154157
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsIndexFmt, id, "metadata.namespace", id, "metadata.namespace")).Return(nil, nil)
155158
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsIndexFmt, id, "metadata.creationTimestamp", id, "metadata.creationTimestamp")).Return(nil, nil)
156159
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsIndexFmt, id, fields[0][0], id, fields[0][0])).Return(nil, nil)
160+
// create labels table
161+
txClient.EXPECT().Exec(fmt.Sprintf(dropLabelsStmtFmt, id)).Return(nil, nil)
157162
txClient.EXPECT().Exec(fmt.Sprintf(createLabelsTableFmt, id, id)).Return(nil, nil)
158163
txClient.EXPECT().Exec(fmt.Sprintf(createLabelsTableIndexFmt, id, id)).Return(nil, nil)
159164
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
@@ -182,6 +187,7 @@ func TestNewListOptionIndexer(t *testing.T) {
182187
store.EXPECT().GetName().Return(id).AnyTimes()
183188
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
184189
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
190+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
185191
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(fmt.Errorf("error")).Do(
186192
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
187193
err := f(txClient)
@@ -207,6 +213,7 @@ func TestNewListOptionIndexer(t *testing.T) {
207213
store.EXPECT().GetName().Return(id).AnyTimes()
208214
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
209215
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
216+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
210217
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
211218
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
212219
err := f(txClient)
@@ -244,6 +251,7 @@ func TestNewListOptionIndexer(t *testing.T) {
244251
store.EXPECT().GetName().Return(id).AnyTimes()
245252
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
246253
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
254+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
247255
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
248256
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
249257
err := f(txClient)
@@ -262,7 +270,9 @@ func TestNewListOptionIndexer(t *testing.T) {
262270
store.EXPECT().RegisterAfterDeleteAll(gomock.Any()).Times(2)
263271
store.EXPECT().RegisterBeforeDropAll(gomock.Any()).AnyTimes()
264272

273+
txClient.EXPECT().Exec(fmt.Sprintf(dropEventsFmt, id)).Return(nil, nil)
265274
txClient.EXPECT().Exec(fmt.Sprintf(createEventsTableFmt, id)).Return(nil, nil)
275+
txClient.EXPECT().Exec(fmt.Sprintf(dropFieldsFmt, id)).Return(nil, nil)
266276
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsTableFmt, id, id, `"metadata.name" TEXT, "metadata.creationTimestamp" TEXT, "metadata.namespace" TEXT, "something" TEXT`)).Return(nil, nil)
267277
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsIndexFmt, id, "metadata.name", id, "metadata.name")).Return(nil, fmt.Errorf("error"))
268278
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(fmt.Errorf("error")).Do(
@@ -291,6 +301,7 @@ func TestNewListOptionIndexer(t *testing.T) {
291301
store.EXPECT().GetName().Return(id).AnyTimes()
292302
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
293303
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
304+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
294305
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
295306
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
296307
err := f(txClient)
@@ -309,12 +320,15 @@ func TestNewListOptionIndexer(t *testing.T) {
309320
store.EXPECT().RegisterAfterDeleteAll(gomock.Any()).Times(2)
310321
store.EXPECT().RegisterBeforeDropAll(gomock.Any()).AnyTimes()
311322

323+
txClient.EXPECT().Exec(fmt.Sprintf(dropEventsFmt, id)).Return(nil, nil)
312324
txClient.EXPECT().Exec(fmt.Sprintf(createEventsTableFmt, id)).Return(nil, nil)
325+
txClient.EXPECT().Exec(fmt.Sprintf(dropFieldsFmt, id)).Return(nil, nil)
313326
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsTableFmt, id, id, `"metadata.name" TEXT, "metadata.creationTimestamp" TEXT, "metadata.namespace" TEXT, "something" TEXT`)).Return(nil, nil)
314327
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsIndexFmt, id, "metadata.name", id, "metadata.name")).Return(nil, nil)
315328
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsIndexFmt, id, "metadata.namespace", id, "metadata.namespace")).Return(nil, nil)
316329
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsIndexFmt, id, "metadata.creationTimestamp", id, "metadata.creationTimestamp")).Return(nil, nil)
317330
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsIndexFmt, id, fields[0][0], id, fields[0][0])).Return(nil, nil)
331+
txClient.EXPECT().Exec(fmt.Sprintf(dropLabelsStmtFmt, id)).Return(nil, nil)
318332
txClient.EXPECT().Exec(fmt.Sprintf(createLabelsTableFmt, id, id)).Return(nil, fmt.Errorf("error"))
319333
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(fmt.Errorf("error")).Do(
320334
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
@@ -342,6 +356,7 @@ func TestNewListOptionIndexer(t *testing.T) {
342356
store.EXPECT().GetName().Return(id).AnyTimes()
343357
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
344358
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
359+
txClient.EXPECT().Exec(gomock.Any()).Return(nil, nil)
345360
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(nil).Do(
346361
func(ctx context.Context, shouldEncrypt bool, f db.WithTransactionFunction) {
347362
err := f(txClient)
@@ -360,12 +375,15 @@ func TestNewListOptionIndexer(t *testing.T) {
360375
store.EXPECT().RegisterAfterDeleteAll(gomock.Any()).Times(2)
361376
store.EXPECT().RegisterBeforeDropAll(gomock.Any()).AnyTimes()
362377

378+
txClient.EXPECT().Exec(fmt.Sprintf(dropEventsFmt, id)).Return(nil, nil)
363379
txClient.EXPECT().Exec(fmt.Sprintf(createEventsTableFmt, id)).Return(nil, nil)
380+
txClient.EXPECT().Exec(fmt.Sprintf(dropFieldsFmt, id)).Return(nil, nil)
364381
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsTableFmt, id, id, `"metadata.name" TEXT, "metadata.creationTimestamp" TEXT, "metadata.namespace" TEXT, "something" TEXT`)).Return(nil, nil)
365382
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsIndexFmt, id, "metadata.name", id, "metadata.name")).Return(nil, nil)
366383
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsIndexFmt, id, "metadata.namespace", id, "metadata.namespace")).Return(nil, nil)
367384
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsIndexFmt, id, "metadata.creationTimestamp", id, "metadata.creationTimestamp")).Return(nil, nil)
368385
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsIndexFmt, id, fields[0][0], id, fields[0][0])).Return(nil, nil)
386+
txClient.EXPECT().Exec(fmt.Sprintf(dropLabelsStmtFmt, id)).Return(nil, nil)
369387
txClient.EXPECT().Exec(fmt.Sprintf(createLabelsTableFmt, id, id)).Return(nil, nil)
370388
txClient.EXPECT().Exec(fmt.Sprintf(createLabelsTableIndexFmt, id, id)).Return(nil, nil)
371389
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(fmt.Errorf("error")).Do(

pkg/sqlcache/store/store.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ func NewStore(ctx context.Context, example any, keyFunc cache.KeyFunc, c db.Clie
9999

100100
// once multiple informer-factories are needed, this can accept the case where table already exists error is received
101101
err := s.WithTransaction(ctx, true, func(tx db.TxClient) error {
102+
dropTableQuery := fmt.Sprintf(dropBaseStmtFmt, dbName)
103+
if _, err := tx.Exec(dropTableQuery); err != nil {
104+
return err
105+
}
106+
102107
createTableQuery := fmt.Sprintf(createTableFmt, dbName)
103108
_, err := tx.Exec(createTableQuery)
104109
return err

0 commit comments

Comments
 (0)