Skip to content

Commit

Permalink
Add storage mode info (#309)
Browse files Browse the repository at this point in the history
* Add storage mode info

* Update mg version

* Add integration test for storage mode switch

* black
  • Loading branch information
katarinasupe authored Jul 10, 2024
1 parent d15aca1 commit 2b8e32a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
20 changes: 20 additions & 0 deletions gqlalchemy/vendors/memgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from enum import Enum
import os
import sqlite3
from typing import List, Optional, Union
Expand Down Expand Up @@ -69,6 +70,15 @@ class MemgraphConstants:
UNIQUE = "unique"


class MemgraphStorageMode(Enum):
IN_MEMORY_TRANSACTIONAL = "IN_MEMORY_TRANSACTIONAL"
IN_MEMORY_ANALYTICAL = "IN_MEMORY_ANALYTICAL"
ON_DISK_TRANSACTIONAL = "ON_DISK_TRANSACTIONAL"

def __str__(self):
return self.value


def create_transaction(transaction_data) -> MemgraphTransaction:
"""Create a MemgraphTransaction object from transaction data.
Args:
Expand Down Expand Up @@ -481,6 +491,16 @@ def with_power_bi(self) -> "Memgraph":

return self.add_query_module(file_path=file_path, module_name=module_name)

def get_storage_mode(self) -> str:
"""Returns the storage mode of the Memgraph instance."""
result = self.execute_and_fetch("SHOW STORAGE INFO;")
storage_mode_value = next((item["value"] for item in result if item["storage info"] == "storage_mode"), None)
return MemgraphStorageMode(storage_mode_value).value

def set_storage_mode(self, storage_mode: MemgraphStorageMode) -> None:
"""Sets the storage mode of the Memgraph instance."""
self.execute(f"STORAGE MODE {storage_mode};")

def get_transactions(self) -> List[MemgraphTransaction]:
"""Get all transactions in the database.
Returns:
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ def memgraph() -> Memgraph:
memgraph.ensure_indexes([])
memgraph.ensure_constraints([])
memgraph.drop_database()
memgraph.set_storage_mode("IN_MEMORY_TRANSACTIONAL")

yield memgraph

memgraph.ensure_indexes([])
memgraph.ensure_constraints([])
memgraph.set_storage_mode("IN_MEMORY_TRANSACTIONAL")


@pytest.fixture
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/test_storage_modes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest
from gqlalchemy.exceptions import GQLAlchemyDatabaseError
from gqlalchemy import create


def test_switch_to_on_disk(memgraph):
create().node(labels="Person", name="Leslie").execute()

with pytest.raises(GQLAlchemyDatabaseError):
memgraph.set_storage_mode("ON_DISK_TRANSACTIONAL")
7 changes: 7 additions & 0 deletions tests/ogm/test_storage_modes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def test_get_storage_mode(memgraph):
assert memgraph.get_storage_mode() == "IN_MEMORY_TRANSACTIONAL"


def test_set_storage_mode(memgraph):
memgraph.set_storage_mode("IN_MEMORY_ANALYTICAL")
assert memgraph.get_storage_mode() == "IN_MEMORY_ANALYTICAL"

0 comments on commit 2b8e32a

Please sign in to comment.