From a0ab6de88fc0dda9c945607bc4b651ba828597d3 Mon Sep 17 00:00:00 2001 From: "P. Myer Nore" Date: Sun, 11 Nov 2018 15:34:33 -0500 Subject: [PATCH 1/2] Add support for Django 2.0 and Django 2.1 * fixed a `queue` import error with multiprocessing_utils.py * Added Django 2.0 and 2.1 to test matrix * Added models.CASCADE to every foreign key except one: Index.active_version. Generated migration for that one to set it to null upon delete instead. * use utf-8 encoding for generating hash, but save bytestring to db instead, so as to avoid saving `b'{json here}'` into the `json` field of IndexVersion in Django 2; see https://docs.djangoproject.com/en/2.1/releases/2.0/#removed-support-for-bytestrings-in-some-places for more info. #6 support django 2 --- .travis.yml | 12 ++++------- .../migrations/0007_auto_20181111_1251.py | 21 +++++++++++++++++++ django_elastic_migrations/models.py | 10 ++++----- django_elastic_migrations/utils/es_utils.py | 4 ++-- .../utils/multiprocessing_utils.py | 7 +++---- tox.ini | 4 +--- 6 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 django_elastic_migrations/migrations/0007_auto_20181111_1251.py diff --git a/.travis.yml b/.travis.yml index 6fc7230..60f7b8f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,14 +15,10 @@ matrix: python: 3.5 - env: TOX_ENV=py35-django111-es60 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt python: 3.5 -# TBD support - will be implemented in #5 -# allow_failures: -# - env: TOX_ENV=py27-django19-es62 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt -# python: 2.7 -# - env: TOX_ENV=py36-django19-es60 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt -# python: 3.6 -# - env: TOX_ENV=py36-django18-es61 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt -# python: 3.6 + - env: TOX_ENV=py35-django20-es60 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt + python: 3.5 + - env: TOX_ENV=py35-django21-es60 ES_APT_URL=https://artifacts.elastic.co/packages/6.x/apt + python: 3.5 before_install: - pip install --upgrade pip diff --git a/django_elastic_migrations/migrations/0007_auto_20181111_1251.py b/django_elastic_migrations/migrations/0007_auto_20181111_1251.py new file mode 100644 index 0000000..25148ef --- /dev/null +++ b/django_elastic_migrations/migrations/0007_auto_20181111_1251.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.15 on 2018-11-11 12:51 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_elastic_migrations', '0006_auto_20180709_2101'), + ] + + operations = [ + migrations.AlterField( + model_name='index', + name='active_version', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='django_elastic_migrations.IndexVersion'), + ), + ] diff --git a/django_elastic_migrations/models.py b/django_elastic_migrations/models.py index 8a744b7..e105839 100644 --- a/django_elastic_migrations/models.py +++ b/django_elastic_migrations/models.py @@ -36,7 +36,7 @@ class Index(models.Model): # See https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.ForeignKey.related_name active_version = models.ForeignKey( 'django_elastic_migrations.IndexVersion', - related_name="+", null=True) + related_name="+", null=True, on_delete=models.SET_NULL) def __str__(self): """ @@ -118,7 +118,7 @@ class IndexVersion(models.Model): a IndexVersion is added to the table, and a new Elasticsearch index is created with that schema. """ - index = models.ForeignKey(Index) + index = models.ForeignKey(Index, models.CASCADE) prefix = models.CharField(verbose_name="Environment Prefix", max_length=32, blank=True) # store the JSON sent to Elasticsearch to configure the index # note: the index name field in this field does NOT include the IndexVersion id @@ -229,11 +229,11 @@ class IndexAction(models.Model): DEFAULT_ACTION = ACTION_CREATE_INDEX # linked models - index = models.ForeignKey(Index) - index_version = models.ForeignKey(IndexVersion, null=True) + index = models.ForeignKey(Index, on_delete=models.CASCADE) + index_version = models.ForeignKey(IndexVersion, null=True, on_delete=models.CASCADE) # if this IndexAction has a parent IndexAction, its id is here - parent = models.ForeignKey("self", null=True, related_name="children") + parent = models.ForeignKey("self", null=True, related_name="children", on_delete=models.CASCADE) # which management command was run action = models.CharField(choices=ACTIONS_ALL_CHOICES, max_length=64) diff --git a/django_elastic_migrations/utils/es_utils.py b/django_elastic_migrations/utils/es_utils.py index 055136a..2c97166 100644 --- a/django_elastic_migrations/utils/es_utils.py +++ b/django_elastic_migrations/utils/es_utils.py @@ -4,7 +4,7 @@ def get_index_hash_and_json(index): spec = index.to_dict() - json_str = json.dumps(spec, sort_keys=True).encode('utf-8') + json_str = json.dumps(spec, sort_keys=True) md5_hash = hashlib.md5() - md5_hash.update(json_str) + md5_hash.update(json_str.encode('utf-8')) return md5_hash.hexdigest(), json_str diff --git a/django_elastic_migrations/utils/multiprocessing_utils.py b/django_elastic_migrations/utils/multiprocessing_utils.py index ae4c47e..5f3dde0 100644 --- a/django_elastic_migrations/utils/multiprocessing_utils.py +++ b/django_elastic_migrations/utils/multiprocessing_utils.py @@ -1,10 +1,9 @@ # coding=utf-8 - +# noinspection PyCompatibility +import queue import time import traceback from multiprocessing import Process, cpu_count, Manager -# noinspection PyCompatibility -from queue import Empty from django import db from django.conf import settings @@ -236,7 +235,7 @@ def results(self): try: while True: rv.append(self.queue.get(block=False)) - except Empty: + except queue.Empty: return rv def __exit__(self, type, value, traceback): diff --git a/tox.ini b/tox.ini index 1b973a4..ccad7a8 100644 --- a/tox.ini +++ b/tox.ini @@ -1,8 +1,6 @@ [tox] envlist = - {py35}-{django110,django111}-{es61} - ; not implemented yet - DocType Changes in es62 pending - see github issue #3 Support elasticsearch-dsl 6.2 - ; {py35}-django19-{es62} + {py35}-{django110,django111,django20,django21}-{es61} [doc8] max-line-length = 120 From 86d5729c58a3bc61acfdb519dbd20caafe56a5dd Mon Sep 17 00:00:00 2001 From: "P. Myer Nore" Date: Tue, 13 Nov 2018 10:59:10 -0500 Subject: [PATCH 2/2] resolve migration conflict #52 bump 0.7.7 to 0.7.8 and release on PyPI --- .../migrations/0007_auto_20181111_1251.py | 21 ------------------- ...112_0937.py => 0007_auto_20181113_0958.py} | 10 ++++++--- 2 files changed, 7 insertions(+), 24 deletions(-) delete mode 100644 django_elastic_migrations/migrations/0007_auto_20181111_1251.py rename django_elastic_migrations/migrations/{0007_auto_20181112_0937.py => 0007_auto_20181113_0958.py} (66%) diff --git a/django_elastic_migrations/migrations/0007_auto_20181111_1251.py b/django_elastic_migrations/migrations/0007_auto_20181111_1251.py deleted file mode 100644 index 25148ef..0000000 --- a/django_elastic_migrations/migrations/0007_auto_20181111_1251.py +++ /dev/null @@ -1,21 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.11.15 on 2018-11-11 12:51 -from __future__ import unicode_literals - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('django_elastic_migrations', '0006_auto_20180709_2101'), - ] - - operations = [ - migrations.AlterField( - model_name='index', - name='active_version', - field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='django_elastic_migrations.IndexVersion'), - ), - ] diff --git a/django_elastic_migrations/migrations/0007_auto_20181112_0937.py b/django_elastic_migrations/migrations/0007_auto_20181113_0958.py similarity index 66% rename from django_elastic_migrations/migrations/0007_auto_20181112_0937.py rename to django_elastic_migrations/migrations/0007_auto_20181113_0958.py index 17bf831..737e6bf 100644 --- a/django_elastic_migrations/migrations/0007_auto_20181112_0937.py +++ b/django_elastic_migrations/migrations/0007_auto_20181113_0958.py @@ -1,8 +1,7 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.11 on 2018-11-12 09:37 -from __future__ import unicode_literals +# Generated by Django 2.1.3 on 2018-11-13 09:58 from django.db import migrations, models +import django.db.models.deletion class Migration(migrations.Migration): @@ -15,6 +14,11 @@ class Migration(migrations.Migration): migrations.DeleteModel( name='DeactivateIndexAction', ), + migrations.AlterField( + model_name='index', + name='active_version', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='django_elastic_migrations.IndexVersion'), + ), migrations.AlterField( model_name='indexaction', name='action',