-
Notifications
You must be signed in to change notification settings - Fork 18
fix: make database function compatible with postgresql and mysql both #260
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c594632
fix: make database function compatible with postgresql and mysql both
qasimgulzar bc7a493
fix: tests and quality issues
qasimgulzar 1f13467
fix: quality issues
qasimgulzar 48194a0
fix: quality issues
qasimgulzar 6042f4e
fix: feedback accommodated
qasimgulzar fe0da59
Merge branch 'openedx:main' into qasim/postgresql
qasimgulzar 71698c3
fix: make delimiter configurable
qasimgulzar 4731b8b
fix: remove ellipsis from comment
qasimgulzar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,15 +1,16 @@ | ||||||
| """ | ||||||
| Utilities for tagging and taxonomy models | ||||||
| """ | ||||||
| from django.db.models import Aggregate, CharField | ||||||
| from django.db.models.expressions import Func | ||||||
| from django.db import connection as db_connection | ||||||
| from django.db.models import Aggregate, CharField, TextField | ||||||
| from django.db.models.expressions import Combinable, Func | ||||||
|
|
||||||
| RESERVED_TAG_CHARS = [ | ||||||
| '\t', # Used in the database to separate tag levels in the "lineage" field | ||||||
| # e.g. lineage="Earth\tNorth America\tMexico\tMexico City" | ||||||
| ' > ', # Used in the search index and Instantsearch frontend to separate tag levels | ||||||
| # e.g. tags_level3="Earth > North America > Mexico > Mexico City" | ||||||
| ';', # Used in CSV exports to separate multiple tags from the same taxonomy | ||||||
| ';', # Used in CSV exports to separate multiple tags from the same taxonomy… | ||||||
| # e.g. languages-v1: en;es;fr | ||||||
| ] | ||||||
| TAGS_CSV_SEPARATOR = RESERVED_TAG_CHARS[2] | ||||||
|
|
@@ -34,21 +35,48 @@ def as_sqlite(self, compiler, connection, **extra_context): | |||||
| ) | ||||||
|
|
||||||
|
|
||||||
| class StringAgg(Aggregate): # pylint: disable=abstract-method | ||||||
| class StringAgg(Aggregate, Combinable): | ||||||
| """ | ||||||
| Aggregate function that collects the values of some column across all rows, | ||||||
| and creates a string by concatenating those values, with "," as a separator. | ||||||
| and creates a string by concatenating those values, with a specified separator. | ||||||
|
|
||||||
| This is the same as Django's django.contrib.postgres.aggregates.StringAgg, | ||||||
| but this version works with MySQL and SQLite. | ||||||
| This version supports PostgreSQL (STRING_AGG), MySQL (GROUP_CONCAT), and SQLite. | ||||||
| """ | ||||||
| # Default function is for MySQL (GROUP_CONCAT) | ||||||
| function = 'GROUP_CONCAT' | ||||||
| template = '%(function)s(%(distinct)s%(expressions)s)' | ||||||
|
|
||||||
| def __init__(self, expression, distinct=False, **extra): | ||||||
| def __init__(self, expression, distinct=False, delimiter=',', **extra): | ||||||
| self.delimiter = delimiter | ||||||
bradenmacdonald marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| # Handle the distinct option and output type | ||||||
| distinct_str = 'DISTINCT ' if distinct else '' | ||||||
|
|
||||||
| extra.update({ | ||||||
| 'distinct': distinct_str, | ||||||
| 'output_field': CharField(), | ||||||
| }) | ||||||
|
|
||||||
| # Check the database backend (PostgreSQL, MySQL, or SQLite) | ||||||
| if 'postgresql' in db_connection.vendor.lower(): | ||||||
pomegranited marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| self.function = 'STRING_AGG' | ||||||
| self.template = '%(function)s(%(distinct)s%(expressions)s, %(delimiter)s)' | ||||||
| extra.update({ | ||||||
| "delimiter": ",", | ||||||
|
||||||
| "delimiter": ",", | |
| "delimiter": delimiter, |
Isn't this all that's needed to address my comment?
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why this ellipsis character was added here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated