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

Refactor settings.DATABASE and settings.DATABASE_DSN #46

Open
dolamroth opened this issue Apr 15, 2023 · 0 comments
Open

Refactor settings.DATABASE and settings.DATABASE_DSN #46

dolamroth opened this issue Apr 15, 2023 · 0 comments
Labels
bug Something isn't working
Milestone

Comments

@dolamroth
Copy link
Owner

dolamroth commented Apr 15, 2023

While at it, also move from initializing app with session maker in each command, but provide a global application object.

Redo settings.DATABASE -> settings.DATABASES and provide global variable connections, in the same manner as Django

ConnectionHandler is a wrapper around project sessionmaker(s). This will be a step for #21
https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html

# using example from "Custom Vertical Partitioning"

import random

from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.ext.asyncio import async_sessionmaker
from sqlalchemy.orm import Session

# construct async engines w/ async drivers
engines = {
    'leader':create_async_engine("sqlite+aiosqlite:///leader.db"),
    'other':create_async_engine("sqlite+aiosqlite:///other.db"),
    'follower1':create_async_engine("sqlite+aiosqlite:///follower1.db"),
    'follower2':create_async_engine("sqlite+aiosqlite:///follower2.db"),
}

class RoutingSession(Session):
    def get_bind(self, mapper=None, clause=None, **kw):
        # within get_bind(), return sync engines
        if mapper and issubclass(mapper.class_, MyOtherClass):
            return engines['other'].sync_engine
        elif self._flushing or isinstance(clause, (Update, Delete)):
            return engines['leader'].sync_engine
        else:
            return engines[
                random.choice(['follower1','follower2'])
            ].sync_engine

# apply to AsyncSession using sync_session_class
AsyncSessionMaker = async_sessionmaker(
    sync_session_class=RoutingSession
)
@dolamroth dolamroth added the bug Something isn't working label Apr 15, 2023
@dolamroth dolamroth added this to the 0.2 milestone Apr 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant