Skip to content
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

#6 support django 2 #48

Merged
merged 5 commits into from
Nov 13, 2018
Merged
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
12 changes: 4 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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',
10 changes: 5 additions & 5 deletions django_elastic_migrations/models.py
Original file line number Diff line number Diff line change
@@ -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):
"""
@@ -110,7 +110,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
@@ -220,11 +220,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)
4 changes: 2 additions & 2 deletions django_elastic_migrations/utils/es_utils.py
Original file line number Diff line number Diff line change
@@ -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'))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In Django 2.0 release notes, they mention they "Removed support for bytestrings in some places". One of those places is saving text to a standard TextField, where starting in 2.0 if you save a bytestring it looks like b'my bytestring' in the database; you need to supply it a normal str to get the expected behavior.

At the same time, md5_hash requires a bytestring, so this moves the .encode('utf-8') to just that function, but returns the str that comes out of json.dumps.

return md5_hash.hexdigest(), json_str
7 changes: 3 additions & 4 deletions django_elastic_migrations/utils/multiprocessing_utils.py
Original file line number Diff line number Diff line change
@@ -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):
4 changes: 1 addition & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -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