Skip to content

Commit

Permalink
refs #134: fix test_alter_size_for_unique with redshift. Redshift can…
Browse files Browse the repository at this point in the history
… not accept ADD COLUMN with UNIQUE, so we neet adding column without UNIQUE then add UNIQUE CONSTRAINT.
  • Loading branch information
shimizukawa committed Jul 16, 2024
1 parent d205e40 commit 9d7dca9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
11 changes: 11 additions & 0 deletions django_redshift_backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ def add_field(self, model, field):
"column": self.quote_name(field.column),
"definition": definition,
}

# ## Redshift
if field.unique:
# temporarily remove UNIQUE constraint from sql
# because Redshift can't add column with UNIQUE constraint.
sql = sql.rstrip(" UNIQUE")

# ## Redshift
if not field.null and self.effective_default(field) is None:
# Redshift Can't add NOT NULL column without DEFAULT.
Expand All @@ -321,6 +328,10 @@ def add_field(self, model, field):
# ## original BasePGDatabaseSchemaEditor.add_field has CREATE INDEX.
# ## Redshift doesn't support INDEX.

# Add UNIQUE constraints later
if field.unique:
self.deferred_sql.append(self._create_unique_sql(model, [field]))

# Add any FK constraints later
if (
field.remote_field
Expand Down
33 changes: 30 additions & 3 deletions tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,32 @@ def test_alter_size(self):
'''ALTER TABLE "test_pony" ALTER COLUMN "name" TYPE varchar(10);''',
], sqls)

@postgres_fixture()
def test_add_unique_column(self):
new_state = self.set_up_test_model('test')
operations = [
migrations.AddField(
model_name='Pony',
name='name_with_default',
field=models.CharField(max_length=10, default='', unique=True),
),
migrations.AddField(
model_name='Pony',
name='name_with_nullable',
field=models.CharField(max_length=10, null=True, unique=True),
),
]

with self.collect_sql() as sqls:
self.apply_operations('test', new_state, operations)

self.assertEqual([
'''ALTER TABLE "test_pony" ADD COLUMN "name_with_default" varchar(10) DEFAULT '' NOT NULL;''',
'''ALTER TABLE "test_pony" ADD COLUMN "name_with_nullable" varchar(10) NULL;''',
'''ALTER TABLE "test_pony" ADD CONSTRAINT "test_pony_name_with_default_b3620670_uniq" UNIQUE ("name_with_default");''',
'''ALTER TABLE "test_pony" ADD CONSTRAINT "test_pony_name_with_nullable_d1043f78_uniq" UNIQUE ("name_with_nullable");''',
], sqls)

@postgres_fixture()
def test_alter_size_for_unique(self):
new_state = self.set_up_test_model('test')
Expand All @@ -86,10 +112,11 @@ def test_alter_size_for_unique(self):
self.apply_operations('test', new_state, operations)

self.assertEqual([
'''ALTER TABLE "test_pony" ADD COLUMN "name" varchar(10) DEFAULT '' NOT NULL UNIQUE;''',
'''ALTER TABLE "test_pony" DROP CONSTRAINT "test_pony_name_key";''',
'''ALTER TABLE "test_pony" ADD COLUMN "name" varchar(10) DEFAULT '' NOT NULL;''',
# ADD UNIQUE "name"
# DROP UNIQUE "name"
'''ALTER TABLE "test_pony" ALTER COLUMN "name" TYPE varchar(20);''',
'''ALTER TABLE "test_pony" ADD CONSTRAINT "test_pony_name_key" UNIQUE ("name");'''
'''ALTER TABLE "test_pony" ADD CONSTRAINT "test_pony_name_2c070d2a_uniq" UNIQUE ("name");'''
], sqls)

@postgres_fixture()
Expand Down

0 comments on commit 9d7dca9

Please sign in to comment.