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

Fixed #33671 -- Added support for oracle alter collation on unique/indexed column #18049

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions django/db/backends/base/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1968,6 +1968,7 @@ def _constraint_names(
return result

def _delete_primary_key(self, model, strict=False):
"""Delete primary key on a column"""
constraint_names = self._constraint_names(model, primary_key=True)
if strict and len(constraint_names) != 1:
raise ValueError(
Expand All @@ -1980,6 +1981,24 @@ def _delete_primary_key(self, model, strict=False):
for constraint_name in constraint_names:
self.execute(self._delete_primary_key_sql(model, constraint_name))

def _delete_unique_key(self, model, column_name):
"""Delete unique constraint on a column, if exists"""
constraint_names = self._constraint_names(
model, column_names=[column_name], unique=True
)

for constraint_name in constraint_names:
self.execute(self._delete_unique_sql(model, constraint_name))

def _delete_index(self, model, column_name):
"""Delete index on a column, if exists"""
constraint_names = self._constraint_names(
model, column_names=[column_name], index=True
)

for constraint_name in constraint_names:
self.execute(self._delete_index_sql(model, constraint_name))

def _create_primary_key_sql(self, model, field):
return Statement(
self.sql_create_pk,
Expand Down
25 changes: 23 additions & 2 deletions django/db/backends/oracle/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,33 @@ def alter_field(self, model, old_field, new_field, strict=False):
self._alter_field_type_workaround(model, old_field, new_field)
# If a collation is changing on a primary key, drop the primary key
# first.
elif "ORA-43923" in description and old_field.primary_key:
self._delete_primary_key(model, strict=True)
elif "ORA-43923" in description:
# If primary key exists, delete it.
if old_field.primary_key:
self._delete_primary_key(model, strict=True)

# If unique key exists, delete it.
if old_field.unique:
self._delete_unique_key(model, old_field.column)

# if indexed field, drop index.
if old_field.db_index:
self._delete_index(model, old_field.column)

# Alter the field collation.
self.alter_field(model, old_field, new_field, strict)

# Restore a primary key, if needed.
if new_field.primary_key:
self.execute(self._create_primary_key_sql(model, new_field))

# Restore unique constraint, if needed.
if new_field.unique:
self.execute(self._create_unique_sql(model, [new_field]))

# Restore index, if needed.
if new_field.db_index:
self.execute(self._create_index_sql(model, fields=[new_field]))
else:
raise

Expand Down
22 changes: 22 additions & 0 deletions tests/schema/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,28 @@ def __str__(self):
return self.when


class UniqueKey(models.Model):
when = models.CharField(max_length=1, unique=True)

class Meta:
apps = new_apps
db_table = "drop_unique"

def __str__(self):
return self.when


class IndexKey(models.Model):
when = models.CharField(max_length=1, db_index=True)

class Meta:
apps = new_apps
db_table = "drop_index"

def __str__(self):
return self.when


class UniqueTest(models.Model):
year = models.IntegerField()
slug = models.SlugField(unique=False)
Expand Down
44 changes: 44 additions & 0 deletions tests/schema/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
BookWithO2O,
BookWithoutAuthor,
BookWithSlug,
IndexKey,
IntegerPK,
Node,
Note,
Expand All @@ -94,6 +95,7 @@
TagM2MTest,
TagUniqueRename,
Thing,
UniqueKey,
UniqueTest,
new_apps,
)
Expand Down Expand Up @@ -5615,6 +5617,48 @@ def test_alter_primary_key_db_collation(self):
self.assertEqual(self.get_primary_key(Thing._meta.db_table), "when")
self.assertIsNone(self.get_column_collation(Thing._meta.db_table, "when"))

@skipUnlessDBFeature("supports_collation_on_charfield")
def test_alter_unique_key_db_collation(self):
collation = connection.features.test_collations.get("non_default")
if not collation:
self.skipTest("Language collations are not supported.")

with connection.schema_editor() as editor:
editor.create_model(UniqueKey)

old_field = UniqueKey._meta.get_field("when")
new_field = CharField(max_length=1, db_collation=collation, unique=True)
new_field.set_attributes_from_name("when")
new_field.model = UniqueKey
with connection.schema_editor() as editor:
editor.alter_field(UniqueKey, old_field, new_field, strict=True)
self.assertTrue("when" in self.get_uniques(UniqueKey._meta.db_table))
self.assertEqual(
self.get_column_collation(UniqueKey._meta.db_table, "when"),
collation,
)

@skipUnlessDBFeature("supports_collation_on_charfield")
def test_alter_index_key_db_collation(self):
collation = connection.features.test_collations.get("non_default")
if not collation:
self.skipTest("Language collations are not supported.")

with connection.schema_editor() as editor:
editor.create_model(IndexKey)

old_field = IndexKey._meta.get_field("when")
new_field = CharField(max_length=1, db_collation=collation, db_index=True)
new_field.set_attributes_from_name("when")
new_field.model = IndexKey
with connection.schema_editor() as editor:
editor.alter_field(IndexKey, old_field, new_field, strict=True)
self.assertTrue("when" in self.get_indexes(IndexKey._meta.db_table))
self.assertEqual(
self.get_column_collation(IndexKey._meta.db_table, "when"),
collation,
)

@skipUnlessDBFeature(
"supports_collation_on_charfield", "supports_collation_on_textfield"
)
Expand Down