Skip to content

Commit 47513db

Browse files
authored
Merge branch 'master' into fix-foreign-primary-key-on-conflict-do-nothing
2 parents 0ed834f + 0124f08 commit 47513db

19 files changed

+324
-59
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ dist/
1919

2020
# Ignore benchmark results
2121
.benchmarks/
22+
23+
# Ignore temporary tox environments
24+
.tox/

.scrutinizer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ build:
2929
- 'pip install coverage --force-reinstall -U'
3030
override:
3131
-
32-
command: python3 -m pytest --cov=psqlextra
32+
command: tox
3333
coverage:
3434
file: '.coverage'
3535
format: 'py-cc'

README.rst

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11

22
.. raw:: html
3-
3+
44
<h1 align="center">
55
<img width="400" src="https://i.imgur.com/79S6OVM.png" alt="django-postgres-extra">
66
<br>
77
<br>
8-
</h1>
8+
</h1>
99

10-
================= ===================
10+
==================== ===================
1111
**Quality** |QualityBadge|_
1212
**Test coverage** |CoverageBadge|_
1313
**License** |LicenseBadge|_
1414
**PyPi** |PyPiBadge|_
15-
================= ===================
15+
**Django versions** >=1.11 (including 2.0)
16+
**Python versions** >=3.5
17+
==================== ===================
1618

1719
.. |QualityBadge| image:: https://scrutinizer-ci.com/g/SectorLabs/django-postgres-extra/badges/quality-score.png
1820
.. _QualityBadge: https://scrutinizer-ci.com/g/SectorLabs/django-postgres-extra/
@@ -37,7 +39,7 @@ Documentation
3739
-------------
3840

3941
* **ReadTheDocs HTML**
40-
42+
4143
http://django-postgres-extra.readthedocs.io
4244

4345
* **Plain MarkDown**
@@ -53,7 +55,7 @@ Major features
5355
* Concurrency safe
5456
* With bulk support (single query)
5557

56-
2. **Extended support for ``HStoreField``**
58+
2. **Extended support for HStoreField**
5759

5860
* Unique constraints
5961
* Null constraints
@@ -126,9 +128,9 @@ FAQ - Frequently asked questions
126128

127129
No. Only Python 3.5 or newer is supported. We're using type hints. These do not work well under older versions of Python.
128130

129-
4. **Does this package work with Django 1.X?**
131+
4. **Which Django versions does this package work with?**
130132

131-
No. Only Django 1.11 or newer is supported.
133+
Django 1.11 or newer (that includes Django 2.0).
132134

133135

134136
Working with the code
@@ -145,31 +147,42 @@ Working with the code
145147
1. Clone the repository:
146148

147149
.. code-block:: bash
148-
150+
149151
λ git clone https://github.com/SectorLabs/django-postgres-extra.git
150152
151153
2. Create a virtual environment:
152154

153155
.. code-block:: bash
154-
156+
155157
λ cd django-postgres-extra
156158
λ virtualenv env
157159
λ source env/bin/activate
158160
159-
3. Install the development/test dependencies:
161+
3. Create a postgres user for use in tests (skip if your default user is a postgres superuser):
162+
163+
.. code-block:: bash
164+
165+
λ createuser --superuser psqlextra --pwprompt
166+
λ export DATABASE_URL=postgres://psqlextra:<password>@localhost/psqlextra
167+
168+
Hint: if you're using virtualenvwrapper, you might find it beneficial to put
169+
the ``export`` line in ``$VIRTUAL_ENV/bin/postactivate`` so that it's always
170+
available when using this virtualenv.
171+
172+
4. Install the development/test dependencies:
160173

161174
.. code-block:: bash
162-
175+
163176
λ pip install -r requirements/test.txt
164-
165-
4. Run the tests:
177+
178+
5. Run the tests:
166179

167180
.. code-block:: bash
168-
169-
λ py.test
170-
171-
5. Run the benchmarks:
181+
182+
λ tox
183+
184+
6. Run the benchmarks:
172185

173186
.. code-block:: bash
174-
187+
175188
λ py.test -c pytest-benchmark.ini

psqlextra/compiler.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,12 @@ def _format_field_value(self, field_name) -> str:
272272
return SQLInsertCompiler.prepare_value(
273273
self,
274274
field,
275-
getattr(self.query.objs[0], field_name)
275+
# Note: this deliberately doesn't use `pre_save_val` as we don't
276+
# want things like auto_now on DateTimeField (etc.) to change the
277+
# value. We rely on pre_save having already been done by the
278+
# underlying compiler so that things like FileField have already had
279+
# the opportunity to save out their data.
280+
getattr(self.query.objs[0], field.attname)
276281
)
277282

