You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
refactor: combine authoring apps to openedx_content
This commit makes the following major refactorings:
- All apps previously under openedx_learning/apps/authoring have now
effectively been merged together to one openedx_content app.
- The models and logic for those apps continues to be encapsulated in
what we're calling "applets", inside of openedx_content/applets/...
- In order to facilitate smooth database migrations, the AppConfigs and
database migrations for the old apps continue to be preserved in
openedx_content/backcompat/...
- A new openedx_learning.api.django.openedx_learning_apps_to_install()
has been created to make it easier for openedx-platform to get all the
necessary apps to install.
The rationale for this change is detailed in
docs/decisions/0020-authoring-as-one-app.rst, but the short version is
that we hope this arrangement will allow us to keep many of the benefits
of having small apps (easy to reason about), while also making it easier
to refactor internally. Refactoring is currently hindered by the
difficulty in moving models across apps.
The name change away from "authoring" also reflects that we intend to
use these APIs and models on the "learning" side of things as well, e.g.
when reading content for rendering to a student. In the longer term, we
will probably make openedx_content a top-level package in this repo, but
that would be a later step.
Most of this commit is just shuffling things around and renaming
references, but the truly complicated bits are around the sequencing of
database migrations, particularly the ones removing model state from the
old apps, and transferring that state into the new openedx_content app
without changing the actual database state. We also have some hacky
logic in openedx_content/apps.py in order to properly patch migration
dependencies so that we don't break existing openedx-platform
migrations.
This also bumps the version to 0.31.0.
Up to this point, Learning Core has used many small apps with a narrow focus (e.g. ``components``, ``collections``, etc.) in order to make each individual app simpler to reason about. This has been useful overall, but it has made refactoring more cumbersome. For instance:
8
+
9
+
#. Moving models between apps is tricky, requiring the use of Django's ``SeparateDatabaseAndState`` functionality to fake a deletion in one app and a creation in another without actually altering the database. It also requires doctoring the migration files for models in other repos that might have foreign key relations to the model being moved, so that they're pointing to the new ``app_label``. This will be an issue when we try to extract container-related models and logic out of publishing and into a new ``containers`` app.
10
+
#. Renaming an app is also cumbersome, because the process requires creating a new app and transitioning the models over. This came up when trying to rename the ``contents`` app to ``media``.
11
+
12
+
There have also been minor inconveniences, like having a long list of ``INSTALLED_APPS`` to maintain in edx-platform over time, or not having these tables easily grouped together in the Django admin interface.
13
+
14
+
Decisions
15
+
---------
16
+
17
+
1. Single openedx_content App
18
+
~~~~~~~~~~~~~~~~~~~~~~~
19
+
20
+
All existing authoring apps will be merged into one Django app (``openedx_learning.app.openedx_content``). Some consequences of this decision:
21
+
22
+
- The tables will be renamed to have the ``openedx_content`` label prefix.
23
+
- All management commands will be moved to the ``openedx_content`` app.
24
+
25
+
2. Logical Separation via Applets
26
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27
+
28
+
We will continue to keep internal API boundaries between individual applets, and use the ``api.py`` modules. This is both to insulate applets from implementation changes in other applets, as well as to provide a set of APIs that third-party plugins can utilize. As before, we will use Import Linter to enforce dependency ordering.
29
+
30
+
3. Restructuring Specifics
31
+
~~~~~~~~~~~~~~~~~~~~~~~~~~
32
+
33
+
In one pull request, we are going to:
34
+
35
+
#. Rename the ``openedx_learning.apps.authoring`` package to be ``openedx_learning.apps.openedx_content``.
36
+
#. Create bare shells of the existing ``authoring`` apps (``backup_restore``, ``collections``, ``components``, ``contents``, ``publishing``, ``sections``, ``subsections``, ``units``), and move them to the ``openedx_learning.apps.openedx_content.backcompat`` package. These shells will have an ``apps.py`` file and the ``migrations`` package for each existing app. This will allow for a smooth schema migration to transition the models from these individual apps to ``openedx_content``.
37
+
#. Move the actual models files and API logic for our existing authoring apps to the ``openedx_learning.apps.openedx_content.applets`` package.
38
+
#. Convert the top level ``openedx_learning.apps.openedx_content`` package to be a Django app. The top level ``admin.py``, ``api.py``, and ``models.py`` modules will do wildcard imports from the corresponding modules across all applet packages.
39
+
40
+
In terms of model migrations, all existing apps will have a final migration that uses ``SeparateDatabaseAndState`` to remove all model state, but make no actual database changes. The initial ``openedx_content`` app migration will then also use ``SeparateDatabaseAndState`` to create the model state without doing any actual database operations. The next ``openedx_content`` app migration will rename all existing database tables to use the ``openedx_content`` prefix, for uniformity.
41
+
42
+
The ordering of these migrations is important, and existing edx-platform migrations should remain unchanged. This is important to make sure that we do not introduce ordering inconsistencies for existing installations that are upgrading.
43
+
44
+
Therefore, the migrations will happen in the following order:
45
+
46
+
#. All ``backcompat.*`` apps migrations except for the final ones that delete model state. This takes us up to where migrations would already be before we make any changes.
47
+
#. The ``openedx_content`` app's ``0001_intial`` migration that adds model state without changing the database. At this point, model state exists for the same models in all the old ``backcompat.*`` apps as well as the new ``openedx_content`` app.
48
+
#. edx-platform apps that had foreign keys to old ``backcompat.*`` apps models will need to be switched to point to the new ``openedx_content`` app models. This will likewise be done without a database change, because they're still pointing to the same tables and columns.
49
+
#. Now that edx-platform references have been updated, we can delete the model state from the old ``backcompat.*`` apps and rename the underlying tables (in either order).
50
+
51
+
The tricky part is to make sure that the old ``backcompat.*`` apps models still exist when the edx-platform migrations to move over the references runs. This is problematic because the edx-platform migrations can only specify that they run *after the new openedx_content models are created*. They cannot specify that they run *before the old backcompat models are dropped*.
52
+
53
+
So in order to enforce this ordering, we do the following:
54
+
55
+
* The ``openedx_content`` migration ``0001_initial`` requires that all ``backcompat.*`` migrations except the last ones removing model state are run.
56
+
* The ``openedx_content`` migration ``0002_rename_tables_to_openedx_content`` migration requires that the edx-platform migrations changing refrences over run. This is important anyway, because we want to make sure those reference changes happen before we change any table names.
57
+
* The final ``backcompat.*`` migrations that remove model field state will list ``openedx_content`` app's ``0002_rename_tables_to_openedx_content`` as a dependency.
58
+
59
+
A further complication is that ``openedx_learning`` will often run its migrations without edx-platform present (e.g. for CI or standalone dev purposes), so we can't force ``0002_rename_tables_to_openedx_content`` in the ``openedx_content`` app to have references to edx-platform migrations. To get around this, we dynamically inject those migration dependencies only if we detect those edx-platform apps exist in the currently loaded Django project. This injection happens in the ``apps.py`` initialization for the ``openedx_content`` app.
60
+
61
+
The final complication is that we want these migration dependencies to be the same regardless of whether you're running edx-platform migrations with the LMS or CMS (Studio) settings, or we run the risk of getting into an inconsistent state and dropping the old models before all the edx-platform apps can run their migrations to move their references. To do this, we have to make sure that the edx-platform apps that reference Learning Core models are present in the ``INSTALLED_APPS`` for both configurations.
62
+
63
+
4. The Bigger Picture
64
+
~~~~~~~~~~~~~~~~~~~~~
65
+
66
+
This practice means that the ``openedx_content`` Django app corresponds to a Subdomain in Domain Driven Design terminology, with each applet being a Bounded Context. We call these "Applets" instead of "Bounded Contexts" because we don't want it to get confused for Django's notion of Contexts and Context Processors (or Python's notion of Context Managers).
0 commit comments