summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2023-11-30 10:10:27 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-11-30 10:11:28 +0100
commit6e2d9f0aa895b34d5d9c06dddc33d06326e52012 (patch)
tree9db1e3cd4f2e07f8bf77f6530a448c608e3e25eb
parent5b698cbcf15b29c194b4a23d179959e17103645b (diff)
[4.2.x] Fixed #35006 -- Fixed migrations crash when altering Meta.db_table_comment on SQLite.
Thanks Юрий for the report. Regression in 78f163a4fb3937aca2e71786fbdd51a0ef39629e. Backport of 37fc832a54ad37e75a898a2c8f9ab0820617c4af from main
-rw-r--r--django/db/backends/base/schema.py15
-rw-r--r--django/db/backends/sqlite3/schema.py2
-rw-r--r--docs/releases/4.2.8.txt3
-rw-r--r--tests/schema/tests.py17
4 files changed, 30 insertions, 7 deletions
diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py
index 070211f6d2..1d44b935eb 100644
--- a/django/db/backends/base/schema.py
+++ b/django/db/backends/base/schema.py
@@ -637,13 +637,14 @@ class BaseDatabaseSchemaEditor:
sql.rename_table_references(old_db_table, new_db_table)
def alter_db_table_comment(self, model, old_db_table_comment, new_db_table_comment):
- self.execute(
- self.sql_alter_table_comment
- % {
- "table": self.quote_name(model._meta.db_table),
- "comment": self.quote_value(new_db_table_comment or ""),
- }
- )
+ if self.sql_alter_table_comment and self.connection.features.supports_comments:
+ self.execute(
+ self.sql_alter_table_comment
+ % {
+ "table": self.quote_name(model._meta.db_table),
+ "comment": self.quote_value(new_db_table_comment or ""),
+ }
+ )
def alter_db_tablespace(self, model, old_db_tablespace, new_db_tablespace):
"""Move a model's table between tablespaces."""
diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py
index 89867b6623..8c41f3c46d 100644
--- a/django/db/backends/sqlite3/schema.py
+++ b/django/db/backends/sqlite3/schema.py
@@ -20,6 +20,8 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
sql_delete_column = "ALTER TABLE %(table)s DROP COLUMN %(column)s"
sql_create_unique = "CREATE UNIQUE INDEX %(name)s ON %(table)s (%(columns)s)"
sql_delete_unique = "DROP INDEX %(name)s"
+ sql_alter_table_comment = None
+ sql_alter_column_comment = None
def __enter__(self):
# Some SQLite schema alterations need foreign key constraints to be
diff --git a/docs/releases/4.2.8.txt b/docs/releases/4.2.8.txt
index 0550e026bb..1cce9030ec 100644
--- a/docs/releases/4.2.8.txt
+++ b/docs/releases/4.2.8.txt
@@ -35,3 +35,6 @@ Bugfixes
* Fixed a regression in Django 4.2 where the admin's read-only password widget
and some help texts were incorrectly aligned at tablet widths
(:ticket:`34982`).
+
+* Fixed a regression in Django 4.2 that caused a migration crash on SQLite when
+ altering unsupported ``Meta.db_table_comment`` (:ticket:`35006`).
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index 9bb71de0be..ff8c284812 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -4728,6 +4728,23 @@ class SchemaTests(TransactionTestCase):
)
@isolate_apps("schema")
+ @skipIfDBFeature("supports_comments")
+ def test_db_comment_table_unsupported(self):
+ class ModelWithDbTableComment(Model):
+ class Meta:
+ app_label = "schema"
+ db_table_comment = "Custom table comment"
+
+ # Table comments are ignored on databases that don't support them.
+ with connection.schema_editor() as editor, self.assertNumQueries(1):
+ editor.create_model(ModelWithDbTableComment)
+ self.isolated_local_models = [ModelWithDbTableComment]
+ with connection.schema_editor() as editor, self.assertNumQueries(0):
+ editor.alter_db_table_comment(
+ ModelWithDbTableComment, "Custom table comment", "New table comment"
+ )
+
+ @isolate_apps("schema")
@skipUnlessDBFeature("supports_comments", "supports_foreign_keys")
def test_db_comments_from_abstract_model(self):
class AbstractModelWithDbComments(Model):