1
0
mirror of https://github.com/rancher/steve.git synced 2025-09-23 20:38:55 +00:00

Drop tables instead of removing the database completely (#807)

* Add drop tables

* Fix deadlock

* Use f.Stop in tests

* Rename dropAllStmtFmt to dropBaseStmtFmt

* Fix comment

* More dropAll->dropBase renaming
This commit is contained in:
Tom Lebreux
2025-09-09 15:52:02 -04:00
committed by GitHub
parent 7cff39371f
commit ca3fa10db5
13 changed files with 257 additions and 39 deletions

View File

@@ -65,10 +65,12 @@ func makeListOptionIndexer(ctx context.Context, opts ListOptionIndexerOptions, s
if err != nil {
return nil, "", err
}
for _, item := range nsList.Items {
err = listOptionIndexer.Add(&item)
if err != nil {
return nil, "", err
if nsList != nil {
for _, item := range nsList.Items {
err = listOptionIndexer.Add(&item)
if err != nil {
return nil, "", err
}
}
}
@@ -144,6 +146,7 @@ func TestNewListOptionIndexer(t *testing.T) {
store.EXPECT().RegisterAfterUpdate(gomock.Any()).Times(3)
store.EXPECT().RegisterAfterDelete(gomock.Any()).Times(3)
store.EXPECT().RegisterAfterDeleteAll(gomock.Any()).Times(2)
store.EXPECT().RegisterBeforeDropAll(gomock.Any()).AnyTimes()
// create events table
txClient.EXPECT().Exec(fmt.Sprintf(createEventsTableFmt, id)).Return(nil, nil)
@@ -221,6 +224,7 @@ func TestNewListOptionIndexer(t *testing.T) {
store.EXPECT().RegisterAfterUpdate(gomock.Any()).Times(3)
store.EXPECT().RegisterAfterDelete(gomock.Any()).Times(3)
store.EXPECT().RegisterAfterDeleteAll(gomock.Any()).Times(2)
store.EXPECT().RegisterBeforeDropAll(gomock.Any()).AnyTimes()
store.EXPECT().WithTransaction(gomock.Any(), true, gomock.Any()).Return(fmt.Errorf("error"))
@@ -256,6 +260,7 @@ func TestNewListOptionIndexer(t *testing.T) {
store.EXPECT().RegisterAfterUpdate(gomock.Any()).Times(3)
store.EXPECT().RegisterAfterDelete(gomock.Any()).Times(3)
store.EXPECT().RegisterAfterDeleteAll(gomock.Any()).Times(2)
store.EXPECT().RegisterBeforeDropAll(gomock.Any()).AnyTimes()
txClient.EXPECT().Exec(fmt.Sprintf(createEventsTableFmt, id)).Return(nil, nil)
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsTableFmt, id, `"metadata.name" TEXT, "metadata.creationTimestamp" TEXT, "metadata.namespace" TEXT, "something" TEXT`)).Return(nil, nil)
@@ -301,6 +306,7 @@ func TestNewListOptionIndexer(t *testing.T) {
store.EXPECT().RegisterAfterUpdate(gomock.Any()).Times(3)
store.EXPECT().RegisterAfterDelete(gomock.Any()).Times(3)
store.EXPECT().RegisterAfterDeleteAll(gomock.Any()).Times(2)
store.EXPECT().RegisterBeforeDropAll(gomock.Any()).AnyTimes()
txClient.EXPECT().Exec(fmt.Sprintf(createEventsTableFmt, id)).Return(nil, nil)
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsTableFmt, id, `"metadata.name" TEXT, "metadata.creationTimestamp" TEXT, "metadata.namespace" TEXT, "something" TEXT`)).Return(nil, nil)
@@ -350,6 +356,7 @@ func TestNewListOptionIndexer(t *testing.T) {
store.EXPECT().RegisterAfterUpdate(gomock.Any()).Times(3)
store.EXPECT().RegisterAfterDelete(gomock.Any()).Times(3)
store.EXPECT().RegisterAfterDeleteAll(gomock.Any()).Times(2)
store.EXPECT().RegisterBeforeDropAll(gomock.Any()).AnyTimes()
txClient.EXPECT().Exec(fmt.Sprintf(createEventsTableFmt, id)).Return(nil, nil)
txClient.EXPECT().Exec(fmt.Sprintf(createFieldsTableFmt, id, `"metadata.name" TEXT, "metadata.creationTimestamp" TEXT, "metadata.namespace" TEXT, "something" TEXT`)).Return(nil, nil)
@@ -1223,6 +1230,35 @@ func TestNewListOptionIndexerEasy(t *testing.T) {
}
}
func TestDropAll(t *testing.T) {
ctx := t.Context()
opts := ListOptionIndexerOptions{
IsNamespaced: true,
}
loi, dbPath, err := makeListOptionIndexer(ctx, opts, false, nil)
defer cleanTempFiles(dbPath)
assert.NoError(t, err)
obj1 := &unstructured.Unstructured{
Object: map[string]any{
"metadata": map[string]any{
"name": "obj1",
},
},
}
err = loi.Add(obj1)
assert.NoError(t, err)
_, _, _, err = loi.ListByOptions(ctx, &sqltypes.ListOptions{}, []partition.Partition{{All: true}}, "")
assert.NoError(t, err)
loi.DropAll(ctx)
_, _, _, err = loi.ListByOptions(ctx, &sqltypes.ListOptions{}, []partition.Partition{{All: true}}, "")
assert.Error(t, err)
}
func makePseudoRandomList(size int) *unstructured.UnstructuredList {
numLength := 1 + int(math.Floor(math.Log10(float64(size))))
name_template := fmt.Sprintf("n%%0%dd", numLength)