Skip to content

Commit

Permalink
Update ORM and improve conn handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Berhent committed Aug 24, 2022
1 parent aab251e commit 3e8a269
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 67 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,22 @@
# Changelog

## [2.1.0] - 2022-08-24

**BREAKING**
Date times need to be updated do datetime aware objects in the database.

In postgres, you can run the following migration:

```
alter table blocks alter column created_at type timestamptz;
alter table accounts alter column created_at type timestamptz;
alter table adhoc_accounts alter column created_at type timestamptz;
alter table wallets alter column created_at type timestamptz;
```

- Update Tortoise ORM
- Improve connection handling

## [2.0.2] - 2022-08-15

- Support blockAward in BoomPoW v2
Expand Down
3 changes: 2 additions & 1 deletion kubernetes/banano/deployment.yaml
Expand Up @@ -15,7 +15,8 @@ spec:
spec:
containers:
- name: pippin-banano
image: bananocoin/pippin:2.0.1
image: bananocoin/pippin:2.1.0
imagePullPolicy: Always
resources:
requests:
cpu: 100m
Expand Down
3 changes: 2 additions & 1 deletion kubernetes/nano/deployment.yaml
Expand Up @@ -15,7 +15,8 @@ spec:
spec:
containers:
- name: pippin-nano
image: bananocoin/pippin:2.0.1
image: bananocoin/pippin:2.1.0
imagePullPolicy: Always
resources:
requests:
cpu: 500m
Expand Down
3 changes: 2 additions & 1 deletion kubernetes/wban/deployment.yaml
Expand Up @@ -15,7 +15,8 @@ spec:
spec:
containers:
- name: pippin-banano
image: bananocoin/pippin:2.0.1
image: bananocoin/pippin:2.1.0
imagePullPolicy: Always
resources:
requests:
cpu: 100m
Expand Down
5 changes: 4 additions & 1 deletion pippin/db/models/block.py
Expand Up @@ -15,4 +15,7 @@ class Block(Model):

class Meta:
table = 'blocks'
unique_together = ('account', 'send_id')
unique_together = ('account', 'send_id')



23 changes: 17 additions & 6 deletions pippin/db/tortoise_config.py
Expand Up @@ -3,11 +3,14 @@
from tortoise import Tortoise
from pippin.util.utils import Utils
import pathlib
from tortoise.contrib.aiohttp import register_tortoise


class DBConfig(object):
def __init__(self, mock = False):
def __init__(self, mock=False):
self.logger = logging.getLogger()
self.modules = {'db': ['pippin.db.models.wallet', 'pippin.db.models.account', 'pippin.db.models.adhoc_account', 'pippin.db.models.block']}
self.modules = {'db': ['pippin.db.models.wallet', 'pippin.db.models.account',
'pippin.db.models.adhoc_account', 'pippin.db.models.block']}
self.mock = mock
if self.mock:
self.use_postgres = False
Expand All @@ -23,7 +26,8 @@ def __init__(self, mock = False):
if self.postgres_db is not None and self.postgres_user is not None and self.postgres_password is not None:
self.use_postgres = True
elif self.postgres_db is not None or self.postgres_user is not None or self.postgres_password is not None:
raise Exception("ERROR: Postgres is not properly configured. POSTGRES_DB, POSTGRES_USER, and POSTGRES_PASSWORD environment variables are all required.")
raise Exception(
"ERROR: Postgres is not properly configured. POSTGRES_DB, POSTGRES_USER, and POSTGRES_PASSWORD environment variables are all required.")
# MySQL
self.use_mysql = False
if not self.use_postgres:
Expand All @@ -35,7 +39,13 @@ def __init__(self, mock = False):
if self.mysql_db is not None and self.mysql_user is not None and self.mysql_password is not None:
self.use_mysql = True
elif self.mysql_db is not None or self.mysql_user is not None or self.mysql_password is not None:
raise Exception("ERROR: Postgres is not properly configured. MYSQL_DB, MYSQL_USER, and MYSQL_PASSWORD environment variables are all required.")
raise Exception(
"ERROR: Postgres is not properly configured. MYSQL_DB, MYSQL_USER, and MYSQL_PASSWORD environment variables are all required.")

def init_db_aiohttp(self, app):
register_tortoise(app, db_url=f'postgres://{self.postgres_user}:{self.postgres_password}@{self.postgres_host}:{self.postgres_port}/{self.postgres_db}',
modules=self.modules,
generate_schemas=True)

async def init_db(self):
if self.use_postgres:
Expand All @@ -52,10 +62,11 @@ async def init_db(self):
)
else:
self.logger.info(f"Using SQLite database pippin.db")
dbpath = Utils.get_project_root().joinpath(pathlib.PurePath('pippin.db')) if not self.mock else Utils.get_project_root().joinpath(pathlib.PurePath('mock.db'))
dbpath = Utils.get_project_root().joinpath(pathlib.PurePath('pippin.db')
) if not self.mock else Utils.get_project_root().joinpath(pathlib.PurePath('mock.db'))
await Tortoise.init(
db_url=f'sqlite://{dbpath}',
modules=self.modules
)
# Create tables
await Tortoise.generate_schemas(safe=True)
await Tortoise.generate_schemas(safe=True)
3 changes: 0 additions & 3 deletions pippin/main.py
Expand Up @@ -18,7 +18,6 @@
from aiohttp import web, log
from pippin.db.redis import RedisDB
from tortoise import Tortoise
from pippin.db.tortoise_config import DBConfig
from pippin.config import Config
from logging.handlers import TimedRotatingFileHandler, WatchedFileHandler
from pippin.network.rpc_client import RPCClient
Expand Down Expand Up @@ -75,7 +74,6 @@ def main():
try:
# Initialize database first
log.server_logger.info("Initializing database")
loop.run_until_complete(DBConfig().init_db())
if os.getenv('BPOW_KEY', None) is not None:
log.server_logger.info(
"💥 Using BoomPOW For Work Generation")
Expand Down Expand Up @@ -112,7 +110,6 @@ def main():
RedisDB.close(),
WorkClient.close(),
NanoUtil.close(),
Tortoise.close_connections()
]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
Expand Down

0 comments on commit 3e8a269

Please sign in to comment.