Skip to content

Commit abcefc7

Browse files
authored
Remove need for 'ISPYB_CREDENTIALS' environment variable (#588)
'ispyb.sqlalchemy.url()' takes credentials as an argument, through which the config file can be passed directly to the function instead of relying on the 'ISPYB_CREDENTIALS' environment variable. This eliminates the last traces of our repo's need to explicitly define 'ISPYB_CREDENTIALS'. Additionally, elements of the CI workflow and test code related to the Murfey and ISPyB databases have also been modified. 'ISPYB_CREDENTIALS' has also been dropped successfully from the test workflow, and fixtures have been put in place to allow for the creation of databases that will be reset to a desired initial state at the end of every test. This means that the database does not have to be shut down and rebooted with every database-related test, which should help with our testing times as we continue to improve Murfey's test coverage. NOTE: Within our code base, it looks like ISPyB spawns SQLAlchemy Session objects, whereas the Murfey database spawns SQLModel ones. As such, SQLModel's functions might not work when querying the ISPyB database. The database session fixtures for the ISPyB and Murfey test databases have been written to reflect this.
1 parent 88a6f93 commit abcefc7

File tree

11 files changed

+475
-123
lines changed

11 files changed

+475
-123
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ jobs:
4141

4242
steps:
4343
- uses: actions/checkout@v4
44+
4445
- name: Use Python ${{ matrix.python-version }}
4546
uses: actions/setup-python@v5
4647
with:
@@ -67,13 +68,13 @@ jobs:
6768
docker run --detach --name rabbitmq -p 127.0.0.1:5672:5672 -p 127.0.0.1:15672:15672 test-rabbitmq
6869
docker container list -a
6970
70-
- name: Get ispyb database
71+
- name: Get ISPyB database
7172
uses: actions/download-artifact@v4
7273
with:
7374
name: database
7475
path: database/
7576

76-
- name: Install package
77+
- name: Install Murfey
7778
run: |
7879
set -eux
7980
pip install --disable-pip-version-check -e "."[cicd,client,server,developer]
@@ -84,7 +85,7 @@ jobs:
8485
mysql-version: "11.3"
8586
auto-start: false
8687

87-
- name: Set up test ipsyb database
88+
- name: Set up test ISPyB database
8889
run: |
8990
set -eu
9091
cp ".github/workflows/config/my.cnf" .my.cnf
@@ -101,9 +102,14 @@ jobs:
101102
schemas/ispyb/routines.sql \
102103
grants/ispyb_processing.sql \
103104
grants/ispyb_import.sql; do
104-
echo Importing ${f}...
105+
106+
echo "Patching ${f} in SQL files to fix CLI escape issues..."
107+
sed -i 's/\\-/-/g' "$f"
108+
109+
echo "Importing ${f}..."
105110
mariadb --defaults-file=.my.cnf < $f
106111
done
112+
107113
mariadb --defaults-file=.my.cnf -e "CREATE USER ispyb_api@'%' IDENTIFIED BY 'password_1234'; GRANT ispyb_processing to ispyb_api@'%'; GRANT ispyb_import to ispyb_api@'%'; SET DEFAULT ROLE ispyb_processing FOR ispyb_api@'%';"
108114
mariadb --defaults-file=.my.cnf -e "CREATE USER ispyb_api_future@'%' IDENTIFIED BY 'password_4321'; GRANT SELECT ON ispybtest.* to ispyb_api_future@'%';"
109115
mariadb --defaults-file=.my.cnf -e "CREATE USER ispyb_api_sqlalchemy@'%' IDENTIFIED BY 'password_5678'; GRANT SELECT ON ispybtest.* to ispyb_api_sqlalchemy@'%'; GRANT INSERT ON ispybtest.* to ispyb_api_sqlalchemy@'%'; GRANT UPDATE ON ispybtest.* to ispyb_api_sqlalchemy@'%';"
@@ -112,18 +118,17 @@ jobs:
112118
- name: Check RabbitMQ is alive
113119
run: wget -t 10 -w 1 http://127.0.0.1:15672 -O -
114120

115-
- name: Run tests
121+
- name: Run Murfey tests
116122
env:
117123
POSTGRES_HOST: localhost
118124
POSTGRES_PORT: 5432
119125
POSTGRES_DB: murfey_test_db
120126
POSTGRES_PASSWORD: psql_pwd
121127
POSTGRES_USER: psql_user
122128
run: |
123-
export ISPYB_CREDENTIALS=".github/workflows/config/ispyb.cfg"
124129
PYTHONDEVMODE=1 pytest -v -ra --cov=murfey --cov-report=xml --cov-branch
125130
126-
- name: Upload to Codecov
131+
- name: Upload test results to Codecov
127132
uses: codecov/codecov-action@v5
128133
with:
129134
name: ${{ matrix.python-version }}

.github/workflows/test.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ name: Build and test
33
on: [push, pull_request]
44

55
env:
6-
DATABASE_SCHEMA: 4.2.1 # released 2024-08-19
6+
ISPYB_DATABASE_SCHEMA: 4.6.0
77
# Installs from GitHub
88
# Versions: https://github.com/DiamondLightSource/ispyb-database/tags
99
# Previous version(s):
10-
# 4.1.0
10+
# 4.2.1 # released 2024-08-19
11+
# 4.1.0 # released 2024-03-26
1112

1213
permissions:
1314
contents: read
@@ -53,10 +54,10 @@ jobs:
5354
runs-on: ubuntu-latest
5455
steps:
5556
- uses: actions/checkout@v4
56-
- name: Download ISPyB DB schema v${{ env.DATABASE_SCHEMA }} for tests
57+
- name: Download ISPyB DB schema v${{ env.ISPYB_DATABASE_SCHEMA }} for tests
5758
run: |
5859
mkdir database
59-
wget -t 3 --waitretry=20 https://github.com/DiamondLightSource/ispyb-database/releases/download/v${{ env.DATABASE_SCHEMA }}/ispyb-database-${{ env.DATABASE_SCHEMA }}.tar.gz -O database/ispyb-database.tar.gz
60+
wget -t 3 --waitretry=20 https://github.com/DiamondLightSource/ispyb-database/releases/download/v${{ env.ISPYB_DATABASE_SCHEMA }}/ispyb-database-${{ env.ISPYB_DATABASE_SCHEMA }}.tar.gz -O database/ispyb-database.tar.gz
6061
- name: Store database artifact
6162
uses: actions/upload-artifact@v4
6263
with:

src/murfey/cli/spa_ispyb_messages.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from murfey.client.contexts.spa import _get_xml_list_index
2121
from murfey.server import _murfey_id, _register
22-
from murfey.server.ispyb import Session, TransportManager, get_session_id
22+
from murfey.server.ispyb import ISPyBSession, TransportManager, get_session_id
2323
from murfey.server.murfey_db import url
2424
from murfey.util import db
2525
from murfey.util.config import get_machine_config, get_microscope, get_security_config
@@ -256,7 +256,7 @@ def run():
256256
proposal_code=args.visit[:2],
257257
proposal_number=args.visit[2:].split("-")[0],
258258
visit_number=args.visit.split("-")[1],
259-
db=Session(),
259+
db=ISPyBSession(),
260260
),
261261
)
262262

