Skip to content

Optional file storage for data persistence#881

Open
ikondov wants to merge 7 commits into
mongomock:developfrom
ikondov:persistence
Open

Optional file storage for data persistence#881
ikondov wants to merge 7 commits into
mongomock:developfrom
ikondov:persistence

Conversation

@ikondov

@ikondov ikondov commented Feb 19, 2024

Copy link
Copy Markdown

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

ikondov commented Feb 26, 2024

Copy link
Copy Markdown
Author

@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.

1 participant