Skip to content

Commit 798149b

Browse files
committed
Add --update-incoming-fks CLI flag for transform command
When renaming columns that are referenced by foreign keys in other tables, the --update-incoming-fks flag will automatically update those FK constraints. Example: sqlite-utils transform mydb.db authors \ --rename id author_pk \ --update-incoming-fks This will rename the 'id' column to 'author_pk' and also update any foreign key constraints in other tables (e.g., books.author_id) that reference the renamed column.
1 parent e22b34e commit 798149b

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

sqlite_utils/cli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,6 +2545,11 @@ def schema(
25452545
multiple=True,
25462546
help="Drop foreign key constraint for this column",
25472547
)
2548+
@click.option(
2549+
"--update-incoming-fks",
2550+
is_flag=True,
2551+
help="Update foreign keys in other tables that reference renamed columns",
2552+
)
25482553
@click.option("--sql", is_flag=True, help="Output SQL without executing it")
25492554
@load_extension_option
25502555
def transform(
@@ -2562,6 +2567,7 @@ def transform(
25622567
default_none,
25632568
add_foreign_keys,
25642569
drop_foreign_keys,
2570+
update_incoming_fks,
25652571
sql,
25662572
load_extension,
25672573
):
@@ -2615,6 +2621,8 @@ def transform(
26152621
kwargs["drop_foreign_keys"] = drop_foreign_keys
26162622
if add_foreign_keys:
26172623
kwargs["add_foreign_keys"] = add_foreign_keys
2624+
if update_incoming_fks:
2625+
kwargs["update_incoming_fks"] = True
26182626

26192627
if sql:
26202628
for line in db.table(table).transform_sql(**kwargs):

tests/test_cli.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,44 @@ def test_transform_add_or_drop_foreign_key(db_path, extra_args, expected_schema)
18101810
assert schema == expected_schema
18111811

18121812

1813+
def test_transform_update_incoming_fks_cli(db_path):
1814+
"""Test --update-incoming-fks flag updates foreign keys in other tables"""
1815+
db = Database(db_path)
1816+
with db.conn:
1817+
db["authors"].insert({"id": 1, "name": "Alice"}, pk="id")
1818+
db["books"].insert(
1819+
{"id": 1, "title": "Book A", "author_id": 1},
1820+
pk="id",
1821+
foreign_keys=[("author_id", "authors", "id")],
1822+
)
1823+
1824+
# Rename authors.id to authors.author_pk with --update-incoming-fks
1825+
result = CliRunner().invoke(
1826+
cli.cli,
1827+
[
1828+
"transform",
1829+
db_path,
1830+
"authors",
1831+
"--rename", "id", "author_pk",
1832+
"--update-incoming-fks",
1833+
],
1834+
)
1835+
assert result.exit_code == 0, result.output
1836+
1837+
# Verify authors column was renamed
1838+
assert "author_pk" in db["authors"].columns_dict
1839+
assert "id" not in db["authors"].columns_dict
1840+
1841+
# Verify books FK was updated
1842+
assert db["books"].schema == (
1843+
'CREATE TABLE "books" (\n'
1844+
' "id" INTEGER PRIMARY KEY,\n'
1845+
' "title" TEXT,\n'
1846+
' "author_id" INTEGER REFERENCES "authors"("author_pk")\n'
1847+
")"
1848+
)
1849+
1850+
18131851
_common_other_schema = (
18141852
'CREATE TABLE "species" (\n "id" INTEGER PRIMARY KEY,\n "species" TEXT\n)'
18151853
)

0 commit comments

Comments
 (0)