mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 04:14:39 +03:00
Add database optimise task (#3929)
* Add database optimise task * Wrap errors * US internationalisation --------- Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
This commit is contained in:
@@ -58,7 +58,7 @@ func (db *Anonymiser) Anonymise(ctx context.Context) error {
|
||||
func() error { return db.anonymiseStudios(ctx) },
|
||||
func() error { return db.anonymiseTags(ctx) },
|
||||
func() error { return db.anonymiseMovies(ctx) },
|
||||
func() error { db.optimise(); return nil },
|
||||
func() error { return db.Optimise(ctx) },
|
||||
})
|
||||
}(); err != nil {
|
||||
// delete the database
|
||||
|
||||
@@ -436,21 +436,28 @@ func (db *Database) RunMigrations() error {
|
||||
}
|
||||
|
||||
// optimize database after migration
|
||||
db.optimise()
|
||||
err = db.Optimise(ctx)
|
||||
if err != nil {
|
||||
logger.Warnf("error while performing post-migration optimisation: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *Database) optimise() {
|
||||
logger.Info("Optimizing database")
|
||||
_, err := db.db.Exec("ANALYZE")
|
||||
func (db *Database) Optimise(ctx context.Context) error {
|
||||
logger.Info("Optimising database")
|
||||
|
||||
err := db.Analyze(ctx)
|
||||
if err != nil {
|
||||
logger.Warnf("error while performing post-migration optimization: %v", err)
|
||||
return fmt.Errorf("performing optimization: %w", err)
|
||||
}
|
||||
_, err = db.db.Exec("VACUUM")
|
||||
|
||||
err = db.Vacuum(ctx)
|
||||
if err != nil {
|
||||
logger.Warnf("error while performing post-migration vacuum: %v", err)
|
||||
return fmt.Errorf("performing vacuum: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Vacuum runs a VACUUM on the database, rebuilding the database file into a minimal amount of disk space.
|
||||
@@ -459,6 +466,12 @@ func (db *Database) Vacuum(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Analyze runs an ANALYZE on the database to improve query performance.
|
||||
func (db *Database) Analyze(ctx context.Context) error {
|
||||
_, err := db.db.ExecContext(ctx, "ANALYZE")
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *Database) ExecSQL(ctx context.Context, query string, args []interface{}) (*int64, *int64, error) {
|
||||
wrapper := dbWrapper{}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user