Add unique index to configs.key
This commit is contained in:
parent
0213637dd6
commit
831332fd6a
21
pigeon/db.go
21
pigeon/db.go
|
@ -15,9 +15,11 @@ type migration struct {
|
|||
var migrations = []migration{
|
||||
migration{
|
||||
up: `CREATE TABLE IF NOT EXISTS configs (
|
||||
id INTEGER PRIMARY KEY,
|
||||
key TEXT NOT NULL
|
||||
value TEXT NOT NULL);`,
|
||||
key string NOT NULL,
|
||||
value string NOT NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS unique_configs_key ON configs (key);
|
||||
`,
|
||||
down: `DROP TABLE IF EXISTS configs`,
|
||||
},
|
||||
}
|
||||
|
@ -44,10 +46,15 @@ func openDB() *sql.DB {
|
|||
}
|
||||
|
||||
for _, migration := range migrations {
|
||||
tx.Exec(migration.up)
|
||||
_, err := tx.Exec(migration.up)
|
||||
if err != nil {
|
||||
log.Fatalf("Migration failure: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
if tx.Commit() != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return db
|
||||
}
|
||||
|
@ -62,7 +69,7 @@ func SetConfig(key string, value []byte) {
|
|||
if err != nil {
|
||||
log.Fatalf("Failed to SetConfig (1): %s", err)
|
||||
}
|
||||
tx.Exec("INSERT INTO configs (?, ?)", key, value)
|
||||
tx.Exec("INSERT INTO configs (?1, ?2)", key, value)
|
||||
err1 := tx.Commit()
|
||||
if err1 != nil {
|
||||
log.Fatalf("Failed to SetConfig (2): %s", err)
|
||||
|
@ -71,7 +78,7 @@ func SetConfig(key string, value []byte) {
|
|||
|
||||
// GetConfig retrieves a key/value pair from the database.
|
||||
func GetConfig(key string) []byte {
|
||||
rows, err := Database.Query("SELECT key FROM configs WHERE value = ? LIMIT 1", key)
|
||||
rows, err := Database.Query("SELECT key FROM configs WHERE value = ?1 LIMIT 1", key)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to retrieve config key(1): %s", err)
|
||||
}
|
||||
|
|
|
@ -2,17 +2,25 @@ package pigeon
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateIdentity(t *testing.T) {
|
||||
pub, priv := CreateIdentity()
|
||||
dbPubKey := GetConfig("public_key")
|
||||
dbPrivKey := GetConfig("private_key")
|
||||
|
||||
if !bytes.Equal(pub, GetConfig("public_key")) {
|
||||
fmt.Printf("pub: %s\n", B32Encode(pub))
|
||||
fmt.Printf("priv: %s\n", B32Encode(priv))
|
||||
fmt.Printf("pub: %s\n", B32Encode(dbPrivKey))
|
||||
fmt.Printf("priv: %s\n", B32Encode(dbPubKey))
|
||||
|
||||
if !bytes.Equal(pub, dbPubKey) {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
if !bytes.Equal(priv, GetConfig("private_key")) {
|
||||
if !bytes.Equal(priv, dbPrivKey) {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue