Skip to content

Commit

Permalink
enable extensions to use AutogenContext.run_name_filters
Browse files Browse the repository at this point in the history
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: #814
Pull-request-sha: 45dbc9c

Change-Id: I75f22b745bd847638b9fdff9c19c1f0091a6b470
  • Loading branch information
olirice authored and zzzeek committed Mar 5, 2021
1 parent c3fc47d commit 70115dc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
19 changes: 10 additions & 9 deletions alembic/autogenerate/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
9 changes: 9 additions & 0 deletions docs/build/unreleased/813.rst
Original file line number Diff line number Diff line change
@@ -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.
54 changes: 54 additions & 0 deletions tests/test_autogen_diffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 70115dc

Please sign in to comment.