Skip to content

Commit

Permalink
Merge pull request #215 from datarootsio/adding-in-progress-status
Browse files Browse the repository at this point in the history
Implementing a cleanup process for old records
  • Loading branch information
bart6114 authored Oct 7, 2024
2 parents 6e95c59 + ac12fd0 commit abdddf7
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.23
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0=
github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
Expand Down
14 changes: 14 additions & 0 deletions pkg/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func OpenDB(dbPath string) (*sqlx.DB, error) {
}

func InitDB(db *sqlx.DB) error {
// Create the log table if it doesn't exist
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
job TEXT,
Expand All @@ -36,5 +37,18 @@ func InitDB(db *sqlx.DB) error {
return fmt.Errorf("create log table: %w", err)
}

// Perform cleanup to remove old, non-conforming records
_, err = db.Exec(`
DELETE FROM log
WHERE id NOT IN (
SELECT MIN(id)
FROM log
GROUP BY job, triggered_at, triggered_by
);
`)
if err != nil {
return fmt.Errorf("cleanup old log records: %w", err)
}

return nil
}
89 changes: 89 additions & 0 deletions pkg/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package cheek

import (
"testing"

"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3" // Import SQLite driver for database/sql
"github.com/stretchr/testify/assert"
)

// TestInitDB tests the InitDB function, including the cleanup logic.
func TestInitDB(t *testing.T) {
// Create an in-memory SQLite database
db, err := sqlx.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("Failed to open in-memory database: %v", err)
}
defer db.Close()

// Create the log table without the UNIQUE constraint temporarily
_, err = db.Exec(`CREATE TABLE log_temp (
id INTEGER PRIMARY KEY AUTOINCREMENT,
job TEXT,
triggered_at DATETIME DEFAULT CURRENT_TIMESTAMP,
triggered_by TEXT,
duration INTEGER,
status INTEGER,
message TEXT
)`)
if err != nil {
t.Fatalf("Failed to create temporary log table: %v", err)
}

// Insert conflicting data into the temporary log table
_, err = db.Exec(`
INSERT INTO log_temp (job, triggered_at, triggered_by, duration, status, message) VALUES
('job1', '2023-10-01 10:00:00', 'user1', 120, 1, 'Success'),
('job1', '2023-10-01 10:00:00', 'user1', 150, 1, 'Success'), -- Duplicate
('job1', '2023-10-01 11:00:00', 'user2', 90, 0, 'Failed')
`)
if err != nil {
t.Fatalf("Failed to insert test data: %v", err)
}

// Create the actual log table with the UNIQUE constraint
_, err = db.Exec(`CREATE TABLE log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
job TEXT,
triggered_at DATETIME DEFAULT CURRENT_TIMESTAMP,
triggered_by TEXT,
duration INTEGER,
status INTEGER,
message TEXT,
UNIQUE(job, triggered_at, triggered_by)
)`)
if err != nil {
t.Fatalf("Failed to create log table: %v", err)
}

// Move data from the temporary table to the log table
_, err = db.Exec(`
INSERT INTO log (job, triggered_at, triggered_by, duration, status, message)
SELECT job, triggered_at, triggered_by, duration, status, message
FROM log_temp
WHERE true
ON CONFLICT (job, triggered_at, triggered_by) DO NOTHING
`)

if err != nil {
t.Fatalf("Failed to transfer data to log table: %v", err)
}

//Drop the temporary table
_, err = db.Exec("DROP TABLE log_temp;")
if err != nil {
t.Fatalf("Failed to drop temporary log table: %v", err)
}

// Call the InitDB function
err = InitDB(db)
assert.NoError(t, err, "InitDB should not return an error")

// Check if cleanup worked correctly
var cleanedCount int
err = db.Get(&cleanedCount, "SELECT COUNT(*) FROM log")
assert.NoError(t, err, "Querying the log table should not return an error")
assert.Equal(t, 2, cleanedCount, "There should be 2 unique records in the log table after cleanup")

}

0 comments on commit abdddf7

Please sign in to comment.