1
0
forked from MTSR/mapserver
mapserver/mapobjectdb/sqlite/migrate.go
2021-04-12 13:03:10 +02:00

49 lines
971 B
Go

package sqlite
import (
"database/sql"
"mapserver/public"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/sirupsen/logrus"
)
type Sqlite3Accessor struct {
db *sql.DB
filename string
}
func (db *Sqlite3Accessor) Migrate() error {
log.WithFields(logrus.Fields{"filename": db.filename}).Info("Migrating database")
start := time.Now()
sql, err := public.Files.ReadFile("sql/sqlite_mapobjectdb_migrate.sql")
if err != nil {
return err
}
_, err = db.db.Exec(string(sql))
if err != nil {
return err
}
t := time.Now()
elapsed := t.Sub(start)
log.WithFields(logrus.Fields{"elapsed": elapsed}).Info("Migration completed")
return nil
}
func (db *Sqlite3Accessor) EnableSpeedSafetyTradeoff(enableSpeed bool) error {
if enableSpeed {
_, err := db.db.Exec("PRAGMA journal_mode = MEMORY; PRAGMA synchronous = OFF;")
return err
} else {
_, err := db.db.Exec("PRAGMA journal_mode = TRUNCATE; PRAGMA synchronous = ON;")
return err
}
}