lbry-sdk/lbry/lbrynet/extras/daemon/migrator/migrate2to3.py

43 lines
1.4 KiB
Python
Raw Normal View History

2017-05-10 17:30:36 +02:00
import sqlite3
import os
import logging
log = logging.getLogger(__name__)
2019-01-21 21:55:50 +01:00
def do_migration(conf):
2017-05-10 17:30:36 +02:00
log.info("Doing the migration")
2019-01-21 21:55:50 +01:00
migrate_blockchainname_db(conf.data_dir)
2017-05-10 17:30:36 +02:00
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()