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: populate DB is broken after upgrading SQLAlchemy to 2.x version #406

Merged
merged 5 commits into from
Apr 29, 2024

Conversation

davidgamez
Copy link
Member

@davidgamez davidgamez commented Apr 25, 2024

Summary:

SQLAlchemy 2.x added support for many-to-many relations mapping. This is handy for querying the entities as it reduces the amount of independent queries, joins, and columns within the code. While updating and creating entities, it is also possible to leave most of the heavy work to the ORM. However, this comes with some restrictions on the entity types to be used as part of the relationship arrays. This PR refactored the populate script, fixing the incompatibility with 2.x version and using the create/update capabilities offered by SqlAchemy

Successful execution on DEV

Fixes #402

Expected behavior:

Populate script executes, creates, and updates without issues.

Testing tips:

On clean DB(locally)

  • Execute:
 ./scripts/docker-localdb-rebuild-data.sh --populate-db
  • Connect to the local DB and verify that entities are added correctly

Create a new Feed

  • Clean DB and populate data
 ./scripts/docker-localdb-rebuild-data.sh --populate-db
  • Open the {your project dir}/data/catalogs.csv file and add a new feed(row). Change the first column, increasing the mdb_source_id by one
  • Execute populate DB script
./scripts/populate-db.sh {your project dir}/data/catalogs.csv
  • Verify that the new feed is created

Update existent Feed

  • Clean DB and populate data
 ./scripts/docker-localdb-rebuild-data.sh --populate-db
  • Open the {your project dir}/data/catalogs.csv file and edit an existing feed feed(row).
  • Execute populate DB script
./scripts/populate-db.sh {your project dir}/data/catalogs.csv
  • Verify that the feed's properties were updated accordingly

Please make sure these boxes are checked before submitting your pull request - thanks!

  • Run the unit tests with ./scripts/api-tests.sh to make sure you didn't break anything
  • Add or update any needed documentation to the repo
  • Format the title like "feat: [new feature short description]". Title must follow the Conventional Commit Specification(https://www.conventionalcommits.org/en/v1.0.0/).
  • Linked all relevant issues
  • Include screenshot(s) showing how this pull request works and fixes the issue(s)

@@ -40,11 +58,12 @@ def __new__(cls, *args, **kwargs):
cls.instance = object.__new__(cls)
return cls.instance

def __init__(self):
def __init__(self, echo_sql=True):
Copy link
Member Author

Choose a reason for hiding this comment

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

Set the default to True to keep the current behavior. We should consider set it to False to avoid unnecessary SQL logs.

@davidgamez davidgamez marked this pull request as ready for review April 25, 2024 20:49
@davidgamez
Copy link
Member Author

I'm planning to add test coverage in a separate PR as this bug is blocking at leat two other issues,

@davidgamez davidgamez requested review from cka-y and jcpitre April 25, 2024 21:56
Copy link
Contributor

@cka-y cka-y left a comment

Choose a reason for hiding this comment

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

LGTM added a non blocking code suggestion to improve logs! I even tested updating the location and the behaviour in the database is as expected 👍

api/src/scripts/populate_db.py Outdated Show resolved Hide resolved
@davidgamez davidgamez merged commit c5addfd into main Apr 29, 2024
2 checks passed
@davidgamez davidgamez deleted the fix/populate-script branch April 29, 2024 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Populate DB is broken after upgrading SQLAlchemy to 2.x version
2 participants