99from .gino import Gino , GinoEngine
1010from .settings .globals import GLOBALS
1111
12- # Explicitly register the gino asyncpg dialect with SQLAlchemy
13- # This ensures it's available before any database connections are attempted
14- try :
15- from sqlalchemy .dialects import registry
16- import gino .dialects .asyncpg
17- registry .register ("asyncpg" , "gino.dialects.asyncpg" , "AsyncpgDialect" )
18- registry .register ("postgresql.asyncpg" , "gino.dialects.asyncpg" , "AsyncpgDialect" )
19- except Exception :
20- # If registration fails, the normal import should still work
21- pass
22-
2312READ_ENGINE : Optional [GinoEngine ] = None
2413SessionLocal : Optional [Session ] = None
2514Base = declarative_base ()
3120
3221db = Gino (
3322 app ,
34- driver = "asyncpg" ,
3523 host = GLOBALS .database_config .host ,
3624 port = GLOBALS .database_config .port ,
3725 user = GLOBALS .database_config .username ,
@@ -53,20 +41,8 @@ def get_synchronous_db() -> Iterator[Session]:
5341 raise RuntimeError ("No database url set." )
5442
5543 if SessionLocal is None :
56- # Create a synchronous database URL using psycopg2 instead of asyncpg
57- # asyncpg only works with async engines, not synchronous ones
58- from sqlalchemy .engine .url import URL
59-
60- db_config = GLOBALS .database_config
61- sync_url = URL (
62- drivername = "postgresql+psycopg2" ,
63- username = db_config .username ,
64- password = str (db_config .password ) if db_config .password else None ,
65- host = db_config .host ,
66- port = db_config .port ,
67- database = db_config .database ,
68- )
69- engine = create_engine (sync_url , pool_size = 5 , max_overflow = 0 )
44+ db_conn = GLOBALS .database_config .url
45+ engine = create_engine (db_conn , pool_size = 5 , max_overflow = 0 )
7046 SessionLocal = sessionmaker (autocommit = False , autoflush = False , bind = engine )
7147
7248 synchronous_db : Optional [Session ] = None
0 commit comments