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

Defers content schema initialization until needed. #8456

Merged
merged 1 commit into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 0 additions & 3 deletions kolibri/core/content/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@ class KolibriContentConfig(AppConfig):

def ready(self):
from .signals import reorder_channels_upon_deletion # noqa: F401
from kolibri.core.content.utils.sqlalchemybridge import prepare_bases
from .signals import cascade_delete_node # noqa: F401

prepare_bases()
36 changes: 23 additions & 13 deletions kolibri/core/content/utils/sqlalchemybridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ def set_sqlite_connection_pragma(dbapi_connection, connection_record):

logger = logging.getLogger(__name__)

BASES = {}


class ClassNotFoundError(Exception):
pass
Expand Down Expand Up @@ -195,18 +193,30 @@ def load_metadata(name):
return module.Base.metadata


def prepare_bases():

for name in CONTENT_DB_SCHEMA_VERSIONS + [CURRENT_SCHEMA_VERSION]:
try:
metadata = load_metadata(name)
BASES[name] = prepare_base(metadata, name=name)
except ImportError:
logger.error(
"Tried to load content schema version {} but valid schema import was not found".format(
name
class LazyBases(object):
_valid_bases = set(CONTENT_DB_SCHEMA_VERSIONS + [CURRENT_SCHEMA_VERSION])
_loaded_bases = {}

def __getitem__(self, name):
if name not in self._valid_bases:
raise AttributeError
if name not in self._loaded_bases:
try:
metadata = load_metadata(name)
self._loaded_bases[name] = prepare_base(metadata, name=name)
except ImportError:
logger.error(
"Tried to load content schema version {} but valid schema import was not found".format(
name
)
)
)
self._loaded_bases[name] = None
if self._loaded_bases[name] is None:
raise AttributeError
return self._loaded_bases[name]


BASES = LazyBases()


def get_model_from_cls(cls):
Expand Down