-
-
Notifications
You must be signed in to change notification settings - Fork 49
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
Database inspection capability - fixes #13 #90
Conversation
Thank you for your PR! |
Codecov Report
@@ Coverage Diff @@
## master #90 +/- ##
==========================================
+ Coverage 47.01% 49.09% +2.08%
==========================================
Files 3 3
Lines 251 277 +26
Branches 62 79 +17
==========================================
+ Hits 118 136 +18
- Misses 123 130 +7
- Partials 10 11 +1
Continue to review full report at Codecov.
|
It might also be worth adding From https://docs.djangoproject.com/en/4.0/releases/3.2/#database-backend-api
|
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.
Thanks, it worked on my Redshift environment!
@@ -553,6 +555,162 @@ class DatabaseCreation(BasePGDatabaseCreation): | |||
pass | |||
|
|||
|
|||
class DatabaseIntrospection(BasePGDatabaseIntrospection): | |||
pass |
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.
nits: I'll remove this pass
line after merging
# contain details of materialized views. | ||
|
||
# This function is based on the version from the Django postgres backend | ||
# from before support for collations were introduced in Django 3.2 |
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.
nits: I'll add a URL to original django source after merging:
# from before support for collations were introduced in Django 3.2 | |
# from before support for collations were introduced in Django 3.2 | |
# https://github.com/django/django/blob/3.1.14/django/db/backends/ | |
# postgresql/introspection.py#L66-L94 |
one or more columns. Also retrieve the definition of expression-based | ||
indexes. | ||
""" | ||
# Based on code from Django 3.2 |
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.
nits: I'll add a URL to original django source after merging:
# Based on code from Django 3.2 | |
# Based on code from Django 3.2 | |
# https://github.com/django/django/blob/3.2.12/django/db/backends/ | |
# postgresql/introspection.py#L148-L182 |
} | ||
|
||
# Now get indexes | ||
# Based on code from Django 1.7 |
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.
nits: I'll add a URL to original django source after merging:
# Based on code from Django 1.7 | |
# Based on code from Django 1.7 | |
# https://github.com/django/django/blob/1.7.11/django/db/backends/ | |
# postgresql_psycopg2/introspection.py#L182-L207 |
Yes, it is currently difficult to provide a connection to Redshift for automated testing. Anyway, thanks to this PR, we can now use inspectdb with django-redshift-backend, and I've prepared a project showcase in the examples directory, so I'll provide Thanks for your awesome contribution! |
It has been released https://pypi.org/project/django-redshift-backend/3.0.0/ |
Subject: Get inspectdb working
Feature or Bugfix
Purpose
inspectdb
management command to work.Redshift is based on PostgreSQL 8.x, but Django supports PostgreSQL >= 9.4, so some of the more modern features of PostgreSQL such as
unnest
andarray_agg
are not available in Redshift. Django uses some of these in the postgres backend introspection methods, so different implementations were needed to retrieve the same information from a Redshift database.get_table_description
to provide its schema view.This has been broken since Django 3.2 introduced support for collations in the postgres backend. Relevant commit
Detail
Implementation of the changes needed for
get_table_description
was straightforward, and involved removing the join to thepg_collation
table from the SQL.The changes required to
get_constraints
, used byinspectdb
, were more complex. The postgres version usesunnest
, but there is no good substitute for this function in Redshift.Rather than attempting to build an overly complex query to achieve the same output as the postgres backend, a second query is made to the
pg_attribute
table and the join made in python. This is done twice within theget_constraints
method, once for constraints and again for indexes.These changes are difficult to write tests for, as the current test suite does not assume a connection to a Redshift cluster, and using a database cursor in a test requires a working database (pytest will enforce use of a
db
fixture).The tests as written mock the cursor, but this has the disadvantage of having to hard code expected SQL and return values, which leaves the tests very brittle.
I have not attempted to implement the
orders
,type
,definition
, oroptions
fields withinget_constraints
. I don't know where these are used or if they're necessary.Relates