diff --git a/example_config.ini b/example_config.ini index f89f33c..a8ec85e 100644 --- a/example_config.ini +++ b/example_config.ini @@ -19,7 +19,9 @@ db-type = sqlite3 # # Sqlite example DSN: "file:moneygo.sqlite?cache=shared&mode=rwc" # MySQL documentation: https://github.com/go-sql-driver/mysql/#dsn-data-source-name -# example DSN: "user:password@localhost/dbname" +# example DSN: "user:password@localhost/dbname&parseTime=true" +# (Note: MySQL DSN's *must* include the +# "parseTime=true" parameter) # Postgres documentation: https://godoc.org/github.com/lib/pq # example DSN: "postgres://user:password@localhost/dbname" db-dsn = file:moneygo.sqlite?cache=shared&mode=rwc diff --git a/internal/db/db.go b/internal/db/db.go index f1d976c..80e3b9d 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -9,6 +9,8 @@ import ( _ "github.com/lib/pq" _ "github.com/mattn/go-sqlite3" "gopkg.in/gorp.v1" + "log" + "strings" ) func GetDbMap(db *sql.DB, dbtype config.DbType) (*gorp.DbMap, error) { @@ -43,3 +45,10 @@ func GetDbMap(db *sql.DB, dbtype config.DbType) (*gorp.DbMap, error) { return dbmap, nil } + +func GetDSN(dbtype config.DbType, dsn string) string { + if dbtype == config.MySQL && !strings.Contains(dsn, "parseTime=true") { + log.Fatalf("The DSN for MySQL MUST contain 'parseTime=True' but does not!") + } + return dsn +} diff --git a/internal/handlers/common_test.go b/internal/handlers/common_test.go index c0d33cb..9e993c8 100644 --- a/internal/handlers/common_test.go +++ b/internal/handlers/common_test.go @@ -182,7 +182,8 @@ func RunTests(m *testing.M) int { defer os.RemoveAll(tmpdir) dbpath := path.Join(tmpdir, "moneygo.sqlite") - database, err := sql.Open("sqlite3", "file:"+dbpath+"?cache=shared&mode=rwc") + dsn := db.GetDSN(config.SQLite, "file:"+dbpath+"?cache=shared&mode=rwc") + database, err := sql.Open("sqlite3", dsn) if err != nil { log.Fatal(err) } diff --git a/internal/handlers/transactions.go b/internal/handlers/transactions.go index 86bc6a8..bf89ae9 100644 --- a/internal/handlers/transactions.go +++ b/internal/handlers/transactions.go @@ -316,8 +316,8 @@ func UpdateTransaction(tx *Tx, t *Transaction, user *User) error { if err != nil { return err } - if count != 1 { - return errors.New("Updated more than one transaction split") + if count > 1 { + return fmt.Errorf("Updated %d transaction splits while attempting to update only 1", count) } delete(s_map, t.Splits[i].SplitId) } else { @@ -360,8 +360,8 @@ func UpdateTransaction(tx *Tx, t *Transaction, user *User) error { if err != nil { return err } - if count != 1 { - return errors.New("Updated more than one transaction") + if count > 1 { + return fmt.Errorf("Updated %d transactions (expected 1)", count) } return nil diff --git a/main.go b/main.go index b426656..4363e82 100644 --- a/main.go +++ b/main.go @@ -66,7 +66,8 @@ func staticHandler(w http.ResponseWriter, r *http.Request, basedir string) { } func main() { - database, err := sql.Open(cfg.MoneyGo.DBType.String(), cfg.MoneyGo.DSN) + dsn := db.GetDSN(cfg.MoneyGo.DbType, cfg.MoneyGo.DSN) + database, err := sql.Open(cfg.MoneyGo.DBType.String(), dsn) if err != nil { log.Fatal(err) }