278283
def _normalize_field_name(self, field_name) -> str:

psqlextra/indexes/conditional_unique_index.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import django
12
from django.db.models.indexes import Index
23

34

@@ -24,13 +25,18 @@ def __init__(self, condition: str, fields=[], name=None):
2425

2526
def create_sql(self, model, schema_editor, using=''):
2627
"""Creates the actual SQL used when applying the migration."""
27-
28-
sql_create_index = self.sql_create_index
29-
sql_parameters = {
30-
**Index.get_sql_create_template_values(self, model, schema_editor, using),
31-
'condition': self.condition
32-
}
33-
return sql_create_index % sql_parameters
28+
if django.VERSION >= (2, 0):
29+
statement = super().create_sql(model, schema_editor, using)
30+
statement.template = self.sql_create_index
31+
statement.parts['condition'] = self.condition
32+
return statement
33+
else:
34+
sql_create_index = self.sql_create_index
35+
sql_parameters = {
36+
**Index.get_sql_create_template_values(self, model, schema_editor, using),
37+
'condition': self.condition
38+
}
39+
return sql_create_index % sql_parameters
3440

3541
def deconstruct(self):
3642
"""Serializes the :see:ConditionalUniqueIndex for the migrations file."""

psqlextra/manager/manager.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ def update(self, **fields):
9393

9494
# build up the query to execute
9595
self._for_write = True
96-
query = self.query.clone(UpdateQuery)
96+
if django.VERSION >= (2, 0):
97+
query = self.query.chain(UpdateQuery)
98+
else:
99+
query = self.query.clone(UpdateQuery)
97100
query._annotations = None
98101
query.add_update_values(fields)
99102

psqlextra/query.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def add_fields(self, field_names: List[str], allow_m2m: bool=True) -> bool:
9999

100100
alias = self.get_initial_alias()
101101
opts = self.get_meta()
102-
102+
cols = []
103103
for name in field_names:
104104
parts = name.split(LOOKUP_SEP)
105105

@@ -108,7 +108,7 @@ def add_fields(self, field_names: List[str], allow_m2m: bool=True) -> bool:
108108
column_name, hstore_key = parts[:2]
109109
is_hstore, field = self._is_hstore_field(column_name)
110110
if is_hstore:
111-
self.add_select(
111+
cols.append(
112112
HStoreColumn(self.model._meta.db_table or self.model.name, field, hstore_key)
113113
)
114114
continue
@@ -117,7 +117,9 @@ def add_fields(self, field_names: List[str], allow_m2m: bool=True) -> bool:
117117
targets, final_alias, joins = self.trim_joins(targets, joins, path)
118118

119119
for target in targets:
120-
self.add_select(target.get_col(final_alias))
120+
cols.append(target.get_col(final_alias))
121+
if cols:
122+
self.set_select(cols)
121123

122124
def _is_hstore_field(self, field_name: str) -> Tuple[bool, Optional[models.Field]]:
123125
"""Gets whether the field with the specified name is a

requirements/analysis.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pylint
22
pylint-celery==0.3
3-
pylint-common==0.2.2
4-
pylint-django==0.7.2
5-
pylint-plugin-utils==0.2.4
3+
pylint-common==0.2.5
4+
pylint-django==0.8.0
5+
pylint-plugin-utils==0.2.6

requirements/base.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
enforce==0.3.2
1+
enforce==0.3.4

requirements/test.txt

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
-r base.txt
22

3-
coverage==4.2
4-
Django==1.11.1
5-
django-coverage-plugin==1.3.1
63
psycopg2==2.7.3.2
7-
pylint==1.6.4
8-
pylint-common==0.2.2
9-
pylint-django==0.7.2
10-
pylint-plugin-utils==0.2.4
11-
coverage==4.2
4+
pylint==1.8.1
5+
pylint-common==0.2.5
6+
pylint-django==0.8.0
7+
pylint-plugin-utils==0.2.6
8+
coverage==4.4.2
129
django-coverage-plugin==1.3.1
13-
flake8==3.0.4
14-
pep8==1.7.0
15-
dj-database-url==0.4.1
16-
pytest==3.0.6
17-
pytest-benchmark==3.0.0
10+
flake8==3.5.0
11+
pep8==1.7.1
12+
dj-database-url==0.4.2
13+
pytest==3.3.2
14+
pytest-benchmark==3.1.1
1815
pytest-django==3.1.2
19-
pytest-cov==2.4.0
16+
pytest-cov==2.5.1
17+
tox==2.9.1

0 commit comments

Comments
 (0)