]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Enhance version_path_separator behaviour by adding a newline option
authorPavel V. Pristupa <pavel.pristupa@quantumsoft.pro>
Tue, 30 Jul 2024 05:45:20 +0000 (01:45 -0400)
committersqla-tester <sqla-tester@sqlalchemy.org>
Tue, 30 Jul 2024 05:45:20 +0000 (01:45 -0400)
### Description
version_path_separator now consists a new option "newline" which allows you to specify multiple version locations across multiple lines like this:
```
version_locations =
  /foo/versions
  /bar/versions
  /baz/versions

version_path_separator = newline
```

### Checklist
This pull request is:

- [ ] A documentation / typographical error fix
- Good to go, no issue or tests are needed
- [ ] A short code fix
- please include the issue number, and create an issue if none exists, which
  must include a complete example of the issue.  one line code fixes without an
  issue and demonstration will not be accepted.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests.   one line code fixes without tests will not be accepted.
- [x] A new feature implementation
- please include the issue number, and create an issue if none exists, which must
  include a complete example of how the feature would look.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests.

**Have a nice day!**

Closes: #1510
Pull-request: https://github.com/sqlalchemy/alembic/pull/1510
Pull-request-sha: 6155da71472fa2727beabd0aeaec1bdc07378b1b

Change-Id: I364906906a9c7164e8f7fa5f51f3097ab118cc65

alembic/script/base.py
alembic/templates/async/alembic.ini.mako
alembic/templates/generic/alembic.ini.mako
alembic/templates/multidb/alembic.ini.mako
alembic/testing/assertions.py
docs/build/tutorial.rst
docs/build/unreleased/1509.rst [new file with mode: 0644]
tests/test_config.py

index 9a955b6bc45df91e1243381adb803997bfdfb7e4..30df6ddb2b79d286308be84da2dfe449199312cb 100644 (file)
@@ -187,6 +187,7 @@ class ScriptDirectory:
             split_on_path = {
                 None: None,
                 "space": " ",
+                "newline": "\n",
                 "os": os.pathsep,
                 ":": ":",
                 ";": ";",
@@ -200,7 +201,8 @@ class ScriptDirectory:
                 raise ValueError(
                     "'%s' is not a valid value for "
                     "version_path_separator; "
-                    "expected 'space', 'os', ':', ';'" % version_path_separator
+                    "expected 'space', 'newline', 'os', ':', ';'"
+                    % version_path_separator
                 ) from ke
             else:
                 if split_char is None:
@@ -210,7 +212,9 @@ class ScriptDirectory:
                     )
                 else:
                     version_locations = [
-                        x for x in version_locations_str.split(split_char) if x
+                        x.strip()
+                        for x in version_locations_str.split(split_char)
+                        if x
                     ]
         else:
             version_locations = None
index 3558295edc1aa58c134e1f1a7e673f08fafcd85e..46a0904ed3142881227d27b55837ba8c9c750e90 100644 (file)
@@ -47,6 +47,7 @@ prepend_sys_path = .
 # version_path_separator = :
 # version_path_separator = ;
 # version_path_separator = space
+# version_path_separator = newline
 version_path_separator = os  # Use os.pathsep. Default configuration used for new projects.
 
 # set to 'true' to search source files recursively
index b3fe053552e2ef93607fb487072b1afc3f047725..dd4ea588e21d47cd0434cc259664428cce93055a 100644 (file)
@@ -49,6 +49,7 @@ prepend_sys_path = .
 # version_path_separator = :
 # version_path_separator = ;
 # version_path_separator = space
+# version_path_separator = newline
 version_path_separator = os  # Use os.pathsep. Default configuration used for new projects.
 
 # set to 'true' to search source files recursively
index 6e90199994c81acfa825d5b93b0f26e112737129..d5cc86f1eb9e98500323877d934a3ab876447184 100644 (file)
@@ -49,6 +49,7 @@ prepend_sys_path = .
 # version_path_separator = :
 # version_path_separator = ;
 # version_path_separator = space
