diff --git a/dvc/state.py b/dvc/state.py index 3c978355b..15a4b0678 100644 --- a/dvc/state.py +++ b/dvc/state.py @@ -449,6 +449,7 @@ class State(StateBase): # pylint: disable=too-many-instance-attributes remove(path) unused.append(relpath) - for relpath in unused: - cmd = "DELETE FROM {} WHERE path = ?".format(self.LINK_STATE_TABLE) - self._execute(cmd, (relpath,)) + cmd = "DELETE FROM {} WHERE path IN ({})".format( + self.LINK_STATE_TABLE, ",".join(["?"] * len(unused)) + ) + self._execute(cmd, tuple(unused)) diff --git a/tests/func/test_state.py b/tests/func/test_state.py index edc2012bf..e3d914eeb 100644 --- a/tests/func/test_state.py +++ b/tests/func/test_state.py @@ -77,3 +77,21 @@ class TestGetStateRecordForInode(TestDvc): state.save(PathInfo(path), md5) ret = state.get_state_record_for_inode(inode) self.assertIsNotNone(ret) + + +def test_remove_unused_links(repo_dir, dvc_repo): + stages = dvc_repo.add(repo_dir.FOO) + assert len(stages) == 1 + + stages = dvc_repo.add(repo_dir.BAR) + assert len(stages) == 1 + + cmd_count_links = "SELECT count(*) FROM {}".format(State.LINK_STATE_TABLE) + with dvc_repo.state: + result = dvc_repo.state._execute(cmd_count_links).fetchone()[0] + assert result == 2 + + dvc_repo.state.remove_unused_links([]) + + result = dvc_repo.state._execute(cmd_count_links).fetchone()[0] + assert result == 0