-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- A few useful functions - Work with DB is refactored - Removed non-stable domain fixers - Removed non-necessary tree command `status`
- Loading branch information
1 parent
0019bf8
commit b556d36
Showing
10 changed files
with
232 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
sources/lib/db/alembic/versions/7b0c01070796_domain_fixers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"""domain_fixers | ||
Revision ID: 7b0c01070796 | ||
Revises: | ||
Create Date: 2024-06-08 00:20:08.625435 | ||
""" | ||
|
||
from typing import Sequence, Union | ||
from sqlalchemy.engine.reflection import Inspector | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '7b0c01070796' | ||
down_revision: Union[str, None] = None | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
conn = op.get_bind() | ||
inspector = Inspector.from_engine(conn) | ||
tables = inspector.get_table_names() | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
dropped_tables = ( | ||
'bot_options', | ||
'domain_fixers', | ||
) | ||
for dropped_table in dropped_tables: | ||
if dropped_table in tables: | ||
op.drop_table(dropped_table) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
'domain_fixers', | ||
sa.Column('original', sa.TEXT(), autoincrement=False, nullable=False), | ||
sa.Column('fixer', sa.TEXT(), autoincrement=False, nullable=False), | ||
sa.Column('enabled', sa.BOOLEAN(), autoincrement=False, nullable=False), | ||
sa.PrimaryKeyConstraint('original', name='domain_fixers_pkey'), | ||
) | ||
op.create_table( | ||
'bot_options', | ||
sa.Column('id', sa.BIGINT(), autoincrement=True, nullable=False), | ||
sa.Column('name', sa.TEXT(), autoincrement=False, nullable=False), | ||
sa.Column('value', sa.TEXT(), autoincrement=False, nullable=False), | ||
sa.PrimaryKeyConstraint('id', name='bot_options_pkey'), | ||
) | ||
# ### end Alembic commands ### |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
"""Base operations with any DB table""" | ||
|
||
from typing import ( | ||
Any, | ||
Type, | ||
) | ||
|
||
from sqlalchemy import select | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from sources.lib.db.models import Base | ||
|
||
|
||
async def get_db_entity( | ||
db_session: AsyncSession, | ||
table_class: Type[Base], | ||
**kwargs, | ||
): | ||
"""Get a DB entity by the params""" | ||
user = select(table_class).filter_by(**kwargs) | ||
return (await db_session.scalars(user)).one_or_none() | ||
|
||
|
||
async def create_db_entity(db_session: AsyncSession, table_class: Type[Base], **kwargs): | ||
"""Add a new DB entity""" | ||
db_entity = table_class( | ||
**kwargs, | ||
) | ||
db_session.add(db_entity) | ||
await db_session.commit() | ||
|
||
|
||
async def update_db_entity( | ||
db_session: AsyncSession, | ||
db_entity: Type[Base], | ||
**kwargs, | ||
): | ||
"""Update an existing DB entity""" | ||
for key, value in kwargs.items(): | ||
setattr(db_entity, key, value) | ||
db_session.add(db_entity) | ||
await db_session.commit() | ||
|
||
|
||
async def delete_db_entity( | ||
db_session: AsyncSession, | ||
db_entity: Type[Base], | ||
): | ||
"""Delete an existing DB entity""" | ||
await db_session.delete(db_entity) | ||
|
||
|
||
async def delete_db_entity_if_exists( | ||
db_session: AsyncSession, | ||
table_class: Type[Base], | ||
**kwargs, | ||
): | ||
"""Delete a DB entity if it exists""" | ||
db_entity = await get_db_entity( | ||
db_session=db_session, | ||
table_class=table_class, | ||
**kwargs, | ||
) | ||
if db_entity is not None: | ||
await delete_db_entity( | ||
db_session=db_session, | ||
db_entity=db_entity, | ||
) | ||
|
||
|
||
async def update_db_entity_or_create( | ||
db_session: AsyncSession, | ||
table_class: Type[Base], | ||
filters: dict[str, Any], | ||
updates: dict[str, Any], | ||
): | ||
"""Update a DB entity if it exists or create if it doesn't exist""" | ||
db_entity = await get_db_entity( | ||
db_session=db_session, | ||
table_class=table_class, | ||
**filters, | ||
) | ||
if db_entity is None: | ||
filters.update(updates) | ||
await create_db_entity( | ||
db_session=db_session, | ||
table_class=table_class, | ||
**filters, | ||
) | ||
else: | ||
await update_db_entity( | ||
db_session=db_session, | ||
db_entity=db_entity, | ||
**updates, | ||
) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.