Skip to content

Support query_group for WLM #59

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
CHANGES
=======

1.0.1
Copy link
Member

Choose a reason for hiding this comment

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

1.0.1 means PATCH version that should be used for minor bug fix.
1.1.0 is good for 'New Features' on semver.

-----

New Features:

* Support `query_group` options for `Work Load Management`_

.. _Work Load Management: https://docs.aws.amazon.com/redshift/latest/dg/cm-c-implementing-workload-management.html

1.0.0 (2019/01/29)
------------------

Expand Down
16 changes: 16 additions & 0 deletions django_redshift_backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,22 @@ def __init__(self, *args, **kwargs):
self.introspection = DatabaseIntrospection(self)
self.validation = BaseDatabaseValidation(self)

def get_connection_params(self):
conn_params = super(DatabaseWrapper, self).get_connection_params()
self.query_group = conn_params.pop('query_group', None)
return conn_params

def init_connection_state(self):
super(DatabaseWrapper, self).init_connection_state()
assignments = []
if self.query_group:
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be better that self.query_group is initialized with None on __Init__ method or class variable. (I know init_connection_state always called after get_connection_params)

assignments.append('SET query_group TO \'{query_group}\''.format(
query_group=self.query_group))

if assignments:
with self.cursor() as cursor:
cursor.execute('; '.join(assignments))
Copy link
Member

Choose a reason for hiding this comment

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

Why are you using LIST type for assignments variable?
It seems it only have single string object in this method.


def check_constraints(self, table_names=None):
"""
No constraints to check in Redshift.
Expand Down
12 changes: 12 additions & 0 deletions doc/basic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,21 @@ ENGINE for DATABASES is 'django_redshift_backend'. You can set the name in your
'PASSWORD': '<your database password>',
'HOST': '<your database hostname>',
'PORT': '5439',
'OPTIONS': {
'query_group': 'webapp',
},
}
}


OPTIONS
-------

- ``query_group``: Set query_group_ to use `Work Load Management`_

.. _query_group: https://docs.aws.amazon.com/redshift/latest/dg/r_query_group.html
.. _Work Load Management: https://docs.aws.amazon.com/redshift/latest/dg/cm-c-implementing-workload-management.html

For more information, please refer :doc:`refs`.


3 changes: 3 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5439',
'OPTIONS': {
'query_group': 'webapp',
},
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/test_redshift_backend.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

import mock
import unittest

from django.db import connections
Expand All @@ -20,6 +21,20 @@ def test_load_redshift_backend(self):
db = connections['default']
self.assertIsNotNone(db)

def test_query_group(self):
db = connections['default']
self.assertIsNotNone(db)

db.get_connection_params()
self.assertEqual('webapp', db.query_group)

db.connection = mock.Mock()
db.ensure_timezone = mock.Mock()
db.force_debug_cursor = True

db.init_connection_state()
self.assertEqual(1, len(db.queries_log))


expected_ddl_normal = norm_sql(
u'''CREATE TABLE "testapp_testmodel" (
Expand Down