Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add migrations #71

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions aerich.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[aerich]
tortoise_orm = app.config.TORTOISE_ORM
location = migrations

31 changes: 31 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,36 @@
DB_PORT = os.getenv("DB_PORT")
DB_PATH = os.getenv("DB_PATH", default=app_dir / "db_data" / "karma.db")


def get_db_connect_string():
if DB_TYPE == 'mysql':
db_url = (
f'{DB_TYPE}://{LOGIN_DB}:{PASSWORD_DB}'
f'@{DB_HOST}:{DB_PORT}/{DB_NAME}'
)
elif DB_TYPE == 'postgres':
db_url = (
f'{DB_TYPE}://{LOGIN_DB}:{PASSWORD_DB}'
f'@{DB_HOST}:{DB_PORT}/{DB_NAME}'
)
elif DB_TYPE == 'sqlite':
db_url = (
f'{DB_TYPE}://{DB_PATH}'
)
else:
raise ValueError("DB_TYPE not mysql, sqlite or postgres")
return db_url


TORTOISE_ORM = {
"connections": {"default": get_db_connect_string()},
"apps": {
"models": {
"models": ["app.models", "aerich.models"],
"default_connection": "default",
},
},
}

API_ID = os.getenv("API_ID", default=6)
API_HASH = os.getenv("API_HASH", default='eb06d4abfb49dc3eeb1aeb98ae0f581e')
12 changes: 6 additions & 6 deletions app/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from .chat import Chat, ChatType
from .user import User
from .user_karma import UserKarma
from .karma_actions import KarmaEvent
from .moderator_actions import ModeratorEvent
from app.models.user import User
from app.models.chat import Chat, ChatType
from app.models.user_karma import UserKarma
from app.models.karma_actions import KarmaEvent
from app.models.moderator_actions import ModeratorEvent

__all__ = [Chat, ChatType, User, UserKarma, KarmaEvent]
__all__ = [User, Chat, ChatType, UserKarma, KarmaEvent, ModeratorEvent]

2 changes: 1 addition & 1 deletion app/models/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from tortoise.transactions import in_transaction

from app.utils.exceptions import NotHaveNeighbours
from .db import karma_filters
from app.models.db import karma_filters


SQL_PREV_NEXT = """
Expand Down
31 changes: 3 additions & 28 deletions app/models/db.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
from aiogram import Dispatcher
from aiogram.utils.executor import Executor
from loguru import logger
from tortoise import Tortoise, run_async

from app import config
__models__ = [
'app.models.user',
'app.models.chat',
'app.models.user_karma',
'app.models.karma_actions',
'app.models.moderator_actions',
]
karma_filters = ("-karma", "uc_id")


Expand All @@ -18,27 +12,8 @@ async def on_startup(_: Dispatcher):


async def db_init():
if config.DB_TYPE == 'mysql':
db_url = (
f'{config.DB_TYPE}://{config.LOGIN_DB}:{config.PASSWORD_DB}'
f'@{config.DB_HOST}:{config.DB_PORT}/{config.DB_NAME}'
)
elif config.DB_TYPE == 'postgres':
db_url = (
f'{config.DB_TYPE}://{config.LOGIN_DB}:{config.PASSWORD_DB}'
f'@{config.DB_HOST}:{config.DB_PORT}/{config.DB_NAME}'
)
elif config.DB_TYPE == 'sqlite':
db_url = (
f'{config.DB_TYPE}://{config.DB_PATH}'
)
else:
raise ValueError("DB_TYPE not mysql, sqlite or postgres")

await Tortoise.init(
db_url=db_url,
modules={'models': __models__}
)
await Tortoise.init(config=config.TORTOISE_ORM)
logger.info("database configured")


async def on_shutdown(_: Dispatcher):
Expand Down
4 changes: 2 additions & 2 deletions app/models/karma_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from tortoise import fields
from tortoise.models import Model

from .user import User
from .chat import Chat
from app.models.user import User
from app.models.chat import Chat
from app import config


Expand Down
4 changes: 2 additions & 2 deletions app/models/moderator_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from tortoise import fields
from tortoise.models import Model

from .user import User
from .chat import Chat
from app.models.user import User
from app.models.chat import Chat

from app import config
from app.utils.timedelta_functions import format_timedelta
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from tortoise.models import Model

from app.utils.exceptions import UserWithoutUserIdError
from .chat import Chat
from app.models.chat import Chat


class User(Model):
Expand Down
6 changes: 3 additions & 3 deletions app/models/user_karma.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from tortoise.models import Model

from app.utils.exceptions import SubZeroKarma
from .chat import Chat
from .user import User
from .db import karma_filters
from app.models.chat import Chat
from app.models.user import User
from app.models.db import karma_filters


class UserKarma(Model):
Expand Down
3 changes: 3 additions & 0 deletions migrations/aerich.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from app.config import TORTOISE_ORM

__all__ = (TORTOISE_ORM, )