Skip to content

Commit

Permalink
Merge pull request #2 from somewes/github-actions
Browse files Browse the repository at this point in the history
GitHub actions
  • Loading branch information
somewes authored Aug 22, 2022
2 parents b2435ca + 924b325 commit f921bd6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ Micah Hausler ([email protected])
Andrew Plummer (https://github.com/plumdog)
Jure Žvelc (https://github.com/jzvelc)
Timothy J Laurent (https://github.com/timothyjlaurent)
NickHilton (https://github.com/NickHilton)
John Vandenberg (https://github.com/jayvdb)
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include README.rst
include LICENSE
recursive-include requirements *
include *.py
recursive-include docs *.py
4 changes: 4 additions & 0 deletions docs/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ v3.0.0
* Add support for python 3.9
* Drop python 3.6

v2.0.1
------
* BUG: 'bigserial' dtype should not be a cast type - NickHilton

v2.0.0
------
* Add support Django 3.0, 3.1
Expand Down
20 changes: 18 additions & 2 deletions querybuilder/query.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from copy import deepcopy

from django import VERSION
from django.db import connection as default_django_connection
from django.db.models import Q, AutoField
from django.db.models.query import QuerySet
Expand All @@ -13,6 +14,8 @@
from querybuilder.helpers import set_value_for_keypath, copy_instance
from querybuilder.tables import TableFactory, ModelTable, QueryTable

SERIAL_DTYPES = ['serial', 'bigserial']


class Join(object):
"""
Expand Down Expand Up @@ -1117,6 +1120,19 @@ def get_insert_sql(self, rows):

return self.sql, sql_args

def should_not_cast_value(self, field_object):
"""
In Django 4.1 on PostgreSQL, AutoField, BigAutoField, and SmallAutoField are now created as identity
columns rather than serial columns with sequences.
"""
db_type = field_object.db_type(self.connection)
if db_type in SERIAL_DTYPES:
return True
if (VERSION[0] == 4 and VERSION[1] >= 1) or VERSION[0] >= 5:
if getattr(field_object, 'primary_key', None) and getattr(field_object, 'serialize', None) is False:
return True
return False

def get_update_sql(self, rows):
"""
Returns SQL UPDATE for rows ``rows``
Expand Down Expand Up @@ -1169,8 +1185,8 @@ def get_update_sql(self, rows):
field_object = self.tables[0].model._meta.get_field(field_names[field_index])
db_type = field_object.db_type(self.connection)

# Don't cast the pk
if db_type == 'serial':
# Don't cast serial types
if self.should_not_cast_value(field_object):
placeholders.append('%s')
else:
# Cast the placeholder to the data type
Expand Down
15 changes: 4 additions & 11 deletions querybuilder/tests/update_tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json

from django import VERSION
from django.test.utils import override_settings
from django_dynamic_fixture import G

Expand All @@ -17,11 +16,6 @@ def setUp(self):
self.logger = Logger()
self.logger.start_logging()

# Starting on Django 4, the id field adds ::integer automatically
self.integer_cast_string = ''
if (VERSION[0] == 4 and VERSION[1] >= 1) or VERSION[0] >= 5:
self.integer_cast_string = '::integer'

def test_update_single_row(self):
query = Query().from_table(
table=Account,
Expand All @@ -46,7 +40,7 @@ def test_update_single_row(self):
'SET user_id = new_values.user_id, '
'first_name = new_values.first_name, '
'last_name = new_values.last_name '
f'FROM (VALUES (%s{self.integer_cast_string}, %s::integer, %s::varchar(64), %s::varchar(64))) '
'FROM (VALUES (%s, %s::integer, %s::varchar(64), %s::varchar(64))) '
'AS new_values (id, user_id, first_name, last_name) '
'WHERE querybuilder_tests_account.id = new_values.id'
)
Expand All @@ -66,8 +60,7 @@ def test_update_single_row(self):
"SET user_id = new_values.user_id, "
"first_name = new_values.first_name, "
"last_name = new_values.last_name "
f"FROM (VALUES (1{self.integer_cast_string}, 1::integer, "
"'Test''s'::varchar(64), '\"User\"'::varchar(64))) "
"FROM (VALUES (1, 1::integer, 'Test''s'::varchar(64), '\"User\"'::varchar(64))) "
"AS new_values (id, user_id, first_name, last_name) "
"WHERE querybuilder_tests_account.id = new_values.id"
)
Expand Down Expand Up @@ -122,7 +115,7 @@ def test_update_multiple_rows(self):
'SET user_id = new_values.user_id, '
'first_name = new_values.first_name, '
'last_name = new_values.last_name '
f'FROM (VALUES (%s{self.integer_cast_string}, %s::integer, %s::varchar(64), %s::varchar(64)), '
'FROM (VALUES (%s, %s::integer, %s::varchar(64), %s::varchar(64)), '
'(%s, %s, %s, %s)) '
'AS new_values (id, user_id, first_name, last_name) '
'WHERE querybuilder_tests_account.id = new_values.id'
Expand All @@ -146,7 +139,7 @@ def test_update_multiple_rows(self):
"SET user_id = new_values.user_id, "
"first_name = new_values.first_name, "
"last_name = new_values.last_name "
f"FROM (VALUES (1{self.integer_cast_string}, 1::integer, 'Test'::varchar(64), 'User'::varchar(64)), "
"FROM (VALUES (1, 1::integer, 'Test'::varchar(64), 'User'::varchar(64)), "
"(2, 2, 'Test2', 'User2')) "
"AS new_values (id, user_id, first_name, last_name) "
"WHERE querybuilder_tests_account.id = new_values.id"
Expand Down

0 comments on commit f921bd6

Please sign in to comment.