Skip to content

Commit 1d050dc

Browse files
committed
insert-files multiple --pk support, closes #621
1 parent 1feb0c4 commit 1d050dc

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

sqlite_utils/cli.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,7 +2594,7 @@ def extract(
25942594
multiple=True,
25952595
help="Column definitions for the table",
25962596
)
2597-
@click.option("--pk", type=str, help="Column to use as primary key")
2597+
@click.option("pks", "--pk", help="Column to use as primary key", multiple=True)
25982598
@click.option("--alter", is_flag=True, help="Alter table to add missing columns")
25992599
@click.option("--replace", is_flag=True, help="Replace files with matching primary key")
26002600
@click.option("--upsert", is_flag=True, help="Upsert files with matching primary key")
@@ -2611,7 +2611,7 @@ def insert_files(
26112611
table,
26122612
file_or_dir,
26132613
column,
2614-
pk,
2614+
pks,
26152615
alter,
26162616
replace,
26172617
upsert,
@@ -2641,8 +2641,8 @@ def insert_files(
26412641
column = ["path:path", "content_text:content_text", "size:size"]
26422642
else:
26432643
column = ["path:path", "content:content", "size:size"]
2644-
if not pk:
2645-
pk = "path"
2644+
if not pks:
2645+
pks = ["path"]
26462646

26472647
def yield_paths_and_relative_paths():
26482648
for f_or_d in file_or_dir:
@@ -2712,7 +2712,11 @@ def _content_text(p):
27122712
try:
27132713
with db.conn:
27142714
db[table].insert_all(
2715-
to_insert(), pk=pk, alter=alter, replace=replace, upsert=upsert
2715+
to_insert(),
2716+
pk=pks[0] if len(pks) == 1 else pks,
2717+
alter=alter,
2718+
replace=replace,
2719+
upsert=upsert,
27162720
)
27172721
except UnicodeDecodeErrorForPath as e:
27182722
raise click.ClickException(

tests/test_insert_files.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77

88

99
@pytest.mark.parametrize("silent", (False, True))
10-
def test_insert_files(silent):
10+
@pytest.mark.parametrize(
11+
"pk_args,expected_pks",
12+
(
13+
(["--pk", "path"], ["path"]),
14+
(["--pk", "path", "--pk", "name"], ["path", "name"]),
15+
),
16+
)
17+
def test_insert_files(silent, pk_args, expected_pks):
1118
runner = CliRunner()
1219
with runner.isolated_filesystem():
1320
tmpdir = pathlib.Path(".")
@@ -42,7 +49,7 @@ def test_insert_files(silent):
4249
cli.cli,
4350
["insert-files", db_path, "files", str(tmpdir)]
4451
+ cols
45-
+ ["--pk", "path"]
52+
+ pk_args
4653
+ (["--silent"] if silent else []),
4754
catch_exceptions=False,
4855
)
@@ -105,6 +112,7 @@ def test_insert_files(silent):
105112
for colname, expected_type in expected_types.items():
106113
for row in (one, two, three):
107114
assert isinstance(row[colname], expected_type)
115+
assert set(db["files"].pks) == set(expected_pks)
108116

109117

110118
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)