-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from bjester/postgres-drop-null
Fix Postgres issue not dropping null constraint
- Loading branch information
Showing
5 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.29 on 2022-01-13 18:07 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations | ||
|
||
|
||
def apply(apps, schema_editor): | ||
# sqlite does not allow ALTER COLUMN, but also isn't affected by this issue | ||
if "postgresql" in schema_editor.connection.vendor: | ||
schema_editor.execute("ALTER TABLE morango_transfersession ALTER COLUMN transfer_stage DROP NOT NULL") | ||
schema_editor.execute("ALTER TABLE morango_transfersession ALTER COLUMN transfer_stage_status DROP NOT NULL") | ||
|
||
|
||
def revert(apps, schema_editor): | ||
# sqlite does not allow ALTER COLUMN, but also isn't affected by this issue | ||
if "postgresql" in schema_editor.connection.vendor: | ||
schema_editor.execute("ALTER TABLE morango_transfersession ALTER COLUMN transfer_stage SET NOT NULL") | ||
schema_editor.execute("ALTER TABLE morango_transfersession ALTER COLUMN transfer_stage_status SET NOT NULL") | ||
|
||
|
||
class Migration(migrations.Migration): | ||
""" | ||
Applies nullable change made to 0018_auto_20210714_2216.py after it was released | ||
""" | ||
|
||
dependencies = [ | ||
("morango", "0019_auto_20220113_1807"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(apply, reverse_code=revert), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import uuid | ||
|
||
import pytest | ||
|
||
from django.conf import settings | ||
from django.db import connection | ||
from django.db.utils import IntegrityError | ||
from django.utils import timezone | ||
|
||
from .helpers import TestMigrations | ||
|
||
|
||
@pytest.mark.skipif(not settings.MORANGO_TEST_POSTGRESQL, reason="Only postgres") | ||
class MorangoNullableMigrationTest(TestMigrations): | ||
""" | ||
Test migration that applies nullable status to `transfer_stage` and `transfer_stage_status` | ||
""" | ||
|
||
app = "morango" | ||
migrate_from = "0018_auto_20210714_2216" | ||
migrate_to = "0020_postgres_fix_nullable" | ||
|
||
def setUpBeforeMigration(self, apps): | ||
# simulate as if 0018_auto_20210714_2216 hadn't applied Nullablity to the columns, | ||
# a change which we added after the migration might have run on other | ||
SyncSession = apps.get_model("morango", "SyncSession") | ||
|
||
with connection.cursor() as cursor: | ||
cursor.execute("ALTER TABLE morango_transfersession ALTER COLUMN transfer_stage SET NOT NULL") | ||
cursor.execute("ALTER TABLE morango_transfersession ALTER COLUMN transfer_stage_status SET NOT NULL") | ||
|
||
self.sync_session = SyncSession.objects.create( | ||
id=uuid.uuid4().hex, | ||
profile="facilitydata", | ||
last_activity_timestamp=timezone.now(), | ||
) | ||
|
||
def test_nullable(self): | ||
TransferSession = self.apps.get_model("morango", "TransferSession") | ||
|
||
try: | ||
transfer_session = TransferSession.objects.create( | ||
id=uuid.uuid4().hex, | ||
sync_session_id=self.sync_session.id, | ||
push=True, | ||
last_activity_timestamp=timezone.now(), | ||
transfer_stage=None, | ||
transfer_stage_status=None, | ||
) | ||
except IntegrityError: | ||
self.fail("Couldn't create TransferSession with nullable fields") | ||
|
||
self.assertIsNone(transfer_session.transfer_stage) | ||
self.assertIsNone(transfer_session.transfer_stage_status) |