Skip to content

Instantly share code, notes, and snippets.

@benjaoming
Created October 16, 2017 11:58
Show Gist options
  • Select an option

  • Save benjaoming/8203d57cc33dce62fc376f68a8fba0e4 to your computer and use it in GitHub Desktop.

Select an option

Save benjaoming/8203d57cc33dce62fc376f68a8fba0e4 to your computer and use it in GitHub Desktop.

Revisions

  1. benjaoming created this gist Oct 16, 2017.
    60 changes: 60 additions & 0 deletions kolibri_legacy_restore.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    #!/usr/bin/env python

    try:
    import kolibri
    except ImportError:
    print(
    "Could not find Kolibri in the system path, if you are using a PEX "
    "file, please run `./file.pex manage shell` and then copy-paste all "
    "contents of this script into the command line of Kolibri."
    )
    raise SystemExit()

    import os
    import sys

    os.environ["DJANGO_SETTINGS_MODULE"] = "kolibri.deployment.default.settings.base"

    os.environ.setdefault(
    "KOLIBRI_HOME", os.path.join(os.path.expanduser("~"), ".kolibri")
    )

    from django import db
    from django.conf import settings

    if not kolibri.__version__ <= "0.6":
    print("This script is only necessary with Kolibri<0.6")
    print("If your intention is to restore an old dump for kolibri 0.6, then you need to install kolibri<0.6 firstly")
    raise SystemExit()

    try:
    input = raw_input
    except NameError:
    pass


    dump_filename = None

    while not dump_filename or not os.path.isfile(dump_filename):
    dump_filename = input("Please enter the file name and path of your dump file and press ENTER: ")
    print("File not found, please try again")

    dst_file = settings.DATABASES['default']['NAME']

    # Close connections
    db.connections.close_all()

    # Wipe current database file
    if not db.connections['default'].is_in_memory_db(dst_file):
    with open(dst_file, "w") as f:
    f.truncate()

    f = open(dump_filename, "r")

    db.connections['default'].connect()
    db.connections['default'].connection.executescript(
    f.read()
    )

    from morango.models import DatabaseIDModel
    DatabaseIDModel.objects.create()