Debian Codegraph @

django/core/management/commands/inspectdb.py

raw
1 import keyword
2 import re
3
4 from django.core.management.base import BaseCommand, CommandError
5 from django.db import DEFAULT_DB_ALIAS, connections
6 from django.db.models.constants import LOOKUP_SEP
7
8
9 class Command(BaseCommand):
10 help = (
11 "Introspects the database tables in the given database and outputs a Django "
12 "model module."
13 )
14 requires_system_checks = []
15 stealth_options = ("table_name_filter",)
16 db_module = "django.db"
17
18 def add_arguments(self, parser):
19 parser.add_argument(
20 "table",
21 nargs="*",
22 type=str,
23 help="Selects what tables or views should be introspected.",
24 )
25 parser.add_argument(
26 "--database",
27 default=DEFAULT_DB_ALIAS,
28 choices=tuple(connections),
29 help=(
30 'Nominates a database to introspect. Defaults to using the "default" '
31 "database."
32 ),
33 )
34 parser.add_argument(
35 "--include-partitions",
36 action="store_true",
37 help="Also output models for partition tables.",
38 )
39 parser.add_argument(
40 "--include-views",
41 action="store_true",
42 help="Also output models for database views.",
43 )
44
45 def handle(self, **options):
46 try:
47 for line in self.handle_inspection(options):
48 self.stdout.write(line)
49 except NotImplementedError:
50 raise CommandError(
51 "Database inspection isn't supported for the currently selected "
52 "database backend."
53 )
54
55 def handle_inspection(self, options):
56 connection = connections[options["database"]]
57 # 'table_name_filter' is a stealth option
58 table_name_filter = options.get("table_name_filter")
59
60 with connection.cursor() as cursor:
61 yield "# This is an auto-generated Django model module."
62 yield "# You'll have to do the following manually to clean this up:"
63 yield "# * Rearrange models' order"
64 yield "# * Make sure each model has one field with primary_key=True"
65 yield (
66 "# * Make sure each ForeignKey and OneToOneField has `on_delete` set "
67 "to the desired behavior"
68 )
69 yield (
70 "# * Remove `managed = False` lines if you wish to allow "
71 "Django to create, modify, and delete the table"
72 )
73 yield (
74 "# Feel free to rename the models, but don't rename db_table values or "
75 "field names."
76 )
77 yield "from %s import models" % self.db_module
78 known_models = []
79 # Determine types of tables and/or views to be introspected.
80 types = {"t"}
81 if options["include_partitions"]:
82 types.add("p")
83 if options["include_views"]:
84 types.add("v")
85 table_info = connection.introspection.get_table_list(cursor)
86 table_info = {info.name: info for info in table_info if info.type in types}
87
88 for table_name in options["table"] or sorted(name for name in table_info):
89 if table_name_filter is not None and callable(table_name_filter):
90 if not table_name_filter(table_name):
91 continue
92 try:
93 try:
94 relations = connection.introspection.get_relations(
95 cursor, table_name
96 )
97 except NotImplementedError:
98 relations = {}
99 try:
100 constraints = connection.introspection.get_constraints(
101 cursor, table_name
102 )
103 except NotImplementedError:
104 constraints = {}
105 primary_key_columns = (
106 connection.introspection.get_primary_key_columns(
107 cursor, table_name
108 )
109 or []
110 )
111 primary_key_column = (
112 primary_key_columns[0]
113 if len(primary_key_columns) == 1
114 else None
115 )
116 unique_columns = [
117 c["columns"][0]
118 for c in constraints.values()
119 if c["unique"] and len(c["columns"]) == 1
120 ]
121 table_description = connection.introspection.get_table_description(
122 cursor, table_name
123 )
124 except Exception as e:
125 yield "# Unable to inspect table '%s'" % table_name
126 yield "# The error was: %s" % e
127 continue
128
129 model_name = self.normalize_table_name(table_name)
130 yield ""
131 yield ""
132 yield "class %s(models.Model):" % model_name
133 known_models.append(model_name)
134
135 if len(primary_key_columns) > 1:
136 fields = ", ".join([f"'{col}'" for col in primary_key_columns])
137 yield f" pk = models.CompositePrimaryKey({fields})"
138
139 used_column_names = [] # Holds column names used in the table so far
140 column_to_field_name = {} # Maps column names to names of model fields
141 used_relations = set() # Holds foreign relations used in the table.
142 for row in table_description:
143 comment_notes = (
144 []
145 ) # Holds Field notes, to be displayed in a Python comment.
146 extra_params = {} # Holds Field parameters such as 'db_column'.
147 column_name = row.name
148 is_relation = column_name in relations
149
150 att_name, params, notes = self.normalize_col_name(
151 column_name, used_column_names, is_relation
152 )
153 extra_params.update(params)
154 comment_notes.extend(notes)
155
156 used_column_names.append(att_name)
157 column_to_field_name[column_name] = att_name
158
159 # Add primary_key and unique, if necessary.
160 if column_name == primary_key_column:
161 extra_params["primary_key"] = True
162 elif column_name in unique_columns:
163 extra_params["unique"] = True
164
165 if is_relation:
166 ref_db_column, ref_db_table = relations[column_name]
167 if extra_params.pop("unique", False) or extra_params.get(
168 "primary_key"
169 ):
170 rel_type = "OneToOneField"
171 else:
172 rel_type = "ForeignKey"
173 ref_pk_column = (
174 connection.introspection.get_primary_key_column(
175 cursor, ref_db_table
176 )
177 )
178 if ref_pk_column and ref_pk_column != ref_db_column:
179 extra_params["to_field"] = ref_db_column
180 rel_to = (
181 "self"
182 if ref_db_table == table_name
183 else self.normalize_table_name(ref_db_table)
184 )
185 if rel_to in known_models:
186 field_type = "%s(%s" % (rel_type, rel_to)
187 else:
188 field_type = "%s('%s'" % (rel_type, rel_to)
189 if rel_to in used_relations:
190 extra_params["related_name"] = "%s_%s_set" % (
191 model_name.lower(),
192 att_name,
193 )
194 used_relations.add(rel_to)
195 else:
196 # Calling `get_field_type` to get the field type string and any
197 # additional parameters and notes.
198 field_type, field_params, field_notes = self.get_field_type(
199 connection, table_name, row
200 )
201 extra_params.update(field_params)
202 comment_notes.extend(field_notes)
203
204 field_type += "("
205
206 # Don't output 'id = meta.AutoField(primary_key=True)', because
207 # that's assumed if it doesn't exist.
208 if att_name == "id" and extra_params == {"primary_key": True}:
209 if field_type == "AutoField(":
210 continue
211 elif (
212 field_type
213 == connection.features.introspected_field_types["AutoField"]
214 + "("
215 ):
216 comment_notes.append("AutoField?")
217
218 # Add 'null' and 'blank', if the 'null_ok' flag was present in the
219 # table description.
220 if row.null_ok: # If it's NULL...
221 extra_params["blank"] = True
222 extra_params["null"] = True
223
224 field_desc = "%s = %s%s" % (
225 att_name,
226 # Custom fields will have a dotted path
227 "" if "." in field_type else "models.",
228 field_type,
229 )
230 if field_type.startswith(("ForeignKey(", "OneToOneField(")):
231 field_desc += ", models.DO_NOTHING"
232
233 # Add comment.
234 if connection.features.supports_comments and row.comment:
235 extra_params["db_comment"] = row.comment
236
237 if extra_params:
238 if not field_desc.endswith("("):
239 field_desc += ", "
240 field_desc += ", ".join(
241 "%s=%r" % (k, v) for k, v in extra_params.items()
242 )
243 field_desc += ")"
244 if comment_notes:
245 field_desc += " # " + " ".join(comment_notes)
246 yield " %s" % field_desc
247 comment = None
248 if info := table_info.get(table_name):
249 is_view = info.type == "v"
250 is_partition = info.type == "p"
251 if connection.features.supports_comments:
252 comment = info.comment
253 else:
254 is_view = False
255 is_partition = False
256 yield from self.get_meta(
257 table_name,
258 constraints,
259 column_to_field_name,
260 is_view,
261 is_partition,
262 comment,
263 )
264
265 def normalize_col_name(self, col_name, used_column_names, is_relation):
266 """
267 Modify the column name to make it Python-compatible as a field name
268 """
269 field_params = {}
270 field_notes = []
271
272 new_name = col_name.lower()
273 if new_name != col_name:
274 field_notes.append("Field name made lowercase.")
275
276 if is_relation:
277 if new_name.endswith("_id"):
278 new_name = new_name.removesuffix("_id")
279 else:
280 field_params["db_column"] = col_name
281
282 new_name, num_repl = re.subn(r"\W", "_", new_name)
283 if num_repl > 0:
284 field_notes.append("Field renamed to remove unsuitable characters.")
285
286 if new_name.find(LOOKUP_SEP) >= 0:
287 while new_name.find(LOOKUP_SEP) >= 0:
288 new_name = new_name.replace(LOOKUP_SEP, "_")
289 if col_name.lower().find(LOOKUP_SEP) >= 0:
290 # Only add the comment if the double underscore was in the original name
291 field_notes.append(
292 "Field renamed because it contained more than one '_' in a row."
293 )
294
295 if new_name.startswith("_"):
296 new_name = "field%s" % new_name
297 field_notes.append("Field renamed because it started with '_'.")
298
299 if new_name.endswith("_"):
300 new_name = "%sfield" % new_name
301 field_notes.append("Field renamed because it ended with '_'.")
302
303 if keyword.iskeyword(new_name):
304 new_name += "_field"
305 field_notes.append("Field renamed because it was a Python reserved word.")
306
307 if new_name[0].isdigit():
308 new_name = "number_%s" % new_name
309 field_notes.append(
310 "Field renamed because it wasn't a valid Python identifier."
311 )
312
313 if new_name in used_column_names:
314 num = 0
315 while "%s_%d" % (new_name, num) in used_column_names:
316 num += 1
317 new_name = "%s_%d" % (new_name, num)
318 field_notes.append("Field renamed because of name conflict.")
319
320 if col_name != new_name and field_notes:
321 field_params["db_column"] = col_name
322
323 return new_name, field_params, field_notes
324
325 def normalize_table_name(self, table_name):
326 """Translate the table name to a Python-compatible model name."""
327 return re.sub(r"[^a-zA-Z0-9]", "", table_name.title())
328
329 def get_field_type(self, connection, table_name, row):
330 """
331 Given the database connection, the table name, and the cursor row
332 description, this routine will return the given field type name, as
333 well as any additional keyword parameters and notes for the field.
334 """
335 field_params = {}
336 field_notes = []
337
338 try:
339 field_type = connection.introspection.get_field_type(row.type_code, row)
340 except KeyError:
341 field_type = "TextField"
342 field_notes.append("This field type is a guess.")
343
344 # Add max_length for all CharFields.
345 if field_type == "CharField" and row.display_size:
346 if (size := int(row.display_size)) and size > 0:
347 field_params["max_length"] = size
348
349 if field_type in {"CharField", "TextField"} and row.collation:
350 field_params["db_collation"] = row.collation
351
352 if field_type == "DecimalField":
353 if row.precision is None or row.scale is None:
354 field_notes.append(
355 "max_digits and decimal_places have been guessed, as this "
356 "database handles decimal fields as float"
357 )
358 field_params["max_digits"] = (
359 row.precision if row.precision is not None else 10
360 )
361 field_params["decimal_places"] = (
362 row.scale if row.scale is not None else 5
363 )
364 else:
365 field_params["max_digits"] = row.precision
366 field_params["decimal_places"] = row.scale
367
368 return field_type, field_params, field_notes
369
370 def get_meta(
371 self,
372 table_name,
373 constraints,
374 column_to_field_name,
375 is_view,
376 is_partition,
377 comment,
378 ):
379 """
380 Return a sequence comprising the lines of code necessary
381 to construct the inner Meta class for the model corresponding
382 to the given database table name.
383 """
384 unique_together = []
385 has_unsupported_constraint = False
386 for params in constraints.values():
387 if params["unique"]:
388 columns = params["columns"]
389 if None in columns:
390 has_unsupported_constraint = True
391 columns = [
392 x for x in columns if x is not None and x in column_to_field_name
393 ]
394 if len(columns) > 1 and not params["primary_key"]:
395 unique_together.append(
396 str(tuple(column_to_field_name[c] for c in columns))
397 )
398 if is_view:
399 managed_comment = " # Created from a view. Don't remove."
400 elif is_partition:
401 managed_comment = " # Created from a partition. Don't remove."
402 else:
403 managed_comment = ""
404 meta = [""]
405 if has_unsupported_constraint:
406 meta.append(" # A unique constraint could not be introspected.")
407 meta += [
408 " class Meta:",
409 " managed = False%s" % managed_comment,
410 " db_table = %r" % table_name,
411 ]
412 if unique_together:
413 tup = "(" + ", ".join(unique_together) + ",)"
414 meta += [" unique_together = %s" % tup]
415 if comment:
416 meta += [f" db_table_comment = {comment!r}"]
417 return meta