MongoEngine / MongoEngine/mongoengine

GridFS cleanups

Open
#18 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

GridFS
Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

FileFields dont clean up when deleted and having the same default collection name makes drop_collection difficult.

Refs: hmarr/mongoengine#495 hmarr/mongoengine#496

Implement cleanup for:

import tempfile
import unittest
from mongoengine import *

import pymongo, gridfs

class GridFS(unittest.TestCase):


    def test_file_delete_cleanup(self):
        """Ensure that the gridfs file is deleted when a document
        with a GridFSProxied Field is deleted"""


        class RecursiveObject(EmbeddedDocument):
            obj = EmbeddedDocumentField('self')
            file = FileField()

        class TestFile(Document):
            recursive_obj = EmbeddedDocumentField(RecursiveObject)

        TestFile.drop_collection()

        def _create_testfile():
            testfile = TestFile(recursive_obj=RecursiveObject(obj=RecursiveObject()))
            testfile.recursive_obj.file.put('Hello, World!')
            testfile.recursive_obj.obj.file.put('MongoEngine')
            testfile.save()
            return testfile

        def _assert(testfile):
            testfile_grid_id = testfile.recursive_obj.file.grid_id
            testfile_fs = testfile.recursive_obj.file.fs

            testfile_grid_id_2 = testfile.recursive_obj.obj.file.grid_id
            testfile_fs_2 = testfile.recursive_obj.obj.file.fs

            self.assertFalse(testfile_fs.exists(testfile_grid_id))
            self.assertFalse(testfile_fs_2.exists(testfile_grid_id_2))

        # Test document.delete()
        testfile = _create_testfile()
        testfile.delete()
        _assert(testfile)

        # Test Queryset delete()
        testfile = _create_testfile()
        TestFile.objects().delete()
        _assert(testfile)

        # Test drop_collection
        testfile = _create_testfile()
        TestFile.drop_collection()
        _assert(testfile)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the FileField and GridFS deletion paths described in the issue, then reproduce the supplied unittest scenario. Verify document.delete(), queryset delete(), and drop_collection() each remove GridFS files from nested FileFields, as checked by the test's fs.exists assertions.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.