src/murfey/server/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import murfey
4747
import murfey.server.prometheus as prom
4848
import murfey.util.db as db
49-
from murfey.server.ispyb import get_session_id
49+
from murfey.server.ispyb import ISPyBSession, get_session_id
5050
from murfey.server.murfey_db import url # murfey_db
5151
from murfey.util import LogFilter
5252
from murfey.util.config import (
@@ -2203,7 +2203,7 @@ def feedback_callback(header: dict, message: dict) -> None:
22032203
proposal_code=message["proposal_code"],
22042204
proposal_number=message["proposal_number"],
22052205
visit_number=message["visit_number"],
2206-
db=murfey.server.ispyb.Session(),
2206+
db=ISPyBSession(),
22072207
)
22082208
if dcg_murfey := murfey_db.exec(
22092209
select(db.DataCollectionGroup)
@@ -2281,7 +2281,7 @@ def feedback_callback(header: dict, message: dict) -> None:
22812281
proposal_code=message["proposal_code"],
22822282
proposal_number=message["proposal_number"],
22832283
visit_number=message["visit_number"],
2284-
db=murfey.server.ispyb.Session(),
2284+
db=ISPyBSession(),
22852285
)
22862286
dcg = murfey_db.exec(
22872287
select(db.DataCollectionGroup)
@@ -2380,7 +2380,7 @@ def feedback_callback(header: dict, message: dict) -> None:
23802380
).all():
23812381
pid = pj_murfey[0].id
23822382
else:
2383-
if murfey.server.ispyb.Session() is None:
2383+
if ISPyBSession() is None:
23842384
murfey_pj = db.ProcessingJob(recipe=message["recipe"], dc_id=_dcid)
23852385
else:
23862386
record = ProcessingJob(
@@ -2410,7 +2410,7 @@ def feedback_callback(header: dict, message: dict) -> None:
24102410
if not murfey_db.exec(
24112411
select(db.AutoProcProgram).where(db.AutoProcProgram.pj_id == pid)
24122412
).all():
2413-
if murfey.server.ispyb.Session() is None:
2413+
if ISPyBSession() is None:
24142414
murfey_app = db.AutoProcProgram(pj_id=pid)
24152415
else:
24162416
record = AutoProcProgram(

src/murfey/server/api/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,9 @@ def _add_tilt():
892892

893893
@router.get("/instruments/{instrument_name}/visits_raw", response_model=List[Visit])
894894
def get_current_visits(instrument_name: str, db=murfey.server.ispyb.DB):
895+
log.debug(
896+
f"Received request to look up ongoing visits for {sanitise(instrument_name)}"
897+
)
895898
return murfey.server.ispyb.get_all_ongoing_visits(instrument_name, db)
896899

897900

0 commit comments

Comments
 (0)