Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preseed the sqlite database #8351

Merged
merged 4 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ writeversion:
@echo ""
@echo "Current version is now `cat kolibri/VERSION`"

preseeddb:
$(eval TEMPKHOME := $(shell mktemp -d /tmp/kolibri-preseeddb-XXXXXXXX))
PYTHONPATH=".:$$PYTHONPATH" KOLIBRI_HOME=$(TEMPKHOME) python -m kolibri manage migrate
yes yes | PYTHONPATH=".:$$PYTHONPATH" KOLIBRI_HOME=$(TEMPKHOME) python -m kolibri manage deprovision
mkdir kolibri/dist/home
mv $(TEMPKHOME)/db.sqlite3 kolibri/dist/home/db.sqlite3
mv $(TEMPKHOME)/notifications.sqlite3 kolibri/dist/home/notifications.sqlite3
rm -r $(TEMPKHOME)

setrequirements:
rm -r requirements.txt || true # remove requirements.txt
git checkout -- requirements.txt # restore requirements.txt
Expand Down Expand Up @@ -198,7 +207,7 @@ i18n-transfer-context:
i18n-django-compilemessages:
# Change working directory to kolibri/ such that compilemessages
# finds only the .po files nested there.
cd kolibri && PYTHONPATH="..:$$PYTHONPATH" python -m kolibri manage compilemessages
cd kolibri && PYTHONPATH="..:$$PYTHONPATH" python -m kolibri manage compilemessages --skip-update

i18n-upload: i18n-extract
python packages/kolibri-tools/lib/i18n/crowdin.py upload-sources ${branch}
Expand Down
11 changes: 7 additions & 4 deletions kolibri/core/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ def filter_upgrade(upgrade):
and matches_version(new_version, upgrade.NEW_VERSION)
)

for version_upgrade in sorted(
filter(filter_upgrade, get_upgrades(app_configs=app_configs))
):
version_upgrade()
# Only run upgrades if we had a previous version, otherwise
# we're just upgrading from a blank slate, so no need to run anything
if old_version:
for version_upgrade in sorted(
filter(filter_upgrade, get_upgrades(app_configs=app_configs))
):
version_upgrade()
29 changes: 28 additions & 1 deletion kolibri/utils/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import logging
import os
import shutil
import sys
from sqlite3 import DatabaseError as SQLite3DatabaseError

Expand Down Expand Up @@ -154,13 +155,39 @@ def _setup_django():


def _upgrades_before_django_setup(updated, version):
if updated:
if version and updated:
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()

if not version and OPTIONS["Database"]["DATABASE_ENGINE"] == "sqlite":
# If there is no registered version, and we are using sqlite,
# we can shortcut migrations by using the preseeded databases
# that we bundle in the Kolibri whl file.
logger.info("Attempting to setup using pre-migrated databases")
try:
import kolibri.dist
main_db_path = os.path.join(kolibri.dist.__file__, "home/db.sqlite3")
shutil.copy(main_db_path, KOLIBRI_HOME)
except (ImportError, IOError, OSError):
logging.warning(
"Unable to copy pre-migrated database from {} to {}".format(
main_db_path, KOLIBRI_HOME
)
)
try:
import kolibri.dist
notifications_db_path = os.path.join(kolibri.dist.__file__, "home/notifications.sqlite3")
shutil.copy(notifications_db_path, KOLIBRI_HOME)
except (ImportError, IOError, OSError):
logging.warning(
"Unable to copy pre-migrated database from {} to {}".format(
notifications_db_path, KOLIBRI_HOME
)
)


def _upgrades_after_django_setup(updated, version):
# If device is not provisioned, attempt automatic provisioning
Expand Down