2019-01-18 17:51:10 +03:00
|
|
|
package mapobjectdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const migrateScript = `
|
2019-01-23 10:37:40 +03:00
|
|
|
PRAGMA foreign_keys = ON;
|
2019-01-23 17:02:13 +03:00
|
|
|
PRAGMA journal_mode = MEMORY;
|
2019-01-23 10:37:40 +03:00
|
|
|
-- PRAGMA synchronous = OFF;
|
|
|
|
|
2019-01-18 17:51:10 +03:00
|
|
|
create table if not exists objects(
|
2019-01-21 17:13:33 +03:00
|
|
|
id integer primary key autoincrement,
|
2019-01-18 17:51:10 +03:00
|
|
|
x int,
|
|
|
|
y int,
|
|
|
|
z int,
|
|
|
|
posx int,
|
|
|
|
posy int,
|
|
|
|
posz int,
|
|
|
|
type varchar,
|
2019-01-21 17:13:33 +03:00
|
|
|
mtime bigint
|
|
|
|
);
|
|
|
|
|
2019-01-23 10:23:33 +03:00
|
|
|
create index if not exists objects_pos on objects(posx,posy,posz);
|
|
|
|
create index if not exists objects_pos_type on objects(posx,posy,posz,type);
|
|
|
|
|
2019-01-23 10:37:40 +03:00
|
|
|
create table if not exists object_attributes(
|
|
|
|
objectid integer not null,
|
|
|
|
key varchar not null,
|
|
|
|
value varchar not null,
|
|
|
|
FOREIGN KEY (objectid) references objects(id) ON DELETE CASCADE
|
2019-01-23 16:05:17 +03:00
|
|
|
primary key(objectid, key)
|
2019-01-23 10:37:40 +03:00
|
|
|
);
|
|
|
|
|
2019-01-21 17:13:33 +03:00
|
|
|
create table if not exists tiles(
|
|
|
|
data blob,
|
2019-01-18 17:51:10 +03:00
|
|
|
mtime bigint,
|
2019-01-21 17:13:33 +03:00
|
|
|
layerid int,
|
|
|
|
x int,
|
|
|
|
y int,
|
|
|
|
zoom int,
|
|
|
|
primary key(x,y,zoom,layerid)
|
2019-01-18 17:51:10 +03:00
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
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()
|
|
|
|
_, 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
|
|
|
|
}
|