From ca092262fb54258d1ebafc0ee01ed4564049fd99 Mon Sep 17 00:00:00 2001
From: Jack Robison <jackrobison@lbry.io>
Date: Wed, 10 May 2017 01:26:20 -0400
Subject: [PATCH] fix name metadata db race condition

---
 lbrynet/core/Wallet.py | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/lbrynet/core/Wallet.py b/lbrynet/core/Wallet.py
index f3c4d86b9..52bced20b 100644
--- a/lbrynet/core/Wallet.py
+++ b/lbrynet/core/Wallet.py
@@ -7,6 +7,7 @@ import time
 from twisted.internet import threads, reactor, defer, task
 from twisted.python.failure import Failure
 from twisted.enterprise import adbapi
+
 from collections import defaultdict, deque
 from zope.interface import implements
 from decimal import Decimal
@@ -227,7 +228,7 @@ class SqliteStorage(MetaDataStorage):
     def load(self):
         def create_tables(transaction):
             transaction.execute("create table if not exists name_metadata (" +
-                                "    name text, " +
+                                "    name text PRIMARY KEY NOT NULL, " +
                                 "    txid text, " +
                                 "    n integer, " +
                                 "    sd_hash text)")
@@ -268,11 +269,7 @@ class SqliteStorage(MetaDataStorage):
     def save_name_metadata(self, name, claim_outpoint, sd_hash):
         # TODO: refactor the 'name_metadata' and 'claim_ids' tables to not be terrible
         txid, nout = claim_outpoint['txid'], claim_outpoint['nout']
-        record_exists = yield self.db.runQuery("SELECT COUNT(*) FROM name_metadata "
-                                               "WHERE name=? AND txid=? AND n=?",
-                                               (name, txid, nout))
-        if not record_exists[0][0]:
-            yield self.db.runOperation("INSERT INTO name_metadata VALUES (?, ?, ?, ?)",
+        yield self.db.runOperation("INSERT OR REPLACE INTO name_metadata VALUES (?, ?, ?, ?)",
                                        (name, txid, nout, sd_hash))
         defer.returnValue(None)