Skip to content

Commit

Permalink
Fix mypy and add better comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen committed May 2, 2024
1 parent 74a23f6 commit 37d72b4
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 123 deletions.
9 changes: 6 additions & 3 deletions src/palace/manager/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import os
import sys
import time
from collections.abc import Sequence
from collections.abc import Callable, Sequence
from datetime import timedelta
from pathlib import Path
from typing import Any

from alembic import command, config
from alembic.util import CommandError
from sqlalchemy import inspect, select
from sqlalchemy.engine import Connection
from sqlalchemy.engine import Connection, Engine
from sqlalchemy.exc import NoResultFound
from sqlalchemy.orm import Session

Expand Down Expand Up @@ -489,6 +489,7 @@ class InstanceInitializationScript:
def __init__(
self,
config_file: Path | None = None,
engine_factory: Callable[[], Engine] = SessionManager.engine,
) -> None:
self._log: logging.Logger | None = None
self._container = container_instance()
Expand All @@ -497,6 +498,8 @@ def __init__(
self._container.init_resources()
self._config_file = config_file

self._engine_factory = engine_factory

@property
def log(self) -> logging.Logger:
if self._log is None:
Expand Down Expand Up @@ -571,7 +574,7 @@ def run(self) -> None:
instance of the script is running at a time. This prevents multiple
instances from trying to initialize the database at the same time.
"""
engine = SessionManager.engine()
engine = self._engine_factory()
with engine.begin() as connection:
with pg_advisory_lock(connection, LOCK_ID_DB_INIT):
self.initialize(connection)
Expand Down
Loading

0 comments on commit 37d72b4

Please sign in to comment.