1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-10-30 15:50:04 -04:00
moneygo/internal/db/db.go
Aaron Lindsay 3869f3a3b2 Get all current tests to pass with MySQL
This is done by requiring 'parseTime=true' in the MySQL DSN's (required
by github.com/go-sql-driver/mysql when Scan()ing to time.Time's), and
not forcing update counts to match if rows were updated to what they
already were.
2017-10-24 20:57:55 -04:00

55 lines
1.7 KiB
Go

package db
import (
"database/sql"
"fmt"
"github.com/aclindsa/moneygo/internal/config"
"github.com/aclindsa/moneygo/internal/handlers"
_ "github.com/go-sql-driver/mysql"
_ "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) {
var dialect gorp.Dialect
if dbtype == config.SQLite {
dialect = gorp.SqliteDialect{}
} else if dbtype == config.MySQL {
dialect = gorp.MySQLDialect{
Engine: "InnoDB",
Encoding: "UTF8",
}
} else if dbtype == config.Postgres {
dialect = gorp.PostgresDialect{}
} else {
return nil, fmt.Errorf("Don't know gorp dialect to go with '%s' DB type", dbtype.String())
}
dbmap := &gorp.DbMap{Db: db, Dialect: dialect}
dbmap.AddTableWithName(handlers.User{}, "users").SetKeys(true, "UserId")
dbmap.AddTableWithName(handlers.Session{}, "sessions").SetKeys(true, "SessionId")
dbmap.AddTableWithName(handlers.Account{}, "accounts").SetKeys(true, "AccountId")
dbmap.AddTableWithName(handlers.Security{}, "securities").SetKeys(true, "SecurityId")
dbmap.AddTableWithName(handlers.Transaction{}, "transactions").SetKeys(true, "TransactionId")
dbmap.AddTableWithName(handlers.Split{}, "splits").SetKeys(true, "SplitId")
dbmap.AddTableWithName(handlers.Price{}, "prices").SetKeys(true, "PriceId")
dbmap.AddTableWithName(handlers.Report{}, "reports").SetKeys(true, "ReportId")
err := dbmap.CreateTablesIfNotExists()
if err != nil {
return nil, err
}
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
}