Skip to content

Commit 045789a

Browse files
committed
raising a ValueError if is_in or not_in query clauses are passed an empty list
Otherwise invalid SQL is generated.
1 parent 67bdba4 commit 045789a

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

piccolo/apps/migrations/commands/clean.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ def get_migration_ids_to_remove(self) -> t.List[str]:
2727
migration_module_dict=migration_module_dict
2828
)
2929

30-
migration_ids_to_remove = (
30+
query = (
3131
Migration.select(Migration.name)
3232
.where(Migration.app_name == self.app_name)
33-
.where(Migration.name.not_in(migration_ids))
3433
.output(as_list=True)
35-
.run_sync()
3634
)
35+
36+
if len(migration_ids) > 0:
37+
query = query.where(Migration.name.not_in(migration_ids))
38+
39+
migration_ids_to_remove = query.run_sync()
3740
return migration_ids_to_remove
3841

3942
def run(self):

piccolo/columns/base.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
)
2525
from piccolo.columns.combination import Where
2626
from piccolo.columns.defaults.base import Default
27-
from piccolo.custom_types import Iterable
2827
from piccolo.querystring import QueryString
2928
from piccolo.utils.warnings import colored_warning
3029

@@ -257,10 +256,18 @@ def _validate_default(
257256
f"{allowed_types}"
258257
)
259258

260-
def is_in(self, values: Iterable) -> Where:
259+
def is_in(self, values: t.List[t.Any]) -> Where:
260+
if len(values) == 0:
261+
raise ValueError(
262+
"The `values` list argument must contain at least one value."
263+
)
261264
return Where(column=self, values=values, operator=In)
262265

263-
def not_in(self, values: Iterable) -> Where:
266+
def not_in(self, values: t.List[t.Any]) -> Where:
267+
if len(values) == 0:
268+
raise ValueError(
269+
"The `values` list argument must contain at least one value."
270+
)
264271
return Where(column=self, values=values, operator=NotIn)
265272

266273
def like(self, value: str) -> Where:

tests/columns/test_combination.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ def test_is_in(self):
2424
sql = _where.__str__()
2525
self.assertEqual(sql, "band.name IN ('Pythonistas', 'Rustaceans')")
2626

27+
with self.assertRaises(ValueError):
28+
Band.name.is_in([])
29+
2730
def test_not_in(self):
2831
_where = Band.name.not_in(["CSharps"])
2932
sql = _where.__str__()
3033
self.assertEqual(sql, "band.name NOT IN ('CSharps')")
34+
35+
with self.assertRaises(ValueError):
36+
Band.name.not_in([])

0 commit comments

Comments
 (0)