Skip to content

Commit

Permalink
Merge pull request #93 from smzkw/fix-92-create_index_sql
Browse files Browse the repository at this point in the history
fixes #92: since django-3.0 sqlmigrate (and migrate) does not work
  • Loading branch information
shimizukawa authored Feb 14, 2022
2 parents a67bb59 + d1f9652 commit 0d1ac06
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 9 deletions.
8 changes: 1 addition & 7 deletions .github/workflows/test-examples-proj1.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
name: Test examples/proj1

on:
push:
paths:
- 'examples/proj1/*'
pull_request:
paths:
- 'examples/proj1/*'
on: [push, pull_request]

jobs:
build:
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ jobs:
- django-version: 'main'
python-version: '3.10'

services:
postgres:
image: postgres:9.6-alpine
env:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: testing
ports:
- 5439:5432

steps:
- uses: actions/checkout@v2

Expand Down Expand Up @@ -50,6 +60,7 @@ jobs:
tox -v
env:
DJANGO: ${{ matrix.django-version }}
TEST_WITH_POSTGRES: 1

- name: Upload coverage
uses: codecov/codecov-action@v1
Expand Down
14 changes: 12 additions & 2 deletions django_redshift_backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,22 @@ def _create_like_index_sql(self, model, field):
# Redshift doesn't support INDEX.
return None

def _create_index_sql(self, model, **kwargs):
# _create_index_sql only called from _create_like_index_sql
# on django/db/backends/postgresql/schema.py
raise NotSupportedError("Redshift doesn't support INDEX")

def alter_index_together(self, model, old_index_together, new_index_together):
# Redshift doesn't support INDEX.
return

def _create_index_sql(self, model, fields, suffix="", sql=None):
raise NotSupportedError("Redshift doesn't support INDEX")
def add_index(self, model, index, concurrently=False):
# Redshift doesn't support INDEX.
pass

def remove_index(self, model, index, concurrently=False):
# Redshift doesn't support INDEX.
pass

def create_model(self, model):
"""
Expand Down
6 changes: 6 additions & 0 deletions doc/dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ Just run tox::

tox have several sections for testing.

To test the database migration as well, start postgres and test it as follows::

$ cd tests
$ docker-compose up -d
$ TEST_WITH_POSTGRES=1 tox

CI (Continuous Integration)
----------------------------

Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ universal = 0
[flake8]
max-line-length=90
ignore = W504
exclude = tests/testapp/migrations
12 changes: 12 additions & 0 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.1'

services:
db:
image: postgres:9.6-alpine
restart: always
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: testing
ports:
- 5439:5432
27 changes: 27 additions & 0 deletions tests/test_redshift_backend.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# -*- coding: utf-8 -*-

import os
import unittest

import django
from django.db import connections
from django.db.utils import NotSupportedError
from django.core.management.color import no_style
import pytest


def norm_sql(sql):
Expand Down Expand Up @@ -153,3 +156,27 @@ def test_create_model(self):
def test_create_table_meta_keys(self):
from testapp.models import TestModelWithMetaKeys
self.check_model_creation(TestModelWithMetaKeys, expected_ddl_meta_keys)

@pytest.mark.skipif(not os.environ.get('TEST_WITH_POSTGRES'),
reason='to run, TEST_WITH_POSTGRES=1 tox')
def test_sqlmigrate(self):
from django.db import connection

if django.VERSION < (3, 0): # for dj22
from django.db.migrations.executor import MigrationExecutor
executor = MigrationExecutor(connection)
loader = executor.loader
collect_sql = executor.collect_sql
else:
from django.db.migrations.loader import MigrationLoader
loader = MigrationLoader(connection)
collect_sql = loader.collect_sql

app_label, migration_name = 'testapp', '0001'
migration = loader.get_migration_by_prefix(app_label, migration_name)
target = (app_label, migration.name)

plan = [(loader.graph.nodes[target], False)]
sql_statements = collect_sql(plan)
print('\n'.join(sql_statements))
assert sql_statements # It doesn't matter what SQL is generated.
63 changes: 63 additions & 0 deletions tests/testapp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Generated by Django 3.2.12 on 2022-02-13 21:03

from django.db import migrations, models
import django.db.models.deletion
import django_redshift_backend.distkey


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='TestModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('ctime', models.DateTimeField()),
('text', models.TextField()),
('uuid', models.UUIDField()),
],
),
migrations.CreateModel(
name='TestParentModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('age', models.IntegerField()),
],
),
migrations.CreateModel(
name='TestReferencedModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.CreateModel(
name='TestModelWithMetaKeys',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('age', models.IntegerField()),
('created_at', models.DateTimeField()),
('fk', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='testapp.testreferencedmodel')),
],
options={
'ordering': ['created_at', '-id'],
},
),
migrations.CreateModel(
name='TestChildModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('age', models.IntegerField()),
('parent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='testapp.testparentmodel')),
],
),
migrations.AddIndex(
model_name='testmodelwithmetakeys',
index=django_redshift_backend.distkey.DistKey(fields=['fk'], name='testapp_tes_fk_id_cd99f5_idx'),
),
]
Empty file.
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ deps =
setenv =
DJANGO_SETTINGS_MODULE = settings
PYTHONPATH = {toxinidir}
TEST_WITH_POSTGRES = {env:TEST_WITH_POSTGRES:}
pip_pre = True
commands =
pytest -v --cov django_redshift_backend --cov-append --cov-report term-missing --cov-report=xml {posargs}
Expand Down

0 comments on commit 0d1ac06

Please sign in to comment.