def test_restore_from_file_to_file(): """ Restores from a file dump to a database stored in a file and reads contents from the new database. """ if not is_sqlite_settings(): return with patch( "kolibri.utils.server.get_status", side_effect=mock_status_not_running ): # Create something special in the database! from kolibri.auth.models import Facility Facility.objects.create(name="test file", kind=FACILITY) # Create a backup file from the current test database dest_folder = tempfile.mkdtemp() # Purposefully destroy the connection pointer, which is the default # state of an unopened connection from django import db db.connections['default'].connection = None backup = dbbackup(kolibri.__version__, dest_folder=dest_folder) # Restore it into a new test database setting with override_settings(DATABASES=MOCK_DATABASES_FILE): # Destroy current connections and create new ones: db.connections.close_all() db.connections = db.ConnectionHandler() # Purposefully destroy the connection pointer, which is the default # state of an unopened connection db.connections['default'].connection = None call_command("dbrestore", dump_file=backup) # Test that the user has been restored! assert Facility.objects.filter(name="test file", kind=FACILITY).count() == 1
def initialize(skip_update=False): """ Currently, always called before running commands. This may change in case commands that conflict with this behavior show up. """ params = get_initialize_params() debug = params["debug"] skip_update = skip_update or params["skip_update"] settings = params["settings"] pythonpath = params["pythonpath"] default_options = DefaultDjangoOptions(settings, pythonpath) handle_default_options(default_options) # Do this here so that we can fix any issues with our configuration file before # we attempt to set up django. autoremove_unavailable_plugins() version = get_version() if version_updated(kolibri.__version__, version): check_plugin_config_file_location(version) # Reset the enabled plugins to the defaults # This needs to be run before dbbackup because # dbbackup relies on settings.INSTALLED_APPS enable_new_default_plugins() _setup_django(debug) if version_updated(kolibri.__version__, version) and not skip_update: if should_back_up(kolibri.__version__, version): # Non-dev version change, make a backup no matter what. from kolibri.core.deviceadmin.utils import dbbackup try: backup = dbbackup(version) logger.info( "Backed up database to: {path}".format(path=backup)) except IncompatibleDatabase: logger.warning( "Skipped automatic database backup, not compatible with " "this DB engine.") if version: logger.info("Version was {old}, new version: {new}".format( old=version, new=kolibri.__version__)) else: logger.info( "New install, version: {new}".format(new=kolibri.__version__)) update(version, kolibri.__version__) if not skip_update: # Run any plugin specific updates here in case they were missed by # our Kolibri version based update logic. run_plugin_updates()
def initialize(debug=False, skip_update=False): """ Currently, always called before running commands. This may change in case commands that conflict with this behavior show up. :param: debug: Tells initialization to setup logging etc. """ if not os.path.isfile(version_file()): django.setup() setup_logging(debug=debug) if not skip_update: _first_run() else: # Do this here so that we can fix any issues with our configuration file before # we attempt to set up django. config.autoremove_unavailable_plugins() version = open(version_file(), "r").read() version = version.strip() if version else "" if should_reset_plugins(kolibri.__version__, version): # Reset the enabled plugins to the defaults # This needs to be run before dbbackup because # dbbackup relies on settings.INSTALLED_APPS config.enable_default_plugins() if should_back_up(kolibri.__version__, version): # Version changed, make a backup no matter what. from kolibri.core.deviceadmin.utils import dbbackup try: backup = dbbackup(version) logger.info("Backed up database to: {path}".format(path=backup)) except IncompatibleDatabase: logger.warning( "Skipped automatic database backup, not compatible with " "this DB engine." ) django.setup() setup_logging(debug=debug) if kolibri.__version__ != version: logger.info( "Version was {old}, new version: {new}".format( old=version, new=kolibri.__version__ ) ) if not skip_update: update()
def conditional_backup(kolibri_version, version_file_contents): if should_back_up(kolibri_version, version_file_contents): # Non-dev version change, make a backup no matter what. from kolibri.core.deviceadmin.utils import dbbackup try: backup = dbbackup(version_file_contents) logger.info("Backed up database to: {path}".format(path=backup)) except IncompatibleDatabase: logger.warning( "Skipped automatic database backup, not compatible with " "this DB engine.")
def initialize(debug=False): """ Currently, always called before running commands. This may change in case commands that conflict with this behavior show up. :param: debug: Tells initialization to setup logging etc. """ if not os.path.isfile(version_file()): django.setup() setup_logging(debug=debug) _first_run() else: # Do this here so that we can fix any issues with our configuration file before # we attempt to setup django. from kolibri.utils.conf import autoremove_unavailable_plugins, enable_default_plugins autoremove_unavailable_plugins() version = open(version_file(), "r").read() version = version.strip() if version else "" change_version = kolibri.__version__ != version if change_version: # Version changed, make a backup no matter what. from kolibri.core.deviceadmin.utils import dbbackup try: backup = dbbackup(version) logger.info( "Backed up database to: {path}".format(path=backup)) except IncompatibleDatabase: logger.warning( "Skipped automatic database backup, not compatible with " "this DB engine.") enable_default_plugins() django.setup() setup_logging(debug=debug) if change_version: logger.info( "Version was {old}, new version: {new}".format( old=version, new=kolibri.__version__ ) ) update()
def initialize(debug=False): """ Currently, always called before running commands. This may change in case commands that conflict with this behavior show up. :param: debug: Tells initialization to setup logging etc. """ if not os.path.isfile(version_file()): django.setup() setup_logging(debug=debug) _first_run() else: # Do this here so that we can fix any issues with our configuration file before # we attempt to set up django. from .conf import autoremove_unavailable_plugins, enable_default_plugins autoremove_unavailable_plugins() version = open(version_file(), "r").read() version = version.strip() if version else "" change_version = kolibri.__version__ != version if change_version: # dbbackup will load settings.INSTALLED_APPS. # we need to ensure plugins are correct in conf.config before enable_default_plugins() # Version changed, make a backup no matter what. from kolibri.core.deviceadmin.utils import dbbackup try: backup = dbbackup(version) logger.info( "Backed up database to: {path}".format(path=backup)) except IncompatibleDatabase: logger.warning( "Skipped automatic database backup, not compatible with " "this DB engine.") django.setup() setup_logging(debug=debug) if change_version: logger.info("Version was {old}, new version: {new}".format( old=version, new=kolibri.__version__)) update()
def test_not_sqlite(): if is_sqlite_settings(): return with pytest.raises(IncompatibleDatabase): dbbackup("/doesnt/matter.file")
def initialize(skip_update=False): """ Currently, always called before running commands. This may change in case commands that conflict with this behavior show up. """ params = get_initialize_params() debug = params["debug"] skip_update = skip_update or params["skip_update"] settings = params["settings"] pythonpath = params["pythonpath"] default_options = DefaultDjangoOptions(settings, pythonpath) handle_default_options(default_options) # Do this here so that we can fix any issues with our configuration file before # we attempt to set up django. config.autoremove_unavailable_plugins() version = get_version() if version_updated(kolibri.__version__, version): # Reset the enabled plugins to the defaults # This needs to be run before dbbackup because # dbbackup relies on settings.INSTALLED_APPS config.enable_default_plugins() try: django.setup() if debug: from django.conf import settings settings.DEBUG = True except (DatabaseError, SQLite3DatabaseError) as e: if "malformed" in str(e): logger.error( u"Your database appears to be corrupted. If you encounter this," u"please immediately back up all files in the .kolibri folder that" u"end in .sqlite3, .sqlite3-shm, .sqlite3-wal, or .log and then" u"contact Learning Equality. Thank you!" ) raise if version_updated(kolibri.__version__, version) and not skip_update: if should_back_up(kolibri.__version__, version): # Non-dev version change, make a backup no matter what. from kolibri.core.deviceadmin.utils import dbbackup try: backup = dbbackup(version) logger.info(u"Backed up database to: {path}".format(path=backup)) except IncompatibleDatabase: logger.warning( u"Skipped automatic database backup, not compatible with " u"this DB engine." ) logger.info( u"Version was {old}, new version: {new}".format( old=version, new=kolibri.__version__ ) ) update(version, kolibri.__version__)