From: jonathan vanasco Date: Mon, 16 Jun 2025 15:36:20 +0000 (-0400) Subject: support 'append_delimiter' to fix #1679 X-Git-Tag: rel_1_16_2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d00e8541dddf3a5a5b1d8ea7425c2bcd97f26a03;p=thirdparty%2Fsqlalchemy%2Falembic.git support 'append_delimiter' to fix #1679 Fixed rendering of ``pyproject.toml`` to include two newlines when appending content to an existing file. Pull request courtesy Jonathan Vanasco. Co-authored-by: Mike Bayer Fixes: #1679 Closes: #1680 Pull-request: https://github.com/sqlalchemy/alembic/pull/1680 Pull-request-sha: 20b2aca0d2f6a8f5957063b950e45874343e9a99 Change-Id: Ic2e76a3f791dbda0bd61f09a2c7bafbcdab5eb7f --- diff --git a/alembic/script/base.py b/alembic/script/base.py index 16c014b8..c3b1c311 100644 --- a/alembic/script/base.py +++ b/alembic/script/base.py @@ -560,7 +560,11 @@ class ScriptDirectory: **self.messaging_opts, ): util.template_to_file( - src, dest, self.output_encoding, append=True, **kw + src, + dest, + self.output_encoding, + append_with_newlines=True, + **kw, ) def _generate_template(self, src: Path, dest: Path, **kw: Any) -> None: diff --git a/alembic/util/pyfiles.py b/alembic/util/pyfiles.py index fc9ef6e5..6b75d577 100644 --- a/alembic/util/pyfiles.py +++ b/alembic/util/pyfiles.py @@ -26,7 +26,7 @@ def template_to_file( dest: Union[str, os.PathLike[str]], output_encoding: str, *, - append: bool = False, + append_with_newlines: bool = False, **kw: Any, ) -> None: template = Template(filename=_preserving_path_as_str(template_file)) @@ -45,7 +45,9 @@ def template_to_file( "template-oriented traceback." % fname ) else: - with open(dest, "ab" if append else "wb") as f: + with open(dest, "ab" if append_with_newlines else "wb") as f: + if append_with_newlines: + f.write("\n\n".encode(output_encoding)) f.write(output) diff --git a/docs/build/unreleased/1679.rst b/docs/build/unreleased/1679.rst new file mode 100644 index 00000000..7dca6953 --- /dev/null +++ b/docs/build/unreleased/1679.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: bug, command + :tickets: 1679 + + Fixed rendering of ``pyproject.toml`` to include two newlines when + appending content to an existing file. Pull request courtesy Jonathan + Vanasco. + diff --git a/tests/test_command.py b/tests/test_command.py index 167cfbee..5920ac70 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -43,6 +43,7 @@ from alembic.testing.env import write_script from alembic.testing.fixtures import capture_context_buffer from alembic.testing.fixtures import capture_engine_context_buffer from alembic.testing.fixtures import TestBase +from alembic.util import compat from alembic.util.sqla_compat import _connectable_has_table @@ -1549,6 +1550,20 @@ class CommandLineTest(TestBase): shutil.rmtree(templates_path) + @testing.fixture + def existing_pyproject_fixture(self): + root = pathlib.Path(_get_staging_directory()) + + with (root / "pyproject.toml").open("w") as file_: + file_.write( + """[tool.sometool] +someconfig = 'bar'""" + ) + yield config.Config( + self.cfg.config_file_name, toml_file=root / "pyproject.toml" + ) + shutil.rmtree(root) + @testing.variation("cmd", ["list_templates", "init"]) def test_init_custom_template_location(self, cmd, custom_template_fixture): """test #1660""" @@ -1573,6 +1588,31 @@ class CommandLineTest(TestBase): else: cmd.fail() + def test_init_append_pyproject(self, existing_pyproject_fixture): + cfg = existing_pyproject_fixture + path = pathlib.Path(_get_staging_directory(), "myproject") + command.init(cfg, directory=path.as_posix(), template="pyproject") + with open(cfg.toml_file_name, "r") as f: + file_content = f.read() + + assert file_content.startswith( + """[tool.sometool] +someconfig = 'bar'\n\n[tool.alembic]""" + ) + toml = compat.tomllib.loads(file_content) + eq_( + toml, + { + "tool": { + "sometool": {"someconfig": "bar"}, + "alembic": { + "script_location": "%(here)s/myproject", + "prepend_sys_path": ["."], + }, + } + }, + ) + def test_init_no_such_template(self): """test #1659"""