Skip to content

Commit 28394fb

Browse files
authored
Explicitly register asynpg dialect with GINO (#259)
1 parent 97e8ef7 commit 28394fb

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

app/application.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
from .gino import Gino, GinoEngine
1010
from .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+
1223
READ_ENGINE: Optional[GinoEngine] = None
1324
SessionLocal: Optional[Session] = None
1425
Base = declarative_base()
@@ -20,6 +31,7 @@
2031

2132
db = Gino(
2233
app,
34+
driver="asyncpg",
2335
host=GLOBALS.database_config.host,
2436
port=GLOBALS.database_config.port,
2537
user=GLOBALS.database_config.username,
@@ -41,8 +53,20 @@ def get_synchronous_db() -> Iterator[Session]:
4153
raise RuntimeError("No database url set.")
4254

4355
if SessionLocal is None:
44-
db_conn = GLOBALS.database_config.url
45-
engine = create_engine(db_conn, pool_size=5, max_overflow=0)
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)
4670
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
4771

4872
synchronous_db: Optional[Session] = None

app/gino.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
from starlette.exceptions import HTTPException
1818
from starlette.types import Receive, Scope, Send
1919

20+
# Explicitly import the asyncpg dialect to ensure it's registered with SQLAlchemy
21+
import gino.dialects.asyncpg # noqa: F401
22+
2023
logger = logging.getLogger("gino.ext.starlette")
2124

2225

app/settings/globals.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def set_database_config(cls, v, values, **kwarg) -> DatabaseURL:
134134
if db_reader_secret:
135135
secret = json.loads(db_reader_secret)
136136
v = DatabaseURL(
137-
drivername="postgresql+asyncpg",
137+
drivername="asyncpg",
138138
username=secret["username"],
139139
password=secret["password"],
140140
host=secret["host"],
@@ -143,7 +143,7 @@ def set_database_config(cls, v, values, **kwarg) -> DatabaseURL:
143143
)
144144
else:
145145
v = DatabaseURL(
146-
drivername="postgresql+asyncpg",
146+
drivername="asyncpg",
147147
username=input.get("reader_username"),
148148
password=input.get("reader_password"),
149149
host=input.get("reader_host"),

tests/run_tests.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
set -e
44

5+
# Clean Python cache to avoid stale bytecode issues
6+
echo "Cleaning Python cache..."
7+
find /app -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
8+
find /app -type f -name "*.pyc" -delete 2>/dev/null || true
9+
510
pushd /app/tests/terraform
611
terraform init && terraform plan && terraform apply -auto-approve
712
popd

0 commit comments

Comments
 (0)