Просмотр исходного кода

Fix database bugs: missing constraints (caused duplicate serverEntryProtocol rows to be inserted); need explicit delete of serverEntryProtocol rows to ensure correct state after an updated server entry is received (in particular, when old capabilities have been removed).

Rod Hynes 11 лет назад
Родитель
Сommit
ebcd2e0d28
1 измененных файлов с 12 добавлено и 5 удалено
  1. 12 5
      psiphon/dataStore.go

+ 12 - 5
psiphon/dataStore.go

@@ -50,9 +50,10 @@ func initDataStore() {
              data blob not null);
 	    create table if not exists serverEntryProtocol
 	        (serverEntryId text not null,
-	         protocol text not null);
+	         protocol text not null,
+	         primary key (serverEntryId, protocol));
         create table if not exists keyValue
-            (key text not null,
+            (key text not null primary key,
              value text not null);
 		pragma journal_mode=WAL;
         `
@@ -150,6 +151,12 @@ func StoreServerEntry(serverEntry *ServerEntry, replaceIfExists bool) error {
             insert or replace into serverEntry (id, rank, region, data)
             values (?, (select coalesce(max(rank)-1, 0) from serverEntry), ?, ?);
             `, serverEntry.IpAddress, serverEntry.Region, data)
+		if err != nil {
+			return err
+		}
+		_, err = transaction.Exec(`
+            delete from serverEntryProtocol where serverEntryId = ?;
+            `, serverEntry.IpAddress)
 		if err != nil {
 			return err
 		}
@@ -159,9 +166,9 @@ func StoreServerEntry(serverEntry *ServerEntry, replaceIfExists bool) error {
 			requiredCapability := strings.TrimSuffix(protocol, "-OSSH")
 			if Contains(serverEntry.Capabilities, requiredCapability) {
 				_, err = transaction.Exec(`
-		            insert or ignore into serverEntryProtocol (serverEntryId, protocol)
-		            values (?, ?);
-		            `, serverEntry.IpAddress, protocol)
+                    insert into serverEntryProtocol (serverEntryId, protocol)
+                    values (?, ?);
+                    `, serverEntry.IpAddress, protocol)
 				if err != nil {
 					return err
 				}