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

fix: migrations to make it postgresql compatible. #2281

Merged
merged 1 commit into from
Apr 9, 2025

Conversation

qasimgulzar
Copy link
Contributor

@qasimgulzar qasimgulzar commented Nov 4, 2024

Summary

This pull request includes updates to several migration files across different integrated channels to handle database-specific operations for creating and dropping indexes. The changes ensure compatibility with various database engines, including SQLite, PostgreSQL, and MySQL.

Key Changes:

Database-Specific Index Handling:

  • Added conditional logic to handle index creation and dropping for PostgreSQL and MySQL in integrated_channels/blackboard/migrations/0013_alter_blackboardlearnerdatatransmissionaudit_index_together.py.
  • Implemented similar changes for integrated_channels/canvas/migrations/0028_alter_canvaslearnerdatatransmissionaudit_index_together.py [1] integrated_channels/cornerstone/migrations/0025_alter_cornerstonelearnerdatatransmissionaudit_index_together.py [2] integrated_channels/degreed/migrations/0027_alter_degreedlearnerdatatransmissionaudit_index_together.py [3] and integrated_channels/degreed2/migrations/0019_alter_degreed2learnerdatatransmissionaudit_index_together.py [4].

Refactoring for Index SQL Generation:

  • Refactored index creation and dropping SQL generation into functions for better readability and maintainability in integrated_channels/moodle/migrations/0023_alter_moodlelearnerdatatransmissionaudit_index_together.py.
  • Applied a similar approach in integrated_channels/sap_success_factors/migrations/0011_alter_sapsuccessfactorslearnerdatatransmissionaudit_index_together.py.

Table Name Specification:

  • Added db_table specification to the Meta class in the model to ensure the correct table name is used in integrated_channels/sap_success_factors/models.py.

These changes improve the robustness and compatibility of the migration scripts across different database backends.

@openedx-webhooks
Copy link

openedx-webhooks commented Nov 4, 2024

Thanks for the pull request, @qasimgulzar!

This repository is currently maintained by @openedx/2u-enterprise.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.


Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Nov 4, 2024
@mphilbrick211 mphilbrick211 added the needs test run Author's first PR to this repository, awaiting test authorization from Axim label Nov 8, 2024
@qasimgulzar
Copy link
Contributor Author

@ormsbee could you please approve the workflow execution?

@ormsbee
Copy link

ormsbee commented Nov 15, 2024

@qasimgulzar: @iloveagent57 said that he could take a look at this one.

@mphilbrick211
Copy link

@qasimgulzar - just flagging some failing checks here.

@mphilbrick211 mphilbrick211 removed the needs test run Author's first PR to this repository, awaiting test authorization from Axim label Dec 10, 2024
@qasimgulzar
Copy link
Contributor Author

I will take care of it

@qasimgulzar qasimgulzar force-pushed the qasim/migrations/postgresql branch from 88112fd to 3e30cce Compare December 12, 2024 11:21
@mphilbrick211 mphilbrick211 requested a review from a team February 24, 2025 22:45
@mphilbrick211
Copy link

Hi @openedx/2u-enterprise! Would someone be able to take a look at this?

@qasimgulzar qasimgulzar force-pushed the qasim/migrations/postgresql branch from 3e30cce to 7496e15 Compare February 25, 2025 07:22
@mphilbrick211
Copy link

Hi @openedx/2u-enterprise! Would someone be able to take a look at this?

Friendly ping on this @openedx/enterprise-titans @openedx/2u-enterprise

Copy link

@ormsbee ormsbee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some questions and a few minor (optional) nit suggestions.

Comment on lines 20 to 21
else:
operations = [
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.AlterIndexTogether(
name='blackboardlearnerdatatransmissionaudit',
index_together={('enterprise_customer_uuid', 'plugin_configuration_id')},
),
],
database_operations=[
migrations.RunSQL(sql="""
CREATE INDEX blackboard_bldta_85936b55_idx
ON blackboard_blackboardlearnerdatatransmissionaudit (enterprise_customer_uuid, plugin_configuration_id)
ALGORITHM=INPLACE LOCK=NONE
""", reverse_sql="""
DROP INDEX blackboard_bldta_85936b55_idx
ON blackboard_blackboardlearnerdatatransmissionaudit
"""),
]
),
]
if 'postgresql' in db_engine:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think this would read a bit easier if it was in an "elif" so that each set of operations is at the same indentation level, i.e.:

if 'sqlite3' in db_engine:
    # ...
elif 'postgresql' in db_engine:
    # ....
