|
4 | 4 | from django.db import DatabaseError |
5 | 5 | import pyodbc as Database |
6 | 6 |
|
| 7 | +from collections import namedtuple |
| 8 | + |
7 | 9 | from django import VERSION |
8 | | -from django.db.backends.base.introspection import ( |
9 | | - BaseDatabaseIntrospection, FieldInfo, TableInfo, |
10 | | -) |
| 10 | +from django.db.backends.base.introspection import BaseDatabaseIntrospection |
| 11 | +from django.db.backends.base.introspection import FieldInfo |
| 12 | +from django.db.backends.base.introspection import TableInfo as BaseTableInfo |
11 | 13 | from django.db.models.indexes import Index |
12 | 14 | from django.conf import settings |
13 | 15 |
|
14 | 16 | SQL_AUTOFIELD = -777555 |
15 | 17 | SQL_BIGAUTOFIELD = -777444 |
16 | 18 | SQL_TIMESTAMP_WITH_TIMEZONE = -155 |
17 | 19 |
|
| 20 | +TableInfo = namedtuple("TableInfo", BaseTableInfo._fields + ("comment",)) |
18 | 21 |
|
19 | 22 | def get_schema_name(): |
20 | 23 | return getattr(settings, 'SCHEMA_TO_INSPECT', 'SCHEMA_NAME()') |
@@ -71,13 +74,28 @@ def get_table_list(self, cursor): |
71 | 74 | """ |
72 | 75 | Returns a list of table and view names in the current database. |
73 | 76 | """ |
74 | | - sql = 'SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = %s' % ( |
| 77 | + sql = """SELECT |
| 78 | + TABLE_NAME, |
| 79 | + TABLE_TYPE, |
| 80 | + CAST(ep.value AS VARCHAR) AS COMMENT |
| 81 | + FROM INFORMATION_SCHEMA.TABLES i |
| 82 | + INNER JOIN sys.tables t ON t.name = i.TABLE_NAME |
| 83 | + LEFT JOIN sys.extended_properties ep ON t.object_id = ep.major_id |
| 84 | + WHERE |
| 85 | + ((ep.name = 'MS_DESCRIPTION' AND ep.minor_id = 0) OR ep.value IS NULL) |
| 86 | + AND |
| 87 | + i.TABLE_SCHEMA = %s""" % ( |
75 | 88 | get_schema_name()) |
76 | 89 | cursor.execute(sql) |
77 | 90 | types = {'BASE TABLE': 't', 'VIEW': 'v'} |
78 | | - return [TableInfo(row[0], types.get(row[1])) |
79 | | - for row in cursor.fetchall() |
80 | | - if row[0] not in self.ignored_tables] |
| 91 | + if VERSION >= (4, 2): |
| 92 | + return [TableInfo(row[0], types.get(row[1]), row[2]) |
| 93 | + for row in cursor.fetchall() |
| 94 | + if row[0] not in self.ignored_tables] |
| 95 | + else: |
| 96 | + return [BaseTableInfo(row[0], types.get(row[1])) |
| 97 | + for row in cursor.fetchall() |
| 98 | + if row[0] not in self.ignored_tables] |
81 | 99 |
|
82 | 100 | def _is_auto_field(self, cursor, table_name, column_name): |
83 | 101 | """ |
@@ -111,7 +129,7 @@ def get_table_description(self, cursor, table_name, identity_check=True): |
111 | 129 |
|
112 | 130 | if not columns: |
113 | 131 | raise DatabaseError(f"Table {table_name} does not exist.") |
114 | | - |
| 132 | + |
115 | 133 | items = [] |
116 | 134 | for column in columns: |
117 | 135 | if VERSION >= (3, 2): |
|
0 commit comments