diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml index a263cc6..428d73a 100644 --- a/.devcontainer/docker-compose.yml +++ b/.devcontainer/docker-compose.yml @@ -10,7 +10,7 @@ services: - ..:/workspace:cached,z command: sleep infinity environment: - DATABASE_URL: mysql://root:rootpassword@ispyb/ispyb_build + DATABASE_URL: mysql+pymysql://root:rootpassword@ispyb/ispyb_build ispyb: image: ghcr.io/diamondlightsource/ispyb-database:v3.0.0 diff --git a/.github/workflows/_container.yml b/.github/workflows/_container.yml index 4857ee9..b913288 100644 --- a/.github/workflows/_container.yml +++ b/.github/workflows/_container.yml @@ -4,7 +4,15 @@ on: jobs: build: runs-on: ubuntu-latest - + services: + ispyb: + image: ghcr.io/diamondlightsource/ispyb-database:v3.0.0 + ports: + - 3306:3306 + env: + MARIADB_ROOT_PASSWORD: rootpassword + options: > + --health-cmd "/usr/local/bin/healthcheck.sh --defaults-file=/ispyb/.my.cnf --connect" steps: - name: Checkout uses: actions/checkout@v4 @@ -15,6 +23,8 @@ jobs: - name: Set up Docker Buildx id: buildx uses: docker/setup-buildx-action@v3 + with: + driver-opts: network=host - name: Log in to GitHub Docker Registry if: github.event_name != 'pull_request' @@ -50,6 +60,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://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 b1c4c93..0941876 100644 --- a/.github/workflows/_dist.yml +++ b/.github/workflows/_dist.yml @@ -4,7 +4,17 @@ on: jobs: build: runs-on: "ubuntu-latest" - + services: + ispyb: + image: ghcr.io/diamondlightsource/ispyb-database:v3.0.0 + ports: + - 3306:3306 + env: + MARIADB_ROOT_PASSWORD: rootpassword + options: > + --health-cmd "/usr/local/bin/healthcheck.sh --defaults-file=/ispyb/.my.cnf --connect" + env: + DATABASE_URL: mysql+pymysql://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 f652d41..be2b915 100644 --- a/.github/workflows/_test.yml +++ b/.github/workflows/_test.yml @@ -20,7 +20,17 @@ env: jobs: run: runs-on: ${{ inputs.runs-on }} - + services: + ispyb: + image: ghcr.io/diamondlightsource/ispyb-database:v3.0.0 + ports: + - 3306:3306 + env: + MARIADB_ROOT_PASSWORD: rootpassword + options: > + --health-cmd "/usr/local/bin/healthcheck.sh --defaults-file=/ispyb/.my.cnf --connect" + env: + DATABASE_URL: mysql+pymysql://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 a13536d..e5ad358 100644 --- a/.github/workflows/_tox.yml +++ b/.github/workflows/_tox.yml @@ -10,7 +10,17 @@ on: jobs: run: runs-on: "ubuntu-latest" - + services: + ispyb: + image: ghcr.io/diamondlightsource/ispyb-database:v3.0.0 + ports: + - 3306:3306 + env: + MARIADB_ROOT_PASSWORD: rootpassword + options: > + --health-cmd "/usr/local/bin/healthcheck.sh --defaults-file=/ispyb/.my.cnf --connect" + env: + DATABASE_URL: mysql+pymysql://root:rootpassword@localhost/ispyb_build steps: - name: Checkout uses: actions/checkout@v4 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 752ca8c..da89f22 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,6 +39,7 @@ jobs: if: needs.check.outputs.branch-pr == '' uses: ./.github/workflows/_container.yml permissions: + contents: read packages: write dist: diff --git a/.gitignore b/.gitignore index 2593ec7..73e42aa 100644 --- a/.gitignore +++ b/.gitignore @@ -68,3 +68,6 @@ lockfiles/ # ruff cache .ruff_cache/ + +# Generated Database Models +src/graph_energy_scan/models.py \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index f449928..0f99738 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,8 @@ # The devcontainer should use the developer target and run as root with podman # or docker with user namespaces. ARG PYTHON_VERSION=3.11 +ARG DATABASE_URL + FROM python:${PYTHON_VERSION} as developer # Add any system dependencies for the developer/build environment here diff --git a/pyproject.toml b/pyproject.toml index 2af1323..bd11f7b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,11 @@ [build-system] -requires = ["setuptools>=64", "setuptools_scm[toml]>=6.2"] +requires = [ + "setuptools>=64", + "setuptools_scm[toml]>=6.2", + "sqlacodegen-v2", + "sqlalchemy", + "pymysql", +] build-backend = "setuptools.build_meta" [project] @@ -14,7 +20,12 @@ classifiers = [ "Programming Language :: Python :: 3.11", ] description = "A service providing energy scan information as part of the Dimaond graph" -dependencies = ["uvicorn", "fastapi", "strawberry-graphql[fastapi]"] +dependencies = [ + "uvicorn", + "fastapi", + "strawberry-graphql[fastapi]", + "sqlalchemy", +] dynamic = ["version"] license.file = "LICENSE" readme = "README.md" @@ -31,6 +42,7 @@ dev = [ "ruff", "tox-direct", "types-mock", + "sqlacodegen-v2", ] [project.scripts] @@ -48,7 +60,8 @@ name = "Garry O'Donnell" write_to = "src/graph_energy_scan/_version.py" [tool.pyright] -reportMissingImports = false # Ignore missing stubs in imported modules +reportMissingImports = false # Ignore missing stubs in imported modules +ignore = ["src/graph_energy_scan/models.py"] [tool.pytest.ini_options] # Run pytest with all our checkers, and don't spam us with massive tracebacks on error diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..7cb0368 --- /dev/null +++ b/setup.py @@ -0,0 +1,14 @@ +import os + +import setuptools +from sqlacodegen_v2.generators import DataclassGenerator +from sqlalchemy import MetaData, create_engine + +if __name__ == "__main__": + engine = create_engine(os.environ["DATABASE_URL"]) + metadata = MetaData() + metadata.reflect(engine) + generator = DataclassGenerator(metadata, engine, set()) + with open("src/graph_energy_scan/models.py", "w") as models_file: + models_file.write(generator.generate()) + setuptools.setup()