diff --git a/migrations/202406071726_vacuum.go b/migrations/202406071726_vacuum.go new file mode 100644 index 00000000..d405cbfd --- /dev/null +++ b/migrations/202406071726_vacuum.go @@ -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 + }, +} diff --git a/migrations/migrate.go b/migrations/migrate.go index dbc30ab7..276567e6 100644 --- a/migrations/migrate.go +++ b/migrations/migrate.go @@ -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() diff --git a/service.go b/service.go index de2e7eb4..d552fa32 100644 --- a/service.go +++ b/service.go @@ -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