2019-10-18 11:28:26 -04:00
|
|
|
# pylint: skip-file
|
2019-06-25 14:36:32 -04:00
|
|
|
import os
|
2019-09-24 12:53:23 -03:00
|
|
|
import sys
|
2015-09-07 23:59:59 -04:00
|
|
|
import logging
|
|
|
|
|
2018-06-08 12:41:38 -04:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2015-09-07 23:59:59 -04:00
|
|
|
|
2019-01-21 15:55:50 -05:00
|
|
|
def migrate_db(conf, start, end):
|
2015-09-04 16:22:02 -04:00
|
|
|
current = start
|
2016-10-14 02:13:37 -04:00
|
|
|
while current < end:
|
|
|
|
if current == 1:
|
2018-11-04 14:16:46 -05:00
|
|
|
from .migrate1to2 import do_migration
|
2017-05-10 11:30:36 -04:00
|
|
|
elif current == 2:
|
2018-11-04 14:16:46 -05:00
|
|
|
from .migrate2to3 import do_migration
|
2017-07-26 13:48:58 -04:00
|
|
|
elif current == 3:
|
2018-11-04 14:16:46 -05:00
|
|
|
from .migrate3to4 import do_migration
|
2017-12-06 18:27:42 -05:00
|
|
|
elif current == 4:
|
2018-11-04 14:16:46 -05:00
|
|
|
from .migrate4to5 import do_migration
|
2018-02-12 13:33:48 -05:00
|
|
|
elif current == 5:
|
2018-11-04 14:16:46 -05:00
|
|
|
from .migrate5to6 import do_migration
|
2018-03-27 14:58:29 -04:00
|
|
|
elif current == 6:
|
2018-11-04 14:16:46 -05:00
|
|
|
from .migrate6to7 import do_migration
|
2018-05-08 13:51:02 -04:00
|
|
|
elif current == 7:
|
2018-11-04 14:16:46 -05:00
|
|
|
from .migrate7to8 import do_migration
|
2018-05-15 21:44:30 -03:00
|
|
|
elif current == 8:
|
2018-11-04 14:16:46 -05:00
|
|
|
from .migrate8to9 import do_migration
|
2019-05-07 01:32:23 -03:00
|
|
|
elif current == 9:
|
|
|
|
from .migrate9to10 import do_migration
|
2019-05-07 14:30:35 -04:00
|
|
|
elif current == 10:
|
|
|
|
from .migrate10to11 import do_migration
|
2019-10-10 19:46:00 -04:00
|
|
|
elif current == 11:
|
|
|
|
from .migrate11to12 import do_migration
|
2019-11-13 18:55:32 -05:00
|
|
|
elif current == 12:
|
|
|
|
from .migrate12to13 import do_migration
|
2019-11-23 20:51:03 +01:00
|
|
|
elif current == 13:
|
|
|
|
from .migrate13to14 import do_migration
|
2021-09-10 10:53:52 -04:00
|
|
|
elif current == 14:
|
|
|
|
from .migrate14to15 import do_migration
|
2016-10-14 02:13:37 -04:00
|
|
|
else:
|
2019-10-08 21:49:01 +05:30
|
|
|
raise Exception(f"DB migration of version {current} to {current+1} is not available")
|
2019-06-25 14:36:32 -04:00
|
|
|
try:
|
|
|
|
do_migration(conf)
|
2019-10-28 12:20:15 -04:00
|
|
|
except Exception:
|
|
|
|
log.exception("failed to migrate database")
|
2019-06-25 14:36:32 -04:00
|
|
|
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
|
2016-10-14 02:13:37 -04:00
|
|
|
current += 1
|
2018-06-08 12:41:38 -04:00
|
|
|
log.info("successfully migrated the database from revision %i to %i", current - 1, current)
|
2016-10-14 02:13:37 -04:00
|
|
|
return None
|
2015-09-04 16:22:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
def run_migration_script():
|
2015-09-07 23:59:59 -04:00
|
|
|
log_format = "(%(asctime)s)[%(filename)s:%(lineno)s] %(funcName)s(): %(message)s"
|
|
|
|
logging.basicConfig(level=logging.DEBUG, format=log_format, filename="migrator.log")
|
|
|
|
sys.stdout = open("migrator.out.log", 'w')
|
|
|
|
sys.stderr = open("migrator.err.log", 'w')
|
|
|
|
migrate_db(sys.argv[1], int(sys.argv[2]), int(sys.argv[3]))
|
2016-11-28 13:23:10 -06:00
|
|
|
|
|
|
|
|
2015-09-07 23:59:59 -04:00
|
|
|
if __name__ == "__main__":
|
2016-10-14 02:13:37 -04:00
|
|
|
run_migration_script()
|