Skip to content

Commit 80c95fe

Browse files
committed
add test for issue 1743
however this issue looks to be simple sqlite reflection issue on sqlalchemy side also fixes the incorrect exclusions code from 20c1087. just skip entire test if SQLite fk reflection is not there. this exclusion should not have been added originally, it looks like this was mistaken for a limitation when it really was a bug in sqlite reflection Change-Id: I3f51ebaf01f41dba86cea0aa31be28334401887b
1 parent 20c1087 commit 80c95fe

File tree

4 files changed

+104
-4
lines changed

4 files changed

+104
-4
lines changed

alembic/testing/requirements.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ def fk_deferrable_is_reflected(self):
164164

165165
@property
166166
def fk_names(self):
167+
return self.foreign_key_name_reflection
168+
169+
@property
170+
def foreign_key_name_reflection(self):
167171
return exclusions.open()
168172

169173
@property

alembic/testing/suite/test_autogen_fks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def test_no_change_composite_fk(self):
199199

200200
eq_(diffs, [])
201201

202-
@config.requirements.fk_names
202+
@config.requirements.foreign_key_name_reflection
203203
def test_casing_convention_changed_so_put_drops_first(self):
204204
m1 = MetaData()
205205
m2 = MetaData()

tests/requirements.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ def unnamed_constraints(self):
6161
return exclusions.only_on(["sqlite"])
6262

6363
@property
64-
def fk_names(self):
64+
def foreign_key_name_reflection(self):
6565
"""backend can reflect foreign key names"""
6666

6767
# issue here was fixed in SQLAlchemy #12954 for sqlite, 2.0
6868
# release
69-
return exclusions.fails_if(
70-
lambda config: not sqla_compat.sqla_2 and config.against("sqlite")
69+
return exclusions.skip_if(
70+
lambda config: not sqla_compat.sqla_2
71+
and exclusions.against(config, "sqlite")
7172
)
7273

7374
@property

tests/test_autogen_diffs.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,3 +1910,98 @@ def test_raise_on_dupe(self):
19101910
[m1a, m1b],
19111911
[m2a, m2b],
19121912
)
1913+
1914+
1915+
class AutogenFKTest(AutogenFixtureTest, TestBase):
1916+
"""Test foreign key constraint handling in autogenerate."""
1917+
1918+
__backend__ = True
1919+
__requires__ = ("foreign_key_constraint_reflection",)
1920+
1921+
@testing.variation("case", ["uppercase", "lowercase"])
1922+
@testing.variation("use_naming_convention", [True, False])
1923+
@config.requirements.foreign_key_name_reflection
1924+
def test_drop_fk_with_mixed_case_name(self, case, use_naming_convention):
1925+
"""Test #1743"""
1926+
if case.uppercase:
1927+
fk_pattern = (
1928+
"FK_%(table_name)s_%(column_0_name)s_%(referred_table_name)s"
1929+
)
1930+
expected_name = "FK_child_parent_id_parent"
1931+
else:
1932+
fk_pattern = (
1933+
"fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s"
1934+
)
1935+
expected_name = "fk_child_parent_id_parent"
1936+
1937+
if use_naming_convention:
1938+
m1 = MetaData(naming_convention={"fk": fk_pattern})
1939+
else:
1940+
m1 = MetaData()
1941+
1942+
Table(
1943+
"parent",
1944+
m1,
1945+
Column("id", Integer, primary_key=True),
1946+
Column("name", String(50)),
1947+
)
1948+
1949+
c = Table(
1950+
"child",
1951+
m1,
1952+
Column("id", Integer, primary_key=True),
1953+
Column("name", String(50)),
1954+
Column("parent_id", Integer),
1955+
(
1956+
ForeignKeyConstraint(["parent_id"], ["parent.id"])
1957+
if use_naming_convention
1958+
else ForeignKeyConstraint(
1959+
["parent_id"], ["parent.id"], name=expected_name
1960+
)
1961+
),
1962+
)
1963+
1964+
eq_(
1965+
[
1966+
fk
1967+
for fk in c.constraints
1968+
if isinstance(fk, ForeignKeyConstraint)
1969+
][0].name,
1970+
expected_name,
1971+
)
1972+
1973+
if use_naming_convention:
1974+
m2 = MetaData(naming_convention={"fk": fk_pattern})
1975+
else:
1976+
m2 = MetaData()
1977+
1978+
Table(
1979+
"parent",
1980+
m2,
1981+
Column("id", Integer, primary_key=True),
1982+
Column("name", String(50)),
1983+
)
1984+
1985+
Table(
1986+
"child",
1987+
m2,
1988+
Column("id", Integer, primary_key=True),
1989+
Column("name", String(50)),
1990+
Column("parent_id", Integer),
1991+
)
1992+
1993+
diffs = self._fixture(m1, m2)
1994+
1995+
# Should detect the FK removal
1996+
eq_(len(diffs), 1, f"Expected 1 diff, got {len(diffs)}: {diffs}")
1997+
1998+
# Verify it's a remove_fk operation with the correct name
1999+
self._assert_fk_diff(
2000+
diffs[0],
2001+
"remove_fk",
2002+
"child",
2003+
["parent_id"],
2004+
"parent",
2005+
["id"],
2006+
name=expected_name,
2007+
)

0 commit comments

Comments
 (0)