@@ -93,6 +93,31 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
9393 sql_rename_table = "EXEC sp_rename %(old_table)s, %(new_table)s"
9494 sql_create_unique_null = "CREATE UNIQUE INDEX %(name)s ON %(table)s(%(columns)s) " \
9595 "WHERE %(columns)s IS NOT NULL"
96+ sql_alter_table_comment = """IF NOT EXISTS (SELECT NULL FROM INFORMATION_SCHEMA.TABLES i
97+ INNER JOIN sys.tables t ON t.name = i.TABLE_NAME
98+ LEFT JOIN sys.extended_properties ep ON t.object_id = ep.major_id
99+ WHERE (ep.name = 'MS_Description' AND ep.minor_id = 0))
100+ EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = %(comment)s,
101+ @level0type = N'SCHEMA', @level0name = N'dbo',
102+ @level1type = N'TABLE', @level1name = %(table)s
103+ ELSE
104+ EXECUTE sp_updateextendedproperty @name = N'MS_Description', @value = %(comment)s,
105+ @level0type = N'SCHEMA', @level0name = N'dbo',
106+ @level1type = N'TABLE', @level1name = %(table)s;"""
107+
108+ sql_alter_column_comment = """IF NOT EXISTS (SELECT NULL FROM INFORMATION_SCHEMA.COLUMNS i
109+ INNER JOIN sys.columns t ON t.name = i.COLUMN_NAME
110+ LEFT JOIN sys.extended_properties ep ON t.object_id = ep.major_id
111+ WHERE (ep.name = N'MS_Description' AND ep.minor_id = column_id))
112+ EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = %(comment)s,
113+ @level0type = N'SCHEMA', @level0name = N'dbo',
114+ @level1type = N'TABLE', @level1name = %(table)s,
115+ @level2type = 'COLUMN', @level2name = %(column)s
116+ ELSE
117+ EXECUTE sp_updateextendedproperty @name = N'MS_Description', @value = %(comment)s,
118+ @level0type = N'SCHEMA', @level0name = N'dbo',
119+ @level1type = N'TABLE', @level1name = %(table)s,
120+ @level2type = 'COLUMN', @level2name = %(column)s; """
96121
97122 _deferred_unique_indexes = defaultdict (list )
98123
@@ -316,7 +341,11 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
316341
317342 # Drop any FK constraints, we'll remake them later
318343 fks_dropped = set ()
319- if old_field .remote_field and old_field .db_constraint :
344+ if old_field .remote_field and old_field .db_constraint and self ._field_should_be_altered (
345+ old_field ,
346+ new_field ,
347+ ignore = {"db_comment" },
348+ ):
320349 # Drop index, SQL Server requires explicit deletion
321350 if not hasattr (new_field , 'db_constraint' ) or not new_field .db_constraint :
322351 index_names = self ._constraint_names (model , [old_field .column ], index = True )
@@ -447,7 +476,10 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
447476 null_actions = []
448477 post_actions = []
449478 # Type change?
450- if old_type != new_type :
479+ if old_type != new_type or (
480+ self .connection .features .supports_comments
481+ and old_field .db_comment != new_field .db_comment
482+ ):
451483 if django_version >= (4 , 2 ):
452484 fragment , other_actions = self ._alter_column_type_sql (
453485 model , old_field , new_field , new_type , old_collation = None , new_collation = None
@@ -910,6 +942,18 @@ def add_field(self, model, field):
910942 "changes" : changes_sql ,
911943 }
912944 self .execute (sql , params )
945+ # Add field comment, if required.
946+ # if (
947+ # field.db_comment
948+ # and self.connection.features.supports_comments
949+ # and not self.connection.features.supports_comments_inline
950+ # ):
951+ # field_type = db_params["type"]
952+ # self.execute(
953+ # *self._alter_column_comment_sql(
954+ # model, field, field_type, field.db_comment
955+ # )
956+ # )
913957 # Add an index, if required
914958 self .deferred_sql .extend (self ._field_indexes_sql (model , field ))
915959 # Add any FK constraints later
@@ -1117,6 +1161,23 @@ def create_model(self, model):
11171161 # Prevent using [] as params, in the case a literal '%' is used in the definition
11181162 self .execute (sql , params or None )
11191163
1164+ if self .connection .features .supports_comments :
1165+ # Add table comment.
1166+ if model ._meta .db_table_comment :
1167+ self .alter_db_table_comment (model , None , model ._meta .db_table_comment )
1168+ # Add column comments.
1169+ # if not self.connection.features.supports_comments_inline:
1170+ # for field in model._meta.local_fields:
1171+ # if field.db_comment:
1172+ # field_db_params = field.db_parameters(
1173+ # connection=self.connection
1174+ # )
1175+ # field_type = field_db_params["type"]
1176+ # self.execute(
1177+ # *self._alter_column_comment_sql(
1178+ # model, field, field_type, field.db_comment
1179+ # )
1180+ # )
11201181 # Add any field index and index_together's (deferred as SQLite3 _remake_table needs it)
11211182 self .deferred_sql .extend (self ._model_indexes_sql (model ))
11221183 self .deferred_sql = list (set (self .deferred_sql ))
@@ -1291,3 +1352,6 @@ def _create_index_name(self, table_name, column_names, suffix=""):
12911352 new_index_name = index_name .replace ('[' , '' ).replace (']' , '' ).replace ('.' , '_' )
12921353 return new_index_name
12931354 return index_name
1355+
1356+ def _alter_column_comment_sql (self , model , new_field , new_type , new_db_comment ):
1357+ return "" , []
0 commit comments