]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
support 'append_delimiter' to fix #1679
authorjonathan vanasco <jonathan@2xlp.com>
Mon, 16 Jun 2025 15:36:20 +0000 (11:36 -0400)
committerMichael Bayer <mike_mp@zzzcomputing.com>
Mon, 16 Jun 2025 17:22:39 +0000 (17:22 +0000)
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 <mike_mp@zzzcomputing.com>
Fixes: #1679
Closes: #1680
Pull-request: https://github.com/sqlalchemy/alembic/pull/1680
Pull-request-sha: 20b2aca0d2f6a8f5957063b950e45874343e9a99

Change-Id: Ic2e76a3f791dbda0bd61f09a2c7bafbcdab5eb7f

alembic/script/base.py
alembic/util/pyfiles.py
docs/build/unreleased/1679.rst [new file with mode: 0644]
tests/test_command.py

index 16c014b852da54303556b4ad45e05f8a7b44c936..c3b1c3115443da5941557b11d66336922b2c63b3 100644 (file)
@@ -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:
index fc9ef6e5a9c87ef21596698bb283640a98b1589f..6b75d57792384b890a44d4c096e9a617254e7344 100644 (file)
@@ -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 (file)
index 0000000..7dca695
--- /dev/null
@@ -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.
+
index 167cfbeed095a00f26fd80dd4c65711f71560375..5920ac706ec9bd149fd29261093e53d1b981e712 100644 (file)
@@ -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"""