feat(database): add a flag to compact on startup BE-12283 (#1255)

This commit is contained in:
andres-portainer
2025-09-24 18:44:09 -03:00
committed by GitHub
parent c8ee2ca4a1
commit b7384874cf
9 changed files with 142 additions and 10 deletions

View File

@@ -6,6 +6,8 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.etcd.io/bbolt"
)
func Test_NeedsEncryptionMigration(t *testing.T) {
@@ -119,3 +121,57 @@ func Test_NeedsEncryptionMigration(t *testing.T) {
})
}
}
func TestDBCompaction(t *testing.T) {
db := &DbConnection{
Path: t.TempDir(),
Compact: true,
}
err := db.Open()
require.NoError(t, err)
err = db.Update(func(tx *bbolt.Tx) error {
b, err := tx.CreateBucketIfNotExists([]byte("testbucket"))
if err != nil {
return err
}
b.Put([]byte("key"), []byte("value"))
return nil
})
require.NoError(t, err)
err = db.Close()
require.NoError(t, err)
// Reopen the DB to trigger compaction
err = db.Open()
require.NoError(t, err)
// Check that the data is still there
err = db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte("testbucket"))
if b == nil {
return nil
}
val := b.Get([]byte("key"))
require.Equal(t, []byte("value"), val)
return nil
})
require.NoError(t, err)
err = db.Close()
require.NoError(t, err)
// Failures
err = os.Mkdir(db.GetDatabaseFilePath()+compactedSuffix, 0o755)
require.NoError(t, err)
err = db.Open()
require.NoError(t, err)
}