diff --git a/.github/workflows/_container.yml b/.github/workflows/_container.yml index 7735d08..eb07873 100644 --- a/.github/workflows/_container.yml +++ b/.github/workflows/_container.yml @@ -37,7 +37,7 @@ jobs: - name: Build and export to Docker local cache uses: docker/build-push-action@v5 with: - build-args: DATABASE_URL=mysql+pymysql://root:rootpassword@localhost/ispyb_build + build-args: DATABASE_URL=mysql+aiomysql://root:rootpassword@localhost/ispyb_build context: . # Need load and tags so we can test it below load: true @@ -61,7 +61,7 @@ jobs: # This does not build the image again, it will find the image in the # Docker cache and publish it with: - build-args: DATABASE_URL=mysql+pymysql://root:rootpassword@localhost/ispyb_build + build-args: DATABASE_URL=mysql+aiomysql://root:rootpassword@localhost/ispyb_build context: . push: true tags: ${{ steps.meta.outputs.tags }} diff --git a/.github/workflows/_dist.yml b/.github/workflows/_dist.yml index 0941876..9db1152 100644 --- a/.github/workflows/_dist.yml +++ b/.github/workflows/_dist.yml @@ -14,7 +14,7 @@ jobs: options: > --health-cmd "/usr/local/bin/healthcheck.sh --defaults-file=/ispyb/.my.cnf --connect" env: - DATABASE_URL: mysql+pymysql://root:rootpassword@localhost/ispyb_build + DATABASE_URL: mysql+aiomysql://root:rootpassword@localhost/ispyb_build steps: - name: Checkout uses: actions/checkout@v4 diff --git a/.github/workflows/_schema.yml b/.github/workflows/_schema.yml index 0b9dea2..c07c2b6 100644 --- a/.github/workflows/_schema.yml +++ b/.github/workflows/_schema.yml @@ -17,7 +17,7 @@ jobs: options: > --health-cmd "/usr/local/bin/healthcheck.sh --defaults-file=/ispyb/.my.cnf --connect" env: - DATABASE_URL: mysql+pymysql://root:rootpassword@localhost/ispyb_build + DATABASE_URL: mysql+aiomysql://root:rootpassword@localhost/ispyb_build steps: - name: Checkout uses: actions/checkout@v4 diff --git a/.github/workflows/_test.yml b/.github/workflows/_test.yml index 93eda2a..4ac692a 100644 --- a/.github/workflows/_test.yml +++ b/.github/workflows/_test.yml @@ -26,7 +26,7 @@ jobs: options: > --health-cmd "/usr/local/bin/healthcheck.sh --defaults-file=/ispyb/.my.cnf --connect" env: - DATABASE_URL: mysql+pymysql://root:rootpassword@localhost/ispyb_build + DATABASE_URL: mysql+aiomysql://root:rootpassword@localhost/ispyb_build steps: - name: Checkout uses: actions/checkout@v4 diff --git a/.github/workflows/_tox.yml b/.github/workflows/_tox.yml index e5ad358..d800ecf 100644 --- a/.github/workflows/_tox.yml +++ b/.github/workflows/_tox.yml @@ -20,7 +20,7 @@ jobs: options: > --health-cmd "/usr/local/bin/healthcheck.sh --defaults-file=/ispyb/.my.cnf --connect" env: - DATABASE_URL: mysql+pymysql://root:rootpassword@localhost/ispyb_build + DATABASE_URL: mysql+aiomysql://root:rootpassword@localhost/ispyb_build steps: - name: Checkout uses: actions/checkout@v4 diff --git a/pyproject.toml b/pyproject.toml index 6300f83..68ec502 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ requires = [ "setuptools_scm[toml]>=6.2", "sqlacodegen-v2", "sqlalchemy", - "pymysql", + "aiomysql", ] build-backend = "setuptools.build_meta" @@ -25,7 +25,7 @@ dependencies = [ "uvicorn", "starlette", "strawberry-graphql[asgi]", - "sqlalchemy", + "sqlalchemy[asyncio]", "aiomysql", "opentelemetry-api", "opentelemetry-sdk", diff --git a/setup.py b/setup.py index 3e7760c..7c7d58e 100644 --- a/setup.py +++ b/setup.py @@ -1,14 +1,26 @@ +import asyncio import os import setuptools from sqlacodegen_v2.generators import DeclarativeGenerator -from sqlalchemy import MetaData, create_engine +from sqlalchemy import MetaData +from sqlalchemy.ext.asyncio import create_async_engine -if __name__ == "__main__": - engine = create_engine(os.environ["DATABASE_URL"]) + +async def generate_models(): + engine = create_async_engine(os.environ["DATABASE_URL"]) metadata = MetaData() - metadata.reflect(engine, only=["EnergyScan"]) - generator = DeclarativeGenerator(metadata, engine, set()) + async with engine.begin() as connection: + await connection.run_sync( + lambda connection: metadata.reflect(connection, only=["EnergyScan"]) + ) + generator = await connection.run_sync( + lambda connection: DeclarativeGenerator(metadata, connection, set()) + ) with open("src/graph_energy_scan/models.py", "w") as models_file: models_file.write(generator.generate()) + + +if __name__ == "__main__": + asyncio.run(generate_models()) setuptools.setup()