postgres wip

This commit is contained in:
Thomas Rudin 2019-02-09 18:54:26 +01:00
parent 26d5de407a
commit 13aade9bcc
3 changed files with 49 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package app
import ( import (
"mapserver/colormapping" "mapserver/colormapping"
"mapserver/db/postgres"
"mapserver/db/sqlite" "mapserver/db/sqlite"
"mapserver/eventbus" "mapserver/eventbus"
"mapserver/mapblockaccessor" "mapserver/mapblockaccessor"
@ -39,6 +40,13 @@ func Setup(p params.ParamsType, cfg *Config) *App {
if err != nil { if err != nil {
panic(err) panic(err)
} }
case worldconfig.BACKEND_POSTGRES:
a.Blockdb, err = postgres.New(a.Worldconfig[worldconfig.CONFIG_PSQL_CONNECTION])
if err != nil {
panic(err)
}
default: default:
panic(errors.New("map-backend not supported: " + a.Worldconfig[worldconfig.CONFIG_BACKEND])) panic(errors.New("map-backend not supported: " + a.Worldconfig[worldconfig.CONFIG_BACKEND]))
} }

View File

@ -0,0 +1,11 @@
package postgres
import (
"github.com/sirupsen/logrus"
)
var log *logrus.Entry
func init() {
log = logrus.WithFields(logrus.Fields{"prefix": "postgres-db"})
}

View File

@ -2,9 +2,12 @@ package postgres
import ( import (
"database/sql" "database/sql"
_ "github.com/lib/pq"
"mapserver/coords" "mapserver/coords"
"mapserver/db" "mapserver/db"
"time"
_ "github.com/lib/pq"
"github.com/sirupsen/logrus"
) )
type PostgresAccessor struct { type PostgresAccessor struct {
@ -12,6 +15,24 @@ type PostgresAccessor struct {
} }
func (db *PostgresAccessor) Migrate() error { func (db *PostgresAccessor) Migrate() error {
hasMtime := true
_, err := db.db.Query("select max(mtime) from blocks")
if err != nil {
hasMtime = false
}
if !hasMtime {
log.Info("Migrating database")
start := time.Now()
_, err = db.db.Exec(migrateScript)
if err != nil {
return err
}
t := time.Now()
elapsed := t.Sub(start)
log.WithFields(logrus.Fields{"elapsed": elapsed}).Info("Migration completed")
}
return nil return nil
} }
@ -121,6 +142,12 @@ func (this *PostgresAccessor) GetBlock(pos *coords.MapBlockCoords) (*db.Block, e
return nil, nil return nil, nil
} }
func NewPostgresAccessor(connStr string) (*PostgresAccessor, error) { func New(connStr string) (*PostgresAccessor, error) {
return nil, nil db, err := sql.Open("postgres", connStr)
if err != nil {
return nil, err
}
sq := &PostgresAccessor{db: db}
return sq, nil
} }