Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions migrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from flask_migrate import upgrade

from webapp.app import app
from webapp.context import database_lock


def migrate() -> None:
# Use lock to prevent multiple concurrent migrations on startup
# Automatically upgrade to head revision
with app.app_context(), database_lock():
upgrade()


if __name__ == "__main__":
migrate()
1 change: 1 addition & 0 deletions rockcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ parts:
- flask/app/.env
- flask/app/app.py
- flask/app/data
- flask/app/migrate.py
- flask/app/migrations
- flask/app/webapp
- flask/app/templates
Expand Down
21 changes: 11 additions & 10 deletions webapp/site_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import subprocess
import time
import traceback
from collections.abc import Callable
from pathlib import Path
from typing import TypedDict
Expand Down Expand Up @@ -265,19 +266,18 @@ def get_tree_from_db(self):
tree = get_tree_struct(db.session, webpages)
# If the tree is empty, load from the repository
if not tree or (
tree.get("children") and not tree.get("parent_id")
not tree.get("children") and not tree.get("parent_id")
):
msg = (
"Reloading incomplete tree root "
f"{self.repository_uri}."
f"{self.repository_uri}. {tree}"
)
self.logger.info(
msg,
)
tree = self.get_new_tree()
return tree
# Raise an error if the project does not exist.
raise SiteRepositoryError("Invalid project_id. Unable to load tree.")
return None

def get_tree(self, no_cache: bool = False):
"""Get the tree from the cache or load a new tree to cache and db."""
Expand Down Expand Up @@ -403,14 +403,15 @@ def get_tree_sync(self, no_cache: bool = False):
self.invalidate_cache()

# Load the tree from database
try:
tree = self.get_tree_from_db()
if tree := self.get_tree_from_db():
self.logger.info(f"Tree refreshed for {self.repository_uri}")
# Update the cache
self.set_tree_in_cache(tree)
try:
# Update the cache
self.set_tree_in_cache(tree)
except Exception as e:
self.logger.exception(traceback.format_exc())
self.logger.error(f"Unable to save tree to cache: {e}")
return tree
except Exception as e:
self.logger.error(f"Error loading tree: {e}")

# Or just return an empty tree
return {
Expand Down
Loading