Skip to content

Commit

Permalink
Added inline inline_comment_sql() and modified _column_sql().
Browse files Browse the repository at this point in the history
  • Loading branch information
felixxm committed Dec 27, 2022
1 parent 2f91b08 commit b1d33ee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
35 changes: 16 additions & 19 deletions django/db/backends/base/schema.py
Expand Up @@ -280,7 +280,7 @@ def table_sql(self, model):
if (
model._meta.db_table_comment
and self.connection.features.supports_comments_inline
and (comment_sql := self._comment_sql(model._meta.db_table_comment))
and (comment_sql := self.inline_comment_sql(model._meta.db_table_comment))
):
sql += comment_sql

Expand Down Expand Up @@ -444,15 +444,6 @@ def quote_value(self, value):
"""
raise NotImplementedError()

def quote_comment_value(self, value):
"""
Each database handles '', None differently.
Postgres: '', None -> NULL
Mysql: '', None -> ''
Oracle: '', None -> ''
"""
return self.quote_value(value or "")

# Actions

def create_model(self, model):
Expand All @@ -475,7 +466,7 @@ def create_model(self, model):
self.sql_alter_table_comment
% {
"table": self.quote_name(model._meta.db_table),
"comment": self.quote_comment_value(model._meta.db_table_comment),
"comment": self._comment_sql(model._meta.db_table_comment),
},
[],
)
Expand All @@ -492,7 +483,7 @@ def create_model(self, model):
% {
"table": self.quote_name(model._meta.db_table),
"column": self.quote_name(field.column),
"comment": self.quote_comment_value(field.db_comment),
"comment": self._comment_sql(field.db_comment),
},
[],
)
Expand Down Expand Up @@ -673,7 +664,7 @@ def alter_db_table_comment(self, model, old_db_table_comment, new_db_table_comme
self.sql_alter_table_comment
% {
"table": self.quote_name(model._meta.db_table),
"comment": self.quote_comment_value(new_db_table_comment),
"comment": self._comment_sql(new_db_table_comment),
}
)

Expand Down Expand Up @@ -767,7 +758,7 @@ def add_field(self, model, field):
% {
"table": self.quote_name(model._meta.db_table),
"column": self.quote_name(field.column),
"comment": self.quote_comment_value(field.db_comment),
"comment": self._comment_sql(field.db_comment),
},
[],
)
Expand Down Expand Up @@ -1250,9 +1241,8 @@ def _alter_column_null_sql(self, model, old_field, new_field):
if new_field.null
else self.sql_alter_column_not_null
)
if (
self.connection.features.supports_comments_inline
and (comment_sql := self._comment_sql(new_field.db_comment))
if self.connection.features.supports_comments_inline and (
comment_sql := self.inline_comment_sql(new_field.db_comment)
):
sql += comment_sql
return (
Expand Down Expand Up @@ -1340,14 +1330,21 @@ def _alter_column_comment_sql(self, model, new_field, new_type, new_db_comment):
% {
"table": self.quote_name(model._meta.db_table),
"column": self.quote_name(new_field.column),
"comment": self.quote_comment_value(new_db_comment),
"comment": self._comment_sql(new_db_comment),
},
[],
)
]

def _comment_sql(self, comment):
return ""
return self.quote_value(comment or "")

def inline_comment_sql(self, value):
"""Only used for backends which support inline comments."""
raise NotImplementedError(
"Subclasses of BaseDatabaseSchemaEditor for backends which support inline "
"comments must provide a inline_comment_sql() method."
)

def _alter_many_to_many(self, model, old_field, new_field, strict):
"""Alter M2Ms to repoint their to= endpoints."""
Expand Down
14 changes: 7 additions & 7 deletions django/db/backends/mysql/schema.py
Expand Up @@ -35,7 +35,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):

sql_alter_table_comment = "ALTER TABLE %(table)s COMMENT = %(comment)s"
sql_alter_column_comment = (
"ALTER TABLE %(table)s MODIFY %(column)s %(type)s COMMENT %(comment)s"
"ALTER TABLE %(table)s MODIFY %(column)s %(type)s%(comment)s"
)

@property
Expand All @@ -60,9 +60,8 @@ def sql_rename_column(self):

def column_sql(self, model, field, include_default=False):
sql, params = super().column_sql(model, field, include_default)

if field.db_comment and not field.many_to_many:
sql += self._comment_sql(field.db_comment)
sql += self.inline_comment_sql(field.db_comment)
return sql, params

def quote_value(self, value):
Expand Down Expand Up @@ -239,7 +238,7 @@ def _alter_column_type_sql(
"column": self.quote_name(new_field.column),
"type": new_type,
}
sql += self._comment_sql(new_field.db_comment)
sql += self.inline_comment_sql(new_field.db_comment)
return (sql, []), []

def _rename_field_sql(self, table, old_field, new_field, new_type):
Expand All @@ -254,11 +253,12 @@ def _alter_column_comment_sql(self, model, new_field, new_type, new_db_comment):
"table": self.quote_name(model._meta.db_table),
"column": self.quote_name(new_field.column),
"type": self._set_field_new_type_null_status(new_field, new_type),
"comment": self.quote_comment_value(new_db_comment),
"comment": self.inline_comment_sql(new_db_comment),
},
[],
)
]

def _comment_sql(self, comment):
return " COMMENT " + self.quote_comment_value(comment)
def inline_comment_sql(self, comment):
comment = self._comment_sql(comment)
return f" COMMENT {comment}"
11 changes: 10 additions & 1 deletion tests/backends/base/test_schema.py
@@ -1,4 +1,4 @@
from django.db import models
from django.db import connection, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.test import SimpleTestCase

Expand All @@ -17,3 +17,12 @@ def _get_default(self):

field = MyCharField(max_length=1, default=MyStr)
self.assertEqual(BaseDatabaseSchemaEditor._effective_default(field), MyStr)

def test_inline_comment_sql(self):
schema_editor = BaseDatabaseSchemaEditor(connection=connection)
msg = (
"Subclasses of BaseDatabaseSchemaEditor for backends which support inline "
"comments must provide a inline_comment_sql() method."
)
with self.assertRaisesMessage(NotImplementedError, msg):
schema_editor.inline_comment_sql(None)

0 comments on commit b1d33ee

Please sign in to comment.