+# version_path_separator = newline
 version_path_separator = os  # Use os.pathsep. Default configuration used for new projects.
 
 # set to 'true' to search source files recursively
index ec9593b713656d7c9a4097c32b5d84b0b570069f..e071697cd7941b680a281b42324b9a90b846711f 100644 (file)
@@ -74,7 +74,9 @@ class _ErrorContainer:
 
 
 @contextlib.contextmanager
-def _expect_raises(except_cls, msg=None, check_context=False):
+def _expect_raises(
+    except_cls, msg=None, check_context=False, text_exact=False
+):
     ec = _ErrorContainer()
     if check_context:
         are_we_already_in_a_traceback = sys.exc_info()[0]
@@ -85,7 +87,10 @@ def _expect_raises(except_cls, msg=None, check_context=False):
         ec.error = err
         success = True
         if msg is not None:
-            assert re.search(msg, str(err), re.UNICODE), f"{msg} !~ {err}"
+            if text_exact:
+                assert str(err) == msg, f"{msg} != {err}"
+            else:
+                assert re.search(msg, str(err), re.UNICODE), f"{msg} !~ {err}"
         if check_context and not are_we_already_in_a_traceback:
             _assert_proper_exception_context(err)
         print(str(err).encode("utf-8"))
@@ -98,8 +103,12 @@ def expect_raises(except_cls, check_context=True):
     return _expect_raises(except_cls, check_context=check_context)
 
 
-def expect_raises_message(except_cls, msg, check_context=True):
-    return _expect_raises(except_cls, msg=msg, check_context=check_context)
+def expect_raises_message(
+    except_cls, msg, check_context=True, text_exact=False
+):
+    return _expect_raises(
+        except_cls, msg=msg, check_context=check_context, text_exact=text_exact
+    )
 
 
 def eq_ignore_whitespace(a, b, msg=None):
index f1cd49165e9f6bb24765bdb2bc4b1b0b224d582f..6cc16697f990a826b18b12f21ac717191944ce43 100644 (file)
@@ -174,6 +174,7 @@ The file generated with the "generic" configuration looks like::
     # version_path_separator = :
     # version_path_separator = ;
     # version_path_separator = space
+    # version_path_separator = newline
     version_path_separator = os  # Use os.pathsep. Default configuration used for new projects.
 
     # set to 'true' to search source files recursively
diff --git a/docs/build/unreleased/1509.rst b/docs/build/unreleased/1509.rst
new file mode 100644 (file)
index 0000000..df3bf97
--- /dev/null
@@ -0,0 +1,5 @@
+.. change::\r
+    :tags: feature, environment\r
+    :tickets: 1509\r
+\r
+    Enhance ``version_locations`` parsing to handle paths containing newlines.\r
index 73ce2aa94375f490ab0653f5a6754da47189165b..a98994c4d22914104f5fb90a8eb86eb468d62567 100644 (file)
@@ -134,6 +134,12 @@ class ConfigTest(TestBase):
             "/foo /bar",
             ["/foo", "/bar"],
         ),
+        (
+            "multiline string 1",
+            "newline",
+            " /foo  \n/bar  ",
+            ["/foo", "/bar"],
+        ),
         (
             "Linux pathsep 1",
             ":",
@@ -171,7 +177,7 @@ class ConfigTest(TestBase):
             "/foo|/bar",
             ValueError(
                 "'|' is not a valid value for version_path_separator; "
-                "expected 'space', 'os', ':', ';'"
+                "expected 'space', 'newline', 'os', ':', ';'"
             ),
         ),
         id_="iaaa",
@@ -188,7 +194,8 @@ class ConfigTest(TestBase):
         cfg.set_main_option("version_locations", string_value)
 
         if isinstance(expected_result, ValueError):
-            with expect_raises_message(ValueError, expected_result.args[0]):
+            message = str(expected_result)
+            with expect_raises_message(ValueError, message, text_exact=True):
                 ScriptDirectory.from_config(cfg)
         else:
             s = ScriptDirectory.from_config(cfg)