Skip to content

Commit

Permalink
Generate models from database
Browse files Browse the repository at this point in the history
  • Loading branch information
garryod committed Apr 15, 2024
1 parent 7709d89 commit 2ca41d8
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion .github/workflows/_container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand Down Expand Up @@ -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 }}
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/_dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion .github/workflows/_tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
if: needs.check.outputs.branch-pr == ''
uses: ./.github/workflows/_container.yml
permissions:
contents: read
packages: write

dist:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ lockfiles/

# ruff cache
.ruff_cache/

# Generated Database Models
src/graph_energy_scan/models.py
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 16 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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"
Expand All @@ -31,6 +42,7 @@ dev = [
"ruff",
"tox-direct",
"types-mock",
"sqlacodegen-v2",
]

[project.scripts]
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 2ca41d8

Please sign in to comment.