Skip to content

Commit

Permalink
Add ipython profile to query the database (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
moisses89 authored Jan 13, 2025
1 parent 0e469d4 commit 4156b22
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,19 @@ To create a new migration based on changes made to the model code, run the follo
alembic revision --autogenerate -m "MIGRATION TITLE"
```

### Querying the database via Python Shell in Docker
To open an interactive Python shell within a Docker container and query the database, use the following command:
```
docker exec -it safe-decoder-service-web-1 python -m IPython -i ./scripts/db_profile.py
```
Example usage:
```
In [11]: contracts = await Contract.get_all(session)
In [12]: contracts[0].address
Out[12]: b'J\xdb\xaa\xc7\xbc#\x9e%\x19\xcb\xfd#\x97\xe0\xf7Z\x1d\xe3U\xc8'
```

## Contributors
[See contributors](https://github.com/safe-global/safe-decoder-service/graphs/contributors)
1 change: 1 addition & 0 deletions docker/web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ RUN set -ex \
&& apt-get install -y --no-install-recommends $buildDeps \
&& pip install -U --no-cache-dir wheel setuptools pip \
&& pip install --no-cache-dir -r requirements.txt \
&& pip install --no-cache-dir ipython \
&& apt-get purge -y --auto-remove $buildDeps \
&& rm -rf /var/lib/apt/lists/* \
&& find /usr/local \
Expand Down
28 changes: 28 additions & 0 deletions scripts/db_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Ipython profile to enable on startup database interactions
"""

import asyncio

from sqlmodel.ext.asyncio.session import AsyncSession

from app.datasources.db.database import get_engine
from app.datasources.db.models import * # noqa: F401, F403

session: AsyncSession | None = None


async def restore_session():
"""
Close default session an open a new one.
:return:
"""
global session
if session:
await session.close()

session = AsyncSession(get_engine(), expire_on_commit=False)


asyncio.run(restore_session())
Empty file removed scripts/fill_db.py
Empty file.

0 comments on commit 4156b22

Please sign in to comment.