migrate name metadata table

This commit is contained in:
Jack Robison 2017-05-10 11:30:36 -04:00
parent 4224d63603
commit ecaa5249b4
4 changed files with 52 additions and 7 deletions

View file

@ -227,11 +227,11 @@ class SqliteStorage(MetaDataStorage):
def load(self): def load(self):
def create_tables(transaction): def create_tables(transaction):
transaction.execute("create table if not exists name_metadata (" + transaction.execute("CREATE TABLE IF NOT EXISTS name_metadata (" +
" name text PRIMARY KEY NOT NULL, " + " name TEXT UNIQUE NOT NULL, " +
" txid text, " + " txid TEXT NOT NULL, " +
" n integer, " + " n INTEGER NOT NULL, " +
" sd_hash text)") " sd_hash TEXT NOT NULL)")
transaction.execute("create table if not exists claim_ids (" + transaction.execute("create table if not exists claim_ids (" +
" claimId text, " + " claimId text, " +
" name text, " + " name text, " +
@ -267,7 +267,7 @@ class SqliteStorage(MetaDataStorage):
@rerun_if_locked @rerun_if_locked
@defer.inlineCallbacks @defer.inlineCallbacks
def save_name_metadata(self, name, claim_outpoint, sd_hash): def save_name_metadata(self, name, claim_outpoint, sd_hash):
# TODO: refactor the 'name_metadata' and 'claim_ids' tables to not be terrible # TODO: refactor the 'claim_ids' table to not be terrible
txid, nout = claim_outpoint['txid'], claim_outpoint['nout'] txid, nout = claim_outpoint['txid'], claim_outpoint['nout']
yield self.db.runOperation("INSERT OR REPLACE INTO name_metadata VALUES (?, ?, ?, ?)", yield self.db.runOperation("INSERT OR REPLACE INTO name_metadata VALUES (?, ?, ?, ?)",
(name, txid, nout, sd_hash)) (name, txid, nout, sd_hash))

View file

@ -7,6 +7,9 @@ def migrate_db(db_dir, start, end):
if current == 1: if current == 1:
from lbrynet.db_migrator.migrate1to2 import do_migration from lbrynet.db_migrator.migrate1to2 import do_migration
do_migration(db_dir) do_migration(db_dir)
elif current == 2:
from lbrynet.db_migrator.migrate2to3 import do_migration
do_migration(db_dir)
else: else:
raise Exception( raise Exception(
"DB migration of version {} to {} is not available".format(current, current+1)) "DB migration of version {} to {} is not available".format(current, current+1))

View file

@ -0,0 +1,42 @@
import sqlite3
import os
import logging
log = logging.getLogger(__name__)
def do_migration(db_dir):
log.info("Doing the migration")
migrate_blockchainname_db(db_dir)
log.info("Migration succeeded")
def migrate_blockchainname_db(db_dir):
blockchainname_db = os.path.join(db_dir, "blockchainname.db")
# skip migration on fresh installs
if not os.path.isfile(blockchainname_db):
return
db_file = sqlite3.connect(blockchainname_db)
file_cursor = db_file.cursor()
tables = file_cursor.execute("SELECT tbl_name FROM sqlite_master "
"WHERE type='table'").fetchall()
if 'tmp_name_metadata_table' in tables and 'name_metadata' not in tables:
file_cursor.execute("ALTER TABLE tmp_name_metadata_table RENAME TO name_metadata")
else:
file_cursor.executescript(
"CREATE TABLE IF NOT EXISTS tmp_name_metadata_table "
" (name TEXT UNIQUE NOT NULL, "
" txid TEXT NOT NULL, "
" n INTEGER NOT NULL, "
" sd_hash TEXT NOT NULL); "
"INSERT OR IGNORE INTO tmp_name_metadata_table "
" (name, txid, n, sd_hash) "
" SELECT name, txid, n, sd_hash FROM name_metadata; "
"DROP TABLE name_metadata; "
"ALTER TABLE tmp_name_metadata_table RENAME TO name_metadata;"
)
db_file.commit()
db_file.close()

View file

@ -207,7 +207,7 @@ class Daemon(AuthJSONRPCServer):
self.platform = None self.platform = None
self.first_run = None self.first_run = None
self.log_file = conf.settings.get_log_filename() self.log_file = conf.settings.get_log_filename()
self.current_db_revision = 2 self.current_db_revision = 3
self.db_revision_file = conf.settings.get_db_revision_filename() self.db_revision_file = conf.settings.get_db_revision_filename()
self.session = None self.session = None
self.uploaded_temp_files = [] self.uploaded_temp_files = []