From 70115dc5d65e1f2e665d3b94cf7734184c3b96e2 Mon Sep 17 00:00:00 2001 From: Oliver Rice Date: Fri, 5 Mar 2021 14:23:40 -0500 Subject: [PATCH] enable extensions to use AutogenContext.run_name_filters Adjusted the recently added :paramref:`.EnvironmentContext.configure.include_name` hook to accommodate for additional object types such as "views" that don't have a parent table, to support third party recipes and extensions. Pull request courtesy Oliver Rice. Fixes: #813 Closes: #814 Pull-request: https://github.com/sqlalchemy/alembic/pull/814 Pull-request-sha: 45dbc9c3baebd02c44cdcafc7ac73f00e87d8a86 Change-Id: I75f22b745bd847638b9fdff9c19c1f0091a6b470 --- alembic/autogenerate/api.py | 19 ++++++------ docs/build/unreleased/813.rst | 9 ++++++ tests/test_autogen_diffs.py | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 docs/build/unreleased/813.rst diff --git a/alembic/autogenerate/api.py b/alembic/autogenerate/api.py index 030bc8bf..bdcfebd6 100644 --- a/alembic/autogenerate/api.py +++ b/alembic/autogenerate/api.py @@ -330,15 +330,16 @@ def run_name_filters(self, name, type_, parent_names): if type_ == "table": table_name = name else: - table_name = parent_names["table_name"] - schema_name = parent_names["schema_name"] - if schema_name: - parent_names["schema_qualified_table_name"] = "%s.%s" % ( - schema_name, - table_name, - ) - else: - parent_names["schema_qualified_table_name"] = table_name + table_name = parent_names.get("table_name", None) + if table_name: + schema_name = parent_names["schema_name"] + if schema_name: + parent_names["schema_qualified_table_name"] = "%s.%s" % ( + schema_name, + table_name, + ) + else: + parent_names["schema_qualified_table_name"] = table_name for fn in self._name_filters: diff --git a/docs/build/unreleased/813.rst b/docs/build/unreleased/813.rst new file mode 100644 index 00000000..898e1807 --- /dev/null +++ b/docs/build/unreleased/813.rst @@ -0,0 +1,9 @@ +.. change:: + :tags: bug, autogenerate + :tickets: 813 + + Adjusted the recently added + :paramref:`.EnvironmentContext.configure.include_name` hook to accommodate + for additional object types such as "views" that don't have a parent table, + to support third party recipes and extensions. Pull request courtesy Oliver + Rice. diff --git a/tests/test_autogen_diffs.py b/tests/test_autogen_diffs.py index a2215aff..02a750a2 100644 --- a/tests/test_autogen_diffs.py +++ b/tests/test_autogen_diffs.py @@ -1188,6 +1188,60 @@ def test_column_type_modified_custom_compare_type_returns_True(self): eq_(diffs[1][0][0], "modify_type") +class IncludeFiltersAPITest(AutogenTest, TestBase): + @classmethod + def _get_db_schema(cls): + return MetaData() + + @classmethod + def _get_model_schema(cls): + return MetaData() + + def test_run_name_filters_supports_extension_types(self): + include_name = mock.Mock() + + self._update_context(name_filters=include_name, include_schemas=True) + + self.autogen_context.run_name_filters( + name="some_function", + type_="function", + parent_names={"schema_name": "public"}, + ) + + eq_( + include_name.mock_calls, + [ + mock.call( + "some_function", "function", {"schema_name": "public"} + ) + ], + ) + + def test_run_object_filters_supports_extension_types(self): + include_object = mock.Mock() + + self._update_context( + object_filters=include_object, include_schemas=True + ) + + class ExtFunction(object): + pass + + extfunc = ExtFunction() + self.autogen_context.run_object_filters( + object_=extfunc, + name="some_function", + type_="function", + reflected=False, + compare_to=None, + ) + + eq_( + include_object.mock_calls, + [mock.call(extfunc, "some_function", "function", False, None)], + ) + + class PKConstraintUpgradesIgnoresNullableTest(AutogenTest, TestBase): __backend__ = True