mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-10 23:32:27 -05:00
reformat
This commit is contained in:
@@ -6,40 +6,48 @@ import sys
|
||||
|
||||
def get_latest_db_migration_to_apply():
|
||||
project_dir = dirname(dirname(abspath(__file__))) # Get the main project directory
|
||||
migrations_dir = '{}/migrations/versions/'.format(project_dir)
|
||||
migration_files = [migration_file for migration_file in os.listdir(migrations_dir) if migration_file.endswith('py')]
|
||||
migrations_dir = "{}/migrations/versions/".format(project_dir)
|
||||
migration_files = [
|
||||
migration_file
|
||||
for migration_file in os.listdir(migrations_dir)
|
||||
if migration_file.endswith("py")
|
||||
]
|
||||
# sometimes there's a trailing underscore, if script was created with `flask db migrate --rev-id=...`
|
||||
latest_file = sorted(migration_files, reverse=True)[0].replace('_.py', '').replace('.py', '')
|
||||
latest_file = (
|
||||
sorted(migration_files, reverse=True)[0].replace("_.py", "").replace(".py", "")
|
||||
)
|
||||
return latest_file
|
||||
|
||||
|
||||
def get_current_db_version():
|
||||
api_status_url = '{}/_status'.format(os.getenv('API_HOST_NAME'))
|
||||
api_status_url = "{}/_status".format(os.getenv("API_HOST_NAME"))
|
||||
|
||||
try:
|
||||
response = requests.get(api_status_url)
|
||||
response.raise_for_status()
|
||||
current_db_version = response.json()['db_version']
|
||||
current_db_version = response.json()["db_version"]
|
||||
return current_db_version
|
||||
except requests.exceptions.ConnectionError:
|
||||
print(f'Could not make web request to {api_status_url}', file=sys.stderr)
|
||||
return ''
|
||||
except Exception: # we expect these to be either either a http status code error, or a json decoding error
|
||||
print(f"Could not make web request to {api_status_url}", file=sys.stderr)
|
||||
return ""
|
||||
except (
|
||||
Exception
|
||||
): # we expect these to be either either a http status code error, or a json decoding error
|
||||
print(
|
||||
f'Could not read status endpoint!\n\ncode {response.status_code}\nresponse "{response.text}"',
|
||||
file=sys.stderr
|
||||
file=sys.stderr,
|
||||
)
|
||||
# if we can't make a request to the API, the API is probably down. By returning a blank string (which won't
|
||||
# match the filename of the latest migration), we force the migration to run, as the code change to fix the api
|
||||
# might involve a migration file.
|
||||
return ''
|
||||
return ""
|
||||
|
||||
|
||||
def run():
|
||||
if get_current_db_version() == get_latest_db_migration_to_apply():
|
||||
print('no')
|
||||
print("no")
|
||||
else:
|
||||
print('yes')
|
||||
print("yes")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -6,7 +6,7 @@ import sys
|
||||
|
||||
from alembic.script import ScriptDirectory
|
||||
|
||||
sys.path.append('.')
|
||||
sys.path.append(".")
|
||||
|
||||
|
||||
def get_branch_points(migrations):
|
||||
@@ -14,8 +14,10 @@ def get_branch_points(migrations):
|
||||
|
||||
|
||||
def get_branches(migrations, branch_point, heads):
|
||||
return [list(migrations.iterate_revisions(m, branch_point.revision))[::-1]
|
||||
for m in heads]
|
||||
return [
|
||||
list(migrations.iterate_revisions(m, branch_point.revision))[::-1]
|
||||
for m in heads
|
||||
]
|
||||
|
||||
|
||||
def choice(prompt, options, option_fmt=lambda x: x):
|
||||
@@ -42,15 +44,19 @@ def reorder_revisions(revisions, old_base, new_base):
|
||||
new_revision_id = rename_revision(head.revision, new_base)
|
||||
|
||||
print("Moving {} to {}".format(head.revision, new_revision_id))
|
||||
with open(head.path, 'r') as rev_file:
|
||||
with open(head.path, "r") as rev_file:
|
||||
file_data = rev_file.read()
|
||||
|
||||
file_data = file_data.replace(head.revision, new_revision_id).replace(old_base, new_base)
|
||||
file_data = file_data.replace(head.revision, new_revision_id).replace(
|
||||
old_base, new_base
|
||||
)
|
||||
new_filename = head.path.replace(head.revision, new_revision_id)
|
||||
|
||||
assert head.path != new_filename, 'Old filename not same as revision id, please rename file before continuing'
|
||||
assert (
|
||||
head.path != new_filename
|
||||
), "Old filename not same as revision id, please rename file before continuing"
|
||||
|
||||
with open(new_filename, 'w') as rev_file:
|
||||
with open(new_filename, "w") as rev_file:
|
||||
rev_file.write(file_data)
|
||||
|
||||
print("Removing {}".format(head.path))
|
||||
@@ -63,8 +69,11 @@ def fix_branch_point(migrations, branch_point, heads):
|
||||
print("Migrations directory has a branch point at {}".format(branch_point.revision))
|
||||
|
||||
branches = get_branches(migrations, branch_point, heads)
|
||||
move_branch = choice("Select migrations to move", branches,
|
||||
lambda x: " -> ".join(m.revision for m in x))
|
||||
move_branch = choice(
|
||||
"Select migrations to move",
|
||||
branches,
|
||||
lambda x: " -> ".join(m.revision for m in x),
|
||||
)
|
||||
branches.remove(move_branch)
|
||||
|
||||
reorder_revisions(move_branch, branch_point.revision, branches[0][-1].revision)
|
||||
@@ -81,10 +90,13 @@ def main(migrations_path):
|
||||
elif len(branch_points) == 1 and len(heads) == 2:
|
||||
fix_branch_point(migrations, branch_points[0], heads)
|
||||
else:
|
||||
print("Found {} branch points and {} heads, can't fix automatically".format(
|
||||
[bp.revision for bp in branch_points], heads))
|
||||
print(
|
||||
"Found {} branch points and {} heads, can't fix automatically".format(
|
||||
[bp.revision for bp in branch_points], heads
|
||||
)
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main('migrations/')
|
||||
if __name__ == "__main__":
|
||||
main("migrations/")
|
||||
|
||||
Reference in New Issue
Block a user