@@ -36,20 +36,23 @@ type tableInfoRow struct {
36
36
type tableInfo = []tableInfoRow
37
37
38
38
func Validate (path string ) error {
39
- expectedTableInfos := [ ]tableInfo {
40
- {
39
+ expectedTableInfos := map [ string ]tableInfo {
40
+ "users" : {
41
41
{cid : 0 , name : "id" , typeDef : "INTEGER" , notnull : 1 , dflt_value : nil , pk : 1 },
42
42
{cid : 1 , name : "username" , typeDef : "TEXT" , notnull : 1 , dflt_value : nil , pk : 0 },
43
43
{cid : 2 , name : "password" , typeDef : "TEXT" , notnull : 1 , dflt_value : nil , pk : 0 },
44
44
},
45
- {
45
+ "sessions" : {
46
46
{cid : 0 , name : "session_key" , typeDef : "TEXT" , notnull : 1 , dflt_value : nil , pk : 1 },
47
47
{cid : 1 , name : "user_id" , typeDef : "INTEGER" , notnull : 1 , dflt_value : nil , pk : 0 },
48
48
},
49
+ "states" : {
50
+ {cid : 0 , name : "user_id" , typeDef : "INTEGER" , notnull : 1 , dflt_value : nil , pk : 1 },
51
+ {cid : 1 , name : "state" , typeDef : "TEXT" , notnull : 1 , dflt_value : nil , pk : 0 },
52
+ },
49
53
}
50
54
51
- tableInfos := []tableInfo {}
52
- for _ , tableName := range []string {"users" , "sessions" } {
55
+ for tableName , expectedTableInfo := range expectedTableInfos {
53
56
rows , err := db .Query (`PRAGMA table_info(` + tableName + `)` )
54
57
if err != nil {
55
58
return err
@@ -66,14 +69,10 @@ func Validate(path string) error {
66
69
tableInfo = append (tableInfo , r )
67
70
}
68
71
69
- tableInfos = append (tableInfos , tableInfo )
70
- rows .Close ()
71
- }
72
-
73
- for i , expectedTableInfo := range expectedTableInfos {
74
- if ! slices .Equal (expectedTableInfo , tableInfos [i ]) {
72
+ if ! slices .Equal (expectedTableInfo , tableInfo ) {
75
73
return fmt .Errorf ("failed to validate the database" )
76
74
}
75
+ rows .Close ()
77
76
}
78
77
79
78
log .Printf ("validated the database" )
@@ -89,6 +88,10 @@ func Init(path string) error {
89
88
CREATE TABLE sessions(
90
89
session_key TEXT NOT NULL PRIMARY KEY,
91
90
user_id INTEGER NOT NULL
91
+ );
92
+ CREATE TABLE states(
93
+ user_id INTEGER NOT NULL PRIMARY KEY,
94
+ state TEXT NOT NULL
92
95
)`
93
96
if _ , err := db .Exec (query ); err != nil {
94
97
log .Println (err )
0 commit comments