Skip to content

Commit

Permalink
cleaning files for Array columns (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed Aug 18, 2022
1 parent ef105f7 commit af4b361
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
7 changes: 6 additions & 1 deletion piccolo_api/media/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import abc
import asyncio
import itertools
import logging
import pathlib
import string
Expand Down Expand Up @@ -259,7 +260,11 @@ async def get_file_keys_from_db(self) -> t.List[str]:
Returns the file key for each file we have in the database.
"""
table = self.column._meta.table
return await table.select(self.column).output(as_list=True)
response = await table.select(self.column).output(as_list=True)
if isinstance(self.column, Array):
return [i for i in itertools.chain(*response)]
else:
return response

async def get_unused_file_keys(self) -> t.List[str]:
"""
Expand Down
19 changes: 18 additions & 1 deletion tests/media/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def setUp(self):
def tearDown(self):
Movie.alter().drop_table().run_sync()

def test_get_file_keys_from_db(self):
def test_varchar(self):
Movie.insert(
Movie(poster="image-1.jpg"),
Movie(poster="image-2.jpg"),
Expand All @@ -153,6 +153,23 @@ def test_get_file_keys_from_db(self):
sorted(response), ["image-1.jpg", "image-2.jpg", "image-3.jpg"]
)

def test_array(self):
Movie.insert(
Movie(screenshots=["image-1.jpg", "image-2.jpg"]),
Movie(screenshots=["image-3.jpg", "image-4.jpg"]),
).run_sync()

storage = LocalMediaStorage(
column=Movie.screenshots, media_path="/tmp/"
)

response = asyncio.run(storage.get_file_keys_from_db())

self.assertListEqual(
sorted(response),
["image-1.jpg", "image-2.jpg", "image-3.jpg", "image-4.jpg"],
)


class TestDeleteUnusedFiles(TestCase):
def setUp(self):
Expand Down

0 comments on commit af4b361

Please sign in to comment.