def get_script(version):
"""
Generate the script to get the database from *version* (the result of
:func:`detect_version`) to the current version of the software. If
*version* is ``None``, this is simply the contents of the
:file:`sql/create_piwheels.sql` script. Otherwise, it is a concatenation of
various update scripts.
"""
if version is None:
return resource_string(__name__, 'sql/create_piwheels.sql').decode('utf-8')
# Build the list of upgradable versions from the scripts in the sql/
# directory
upgrades = {}
ver_regex = re.compile(r'update_piwheels_(?P<from>.*)_to_(?P<to>.*)\.sql$')
for filename in resource_listdir(__name__, 'sql'):
match = ver_regex.match(filename)
if match is not None:
upgrades[match.group('from')] = (match.group('to'), filename)
# Attempt to find a list of scripts which'll get us from the existing
# version to the desired one. NOTE: This is a stupid algorithm which won't
# attempt different branches or back-tracking so if you wind up with custom
# versions or downgrade scripts in the sql directory, things will probably
# break
this_version = version
output = []
try:
while this_version != __version__:
this_version, filename = upgrades[this_version]
output.append(resource_string(__name__, 'sql/' + filename))
except KeyError:
raise RuntimeError("Unable to find upgrade path from %s to %s" % (
version, __version__))
return ''.join(script.decode('utf-8') for script in output)
评论列表
文章目录