else:
    # ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would agree it make sense.

],
database_operations=[
migrations.RunSQL(sql="""
CREATE INDEX cornerstone_cldta_85936b55_idx
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Why not CREATE INDEX CONCURRENTLY here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it will be fresh start of database if anyone will be choosing to go with Postgresql and there will be no pre-populated data. However If I use CONCURRENTLY there is a potential if could have error in background which is very unlikely but there is a chance. So I believe it will only add complexity with no benefits

],
database_operations=[
migrations.RunSQL(sql="""
CREATE INDEX degreed_dldta_85936b55_idx
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Why not CREATE INDEX CONCURRENTLY here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it will be fresh start of database if anyone will be choosing to go with Postgresql and there will be no pre-populated data. However If I use CONCURRENTLY there is a potential if could have error in background which is very unlikely but there is a chance. So I believe it will only add complexity with no benefits

else:
operations = [
migrations.AlterIndexTogether(
name='degreed2learnerdatatransmissionaudit',
index_together={('enterprise_customer_uuid', 'plugin_configuration_id')},
),
]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused by this. Why is this here? When would this ever execute for us, if it's not sqlite, postgres, or mysql?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right I might have overthink here I can cover sqlite with else only.

CREATE UNIQUE INDEX content_transmission_channel_code_plugin_id_content_id_unique
ON integrated_channel_contentmetadataitemtransmission (integrated_channel_code, plugin_configuration_id, content_id)
ALGORITHM=INPLACE LOCK=NONE
if 'postgresql' in db_engine:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same nit about using elif to reduce indentation here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree.

ON moodle_moodlelearnerdatatransmissionaudit (enterprise_customer_uuid, plugin_configuration_id)
"""
else:
raise ValueError("Unsupported database engine")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment about why it's okay to not account for sqlite3 here.

Comment on lines +79 to +81
options={
'db_table': 'sap_success_factors_sapsuccessfactorslearnerdatatransmission3ce5',
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Why was this change necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If don't do this change the table name generated in postgresql doesn't match with the table name it generates in mysql. I believe it is because of the make table name length constraints.
The reason of pinning it is as we are using the table name while creating index in subsequent migration.
0011_alter_sapsuccessfactorslearnerdatatransmissionaudit_index_together.py

return """
CREATE INDEX sapsf_sldta_85936b55_idx
ON sap_success_factors_sapsuccessfactorslearnerdatatransmission3ce5 (enterprise_customer_uuid, plugin_configuration_id)
"""
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIt: I think this is okay either way, but if it's only being used once, I think you might as well inline it like you did elsewhere in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noted

ON xapi_xapilearnerdatatransmissionaudit (enterprise_customer_uuid, plugin_configuration_id)
ALGORITHM=INPLACE LOCK=NONE
""", reverse_sql="""
migrations.RunSQL(sql=generate_index_sql(db_engine), reverse_sql="""
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think this is okay either way, but if it's only being used once, I think you might as well inline it like you did elsewhere in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noted

@qasimgulzar qasimgulzar requested a review from ormsbee March 23, 2025 08:08
@qasimgulzar
Copy link
Contributor Author

@ormsbee I had accommodated your feedback please give it a look again. Also I have responded to few of your questions.

Copy link

@ormsbee ormsbee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll merge when tests pass.

@qasimgulzar qasimgulzar force-pushed the qasim/migrations/postgresql branch from 7d83fc3 to 3531071 Compare April 7, 2025 19:40
@ormsbee
Copy link

ormsbee commented Apr 7, 2025

@openedx/2u-enterprise: I'm planning to merge this tomorrow. Please let me know if anyone wants to look it over before I do so. Thank you.

@qasimgulzar
Copy link
Contributor Author

@ormsbee could you please approve the tests again?

@qasimgulzar
Copy link
Contributor Author

@ormsbee could you please approve the workflows now?

@iloveagent57
Copy link
Contributor

@qasimgulzar @ormsbee approved by enterprise, workflows are running now.

@iloveagent57
Copy link
Contributor

@qasimgulzar could you squash the commits, add a CHANGELOG entry, and bump the version, please, and then this will be ready to merge. Thanks!

@qasimgulzar
Copy link
Contributor Author

@iloveagent57 sure I will do it today

@qasimgulzar qasimgulzar force-pushed the qasim/migrations/postgresql branch from d9bb855 to 71abea6 Compare April 9, 2025 05:29
@qasimgulzar
Copy link
Contributor Author

qasimgulzar commented Apr 9, 2025

@iloveagent57 I just pushed these changes.

  • Update CHANGELOG.rst
  • Bump Version [5.12.4]
  • Squashed the commits

@ormsbee ormsbee merged commit 2a6a89a into openedx:master Apr 9, 2025
9 of 11 checks passed
@github-project-automation github-project-automation bot moved this from Ready for Review to Done in Contributions Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open-source-contribution PR author is not from Axim or 2U
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants