Skip to content

Commit 5ce545b

Browse files
committed
Run black and cog for formatting and CLI reference update
1 parent 0a567b7 commit 5ce545b

File tree

4 files changed

+59
-19
lines changed

4 files changed

+59
-19
lines changed

docs/cli-reference.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@ See :ref:`cli_transform_table`.
474474
Add a foreign key constraint from a column to
475475
another table with another column
476476
--drop-foreign-key TEXT Drop foreign key constraint for this column
477+
--update-incoming-fks Update foreign keys in other tables that
478+
reference renamed columns
477479
--sql Output SQL without executing it
478480
--load-extension TEXT Path to SQLite extension, with optional
479481
:entrypoint

sqlite_utils/db.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,9 +1877,7 @@ def _get_incoming_fks_needing_update(self, rename: dict) -> list:
18771877
if fk.other_table == self.name and fk.other_column in rename:
18781878
# This FK needs updating
18791879
needs_update = True
1880-
new_fks.append(
1881-
(fk.column, fk.other_table, rename[fk.other_column])
1882-
)
1880+
new_fks.append((fk.column, fk.other_table, rename[fk.other_column]))
18831881
else:
18841882
new_fks.append((fk.column, fk.other_table, fk.other_column))
18851883

tests/test_cli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1828,7 +1828,9 @@ def test_transform_update_incoming_fks_cli(db_path):
18281828
"transform",
18291829
db_path,
18301830
"authors",
1831-
"--rename", "id", "author_pk",
1831+
"--rename",
1832+
"id",
1833+
"author_pk",
18321834
"--update-incoming-fks",
18331835
],
18341836
)

tests/test_transform.py

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,9 @@ def test_transform_update_incoming_fks_on_column_rename(fresh_db):
682682

683683
# Verify initial FK
684684
assert fresh_db["books"].foreign_keys == [
685-
ForeignKey(table="books", column="author_id", other_table="authors", other_column="id")
685+
ForeignKey(
686+
table="books", column="author_id", other_table="authors", other_column="id"
687+
)
686688
]
687689

688690
# Rename authors.id to authors.author_pk with update_incoming_fks=True
@@ -697,12 +699,19 @@ def test_transform_update_incoming_fks_on_column_rename(fresh_db):
697699

698700
# Verify books FK was updated to point to new column name
699701
assert fresh_db["books"].foreign_keys == [
700-
ForeignKey(table="books", column="author_id", other_table="authors", other_column="author_pk")
702+
ForeignKey(
703+
table="books",
704+
column="author_id",
705+
other_table="authors",
706+
other_column="author_pk",
707+
)
701708
]
702709

703710
# Verify data integrity
704711
assert list(fresh_db["authors"].rows) == [{"author_pk": 1, "name": "Alice"}]
705-
assert list(fresh_db["books"].rows) == [{"id": 1, "title": "Book A", "author_id": 1}]
712+
assert list(fresh_db["books"].rows) == [
713+
{"id": 1, "title": "Book A", "author_id": 1}
714+
]
706715

707716
# Verify FK enforcement still works
708717
assert fresh_db.execute("PRAGMA foreign_keys").fetchone()[0] == 1
@@ -749,13 +758,28 @@ def test_transform_update_incoming_fks_multiple_tables(fresh_db):
749758

750759
# Verify all FKs were updated
751760
assert fresh_db["books"].foreign_keys == [
752-
ForeignKey(table="books", column="author_id", other_table="authors", other_column="author_pk")
761+
ForeignKey(
762+
table="books",
763+
column="author_id",
764+
other_table="authors",
765+
other_column="author_pk",
766+
)
753767
]
754768
assert fresh_db["articles"].foreign_keys == [
755-
ForeignKey(table="articles", column="writer_id", other_table="authors", other_column="author_pk")
769+
ForeignKey(
770+
table="articles",
771+
column="writer_id",
772+
other_table="authors",
773+
other_column="author_pk",
774+
)
756775
]
757776
assert fresh_db["quotes"].foreign_keys == [
758-
ForeignKey(table="quotes", column="speaker_id", other_table="authors", other_column="author_pk")
777+
ForeignKey(
778+
table="quotes",
779+
column="speaker_id",
780+
other_table="authors",
781+
other_column="author_pk",
782+
)
759783
]
760784

761785
# Verify FK enforcement still works
@@ -770,22 +794,31 @@ def test_transform_update_incoming_fks_self_referential(fresh_db):
770794
fresh_db.execute("PRAGMA foreign_keys=ON")
771795

772796
# Create employees table with self-referential FK (manager_id -> id)
773-
fresh_db.execute("""
797+
fresh_db.execute(
798+
"""
774799
CREATE TABLE employees (
775800
id INTEGER PRIMARY KEY,
776801
name TEXT,
777802
manager_id INTEGER REFERENCES employees(id)
778803
)
779-
""")
780-
fresh_db["employees"].insert_all([
781-
{"id": 1, "name": "CEO", "manager_id": None},
782-
{"id": 2, "name": "VP", "manager_id": 1},
783-
{"id": 3, "name": "Dev", "manager_id": 2},
784-
])
804+
"""
805+
)
806+
fresh_db["employees"].insert_all(
807+
[
808+
{"id": 1, "name": "CEO", "manager_id": None},
809+
{"id": 2, "name": "VP", "manager_id": 1},
810+
{"id": 3, "name": "Dev", "manager_id": 2},
811+
]
812+
)
785813

786814
# Verify initial FK
787815
assert fresh_db["employees"].foreign_keys == [
788-
ForeignKey(table="employees", column="manager_id", other_table="employees", other_column="id")
816+
ForeignKey(
817+
table="employees",
818+
column="manager_id",
819+
other_table="employees",
820+
other_column="id",
821+
)
789822
]
790823

791824
# Rename employees.id to employees.emp_id with update_incoming_fks=True
@@ -800,7 +833,12 @@ def test_transform_update_incoming_fks_self_referential(fresh_db):
800833

801834
# Verify self-referential FK was updated
802835
assert fresh_db["employees"].foreign_keys == [
803-
ForeignKey(table="employees", column="manager_id", other_table="employees", other_column="emp_id")
836+
ForeignKey(
837+
table="employees",
838+
column="manager_id",
839+
other_table="employees",
840+
other_column="emp_id",
841+
)
804842
]
805843

806844
# Verify data integrity

0 commit comments

Comments
 (0)