Check actor event database schema version
This commit is contained in:
parent
32839f6919
commit
bf1fcf88b1
@ -59,6 +59,8 @@ var ddls = []string{
|
|||||||
`INSERT OR IGNORE INTO _meta (version) VALUES (1)`,
|
`INSERT OR IGNORE INTO _meta (version) VALUES (1)`,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const schemaVersion = 1
|
||||||
|
|
||||||
const (
|
const (
|
||||||
insertEvent = `INSERT OR IGNORE INTO event
|
insertEvent = `INSERT OR IGNORE INTO event
|
||||||
(height, tipset_key, tipset_key_cid, emitter_addr, event_index, message_cid, message_index, reverted)
|
(height, tipset_key, tipset_key_cid, emitter_addr, event_index, message_cid, message_index, reverted)
|
||||||
@ -86,12 +88,33 @@ func NewEventIndex(path string) (*EventIndex, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
q, err := db.Query("SELECT name FROM sqlite_master WHERE type='table' AND name='_meta';")
|
||||||
|
if err == sql.ErrNoRows || !q.Next() {
|
||||||
|
// empty database, create the schema
|
||||||
for _, ddl := range ddls {
|
for _, ddl := range ddls {
|
||||||
if _, err := db.Exec(ddl); err != nil {
|
if _, err := db.Exec(ddl); err != nil {
|
||||||
_ = db.Close()
|
_ = db.Close()
|
||||||
return nil, xerrors.Errorf("exec ddl %q: %w", ddl, err)
|
return nil, xerrors.Errorf("exec ddl %q: %w", ddl, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if err != nil {
|
||||||
|
_ = db.Close()
|
||||||
|
return nil, xerrors.Errorf("looking for _meta table: %w", err)
|
||||||
|
} else {
|
||||||
|
// Ensure we don't open a database from a different schema version
|
||||||
|
|
||||||
|
row := db.QueryRow("SELECT max(version) FROM _meta")
|
||||||
|
var version int
|
||||||
|
err := row.Scan(&version)
|
||||||
|
if err != nil {
|
||||||
|
_ = db.Close()
|
||||||
|
return nil, xerrors.Errorf("invalid database version: no version found")
|
||||||
|
}
|
||||||
|
if version != schemaVersion {
|
||||||
|
_ = db.Close()
|
||||||
|
return nil, xerrors.Errorf("invalid database version: got %d, expected %d", version, schemaVersion)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &EventIndex{
|
return &EventIndex{
|
||||||
db: db,
|
db: db,
|
||||||
|
Loading…
Reference in New Issue
Block a user