From ecaa5249b470d3166a909a7e0fa981a29feca974 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Wed, 10 May 2017 11:30:36 -0400 Subject: [PATCH] migrate name metadata table --- lbrynet/core/Wallet.py | 12 ++++----- lbrynet/db_migrator/dbmigrator.py | 3 +++ lbrynet/db_migrator/migrate2to3.py | 42 ++++++++++++++++++++++++++++++ lbrynet/lbrynet_daemon/Daemon.py | 2 +- 4 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 lbrynet/db_migrator/migrate2to3.py diff --git a/lbrynet/core/Wallet.py b/lbrynet/core/Wallet.py index 52bced20b..2dfd0bb94 100644 --- a/lbrynet/core/Wallet.py +++ b/lbrynet/core/Wallet.py @@ -227,11 +227,11 @@ class SqliteStorage(MetaDataStorage): def load(self): def create_tables(transaction): - transaction.execute("create table if not exists name_metadata (" + - " name text PRIMARY KEY NOT NULL, " + - " txid text, " + - " n integer, " + - " sd_hash text)") + transaction.execute("CREATE TABLE IF NOT EXISTS name_metadata (" + + " name TEXT UNIQUE NOT NULL, " + + " txid TEXT NOT NULL, " + + " n INTEGER NOT NULL, " + + " sd_hash TEXT NOT NULL)") transaction.execute("create table if not exists claim_ids (" + " claimId text, " + " name text, " + @@ -267,7 +267,7 @@ class SqliteStorage(MetaDataStorage): @rerun_if_locked @defer.inlineCallbacks 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'] yield self.db.runOperation("INSERT OR REPLACE INTO name_metadata VALUES (?, ?, ?, ?)", (name, txid, nout, sd_hash)) diff --git a/lbrynet/db_migrator/dbmigrator.py b/lbrynet/db_migrator/dbmigrator.py index 29603f821..4ef34c0ad 100644 --- a/lbrynet/db_migrator/dbmigrator.py +++ b/lbrynet/db_migrator/dbmigrator.py @@ -7,6 +7,9 @@ def migrate_db(db_dir, start, end): if current == 1: from lbrynet.db_migrator.migrate1to2 import do_migration do_migration(db_dir) + elif current == 2: + from lbrynet.db_migrator.migrate2to3 import do_migration + do_migration(db_dir) else: raise Exception( "DB migration of version {} to {} is not available".format(current, current+1)) diff --git a/lbrynet/db_migrator/migrate2to3.py b/lbrynet/db_migrator/migrate2to3.py new file mode 100644 index 000000000..e3530ba0a --- /dev/null +++ b/lbrynet/db_migrator/migrate2to3.py @@ -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() diff --git a/lbrynet/lbrynet_daemon/Daemon.py b/lbrynet/lbrynet_daemon/Daemon.py index f866ea163..80c8e6b73 100644 --- a/lbrynet/lbrynet_daemon/Daemon.py +++ b/lbrynet/lbrynet_daemon/Daemon.py @@ -207,7 +207,7 @@ class Daemon(AuthJSONRPCServer): self.platform = None self.first_run = None 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.session = None self.uploaded_temp_files = []