-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refact: SettingManager for streamlined settings retrieval and toggling
- Loading branch information
Showing
9 changed files
with
114 additions
and
117 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Refactor settings table | ||
Revision ID: 4abf3adb8ab8 | ||
Revises: ab1ce3ef2a57 | ||
Create Date: 2024-11-08 02:42:49.391685 | ||
""" | ||
# pylint: disable=all | ||
|
||
from typing import Sequence, Union | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
# Revision identifiers, used by Alembic. | ||
revision: str = "4abf3adb8ab8" | ||
down_revision: Union[str, None] = "ab1ce3ef2a57" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
"""Refactor settings table by dropping the old table and creating a new structure.""" | ||
# Drop the old settings table | ||
op.drop_table("settings") # pylint: disable=no-member | ||
|
||
# Recreate the settings table with the new structure | ||
op.create_table( | ||
"settings", | ||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True), | ||
sa.Column("node_monitoring", sa.Boolean(), nullable=False), | ||
sa.Column("node_auto_restart", sa.Boolean(), nullable=False), | ||
) # pylint: disable=no-member | ||
|
||
|
||
def downgrade() -> None: | ||
"""Revert the settings table to its original structure.""" | ||
# Drop the new settings table | ||
op.drop_table("settings") # pylint: disable=no-member | ||
|
||
# Recreate the original settings table structure | ||
op.create_table( | ||
"settings", | ||
sa.Column("key", sa.VARCHAR(length=256), nullable=False), | ||
sa.Column("value", sa.VARCHAR(length=2048), nullable=True), | ||
sa.Column( | ||
"created_at", | ||
sa.DATETIME(), | ||
server_default=sa.text("(CURRENT_TIMESTAMP)"), | ||
nullable=False, | ||
), | ||
sa.Column("updated_at", sa.DATETIME(), nullable=True), | ||
) # pylint: disable=no-member |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,62 @@ | ||
""" | ||
This module provides functionality for managing settings in the application. | ||
It includes methods for upserting and retrieving settings from the database. | ||
This module provides functionality for managing application settings, | ||
including methods for updating and retrieving settings. | ||
""" | ||
|
||
from sqlalchemy.future import select | ||
from db.base import get_db | ||
from db.models import Setting | ||
from models import SettingData, SettingUpsert, SettingKeys | ||
from models import SettingKeys | ||
|
||
|
||
class SettingManager: | ||
"""Manager class for handling settings operations.""" | ||
"""Handles the application's settings management, ensuring a single settings record exists.""" | ||
|
||
@staticmethod | ||
async def upsert(setting_upsert: SettingUpsert) -> SettingData | None: | ||
async def get(field: SettingKeys) -> bool: | ||
""" | ||
Upsert a setting in the database. | ||
If the setting exists and the value is None, it will be deleted. | ||
If the setting does not exist, it will be created. | ||
Args: | ||
setting_upsert (SettingUpsert): The setting data to upsert. | ||
Returns: | ||
SettingData | None: The upserted setting data or None if deleted. | ||
Retrieve the specified setting field, ensuring the settings record exists. | ||
If the settings record does not exist, it will be created. | ||
""" | ||
async with get_db() as db: | ||
existing_setting = await db.execute( | ||
select(Setting).where(Setting.key == setting_upsert.key) | ||
) | ||
setting = existing_setting.scalar_one_or_none() | ||
# Attempt to retrieve the settings record | ||
result = await db.execute(select(Setting)) | ||
settings = result.scalar_one_or_none() | ||
|
||
if setting: | ||
if setting_upsert.value is None: | ||
await db.delete(setting) | ||
await db.commit() | ||
return None | ||
setting.value = setting_upsert.value | ||
else: | ||
if setting_upsert.value is not None: | ||
setting = Setting( | ||
key=setting_upsert.key, value=setting_upsert.value | ||
) | ||
db.add(setting) | ||
# If no settings record, create it | ||
if not settings: | ||
settings = Setting() | ||
db.add(settings) | ||
await db.commit() | ||
await db.refresh(settings) # Refresh after commit to access attributes | ||
|
||
await db.commit() | ||
await db.refresh(setting) | ||
return SettingData.from_orm(setting) | ||
# Return the value of the specified field | ||
return getattr(settings, field.value) | ||
|
||
@staticmethod | ||
async def get(key: SettingKeys) -> SettingData | None: | ||
async def toggle_field(field: SettingKeys) -> bool: | ||
""" | ||
Retrieve a setting by its key. | ||
Args: | ||
key (SettingKeys): The key of the setting to retrieve. | ||
Returns: | ||
SettingData | None: The retrieved setting data or None if not found. | ||
Toggle a boolean setting field and return its new value. | ||
Ensures the settings record exists before toggling. | ||
""" | ||
async with get_db() as db: | ||
result = await db.execute(select(Setting).where(Setting.key == key)) | ||
setting = result.scalar_one_or_none() | ||
return SettingData.from_orm(setting) if setting else None | ||
# Retrieve or create the settings record | ||
result = await db.execute(select(Setting)) | ||
settings = result.scalar_one_or_none() | ||
|
||
if not settings: | ||
settings = Setting() | ||
db.add(settings) | ||
await db.commit() | ||
await db.refresh(settings) | ||
|
||
# Toggle the field's current value | ||
current_value = getattr(settings, field.value) | ||
new_value = not current_value | ||
setattr(settings, field.value, new_value) | ||
|
||
# Commit the change | ||
db.add(settings) | ||
await db.commit() | ||
|
||
return new_value |
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
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