Skip to content

Commit

Permalink
fix: use app logic to avoid broken migration & apply DRY scidsg#622
Browse files Browse the repository at this point in the history
When running ``make migrate-dev`` there's an error with the new
'host_organization' table migration. Filling & commiting the default
record on app startup if it's missing removes the error, reduces
repetition of magic constants which were given variable names in the
app, & avoids an incomplete DB state which breaks rendering of CSS.

Partial reversion of 9cad571

For Roadmap Item scidsg#533
  • Loading branch information
rmlibre committed Sep 29, 2024
1 parent 9674b1e commit 7a9d86b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 19 deletions.
18 changes: 4 additions & 14 deletions hushline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ def inject_user() -> dict[str, Any]:

@app.context_processor
def inject_host() -> dict[str, HostOrganization]:
return dict(host_org=HostOrganization.fetch_or_default())
if (host_org := HostOrganization.fetch()) is None:
db.session.add(host_org := HostOrganization())
db.session.commit()
return dict(host_org=host_org)

@app.context_processor
def inject_is_personal_server() -> dict[str, Any]:
Expand All @@ -109,17 +112,4 @@ def add_onion_location_header(response: Response) -> Response:
)
return response

# we can't
if app.config.get("FLASK_ENV", None) != "development":
with app.app_context():
try:
host_org = HostOrganization.fetch()
except ProgrammingError:
app.logger.warning(
"Could not check for existence of HostOrganization", exc_info=True
)
else:
if host_org is None:
app.logger.warning("HostOrganization data not found in database.")

return app
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ def upgrade() -> None:
sa.PrimaryKeyConstraint("id", name=op.f("pk_host_organization")),
)

op.execute(
"INSERT INTO host_organization (id, brand_app_name, brand_primary_hex_color) "
"VALUES (1, '🤫 Hush Line', '#7d25c1')"
)


def downgrade() -> None:
op.drop_table("host_organization")

0 comments on commit 7a9d86b

Please sign in to comment.