From da9e3c0e51dc31f7195f68ac371c0038708e4272 Mon Sep 17 00:00:00 2001 From: redshiftzero Date: Mon, 19 Aug 2019 19:05:09 -0400 Subject: [PATCH] alembic: guard against config.py not existing yet one of the quirks of SecureDrop is that config.py might not yet exist until the app Ansible role has ran this means for fresh installs, the database must be created via alembic upgrade head without any errors raising, else the securedrop-app-code package install will fail --- ...da3fcab826a_delete_orphaned_submissions.py | 99 +++++++++++-------- 1 file changed, 56 insertions(+), 43 deletions(-) diff --git a/securedrop/alembic/versions/3da3fcab826a_delete_orphaned_submissions.py b/securedrop/alembic/versions/3da3fcab826a_delete_orphaned_submissions.py index 897bdd2dc69..627751b4a64 100644 --- a/securedrop/alembic/versions/3da3fcab826a_delete_orphaned_submissions.py +++ b/securedrop/alembic/versions/3da3fcab826a_delete_orphaned_submissions.py @@ -7,15 +7,24 @@ Create Date: 2018-11-25 19:40:25.873292 """ +import os from alembic import op import sqlalchemy as sa from journalist_app import create_app -from models import Submission, Reply from rm import srm -from sdconfig import config from store import NoFileFoundException, TooManyFilesException from worker import rq_worker_queue +# raise the errors if we're not in production +raise_errors = os.environ.get("SECUREDROP_ENV", "prod") != "prod" + +try: + from sdconfig import config +except ImportError: + # This is a fresh install, and config.py has not been created yet. + if raise_errors: + raise + # revision identifiers, used by Alembic. revision = '3da3fcab826a' down_revision = 'a9fe328b053a' @@ -42,51 +51,55 @@ def upgrade(): sa.text(raw_sql_grab_orphaned_objects('replies')) ).fetchall() - app = create_app(config) - with app.app_context(): - for submission in submissions: - try: - conn.execute( - sa.text(""" - DELETE FROM submissions - WHERE id=:id - """).bindparams(id=submission.id) - ) - - file_path = app.storage.path_without_filesystem_id(submission.filename) - rq_worker_queue.enqueue(srm, file_path) - except NoFileFoundException: - # The file must have been deleted by the admin, remove the row - conn.execute( - sa.text(""" - DELETE FROM submissions - WHERE id=:id - """).bindparams(id=submission.id) - ) - except TooManyFilesException: - pass - - for reply in replies: - try: - conn.execute( + try: + app = create_app(config) + with app.app_context(): + for submission in submissions: + try: + conn.execute( sa.text(""" - DELETE FROM replies + DELETE FROM submissions WHERE id=:id - """).bindparams(id=reply.id) - ) - - file_path = app.storage.path_without_filesystem_id(reply.filename) - rq_worker_queue.enqueue(srm, file_path) - except NoFileFoundException: - # The file must have been deleted by the admin, remove the row - conn.execute( + """).bindparams(id=submission.id) + ) + + file_path = app.storage.path_without_filesystem_id(submission.filename) + rq_worker_queue.enqueue(srm, file_path) + except NoFileFoundException: + # The file must have been deleted by the admin, remove the row + conn.execute( sa.text(""" - DELETE FROM replies + DELETE FROM submissions WHERE id=:id - """).bindparams(id=reply.id) - ) - except TooManyFilesException: - pass + """).bindparams(id=submission.id) + ) + except TooManyFilesException: + pass + + for reply in replies: + try: + conn.execute( + sa.text(""" + DELETE FROM replies + WHERE id=:id + """).bindparams(id=reply.id) + ) + + file_path = app.storage.path_without_filesystem_id(reply.filename) + rq_worker_queue.enqueue(srm, file_path) + except NoFileFoundException: + # The file must have been deleted by the admin, remove the row + conn.execute( + sa.text(""" + DELETE FROM replies + WHERE id=:id + """).bindparams(id=reply.id) + ) + except TooManyFilesException: + pass + except: # noqa + if raise_errors: + raise def downgrade():