mapserver/mapobjectdb/sqlite/migrate.go

49 lines
971 B
Go
Raw Normal View History

2019-02-05 15:25:01 +03:00
package sqlite
2019-01-18 17:51:10 +03:00
import (
"database/sql"
2021-04-12 14:03:10 +03:00
"mapserver/public"
"time"
2019-01-18 17:51:10 +03:00
_ "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()
2021-04-12 14:03:10 +03:00
sql, err := public.Files.ReadFile("sql/sqlite_mapobjectdb_migrate.sql")
2019-01-18 17:51:10 +03:00
if err != nil {
return err
}
2021-04-12 14:03:10 +03:00
_, err = db.db.Exec(string(sql))
if err != nil {
return err
}
2019-01-18 17:51:10 +03:00
t := time.Now()
elapsed := t.Sub(start)
log.WithFields(logrus.Fields{"elapsed": elapsed}).Info("Migration completed")
return nil
}
2019-01-29 20:00:00 +03:00
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
}
}