Merge pull request #13 from caarlos0/10

Allowing the use of an existing database/sql pool
This commit is contained in:
Anton Lindström 2015-09-09 14:14:02 +02:00
commit 49077d4e04
1 changed files with 12 additions and 7 deletions

View File

@ -31,9 +31,19 @@ type Session struct {
ExpiresOn time.Time `db:"expires_on"`
}
// NewPGStore creates a new PGStore instance
// NewPGStore creates a new PGStore instance and a new database/sql pool
func NewPGStore(dbURL string, keyPairs ...[]byte) *PGStore {
db, err := sql.Open("postgres", dbURL)
if err != nil {
// Ignore and return nil
return nil
}
return NewPGStoreFromPool(db, keyPairs...)
}
// NewPGStoreFromPool creates a new PGStore instance from an existing
// database/sql pool
func NewPGStoreFromPool(db *sql.DB, keyPairs ...[]byte) *PGStore {
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}}
dbStore := &PGStore{
@ -45,14 +55,9 @@ func NewPGStore(dbURL string, keyPairs ...[]byte) *PGStore {
DbMap: dbmap,
}
if err != nil {
// Ignore and return nil
return nil
}
// Create table if it doesn't exist
dbmap.AddTableWithName(Session{}, "http_sessions").SetKeys(true, "Id")
err = dbmap.CreateTablesIfNotExists()
err := dbmap.CreateTablesIfNotExists()
if err != nil {
// Ignore and return nil