Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional file storage for data persistence #881

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

ikondov
Copy link

@ikondov ikondov commented Feb 19, 2024

The persistence topic has been already addressed in issues #350 and #406. We have another use case where the tests of the command line interface require saving the database and making it available across several processes. Therefore, some temporary file storage is necessary.

This can be accomplished by a small extension of the classes in store.py. The file store is not intended to keep database state instantly on disk. Rather the database is eventually dumped to a file just before the ServerStore object is destroyed.

It is a work in progress: TODO: a transparent method to pass the option to use a database file with a database file path. This can be accomplished e.g. by using an environment variable.

The persistence is activated either by setting the environment variable MONGOMOCK_SERVERSTORE_FILE to a non-empty JSON file (initialized with '{}'):

export MONGOMOCK_SERVERSTORE_FILE=/full/path/to/mongomock_file.json

or create a custom ServerStore object by passing the filename keyword argument:

from mongomock.store import ServerStore
from mongomock import MongoClient

mongo_client = MongoClient(_store=ServerStore(filename='/full/path/to/mongomock_file.json'))

@ikondov ikondov changed the title WIP: Optional file storage for data persistence Optional file storage for data persistence Feb 20, 2024
@ikondov ikondov marked this pull request as ready for review February 20, 2024 16:14
@ikondov
Copy link
Author

ikondov commented Feb 26, 2024

@pcorpet: Addressing your suggestion in #350, the Database API is not modified at all. Only the storage classes are extended with serialization methods and the ServerStore class with a file dump method. The methods to activate persistence are tested in the test module (see test__persistence.py). By default it is deactivated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant