Commit cea8d211 authored by Las Zenow's avatar Las Zenow

Clean up old submissions

parent 60379ba1
...@@ -75,6 +75,7 @@ func Init(options Options) (DB, error) { ...@@ -75,6 +75,7 @@ func Init(options Options) (DB, error) {
return nil, err return nil, err
} }
go db.frontPageUpdater() go db.frontPageUpdater()
go db.submissionsCleaner()
return db, nil return db, nil
} }
......
package database package database
// TODO: clean up old submissions import (
"fmt"
"time"
"github.com/prometheus/common/log"
)
const (
daysExpireSubmission = 30
)
type Submission struct { type Submission struct {
ID int `sql:"type:serial"` ID int `sql:"type:serial"`
...@@ -8,6 +17,7 @@ type Submission struct { ...@@ -8,6 +17,7 @@ type Submission struct {
Filename string Filename string
Status string Status string
Comment string Comment string
LastModified time.Time
UserID int `sql:"type:integer,unique"` UserID int `sql:"type:integer,unique"`
User *User User *User
BookID string `sql:"type:varchar(16),unique"` BookID string `sql:"type:varchar(16),unique"`
...@@ -21,6 +31,7 @@ func (db *pgDB) AddSubmission(submission Submission, userName string) (id int, e ...@@ -21,6 +31,7 @@ func (db *pgDB) AddSubmission(submission Submission, userName string) (id int, e
submission.UserID = user.ID submission.UserID = user.ID
} }
} }
submission.LastModified = time.Now()
err = db.sql.Insert(&submission) err = db.sql.Insert(&submission)
return submission.ID, err return submission.ID, err
} }
...@@ -29,6 +40,7 @@ func (db *pgDB) UpdateSubmission(id int, status string, book *Book) error { ...@@ -29,6 +40,7 @@ func (db *pgDB) UpdateSubmission(id int, status string, book *Book) error {
_, err := db.sql.Model(&Submission{}). _, err := db.sql.Model(&Submission{}).
Set("status = ?", status). Set("status = ?", status).
Set("book_id = ?", extractID(book)). Set("book_id = ?", extractID(book)).
Set("last_modified = CURRENT_TIMESTAMP").
Where("id = ?", id). Where("id = ?", id).
Update() Update()
return err return err
...@@ -38,6 +50,7 @@ func (db *pgDB) UpdateSubmissionByBook(bookID string, status string, book *Book) ...@@ -38,6 +50,7 @@ func (db *pgDB) UpdateSubmissionByBook(bookID string, status string, book *Book)
_, err := db.sql.Model(&Submission{}). _, err := db.sql.Model(&Submission{}).
Set("status = ?", status). Set("status = ?", status).
Set("book_id = ?", extractID(book)). Set("book_id = ?", extractID(book)).
Set("last_modified = CURRENT_TIMESTAMP").
Where("book_id = ?", bookID). Where("book_id = ?", bookID).
Update() Update()
return err return err
...@@ -75,11 +88,26 @@ func (db *pgDB) GetUserSubmissions(userName string) (submission []Submission, er ...@@ -75,11 +88,26 @@ func (db *pgDB) GetUserSubmissions(userName string) (submission []Submission, er
err = db.sql.Model(&submission). err = db.sql.Model(&submission).
Column("Book", "User"). Column("Book", "User").
Where("username = ?", userName). Where("username = ?", userName).
Order("id DESC"). Order("last_modified DESC").
Select() Select()
return return
} }
func (db *pgDB) submissionsCleaner() {
periodicity := 60 * time.Minute
for true {
time.Sleep(periodicity)
_, err := db.sql.Model(&Submission{}).
Where("book_id is null OR book_id in (SELECT id FROM books WHERE active is true)").
Where("last_modified < CURRENT_TIMESTAMP - interval ?", fmt.Sprintf("%d days", daysExpireSubmission)).
Delete()
if err != nil {
log.Error("Error deleting submissions: ", err)
}
}
}
func extractID(book *Book) interface{} { func extractID(book *Book) interface{} {
if book == nil { if book == nil {
return nil return nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment