Skip to content

Commit

Permalink
Merge pull request #8456 from rtibbles/defer_metadata
Browse files Browse the repository at this point in the history
Defers content schema initialization until needed.
  • Loading branch information
rtibbles committed Sep 22, 2021
2 parents c448fcf + 8470acb commit a93fe7e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
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

0 comments on commit a93fe7e

Please sign in to comment.