Skip to content

Commit

Permalink
Concurrency & Database updates
Browse files Browse the repository at this point in the history
 - Extractor concurrency.
 - Database SQL was missing 'INTO' keyword, it was added.
 - Moved 'init.sql'
 - Some minor clean-ups for c.complexity reasons
  • Loading branch information
HansenChristoffer committed Jun 7, 2024
1 parent c162537 commit c05cf63
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 173 deletions.
23 changes: 15 additions & 8 deletions cli/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ import (
"goaddons/database"
"goaddons/updater"
"goaddons/utils"
"log"
"os"
)

const (
databasePath string = "./bin/acd.db"
)

var db *sql.DB

func StartCli() {
utils.ClearScreen()

fmt.Printf(`
»»» GoAddons «««
Expand Down Expand Up @@ -58,8 +62,7 @@ func StartCli() {

func addonManagement() {
utils.ClearScreen()
tryDatabaseConnect()

initDatabase() // init database connection if it is not already initialized
fmt.Printf(`
»»» Addon Management «««
Expand Down Expand Up @@ -98,7 +101,7 @@ func addonManagement() {

func updaterMenu() {
utils.ClearScreen()

initDatabase() // init database connection if it is not already initialized
fmt.Printf(`
»»» Updater Menu «««
Expand All @@ -110,7 +113,7 @@ func updaterMenu() {
if len(input) > 0 {
switch input {
case "1":
updater.StartUpdater()
updater.StartUpdater(db)
case "X", "x":
StartCli()
default:
Expand All @@ -123,7 +126,6 @@ func updaterMenu() {

func about() {
utils.ClearScreen()

fmt.Printf(`
»»» About GoAddons «««
Expand All @@ -150,9 +152,14 @@ func about() {
StartCli()
}

func tryDatabaseConnect() {
// initDatabase initializes the global database connection.
func initDatabase() {
if db == nil {
db = database.ConnectToServer()
var err error
db, err = database.ConnectToServer(databasePath)
if err != nil {
log.Fatalf("cli:core.initDatabase():ConnectToServer(%s) -> %v", databasePath, err)
}
}
}

Expand Down
32 changes: 20 additions & 12 deletions database/Connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,54 @@ package database

import (
"database/sql"
"fmt"
"log"
"time"

_ "modernc.org/sqlite"
)

const (
databasePath = "./init.sql"
databaseType = "sqlite"
maxConnLifetimeInMinutes = 30
maxOpenConns = 1
maxIdleConns = 1
)

func ConnectToServer() (dbConn *sql.DB) {
// Get a database handle.
dbConn, err := sql.Open(databaseType, "./bin/acd.db")
// ConnectToServer opens a connection to the SQLite database and sets up the connection parameters.
func ConnectToServer(database string) (dbConn *sql.DB, err error) {
// Open a connection to the SQLite database.
dbConn, err = sql.Open(databaseType, database)
if err != nil {
log.Fatal(err)
return nil, fmt.Errorf("database:Connection.ConnectToServer():sql.Open(%s, %s) "+
"-> %w", databaseType, database, err)
}

// Set the connection parameters.
dbConn.SetConnMaxLifetime(time.Minute * maxConnLifetimeInMinutes)
dbConn.SetMaxOpenConns(maxOpenConns)
dbConn.SetMaxIdleConns(maxIdleConns)

pingErr := dbConn.Ping()
if pingErr != nil {
log.Fatal(pingErr)
// Ping the database to ensure the connection is valid.
err = dbConn.Ping()
if err != nil {
return nil, fmt.Errorf("database:Connection.ConnectToServer():dbConn.Ping() "+
"-> %w", err)
}
log.Println("Connected!")

// Stats
// Log connection statistics.
stats := dbConn.Stats()
log.Printf("\n### Statistics ###\nConnections: %d/%d\nNumber of active connections: %d\n"+
"Number of idle connections: %d\n",
stats.OpenConnections, stats.MaxOpenConnections, stats.InUse, stats.Idle)

err = ExecuteInitSQL(dbConn)
// Execute any initialization SQL.
err = ExecuteInitSQL(dbConn, databasePath)
if err != nil {
log.Fatal(err)
return nil, fmt.Errorf("database:Connection.ConnectToServer():ExecuteInitSQL(dbConn) "+
"-> %w", err)
}

return
return dbConn, nil
}
36 changes: 36 additions & 0 deletions database/DatabaseInitializerIT_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package database

import (
"database/sql"
"log"
"testing"

"github.com/stretchr/testify/assert"
)

const (
sqliteInMemoryOption = "file::memory:?cache=shared"
)

func TestInitDatabase(t *testing.T) {
dbConn, err := ConnectToServer(sqliteInMemoryOption)
assert.NotNil(t, dbConn)
assert.NoError(t, err)

defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Fatalf("TestInitDatabase: Problem occurred while trying to close database -> %v", err)
}
}(dbConn)

err = ExecuteInitSQL(dbConn, "./init.sql")
assert.NoError(t, err)

tables := [5]string{"system_config_default", "addon", "run_log", "download_log", "extract_log"}

for _, table := range tables {
sqliteTableCheck := "SELECT name FROM sqlite_master WHERE type='table' AND name='" + table + "'"
assert.NotNil(t, sqliteTableCheck)
}
}
6 changes: 3 additions & 3 deletions database/DatabaseLogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

func InsertRLog(db *sql.DB, rlog models.RLog) (int64, error) {
result, err := db.Exec("INSERT OR IGNORE run_log (run_id, service) VALUES (?, ?)",
result, err := db.Exec("INSERT OR IGNORE INTO run_log (run_id, service) VALUES (?, ?)",
rlog.RunId, rlog.Service)
if err != nil {
return 0, fmt.Errorf("InsertRLog: %v\n", err)
Expand All @@ -35,7 +35,7 @@ func InsertRLog(db *sql.DB, rlog models.RLog) (int64, error) {
}

func InsertDLog(db *sql.DB, dlog models.DLog) (int64, error) {
result, err := db.Exec("INSERT OR IGNORE download_log (run_id, url) VALUES (?, ?)",
result, err := db.Exec("INSERT OR IGNORE INTO download_log (run_id, url) VALUES (?, ?)",
dlog.RunId, dlog.Url)
if err != nil {
return 0, fmt.Errorf("InsertDLog: %v\n", err)
Expand All @@ -50,7 +50,7 @@ func InsertDLog(db *sql.DB, dlog models.DLog) (int64, error) {
}

func InsertELog(db *sql.DB, elog models.ELog) (int64, error) {
result, err := db.Exec("INSERT OR IGNORE extract_log (run_id, file) VALUES (?, ?)",
result, err := db.Exec("INSERT OR IGNORE INTO extract_log (run_id, file) VALUES (?, ?)",
elog.RunId, elog.File)
if err != nil {
return 0, fmt.Errorf("InsertELog: %v\n", err)
Expand Down
6 changes: 3 additions & 3 deletions database/DatabaseLogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestInsertRLog(t *testing.T) {

result := sqlmock.NewResult(1, 1)

mock.ExpectExec("INSERT OR IGNORE run_log \\(run_id, service\\) VALUES \\(\\?, \\?\\)").
mock.ExpectExec("INSERT OR IGNORE INTO run_log \\(run_id, service\\) VALUES \\(\\?, \\?\\)").
WithArgs(testUUID.String(), "TestService1").
WillReturnResult(result)

Expand All @@ -75,7 +75,7 @@ func TestInsertDLog(t *testing.T) {

result := sqlmock.NewResult(1, 1)

mock.ExpectExec("INSERT OR IGNORE download_log \\(run_id, url\\) VALUES \\(\\?, \\?\\)").
mock.ExpectExec("INSERT OR IGNORE INTO download_log \\(run_id, url\\) VALUES \\(\\?, \\?\\)").
WithArgs(testUUID.String(), "TestURL1").
WillReturnResult(result)

Expand All @@ -97,7 +97,7 @@ func TestInsertELog(t *testing.T) {

result := sqlmock.NewResult(1, 1)

mock.ExpectExec("INSERT OR IGNORE extract_log \\(run_id, file\\) VALUES \\(\\?, \\?\\)").
mock.ExpectExec("INSERT OR IGNORE INTO extract_log \\(run_id, file\\) VALUES \\(\\?, \\?\\)").
WithArgs(testUUID.String(), "TestFile1").
WillReturnResult(result)

Expand Down
4 changes: 2 additions & 2 deletions database/Initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strings"
)

func ExecuteInitSQL(dbConn *sql.DB) error {
sqlFileContent, err := os.ReadFile("init.sql")
func ExecuteInitSQL(dbConn *sql.DB, dbPath string) error {
sqlFileContent, err := os.ReadFile(dbPath)
if err != nil {
return err
}
Expand Down
File renamed without changes.
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package main

import (
"database/sql"
"fmt"
"goaddons/cli"
"goaddons/updater"
Expand All @@ -22,11 +23,13 @@ import (
"os"
)

var db *sql.DB

func main() {
if len(os.Args) >= 2 {
switch os.Args[1] {
case "--updater", "-u":
updater.StartUpdater()
updater.StartUpdater(db)
case "--cli", "c":
cli.StartCli()
case "--version", "-v":
Expand Down
Loading

0 comments on commit c05cf63

Please sign in to comment.