Implement special flags for dump and reset db

This commit is contained in:
2025-07-20 11:47:11 +02:00
parent 774ac0f0ca
commit 9ecbbff6fa
3 changed files with 76 additions and 0 deletions

View File

@@ -15,6 +15,8 @@ type DB interface {
Raw(sql string, args ...any) *gorm.DB
SaveFile(filePath string, fileData []byte) error
GetFile(filePath string) ([]byte, error)
GetAllFiles() ([]FileSnapshot, error)
RemoveAllFiles() error
}
type FileSnapshot struct {
@@ -93,3 +95,26 @@ func (db *DBWrapper) GetFile(filePath string) ([]byte, error) {
log.Debug("File found in database")
return fileSnapshot.FileData, nil
}
func (db *DBWrapper) GetAllFiles() ([]FileSnapshot, error) {
log := cylogger.Default.WithPrefix("GetAllFiles")
log.Debug("Getting all files from database")
var fileSnapshots []FileSnapshot
err := db.db.Model(&FileSnapshot{}).Find(&fileSnapshots).Error
if err != nil {
return nil, err
}
log.Debug("Found %d files in database", len(fileSnapshots))
return fileSnapshots, nil
}
func (db *DBWrapper) RemoveAllFiles() error {
log := cylogger.Default.WithPrefix("RemoveAllFiles")
log.Debug("Removing all files from database")
err := db.db.Exec("DELETE FROM file_snapshots").Error
if err != nil {
return err
}
log.Debug("All files removed from database")
return nil
}

View File

@@ -71,3 +71,23 @@ func ResetWhereNecessary(associations map[string]FileCommandAssociation, db DB)
log.Debug("Done")
return nil
}
func ResetAllFiles(db DB) error {
log := cylogger.Default.WithPrefix("ResetAllFiles")
log.Debug("Start")
fileSnapshots, err := db.GetAllFiles()
if err != nil {
return err
}
log.Debug("Found %d files in database", len(fileSnapshots))
for _, fileSnapshot := range fileSnapshots {
log.Debug("Resetting file %q", fileSnapshot.FilePath)
err = os.WriteFile(fileSnapshot.FilePath, fileSnapshot.FileData, 0644)
if err != nil {
return err
}
log.Debug("File %q written to disk", fileSnapshot.FilePath)
}
log.Debug("Done")
return nil
}