28
28
# This is a map from type names (as returned by a 'describe'
29
29
# operation) to synonymous type names.
30
30
31
- type_map = {
32
- 'smallint(6)' : 'smallint' ,
31
+ type_map = {
32
+ 'smallint(6)' : 'smallint' ,
33
33
'mediumint(9)' : 'mediumint' ,
34
- 'tinyint(4)' : 'tinyint' ,
35
- 'int(11)' : 'int' ,
36
- 'bigint(20)' : 'bigint' ,
37
- }
34
+ 'tinyint(4)' : 'tinyint' ,
35
+ 'int(11)' : 'int' ,
36
+ 'bigint(20)' : 'bigint' ,
37
+ }
38
38
39
39
# Given output from a 'describe table' operation, return a map from
40
40
# column name to a map with the following entries:
41
- #
41
+ #
42
42
# 'Name': column name,
43
43
# 'Default': default value (or "None"),
44
44
# 'Type': type name,
48
48
# Because almost all columns are "NOT NULL", that is the default, and
49
49
# other columns are marked 'null' under 'Properties'.
50
50
51
+
51
52
def reduce_columns (table , description , errors ):
52
53
columns = {}
53
54
if table not in schema_remarks .column_remark :
@@ -73,42 +74,49 @@ def reduce_columns(table, description, errors):
73
74
# More recent versions of Bugzilla show defaults for numeric types as '',
74
75
# instead of (say) 0 or 0.00. This is not an actual schema change so we
75
76
# normalise the default values.
76
- if ( sqltype [- 3 :] == 'int' and default == '' ) :
77
+ if sqltype [- 3 :] == 'int' and default == '' :
77
78
default = '0'
78
- if ( sqltype == 'datetime' and default == '' ) :
79
+ if sqltype == 'datetime' and default == '' :
79
80
default = '0000-00-00 00:00:00'
80
- if (sqltype [:7 ] == 'decimal' and (default == '' or default == None or float (default ) == 0.0 )):
81
+ if sqltype [:7 ] == 'decimal' and (
82
+ default == '' or default == None or float (default ) == 0.0
83
+ ):
81
84
default = "0.0"
82
85
if default == '' :
83
86
default = "''"
84
87
if default is None :
85
88
default = 'None'
86
89
87
- if (table in schema_remarks .column_renamed and
88
- name in schema_remarks .column_renamed [table ]):
90
+ if (
91
+ table in schema_remarks .column_renamed
92
+ and name in schema_remarks .column_renamed [table ]
93
+ ):
89
94
canonical_name = schema_remarks .column_renamed [table ][name ]
90
95
else :
91
96
canonical_name = name
92
97
remark = None
93
98
if canonical_name not in schema_remarks .column_remark [table ]:
94
- errors .append ("Table '%s' has no remark for column '%s'." % (table , canonical_name ))
99
+ errors .append (
100
+ "Table '%s' has no remark for column '%s'." % (table , canonical_name )
101
+ )
95
102
else :
96
103
remark = schema_remarks .column_remark [table ][canonical_name ]
97
104
if remark is None :
98
- remarks = []
105
+ remarks = []
99
106
elif type (remark ) == list :
100
- remarks = remark
107
+ remarks = remark
101
108
else :
102
- remarks = [remark ]
109
+ remarks = [remark ]
103
110
columns [canonical_name ] = {
104
111
'Name' : name ,
105
112
'Default' : default ,
106
113
'Type' : sqltype ,
107
114
'Properties' : extra ,
108
115
'Remarks' : remarks ,
109
- }
116
+ }
110
117
return columns
111
118
119
+
112
120
# Given output from "show index", return a map from index name to a
113
121
# map with the following entries:
114
122
#
@@ -117,7 +125,8 @@ def reduce_columns(table, description, errors):
117
125
# 'Properties': A string with such properties as 'unique' and 'full text'
118
126
# 'Remarks': A list of remarks.
119
127
120
- foreign_key_index_re = re .compile ('^fk_.*' )
128
+ foreign_key_index_re = re .compile ('^fk_.*' )
129
+
121
130
122
131
def reduce_indexes (table , index_list , errors ):
123
132
indexes = {}
@@ -129,8 +138,10 @@ def reduce_indexes(table, index_list, errors):
129
138
if foreign_key_index_re .match (kn ):
130
139
# a foreign key constraint; not really an index
131
140
continue
132
- if (table in schema_remarks .index_renamed and
133
- kn in schema_remarks .index_renamed [table ]):
141
+ if (
142
+ table in schema_remarks .index_renamed
143
+ and kn in schema_remarks .index_renamed [table ]
144
+ ):
134
145
canon = schema_remarks .index_renamed [table ][kn ]
135
146
else :
136
147
canon = kn
@@ -145,30 +156,35 @@ def reduce_indexes(table, index_list, errors):
145
156
props = str .join (', ' , props )
146
157
remark = None
147
158
if canon not in schema_remarks .index_remark [table ]:
148
- errors .append ("Table '%s' has no remark for index '%s'." % (table , canon ))
159
+ errors .append (
160
+ "Table '%s' has no remark for index '%s'." % (table , canon )
161
+ )
149
162
else :
150
163
remark = schema_remarks .index_remark [table ][canon ]
151
164
if remark :
152
165
remarks = [remark ]
153
166
else :
154
167
remarks = []
155
- indexes [canon ] = {'Name' : kn ,
156
- 'Fields' : {i ['Seq_in_index' ]: i ['Column_name' ]},
157
- 'Properties' : props ,
158
- 'Remarks' : remarks ,
159
- }
168
+ indexes [canon ] = {
169
+ 'Name' : kn ,
170
+ 'Fields' : {i ['Seq_in_index' ]: i ['Column_name' ]},
171
+ 'Properties' : props ,
172
+ 'Remarks' : remarks ,
173
+ }
160
174
# replace the 'Fields' map with an ordered list.
161
175
for k in list (indexes .keys ()):
162
176
f = list (indexes [k ]['Fields' ].items ())
163
177
f .sort ()
164
178
indexes [k ]['Fields' ] = str .join (', ' , list (map ((lambda l : l [1 ]), f )))
165
179
return indexes
166
180
181
+
167
182
# Given a schema version name, get the schema for that database as a
168
183
# map from table name to (columns, indexes), where columns is a map
169
184
# produced by reduce_columns and indexes is a map produced by
170
185
# reduce_indexes.
171
186
187
+
172
188
def get_schema (schema_version , errors ):
173
189
f = None
174
190
schema = {}
@@ -177,23 +193,28 @@ def get_schema(schema_version, errors):
177
193
(sv , schema ) = pickle .load (f )
178
194
f .close ()
179
195
except FileNotFoundError :
180
- errors .append ("Unable to locate schema data file for version %s" % (schema_version ))
196
+ errors .append (
197
+ "Unable to locate schema data file for version %s" % (schema_version )
198
+ )
181
199
except Exception as e :
182
200
errors .append ("%s: %s" % (type (e ).__name__ , str (e )))
183
201
tables = list (schema .keys ())
184
202
for table in tables :
185
203
(columns , indexes ) = schema [table ]
186
- schema [table ] = (reduce_columns (table , columns , errors ),
187
- reduce_indexes (table , indexes , errors ))
204
+ schema [table ] = (
205
+ reduce_columns (table , columns , errors ),
206
+ reduce_indexes (table , indexes , errors ),
207
+ )
188
208
return schema , errors
189
209
210
+
190
211
# A. REFERENCES
191
212
#
192
213
#
193
214
# B. DOCUMENT HISTORY
194
215
#
195
216
# 2004-11-11 NB Created, partly from make_schema_doc.py.
196
- #
217
+ #
197
218
#
198
219
# C. COPYRIGHT AND LICENSE
199
220
#
0 commit comments