Skip to content

Commit

Permalink
Merge pull request #1358 from microbiomedata/issue-field-notes-156-sc…
Browse files Browse the repository at this point in the history
…hema-version-info

Add schema version identifiers to /api/version response
  • Loading branch information
pkalita-lbl authored Aug 22, 2024
2 parents 4129d17 + 1086312 commit 378cc03
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
8 changes: 4 additions & 4 deletions nmdc_server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from sqlalchemy.orm import Session
from starlette.responses import StreamingResponse

from nmdc_server import __version__, crud, jobs, models, query, schemas, schemas_submission
from nmdc_server import crud, jobs, models, query, schemas, schemas_submission
from nmdc_server.auth import admin_required, get_current_user, login_required_responses
from nmdc_server.bulk_download_schema import BulkDownload, BulkDownloadCreate
from nmdc_server.config import Settings
Expand Down Expand Up @@ -39,9 +39,9 @@ async def get_settings() -> Dict[str, Any]:


# get application version number
@router.get("/version", name="Get application version identifier")
async def get_version() -> Dict[str, Any]:
return {"nmdc-server": __version__}
@router.get("/version", name="Get application and schema version identifiers")
async def get_version() -> schemas.VersionInfo:
return schemas.VersionInfo()


# get the current user information
Expand Down
20 changes: 19 additions & 1 deletion nmdc_server/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from datetime import date, datetime
from enum import Enum
from importlib.metadata import version
from typing import Any, Dict, List, Optional, Union
from urllib.parse import quote
from uuid import UUID
Expand All @@ -18,7 +19,7 @@
from sqlalchemy import BigInteger, Column, DateTime, Float, Integer, LargeBinary, String
from sqlalchemy.dialects.postgresql.json import JSONB

from nmdc_server import models
from nmdc_server import __version__, models
from nmdc_server.data_object_filters import DataObjectFilter, WorkflowActivityTypeEnum

DateType = Union[datetime, date]
Expand Down Expand Up @@ -688,3 +689,20 @@ class LockOperationResult(BaseModel):
message: str
locked_by: Optional[User]
lock_updated: Optional[datetime]


class VersionInfo(BaseModel):
"""Version information for the nmdc-server itself and the schemas.
This model has default field values and is immutable because these values cannot
change at runtime.
"""

nmdc_server: str = __version__
nmdc_schema: str = version("nmdc-schema")
nmdc_submission_schema: str = version("nmdc-submission-schema")

class Config:
# In Pydantic V2, use `frozen=True`
# https://docs.pydantic.dev/2.8/concepts/models/#faux-immutability
allow_mutation = False
7 changes: 6 additions & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from importlib.metadata import version
from itertools import product

import pytest
Expand Down Expand Up @@ -33,7 +34,11 @@ def test_get_settings(client: TestClient):
def test_get_version(client: TestClient):
resp = client.get("/api/version")
assert resp.status_code == 200
assert resp.json()["nmdc-server"] == nmdc_server.__version__

body = resp.json()
assert body["nmdc_server"] == nmdc_server.__version__
assert body["nmdc_schema"] == version("nmdc-schema")
assert body["nmdc_submission_schema"] == version("nmdc-submission-schema")


@pytest.mark.parametrize(
Expand Down

0 comments on commit 378cc03

Please sign in to comment.