Skip to content

Commit

Permalink
reformat source with black for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
justdave committed Apr 30, 2024
1 parent 3657749 commit e374608
Show file tree
Hide file tree
Showing 7 changed files with 1,974 additions and 2,156 deletions.
81 changes: 51 additions & 30 deletions get_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@
# This is a map from type names (as returned by a 'describe'
# operation) to synonymous type names.

type_map={
'smallint(6)': 'smallint',
type_map = {
'smallint(6)': 'smallint',
'mediumint(9)': 'mediumint',
'tinyint(4)': 'tinyint',
'int(11)': 'int',
'bigint(20)': 'bigint',
}
'tinyint(4)': 'tinyint',
'int(11)': 'int',
'bigint(20)': 'bigint',
}

# Given output from a 'describe table' operation, return a map from
# column name to a map with the following entries:
#
#
# 'Name': column name,
# 'Default': default value (or "None"),
# 'Type': type name,
Expand All @@ -48,6 +48,7 @@
# Because almost all columns are "NOT NULL", that is the default, and
# other columns are marked 'null' under 'Properties'.


def reduce_columns(table, description, errors):
columns = {}
if table not in schema_remarks.column_remark:
Expand All @@ -73,42 +74,49 @@ def reduce_columns(table, description, errors):
# More recent versions of Bugzilla show defaults for numeric types as '',
# instead of (say) 0 or 0.00. This is not an actual schema change so we
# normalise the default values.
if (sqltype[-3:] == 'int' and default == ''):
if sqltype[-3:] == 'int' and default == '':
default = '0'
if (sqltype == 'datetime' and default == ''):
if sqltype == 'datetime' and default == '':
default = '0000-00-00 00:00:00'
if (sqltype[:7] == 'decimal' and (default == '' or default == None or float(default) == 0.0)):
if sqltype[:7] == 'decimal' and (
default == '' or default == None or float(default) == 0.0
):
default = "0.0"
if default == '':
default = "''"
if default is None:
default = 'None'

if (table in schema_remarks.column_renamed and
name in schema_remarks.column_renamed[table]):
if (
table in schema_remarks.column_renamed
and name in schema_remarks.column_renamed[table]
):
canonical_name = schema_remarks.column_renamed[table][name]
else:
canonical_name = name
remark = None
if canonical_name not in schema_remarks.column_remark[table]:
errors.append("Table '%s' has no remark for column '%s'." % (table, canonical_name))
errors.append(
"Table '%s' has no remark for column '%s'." % (table, canonical_name)
)
else:
remark = schema_remarks.column_remark[table][canonical_name]
if remark is None:
remarks=[]
remarks = []
elif type(remark) == list:
remarks=remark
remarks = remark
else:
remarks=[remark]
remarks = [remark]
columns[canonical_name] = {
'Name': name,
'Default': default,
'Type': sqltype,
'Properties': extra,
'Remarks': remarks,
}
}
return columns


# Given output from "show index", return a map from index name to a
# map with the following entries:
#
Expand All @@ -117,7 +125,8 @@ def reduce_columns(table, description, errors):
# 'Properties': A string with such properties as 'unique' and 'full text'
# 'Remarks': A list of remarks.

foreign_key_index_re=re.compile('^fk_.*')
foreign_key_index_re = re.compile('^fk_.*')


def reduce_indexes(table, index_list, errors):
indexes = {}
Expand All @@ -129,8 +138,10 @@ def reduce_indexes(table, index_list, errors):
if foreign_key_index_re.match(kn):
# a foreign key constraint; not really an index
continue
if (table in schema_remarks.index_renamed and
kn in schema_remarks.index_renamed[table]):
if (
table in schema_remarks.index_renamed
and kn in schema_remarks.index_renamed[table]
):
canon = schema_remarks.index_renamed[table][kn]
else:
canon = kn
Expand All @@ -145,30 +156,35 @@ def reduce_indexes(table, index_list, errors):
props = str.join(', ', props)
remark = None
if canon not in schema_remarks.index_remark[table]:
errors.append("Table '%s' has no remark for index '%s'." % (table, canon))
errors.append(
"Table '%s' has no remark for index '%s'." % (table, canon)
)
else:
remark = schema_remarks.index_remark[table][canon]
if remark:
remarks = [remark]
else:
remarks = []
indexes[canon] = {'Name': kn,
'Fields': {i['Seq_in_index']: i['Column_name']},
'Properties': props,
'Remarks': remarks,
}
indexes[canon] = {
'Name': kn,
'Fields': {i['Seq_in_index']: i['Column_name']},
'Properties': props,
'Remarks': remarks,
}
# replace the 'Fields' map with an ordered list.
for k in list(indexes.keys()):
f = list(indexes[k]['Fields'].items())
f.sort()
indexes[k]['Fields'] = str.join(', ', list(map((lambda l: l[1]), f)))
return indexes


# Given a schema version name, get the schema for that database as a
# map from table name to (columns, indexes), where columns is a map
# produced by reduce_columns and indexes is a map produced by
# reduce_indexes.


def get_schema(schema_version, errors):
f = None
schema = {}
Expand All @@ -177,23 +193,28 @@ def get_schema(schema_version, errors):
(sv, schema) = pickle.load(f)
f.close()
except FileNotFoundError:
errors.append("Unable to locate schema data file for version %s" % (schema_version))
errors.append(
"Unable to locate schema data file for version %s" % (schema_version)
)
except Exception as e:
errors.append("%s: %s" % (type(e).__name__, str(e)))
tables = list(schema.keys())
for table in tables:
(columns, indexes) = schema[table]
schema[table] = (reduce_columns(table, columns, errors),
reduce_indexes(table, indexes, errors))
schema[table] = (
reduce_columns(table, columns, errors),
reduce_indexes(table, indexes, errors),
)
return schema, errors


# A. REFERENCES
#
#
# B. DOCUMENT HISTORY
#
# 2004-11-11 NB Created, partly from make_schema_doc.py.
#
#
#
# C. COPYRIGHT AND LICENSE
#
Expand Down
5 changes: 3 additions & 2 deletions index.cgi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/python3
#
#
# Ravenbrook
# <http://www.ravenbrook.com/>
#
Expand All @@ -14,8 +14,9 @@
# rather than having it obscured by this CGI front-end.

from index import *

if __name__ == '__main__':
show_page()
show_page()

# A. REFERENCES
#
Expand Down
Loading

0 comments on commit e374608

Please sign in to comment.