mem0-history-db-patch


Namemem0-history-db-patch JSON
Version 1.0.3 PyPI version JSON
download
home_pageNone
SummarySQLAlchemy-based database backend patch for mem0 memory storage
upload_time2025-08-02 08:00:48
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT
keywords mem0 sqlite postgres sqlalchemy patch memory
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # mem0-history-db-patch

This patch replaces the default SQLite-only memory storage layer in the Python SDK of [`mem0`](https://github.com/mem0ai/mem0) with a flexible, SQLAlchemy-based implementation that supports other databases β€” including PostgreSQL, Supabase, and more.
![](imgs/postgres_stored_memory.png)
![](imgs/sqlite3_stored_memory.png)
> πŸ”§ **Why?**  
The official Node.js SDK for `mem0` already supports robust production databases (like Postgres and Supabase) for the history store. However, the Python SDK is currently limited to SQLite β€” which:
- Only allows one writer at a time (`threading.Lock()` is used to compensate),
- Is not efficient or scalable for concurrent, multi-user deployments,
- Lacks native support for migrations or backend flexibility.

This patch fixes that.
(also reduces lines of code from 217l to 145l while being more robust, more readable and supporting more DB Providers)
---

## ✨ What This Patch Does

- Replaces raw SQLite logic with SQLAlchemy-backed ORM and connection management.
- Supports multiple SQL backends:
  - PostgreSQL
  - Supabase (PostgreSQL-compatible)
  - SQLite (still works)
  - Any other SQLAlchemy-compatible engine
- Maintains the **original class and method interfaces** (`SQLiteManager`) for drop-in compatibility.
- Automatically handles:
  - UUID primary key generation
  - Timestamp fields (`created_at`, `updated_at`)
  - Thread-safe session access (lock only used with SQLite)

---

## βš™οΈ Installation

Install the patch:

```bash
pip install mem0-history-db-patch
````

This overrides `mem0.memory.storage` with your patched version while keeping all public interfaces unchanged.

---

## πŸ§ͺ Usage

You don't need to change how you use `mem0`. Just point it at a database of your choice:

```python
from mem0.memory.storage import SQLiteManager  # Still called that for compatibility

db = SQLiteManager(db_url="postgresql://user:pass@localhost:5432/mem0")  # this db url is what you would insert into your history_db_path config in mem0
```

For SQLite (default, still supported):

```python
db = SQLiteManager()  # Uses sqlite:///:memory: by default
```

---

## πŸ“¦ Migration Notes

At this time, the automatic migration logic is **disabled**.

Why?

* If you're switching to a new backend (e.g., PostgreSQL), you're probably starting fresh.
* Adding migrations would increase patch complexity and risk breaking assumptions.

That said:

* If you need to preserve your SQLite data, you'll need to migrate it manually (e.g., by dumping to CSV and reloading).
* If you're starting with a new database, just set your `db_url` and go.

> Migration tooling may be added in the future if needed.

---

## πŸ“Œ Technical Notes

* All history records are stored in the same schema as before.
* `id` is now a UUID string (auto-generated by SQLAlchemy).
* `created_at` and `updated_at` use database time (`func.now()`).
* For SQLite, `threading.Lock()` is still used to prevent concurrent write issues.
* For other backends (like Postgres), locking is bypassed for performance.

---

## πŸ“¬ Example Postgres Connection String

```python
SQLiteManager(db_url="postgresql+psycopg2://username:password@localhost:5432/mydb")
```

Make sure `psycopg2` is installed:

```bash
pip install psycopg2-binary
```

---

## πŸ“š Future Enhancements

* Optional: Migration from existing SQLite DBs
* Optional: Rename `SQLiteManager` internally (if `mem0` adopts the patch upstream)
* Optional: Add tests for concurrency and Postgres behavior

---

## πŸ“ License

MIT β€” do whatever you want with it, just don’t blame us if your DB gets angry.

---

## πŸ‘€ Author

Maintained by [@nocyphr](https://github.com/nocyphr)

Feedback, issues, and PRs welcome.


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "mem0-history-db-patch",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "Nocyphr <sgu@nocyphr.com>",
    "keywords": "mem0, sqlite, postgres, sqlalchemy, patch, memory",
    "author": null,
    "author_email": "Nocyphr <sgu@nocyphr.com>",
    "download_url": "https://files.pythonhosted.org/packages/14/d3/34d5777fe07ffdb6a2d7b441768400dbe25686e3b8d4d7f19e0ac8e39c8a/mem0_history_db_patch-1.0.3.tar.gz",
    "platform": null,
    "description": "# mem0-history-db-patch\n\nThis patch replaces the default SQLite-only memory storage layer in the Python SDK of [`mem0`](https://github.com/mem0ai/mem0) with a flexible, SQLAlchemy-based implementation that supports other databases \u2014 including PostgreSQL, Supabase, and more.\n![](imgs/postgres_stored_memory.png)\n![](imgs/sqlite3_stored_memory.png)\n> \ud83d\udd27 **Why?**  \nThe official Node.js SDK for `mem0` already supports robust production databases (like Postgres and Supabase) for the history store. However, the Python SDK is currently limited to SQLite \u2014 which:\n- Only allows one writer at a time (`threading.Lock()` is used to compensate),\n- Is not efficient or scalable for concurrent, multi-user deployments,\n- Lacks native support for migrations or backend flexibility.\n\nThis patch fixes that.\n(also reduces lines of code from 217l to 145l while being more robust, more readable and supporting more DB Providers)\n---\n\n## \u2728 What This Patch Does\n\n- Replaces raw SQLite logic with SQLAlchemy-backed ORM and connection management.\n- Supports multiple SQL backends:\n  - PostgreSQL\n  - Supabase (PostgreSQL-compatible)\n  - SQLite (still works)\n  - Any other SQLAlchemy-compatible engine\n- Maintains the **original class and method interfaces** (`SQLiteManager`) for drop-in compatibility.\n- Automatically handles:\n  - UUID primary key generation\n  - Timestamp fields (`created_at`, `updated_at`)\n  - Thread-safe session access (lock only used with SQLite)\n\n---\n\n## \u2699\ufe0f Installation\n\nInstall the patch:\n\n```bash\npip install mem0-history-db-patch\n````\n\nThis overrides `mem0.memory.storage` with your patched version while keeping all public interfaces unchanged.\n\n---\n\n## \ud83e\uddea Usage\n\nYou don't need to change how you use `mem0`. Just point it at a database of your choice:\n\n```python\nfrom mem0.memory.storage import SQLiteManager  # Still called that for compatibility\n\ndb = SQLiteManager(db_url=\"postgresql://user:pass@localhost:5432/mem0\")  # this db url is what you would insert into your history_db_path config in mem0\n```\n\nFor SQLite (default, still supported):\n\n```python\ndb = SQLiteManager()  # Uses sqlite:///:memory: by default\n```\n\n---\n\n## \ud83d\udce6 Migration Notes\n\nAt this time, the automatic migration logic is **disabled**.\n\nWhy?\n\n* If you're switching to a new backend (e.g., PostgreSQL), you're probably starting fresh.\n* Adding migrations would increase patch complexity and risk breaking assumptions.\n\nThat said:\n\n* If you need to preserve your SQLite data, you'll need to migrate it manually (e.g., by dumping to CSV and reloading).\n* If you're starting with a new database, just set your `db_url` and go.\n\n> Migration tooling may be added in the future if needed.\n\n---\n\n## \ud83d\udccc Technical Notes\n\n* All history records are stored in the same schema as before.\n* `id` is now a UUID string (auto-generated by SQLAlchemy).\n* `created_at` and `updated_at` use database time (`func.now()`).\n* For SQLite, `threading.Lock()` is still used to prevent concurrent write issues.\n* For other backends (like Postgres), locking is bypassed for performance.\n\n---\n\n## \ud83d\udcec Example Postgres Connection String\n\n```python\nSQLiteManager(db_url=\"postgresql+psycopg2://username:password@localhost:5432/mydb\")\n```\n\nMake sure `psycopg2` is installed:\n\n```bash\npip install psycopg2-binary\n```\n\n---\n\n## \ud83d\udcda Future Enhancements\n\n* Optional: Migration from existing SQLite DBs\n* Optional: Rename `SQLiteManager` internally (if `mem0` adopts the patch upstream)\n* Optional: Add tests for concurrency and Postgres behavior\n\n---\n\n## \ud83d\udcdd License\n\nMIT \u2014 do whatever you want with it, just don\u2019t blame us if your DB gets angry.\n\n---\n\n## \ud83d\udc64 Author\n\nMaintained by [@nocyphr](https://github.com/nocyphr)\n\nFeedback, issues, and PRs welcome.\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "SQLAlchemy-based database backend patch for mem0 memory storage",
    "version": "1.0.3",
    "project_urls": {
        "Homepage": "https://github.com/nocyphr/mem0-history-db-patch",
        "Repository": "https://github.com/nocyphr/mem0-history-db-patch"
    },
    "split_keywords": [
        "mem0",
        " sqlite",
        " postgres",
        " sqlalchemy",
        " patch",
        " memory"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "4b55dc1755d91342a746c22db8a9ec64e7b808e733ae2e85f5cd11b618be0d87",
                "md5": "fbb7fa125a74a4a51b5b26d7fbd02b35",
                "sha256": "45e622b6dd477da57291666fb93bc6499872daf19af9a21592a5988fc707f949"
            },
            "downloads": -1,
            "filename": "mem0_history_db_patch-1.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fbb7fa125a74a4a51b5b26d7fbd02b35",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 4627,
            "upload_time": "2025-08-02T08:00:47",
            "upload_time_iso_8601": "2025-08-02T08:00:47.356229Z",
            "url": "https://files.pythonhosted.org/packages/4b/55/dc1755d91342a746c22db8a9ec64e7b808e733ae2e85f5cd11b618be0d87/mem0_history_db_patch-1.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "14d334d5777fe07ffdb6a2d7b441768400dbe25686e3b8d4d7f19e0ac8e39c8a",
                "md5": "86e090f9a927b22b85b2dc73eefc38ea",
                "sha256": "849b8ebdc306ac4184678989b16ecb3dd0b1488fc7667131f843f246f990aeb6"
            },
            "downloads": -1,
            "filename": "mem0_history_db_patch-1.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "86e090f9a927b22b85b2dc73eefc38ea",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 4552,
            "upload_time": "2025-08-02T08:00:48",
            "upload_time_iso_8601": "2025-08-02T08:00:48.591850Z",
            "url": "https://files.pythonhosted.org/packages/14/d3/34d5777fe07ffdb6a2d7b441768400dbe25686e3b8d4d7f19e0ac8e39c8a/mem0_history_db_patch-1.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-02 08:00:48",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "nocyphr",
    "github_project": "mem0-history-db-patch",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "mem0-history-db-patch"
}
        
Elapsed time: 1.51564s