Skip to content

Commit

Permalink
Add BL_Python.all package back to Makefile.
Browse files Browse the repository at this point in the history
  • Loading branch information
aholmes committed Mar 28, 2024
1 parent 822a913 commit 522bb74
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ _cicd_build : _cicd_configure

@$(REPORT_VENV_USAGE)

BL_Python.all: $(DEFAULT_TARGET)
$(PACKAGES) : BL_Python.%: src/%/pyproject.toml $(VENV) $(CONFIGURE_TARGET) $(PYPROJECT_FILES)
@if [ -d $(call package_to_dist,$*) ]; then
@echo "Package $@ is already built, skipping..."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ def bl_alembic(
logger = logging.getLogger()
logger.setLevel(log_level)

if allow_overwrite is None:
_allow_overwrite = environ.get(BLAlembic.ALLOW_OVERWRITE_NAME)
allow_overwrite = (_allow_overwrite.lower() if _allow_overwrite else None) in [
"true",
"1",
]

BLAlembic(argv, logger, allow_overwrite).run()
BLAlembic(argv, logger).run()


if __name__ == "__main__":
Expand Down
26 changes: 13 additions & 13 deletions src/database/BL_Python/database/migrations/alembic/bl_alembic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@
class BLAlembic:
DEFAULT_CONFIG_NAME: str = "alembic.ini"
LOG_LEVEL_NAME: str = "LOG_LEVEL"
ALLOW_OVERWRITE_NAME: str = "ALLOW_OVERWRITE"

_run: Callable[[], None]
_log: Logger
_allow_overwrite: bool = False

@dataclass
class FileCopy:
source: Path
destination: Path

def __init__(
self, argv: list[str] | None, logger: Logger, allow_overwrite: bool = False
) -> None:
def __init__(self, argv: list[str] | None, logger: Logger) -> None:
"""
_summary_
Expand All @@ -39,7 +35,6 @@ def __init__(
:param Logger logger: A logger for writing messages.
"""
self._log = logger
self._allow_overwrite = allow_overwrite

if not argv:
argv = sys.argv[1:]
Expand Down Expand Up @@ -206,21 +201,28 @@ def _write_bl_alembic_config(
:yield Generator[tempfile._TemporaryFileWrapper[bytes], Any, None]: The temp file.
"""
# need to _not_ use a temp file, and copy the default alembic.ini
config_file_destination = Path(Path.cwd(), BLAlembic.DEFAULT_CONFIG_NAME)
if config_file_destination.exists():
self._log.debug(
f"Configuration file '{BLAlembic.DEFAULT_CONFIG_NAME}' exists. Will not attempt to create it."
)
return

# copy the default alembic.ini
# to the directory in which bl-alembic is executed.
self._log.debug(
f"Writing configuration file '{BLAlembic.DEFAULT_CONFIG_NAME}'."
)
self._copy_files([
BLAlembic.FileCopy(
Path(Path(__file__).resolve().parent, BLAlembic.DEFAULT_CONFIG_NAME),
Path(Path.cwd(), BLAlembic.DEFAULT_CONFIG_NAME),
config_file_destination,
)
])

def _copy_files(self, files: list[FileCopy], force_overwrite: bool = False):
for file in files:
write_mode = "w+b" if self._allow_overwrite or force_overwrite else "x+b"
write_mode = "w+b" if force_overwrite else "x+b"
try:
with (
open(file.source, "r") as source,
Expand All @@ -231,10 +233,8 @@ def _copy_files(self, files: list[FileCopy], force_overwrite: bool = False):
if e.filename != str(file.destination):
raise

self._log.warn(
f"""The file '{file.destination}' already exists, but this is unexpected. Refusing to overwrite.
To use the default configuration, delete the existing file,
or set the envvar `{BLAlembic.ALLOW_OVERWRITE_NAME}=True`."""
self._log.debug(
f"The file '{file.destination}' already exists. Refusing to overwrite, but ignoring exception."
)

def _alembic_msg_capture(
Expand Down
36 changes: 34 additions & 2 deletions src/database/test/unit/migrations/alembic/test_bl_alembic.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def path_se(*args: Any, **kwargs: Any):
# set the call args for the Path mocks that are passed
# into the FileCopy mock so we can examine them when FileCopy
# is called
return MagicMock(args=args)
return MagicMock(args=args, exists=MagicMock(return_value=False))

def file_copy_se(*args: Any, **kwargs: Any):
# set a mocked FileCopy whose src/dest are strings (filenames)
Expand Down Expand Up @@ -119,7 +119,39 @@ def test__BLAlembic__does_not_overwrite_existing_config(
_ = mock_alembic(mocker)
_ = mock_argv(["upgrade", "head"])

_ = mocker.patch("BL_Python.database.migrations.alembic.bl_alembic.Path")
_ = mocker.patch(
"BL_Python.database.migrations.alembic.bl_alembic.Path",
return_value=MagicMock(exists=MagicMock(return_value=True)),
)
_ = mocker.patch("builtins.open", mocker.mock_open())
log_mock = mocker.patch("BL_Python.database.migrations.alembic.bl_alembic.Logger")

bl_alembic = BLAlembic(None, log_mock)

try:
bl_alembic.run()
except:
pass

assert [
True
for call in log_mock.mock_calls
if call.args[0].startswith(
f"Configuration file '{BLAlembic.DEFAULT_CONFIG_NAME}' exists."
)
]


def test__BLAlembic__crashes_when_overwriting_unexpected_file(
mock_argv: MockArgv, mocker: MockerFixture
):
_ = mock_alembic(mocker)
_ = mock_argv(["upgrade", "head"])

_ = mocker.patch(
"BL_Python.database.migrations.alembic.bl_alembic.Path",
return_value=MagicMock(exists=MagicMock(return_value=False)),
)
open_mock = mocker.patch("builtins.open", mocker.mock_open())

def raise_file_exists_error(*args: Any, **kwargs: Any):
Expand Down

0 comments on commit 522bb74

Please sign in to comment.