forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite.go
105 lines (99 loc) · 3.26 KB
/
sqlite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2018 ETH Zurich, Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sqlite
import (
"database/sql"
"fmt"
"github.com/scionproto/scion/go/lib/common"
)
// New returns a new SQLite backend opening a database at the given path. If
// no database exists a new database is be created. If the schema version of the
// stored database is different from schemaVersion, an error is returned.
func New(path string, schema string, schemaVersion int) (*sql.DB, error) {
var err error
db, err := open(path)
if err != nil {
return nil, err
}
// On future errors, close the sql database before exiting
defer func() {
if err != nil {
db.Close()
}
}()
// prevent weird errors. (see https://stackoverflow.com/a/35805826)
db.SetMaxOpenConns(1)
if _, err = db.Exec("PRAGMA journal_mode=WAL"); err != nil {
return nil, common.NewBasicError("Unable to set WAL journal mode", err)
}
// Check the schema version and set up new DB if necessary.
var existingVersion int
err = db.QueryRow("PRAGMA user_version;").Scan(&existingVersion)
if err != nil {
return nil, common.NewBasicError("Failed to check schema version", err)
}
if existingVersion == 0 {
if err = setup(db, schema, schemaVersion); err != nil {
return nil, err
}
} else if existingVersion != schemaVersion {
return nil, common.NewBasicError("Database schema version mismatch", nil,
"expected", schemaVersion, "have", existingVersion)
}
return db, nil
}
func open(path string) (*sql.DB, error) {
var err error
// Add foreign_key parameter to path to enable foreign key support.
uri := fmt.Sprintf("%s?_foreign_keys=1", path)
db, err := sql.Open("sqlite3", uri)
if err != nil {
return nil, common.NewBasicError("Couldn't open SQLite database", err)
}
// On future errors, close the sql database before exiting
defer func() {
if err != nil {
db.Close()
}
}()
// Ensure foreign keys are supported and enabled.
var enabled bool
err = db.QueryRow("PRAGMA foreign_keys;").Scan(&enabled)
if err == sql.ErrNoRows {
return nil, common.NewBasicError("Foreign keys not supported", err)
}
if err != nil {
return nil, common.NewBasicError("Failed to check for foreign key support", err)
}
if !enabled {
db.Close()
return nil, common.NewBasicError("Failed to enable foreign key support", nil)
}
return db, nil
}
func setup(db *sql.DB, schema string, schemaVersion int) error {
if db == nil {
return common.NewBasicError("No database open", nil)
}
_, err := db.Exec(schema)
if err != nil {
return common.NewBasicError("Failed to set up SQLite database", err)
}
// Write schema version to database.
_, err = db.Exec(fmt.Sprintf("PRAGMA user_version = %d", schemaVersion))
if err != nil {
return common.NewBasicError("Failed to write schema version", err)
}
return nil
}