Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

fix: enable auto vacuum to ensure DB does not grow infinitely #399

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions migrations/202406071726_vacuum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package migrations

import (
_ "embed"

"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)

// VACUUM to finish the update the vacuum mode to auto_vacuum
// See https://sqlite.org/pragma.html
// "The database connection can be changed between full and incremental autovacuum mode at any time.
// However, changing from "none" to "full" or "incremental" can only occur when the database is new
// (no tables have yet been created) or by running the VACUUM command."
var _202406071726_vacuum = &gormigrate.Migration{
ID: "202406071726_vacuum",
Migrate: func(tx *gorm.DB) error {
if err := tx.Exec("VACUUM").Error; err != nil {
return err
}

return nil
},
Rollback: func(tx *gorm.DB) error {
return nil
},
}
1 change: 1 addition & 0 deletions migrations/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func Migrate(db *gorm.DB, appConfig *config.AppConfig, logger *logrus.Logger) er
_202404021909_nullable_expires_at,
_202405302121_store_decrypted_request,
_202406061259_delete_content,
_202406071726_vacuum,
})

return m.Migrate()
Expand Down
10 changes: 8 additions & 2 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,14 @@ func NewService(ctx context.Context) (*Service, error) {
if err != nil {
return nil, err
}
// Enable foreign keys for sqlite
gormDB.Exec("PRAGMA foreign_keys=ON;")
err = gormDB.Exec("PRAGMA foreign_keys=ON;").Error
if err != nil {
return nil, err
}
err = gormDB.Exec("PRAGMA auto_vacuum=FULL;").Error
if err != nil {
return nil, err
}
sqlDb, err = gormDB.DB()
if err != nil {
return nil, err
Expand Down
Loading