# Complex Cairn Migrations

**URL:** <https://discuss.openedx.org/t/complex-cairn-migrations/9929>\
**Category:** Tutor Help\
**Created:** [April 21, 2023, 9:19pm UTC](https://discuss.openedx.org/t/complex-cairn-migrations/9929 "2023-04-21T21:19:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Cohen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/jeff_cohen/32/3417_2.png) [@Jeff\_Cohen](https://discuss.openedx.org/u/Jeff_Cohen)\
**Post date:** [April 21, 2023, 9:19pm UTC](https://discuss.openedx.org/t/complex-cairn-migrations/9929/1 "2023-04-21T21:19:46Z")

</div>

I am looking to push a new dataset into clickhouse that joins details that are currently stored in mongodb and mysql. I have a prototype that extracts and transforms this data using a python script but I am trying to determine the best way to load it into clickhouse. All I have found in the tutorcairn source code are sql migration files, which wouldn’t be sufficient for loading the the data points from mongo. Are there examples of python migrations that others have been created for loading data into cairn?

Thanks in advance!

cc [@tutor-maintainers](https://discuss.openedx.org/groups/tutor-maintainers)

---

<div class="post-metadata">

**Author:** ![regis](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/regis/32/2640_2.png) [@regis](https://discuss.openedx.org/u/regis)\
**Post date:** [April 24, 2023, 4:53pm UTC](https://discuss.openedx.org/t/complex-cairn-migrations/9929/2 "2023-04-24T16:53:27Z")

</div>

Hi Jeff! Cairn does have a script to import aggregated data from MongoDB to CLickhouse – it’s a bit clunky, but it’s really because the MongoDB data model is just difficult to work with. You should have a look at the “importcoursedata.py” script in `$(tutor config printroot)/env/plugins/cairn/apps/openedx/scripts/importcoursedata.py`.

Basically, the only way I found to read data jointly from MySQL and MongoDB was first to import data from MongoDB to Clickhouse. Cairn does this to import the structure of courses (section names and hierarchy). Then, joining this data with MySQL becomes easier.

Does this answer your question?

---

<div class="post-metadata">

**Author:** ![Jeff\_Cohen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/jeff_cohen/32/3417_2.png) [@Jeff\_Cohen](https://discuss.openedx.org/u/Jeff_Cohen)\
**Post date:** [April 24, 2023, 8:20pm UTC](https://discuss.openedx.org/t/complex-cairn-migrations/9929/3 "2023-04-24T20:20:21Z")

</div>

Thank you @regis! That is a great example. Can you instruct me how run a custom script similar to importcoursedata on a schedule to refresh the data in clickhouse?

---

<div class="post-metadata">

**Author:** ![Jeff\_Cohen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/jeff_cohen/32/3417_2.png) [@Jeff\_Cohen](https://discuss.openedx.org/u/Jeff_Cohen)\
**Post date:** [April 24, 2023, 9:06pm UTC](https://discuss.openedx.org/t/complex-cairn-migrations/9929/4 "2023-04-24T21:06:32Z")

</div>

The `importcoursedata` script is a great start toward getting the data I need into clickhouse. I would want to enhance it, though, to include fields such as `parent_id` in order to reconstruct the course hierarchy and `definition_id` in order to track library problem blocks that are reused in multiple course contexts.

---

<div class="post-metadata">

**Author:** ![regis](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/regis/32/2640_2.png) [@regis](https://discuss.openedx.org/u/regis)\
**Post date:** [April 25, 2023, 8:22am UTC](https://discuss.openedx.org/t/complex-cairn-migrations/9929/5 "2023-04-25T08:22:28Z")

</div>

> [@Jeff\_Cohen](#):
>
> Can you instruct me how run a custom script similar to importcoursedata on a schedule to refresh the data in clickhouse?

There are many ways to achieve this. If you are running `tutor local` you could for instance create a cron job on the host that would execute the following command:

```
tutor local run -v /host/path/to/myscript.py:/tmp/myscript.py:ro lms /tmp/myscript.py

```

> [@Jeff\_Cohen](#):
>
> The `importcoursedata` script is a great start toward getting the data I need into clickhouse. I would want to enhance it, though, to include fields such as `parent_id` in order to reconstruct the course hierarchy and `definition_id` in order to track library problem blocks that are reused in multiple course contexts.

That script is not really extensible, and I’m not sure whether it would make sense to modify it. Maybe you could create a separate table to import your data?

Alternatively, I may have heard recently that the course structure was also stored in MySQL – but I’m not sure in what table. I propose to summon @dave’s omnipotent knowledge to enlighten us! Dave, could you give us a clue? I searched in edx-platform but could not find anything. Thanks!

---

<div class="post-metadata">

**Author:** ![dave](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/dave/32/263_2.png) [@dave](https://discuss.openedx.org/u/dave)\
**Post date:** [April 25, 2023, 2:14pm UTC](https://discuss.openedx.org/t/complex-cairn-migrations/9929/6 "2023-04-25T14:14:58Z")

</div>

> [@regis](#):
>
> Alternatively, I may have heard recently that the course structure was also stored in MySQL – but I’m not sure in what table. I propose to summon @dave’s omnipotent knowledge to enlighten us! Dave, could you give us a clue? I searched in edx-platform but could not find anything. Thanks!

In turn, I summon @BrianMesick and his recent work on getting edx-platform’s `coursegraph` app to [push its course structure data into clickhouse](https://github.com/openedx/edx-platform/pull/32055).

> [@Jeff\_Cohen](#):
>
> … and `definition_id` in order to track library problem blocks that are reused in multiple course contexts.

The `definition_id` is a low level implementation detail, and will change even with minor edits to the source library content without a backwards link to the previous one. You might be better served by looking at the `library_content` blocks in the course content data that gets exported, which will give you explicit links to the library and version being used.

FWIW, in the new copy-paste Studio work, [`copied_from_block`](https://github.com/openedx/edx-platform/pull/31969/files#diff-36787346e587ea0cc86f1854e5c5ec3b37b6c9eb594c078d609336d2818192a1R48) is an explicit attribute that will be mixed into the OLX–though I don’t know how useful vs. noisy that’s going to be in practice.

---

<div class="post-metadata">

**Author:** ![BrianMesick](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/brianmesick/32/4249_2.png) [@BrianMesick](https://discuss.openedx.org/u/BrianMesick)\
**Post date:** [April 25, 2023, 2:39pm UTC](https://discuss.openedx.org/t/complex-cairn-migrations/9929/7 "2023-04-25T14:39:55Z")

</div>

I am summoned! So the branch above is defunct, but I’m actively working on doing a more general version of it that will allow other signals / events to trigger sending data to ClickHouse in real-ish time (initially over Celery tasks, later over the event bus 🤞). Work is happening over here, initially based on that branch, but only started yesterday: [Add Course Published event listener and plugin plumbing by bmtcril · Pull Request #1 · openedx/openedx-event-sink-clickhouse · GitHub](https://github.com/openedx/openedx-event-sink-clickhouse/pull/1)

This does a similar thing to what Coursegraph does, which is listen to the [COURSE\_PUBLISHED](https://github.com/openedx/edx-platform/blob/d338f00e396d9d485e02e55b5e91875ba97a2fa7/xmodule/modulestore/django.py#L180) signal, fire off a Celery task which uses the in-platform code to pull the course structure from modulestore and serialize it out to ClickHouse in JSON. Once the signal version is working I’ll add in the [management command](https://github.com/openedx/edx-platform/blob/d338f00e396d9d485e02e55b5e91875ba97a2fa7/cms/djangoapps/coursegraph/management/commands/dump_to_neo4j.py) that allows you to dump (one / some / all) courses on demand.

I’m hoping to have that plugin ready for real review in a few days, but it’s currently in the ugly prototype-to-production phase of development.

---

<div class="post-metadata">

**Author:** ![Jeff\_Cohen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/jeff_cohen/32/3417_2.png) [@Jeff\_Cohen](https://discuss.openedx.org/u/Jeff_Cohen)\
**Post date:** [April 25, 2023, 8:44pm UTC](https://discuss.openedx.org/t/complex-cairn-migrations/9929/8 "2023-04-25T20:44:14Z")

</div>

> [@dave](#):
>
> The `definition_id` is a low level implementation detail, and will change even with minor edits to the source library content without a backwards link to the previous one. You might be better served by looking at the `library_content` blocks in the course content data that gets exported, which will give you explicit links to the library and version being used.

Our goal is to track a user’s performance on a particular problem from a library across all courses that utilize that library content. I was under the impression that `definition_id` is the common identifier that is referenced by all instances. From what I have seen, this is how I understand the library\_content block to work:

1. When a particular library is selected in a library\_content block, all of the library’s children are copied as children of the library\_content block with new unique block\_ids
2. Corresponding children of the library and library\_content block can be associated via their shared definition\_id
3. If a new library\_content block references the same library, the children will be copied again and receive new unique block\_ids but still share the underlying definition\_id

Is this an accurate description and can I rely on the definition\_id to associate these problem instances across contexts? Or am I missing details?

---

<div class="post-metadata">

**Author:** ![dave](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/dave/32/263_2.png) [@dave](https://discuss.openedx.org/u/dave)\
**Post date:** [April 26, 2023, 6:08pm UTC](https://discuss.openedx.org/t/complex-cairn-migrations/9929/9 "2023-04-26T18:08:53Z")

</div>

You’re correct in terms of what happens, but I think the issues are:

1. The `definition_id` used by the library component changes whenever you make any sort of edit to that component, but the individual courses will still point to the old `definition_id` until you sync from the library.
2. The `definition_id` is a low-level implementation detail (i.e. it’s not part of the OLX export), and may not be safe to rely on in the long term.

I dug into this a bit more this morning, and I think what you want is to call [`get_block_original_usage`](https://github.com/openedx/edx-platform/blob/4afb210935d188468c5f7f54ac2c9631fa9fe314/xmodule/modulestore/split_mongo/split.py#L1476-L1489) on the modulestore, and pass it the UsageKey of the ProblemBlock in the course. That’ll get you the stable UsageKey for the original in the library. The LibraryToolsService [uses this](https://github.com/openedx/edx-platform/blob/4afb210935d188468c5f7f54ac2c9631fa9fe314/xmodule/library_tools.py#L79-L86) for analytics.

---

<div class="post-metadata">

**Author:** ![Jeff\_Cohen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/jeff_cohen/32/3417_2.png) [@Jeff\_Cohen](https://discuss.openedx.org/u/Jeff_Cohen)\
**Post date:** [May 10, 2023, 12:38pm UTC](https://discuss.openedx.org/t/complex-cairn-migrations/9929/10 "2023-05-10T12:38:16Z")

</div>

Thank you for your guidance! I’m sorry for not replying sooner. I have been absorbed with a different project and haven’t had a chance to follow up yet. I’ll share my results once I have a chance to experiment with your advice. Thanks again.

---

<div class="post-metadata">

**Author:** ![Jeff\_Cohen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.openedx.org/jeff_cohen/32/3417_2.png) [@Jeff\_Cohen](https://discuss.openedx.org/u/Jeff_Cohen)\
**Post date:** [June 23, 2023, 5:45pm UTC](https://discuss.openedx.org/t/complex-cairn-migrations/9929/11 "2023-06-23T17:45:27Z")

</div>

@dave I finally got back around to this task and it appears that your suggestion to use `get_block_original_usage` is working like a charm. I am now able to track a user’s performance on a particular problem (defined in a library) over time across all contexts in which that library problem is used. This is flowing into Cairn for internal reporting and tracking. Thanks again!
