forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
70 lines (62 loc) · 1.65 KB
/
query.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
package schema
import (
"database/sql"
"fmt"
"github.com/lxc/lxd/lxd/db/query"
)
// Return whether the schema table is present in the database.
func doesSchemaTableExist(tx *sql.Tx) (bool, error) {
statement := `
SELECT COUNT(name) FROM sqlite_master WHERE type = 'table' AND name = 'schema'
`
rows, err := tx.Query(statement)
if err != nil {
return false, err
}
defer rows.Close()
if !rows.Next() {
return false, fmt.Errorf("schema table query returned no rows")
}
var count int
err = rows.Scan(&count)
if err != nil {
return false, err
}
return count == 1, nil
}
// Return all versions in the schema table, in increasing order.
func selectSchemaVersions(tx *sql.Tx) ([]int, error) {
statement := `
SELECT version FROM schema ORDER BY version
`
return query.SelectIntegers(tx, statement)
}
// Return a list of SQL statements that can be used to create all tables in the
// database.
func selectTablesSQL(tx *sql.Tx) ([]string, error) {
statement := `
SELECT sql FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' AND name != 'schema' ORDER BY name
`
return query.SelectStrings(tx, statement)
}
// Create the schema table.
func createSchemaTable(tx *sql.Tx) error {
statement := `
CREATE TABLE schema (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
version INTEGER NOT NULL,
updated_at DATETIME NOT NULL,
UNIQUE (version)
)
`
_, err := tx.Exec(statement)
return err
}
// Insert a new version into the schema table.
func insertSchemaVersion(tx *sql.Tx, new int) error {
statement := `
INSERT INTO schema (version, updated_at) VALUES (?, strftime("%s"))
`
_, err := tx.Exec(statement, new)
return err
}