Skip to content

Commit ad5eed7

Browse files
authored
Add continue option (#23)
1 parent b22705c commit ad5eed7

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

doltcli/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def write_file(
6969
commit: Optional[bool] = False,
7070
commit_message: Optional[str] = None,
7171
commit_date: Optional[datetime.datetime] = None,
72+
do_continue: Optional[bool] = False,
7273
):
7374
def writer(filepath: str):
7475
with open(filepath, "w", newline="") as f:
@@ -83,6 +84,7 @@ def writer(filepath: str):
8384
commit=commit,
8485
commit_message=commit_message,
8586
commit_date=commit_date,
87+
do_continue=do_continue,
8688
)
8789

8890

@@ -95,6 +97,7 @@ def write_columns(
9597
commit: Optional[bool] = False,
9698
commit_message: Optional[str] = None,
9799
commit_date: Optional[datetime.datetime] = None,
100+
do_continue: Optional[bool] = False,
98101
):
99102
"""
100103
@@ -128,6 +131,7 @@ def writer(filepath: str):
128131
commit=commit,
129132
commit_message=commit_message,
130133
commit_date=commit_date,
134+
do_continue=do_continue,
131135
)
132136

133137

@@ -140,6 +144,7 @@ def write_rows(
140144
commit: Optional[bool] = False,
141145
commit_message: Optional[str] = None,
142146
commit_date: Optional[datetime.datetime] = None,
147+
do_continue: Optional[bool] = False,
143148
):
144149
"""
145150
@@ -174,6 +179,7 @@ def writer(filepath: str):
174179
commit=commit,
175180
commit_message=commit_message,
176181
commit_date=commit_date,
182+
do_continue=do_continue,
177183
)
178184

179185

@@ -183,6 +189,7 @@ def _import_helper(
183189
write_import_file: Callable[[str], None],
184190
import_mode: Optional[str] = None,
185191
primary_key: Optional[List[str]] = None,
192+
do_continue: Optional[bool] = False,
186193
commit: Optional[bool] = False,
187194
commit_message: Optional[str] = None,
188195
commit_date: Optional[datetime.datetime] = None,
@@ -199,6 +206,8 @@ def _import_helper(
199206
args = ["table", "import", table] + import_flags
200207
if primary_key:
201208
args += ["--pk={}".format(",".join(primary_key))]
209+
if do_continue is True:
210+
args += ["--continue"]
202211

203212
dolt.execute(args + [fname])
204213

tests/test_write.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

3-
from doltcli import CREATE, read_rows, write_columns, write_rows
4-
from tests.helpers import compare_rows_helper
3+
from doltcli import CREATE, read_rows, write_columns, write_rows, DoltException, write_file
4+
from tests.helpers import compare_rows_helper, write_dict_to_csv
55

66
# Note that we use string values here as serializing via CSV does preserve type information in any meaningful way
77
TEST_ROWS = [
@@ -45,3 +45,32 @@ def test_write_columns_uneven(init_empty_test_repo):
4545
repo = init_empty_test_repo
4646
with pytest.raises(ValueError):
4747
write_columns(repo, "players", DICT_OF_LISTS_UNEVEN_LENGTHS, CREATE, ["name"])
48+
49+
50+
def test_write_file(init_empty_test_repo, tmp_path):
51+
tempfile = tmp_path / "test.csv"
52+
TEST_ROWS = [
53+
{"name": "Anna", "adjective": "tragic", "id": "1", "date_of_death": "1877-01-01"},
54+
{"name": "Vronksy", "adjective": "honorable", "id": "2", "date_of_death": ""},
55+
{"name": "Vronksy", "adjective": "honorable", "id": "2", "date_of_death": ""},
56+
]
57+
write_dict_to_csv(TEST_ROWS, tempfile)
58+
dolt = init_empty_test_repo
59+
with pytest.raises(DoltException):
60+
write_file(
61+
dolt=dolt,
62+
table="characters",
63+
file_handle=open(tempfile),
64+
import_mode=CREATE,
65+
primary_key=["id"],
66+
)
67+
write_file(
68+
dolt=dolt,
69+
table="characters",
70+
file_handle=open(tempfile),
71+
import_mode=CREATE,
72+
primary_key=["id"],
73+
do_continue=True,
74+
)
75+
actual = read_rows(dolt, "characters")
76+
compare_rows_helper(TEST_ROWS[:2], actual)

0 commit comments

Comments
 (0)