mirror of
https://github.com/LBRYFoundation/lbry-sdk.git
synced 2025-09-01 01:35:14 +00:00
fix database migration bugs
This commit is contained in:
parent
c8752b03c1
commit
305226a3bd
4 changed files with 22 additions and 9 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
@ -27,9 +28,23 @@ def migrate_db(conf, start, end):
|
||||||
elif current == 10:
|
elif current == 10:
|
||||||
from .migrate10to11 import do_migration
|
from .migrate10to11 import do_migration
|
||||||
else:
|
else:
|
||||||
raise Exception("DB migration of version {} to {} is not available".format(current,
|
raise Exception("DB migration of version {} to {} is not available".format(current, current+1))
|
||||||
current+1))
|
try:
|
||||||
do_migration(conf)
|
do_migration(conf)
|
||||||
|
except Exception as err:
|
||||||
|
log.info("failed to migrate database: %s", str(err))
|
||||||
|
if os.path.exists(os.path.join(conf.data_dir, "lbrynet.sqlite")):
|
||||||
|
backup_name = f"rev_{current}_unmigrated_database"
|
||||||
|
count = 0
|
||||||
|
while os.path.exists(os.path.join(conf.data_dir, backup_name + ".sqlite")):
|
||||||
|
count += 1
|
||||||
|
backup_name = f"rev_{current}_unmigrated_database_{count}"
|
||||||
|
backup_path = os.path.join(conf.data_dir, backup_name + ".sqlite")
|
||||||
|
os.rename(os.path.join(conf.data_dir, "lbrynet.sqlite"), backup_path)
|
||||||
|
log.info("made a backup of the unmigrated database: %s", backup_path)
|
||||||
|
if os.path.isfile(os.path.join(conf.data_dir, "db_revision")):
|
||||||
|
os.remove(os.path.join(conf.data_dir, "db_revision"))
|
||||||
|
return None
|
||||||
current += 1
|
current += 1
|
||||||
log.info("successfully migrated the database from revision %i to %i", current - 1, current)
|
log.info("successfully migrated the database from revision %i to %i", current - 1, current)
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -26,7 +26,7 @@ def migrate_blobs_db(db_dir):
|
||||||
|
|
||||||
# if blobs.db doesn't exist, skip migration
|
# if blobs.db doesn't exist, skip migration
|
||||||
if not os.path.isfile(blobs_db):
|
if not os.path.isfile(blobs_db):
|
||||||
log.error("blobs.db was not found but lbryfile_info.db was found, skipping migration")
|
log.info("blobs.db was not found but lbryfile_info.db was found, skipping migration")
|
||||||
return
|
return
|
||||||
|
|
||||||
blobs_db_file = sqlite3.connect(blobs_db)
|
blobs_db_file = sqlite3.connect(blobs_db)
|
||||||
|
|
|
@ -24,8 +24,7 @@ def add_lbry_file_metadata(db_dir):
|
||||||
return
|
return
|
||||||
|
|
||||||
if not os.path.isfile(lbryfile_info_db):
|
if not os.path.isfile(lbryfile_info_db):
|
||||||
log.error(
|
log.info("blockchainname.db was not found but lbryfile_info.db was found, skipping migration")
|
||||||
"blockchainname.db was not found but lbryfile_info.db was found, skipping migration")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
name_metadata_db = sqlite3.connect(name_metadata)
|
name_metadata_db = sqlite3.connect(name_metadata)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import asyncio
|
|
||||||
from lbry.blob.blob_info import BlobInfo
|
from lbry.blob.blob_info import BlobInfo
|
||||||
from lbry.stream.descriptor import StreamDescriptor
|
from lbry.stream.descriptor import StreamDescriptor
|
||||||
|
|
||||||
|
@ -24,10 +23,10 @@ def do_migration(conf):
|
||||||
blobs_by_stream.setdefault(stream_hash, []).append(BlobInfo(position, blob_length or 0, iv, blob_hash))
|
blobs_by_stream.setdefault(stream_hash, []).append(BlobInfo(position, blob_length or 0, iv, blob_hash))
|
||||||
|
|
||||||
for stream_name, stream_key, suggested_filename, sd_hash, stream_hash in streams:
|
for stream_name, stream_key, suggested_filename, sd_hash, stream_hash in streams:
|
||||||
sd = StreamDescriptor(asyncio.get_event_loop(), blob_dir, stream_name, stream_key, suggested_filename,
|
sd = StreamDescriptor(None, blob_dir, stream_name, stream_key, suggested_filename,
|
||||||
blobs_by_stream[stream_hash], stream_hash, sd_hash)
|
blobs_by_stream[stream_hash], stream_hash, sd_hash)
|
||||||
if sd_hash != sd.calculate_sd_hash():
|
if sd_hash != sd.calculate_sd_hash():
|
||||||
log.warning("Stream for descriptor %s is invalid, cleaning it up", sd_hash)
|
log.info("Stream for descriptor %s is invalid, cleaning it up", sd_hash)
|
||||||
blob_hashes = [blob.blob_hash for blob in blobs_by_stream[stream_hash]]
|
blob_hashes = [blob.blob_hash for blob in blobs_by_stream[stream_hash]]
|
||||||
delete_stream(cursor, stream_hash, sd_hash, blob_hashes, blob_dir)
|
delete_stream(cursor, stream_hash, sd_hash, blob_hashes, blob_dir)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue