Skip to content

Commit

Permalink
Fixes for the automatic ORM update workflow (#216)
Browse files Browse the repository at this point in the history
* Add outputs name to orm update checks

* Change database dist location

* Missed translation of items into orm update

* Incorrect working dir

* Allow ORM update on other branches

* Pass env to script

* Grant permissions in orm workflow

* Nested job permissions needed

* Move from hub to gh commands

* Edit flags on gh pr commands

* New flags for finding existing PRs

* Previous commit always returns true

* Update ISPyB ORM schema to database schema v4.2.1 (#217)

Generated with
sqlacodegen            2.3.0.post1
SQLAlchemy             1.4.53

Co-authored-by: Stephen Riggs <122790971+stephen-riggs@users.noreply.github.com>
Co-authored-by: ISPyB-API Azure build <DiamondLightSource-build-server@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: ISPyB-API Azure build <DiamondLightSource-build-server@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 23, 2024
1 parent 2350613 commit e34a02b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
13 changes: 2 additions & 11 deletions .github/workflows/scripts/create-orm-update-pull-request
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@ git config credential.helper "store --file=.git/credentials"
echo "https://${GITHUB_TOKEN}:@github.com" > .git/credentials
echo

echo "##[section]Installing github cli"
pushd .. >/dev/null
wget -nv https://github.com/github/hub/releases/download/v2.6.0/hub-linux-amd64-2.6.0.tgz -O - | tar xz
export PATH=$(pwd)/hub-linux-amd64-2.6.0/bin/:${PATH}
popd >/dev/null
hub --version
echo

echo "##[section]Check for existing pull request"
hub pr list -b main -f '%H%n' | grep "^orm-update$" && {
echo $( gh pr list -B ${BRANCH} -H orm-update ) | grep orm-update && {
echo Pull request already exists. Stopping here.
exit
} || {
Expand All @@ -39,8 +31,7 @@ echo

echo "##[section]Creating pull request"
git push -f --set-upstream origin orm-update
HUB_VERBOSE=1 hub pull-request -b main -f -m "ISPyB ORM schema update v${DATABASE_SCHEMA}
gh pr create -B ${BRANCH} -H orm-update -t "ISPyB ORM schema update v${DATABASE_SCHEMA}" -b "
This is an automated pull request to update the ISPyB ORM schema.
Before merging this pull request you may want to
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ jobs:

update_ORM:
name: Call orm update workflow
permissions:
contents: write
pull-requests: write
needs:
- build
- get-env-vars
Expand Down
66 changes: 36 additions & 30 deletions .github/workflows/update-orm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ jobs:
orm-update:
name: Update ORM
runs-on: ubuntu-latest
container: mariadb
permissions:
contents: write
pull-requests: write

services:
mariadb:
image: mariadb:10.6
env:
MYSQL_DATABASE: ispybtest
MYSQL_ROOT_PASSWORD: mysql_root_pwd
MARIADB_DATABASE: ispybtest
MARIADB_ROOT_PASSWORD: mariadb_root_pwd
ports:
- 3306:3306
options: --health-cmd="healthcheck.sh --connect --innodb_initialized" --health-interval=10s --health-timeout=5s --health-retries=3
outputs:
output1: ${{ steps.checkUpdate.needsUpdate }}
needsUpdate: ${{ steps.checkUpdate.outputs.needsUpdate }}

steps:
- uses: actions/checkout@v4
Expand All @@ -45,57 +49,57 @@ jobs:
working-directory: ./src/ispyb/sqlalchemy

- name: Install sqlacodegen
if: ${{ steps.checkUpdate.needsUpdate == 'true' }}
if: ${{ steps.checkUpdate.outputs.needsUpdate == 'true' }}
run: |
set -eux
pip install -r requirements_orm.txt
pip install pre-commit
- name: Get database schema
if: ${{ steps.checkUpdate.needsUpdate == 'true' }}
if: ${{ steps.checkUpdate.outputs.needsUpdate == 'true' }}
uses: actions/download-artifact@v4
with:
name: ispyb-database.tar.gz
name: package-distributions
path: dist/

- uses: shogo82148/actions-setup-mysql@v1
with:
distribution: 'mariadb'
mysql-version: '10.6'
auto-start: false

- name: Set up reference database schema
if: ${{ steps.checkUpdate.needsUpdate == 'true' }}
if: ${{ steps.checkUpdate.outputs.needsUpdate == 'true' }}
run: |
set -eu
cat >~/.my.cnf <<EOF
cat > .my.cnf <<EOF
[client]
user=root
host=127.0.0.1
password=mysql_root_pwd
database=ispybtest
user = root
password = mariadb_root_pwd
host = 127.0.0.1
port = 3306
database = ispybtest
EOF
echo "Installing mariadb-client"
sudo apt-get install mariadb-client

mkdir schema
cd schema
tar xfz "$(System.ArtifactsDirectory)/package/ispyb-database.tar.gz"
patch -p1 < "$(Build.SourcesDirectory)/src/ispyb/sqlalchemy/sqlacodegen.patch"
ln ~/.my.cnf .my.cnf -s
tar xfz "dist/ispyb-database.tar.gz"
patch -p1 < "src/ispyb/sqlalchemy/sqlacodegen.patch"
printf 'Waiting for MySQL database to accept connections'
until mysql -e "SHOW DATABASES" >/dev/null; do printf '.'; sleep 0.5; done
until mariadb --defaults-file=.my.cnf -e "SHOW DATABASES" >/dev/null; do printf '.'; sleep 0.5; done
printf '\n'

echo "Installing reference database"
./build.sh

echo
echo "Installed tables:"
mysql -D ispyb_build -e "SHOW TABLES"
working-directory: dist
mariadb --defaults-file=.my.cnf -D ispyb_build -e "SHOW TABLES"

- name: Generate ORM
if: ${{ steps.checkUpdate.needsUpdate == 'true' }}
if: ${{ steps.checkUpdate.outputs.needsUpdate == 'true' }}
run: |
set -eux
sqlacodegen mysql+mysqlconnector://root:mysql_root_pwd@127.0.0.1/ispyb_build --noinflect --nojoined --outfile _auto_db_schema.py.in
sqlacodegen mysql+mysqlconnector://root:mariadb_root_pwd@127.0.0.1/ispyb_build --noinflect --nojoined --outfile _auto_db_schema.py.in
# This code produces false positives due to non-deterministic ordering.
# Add an identifier to the file, and only update/PR the changes when the
# identifier indicates the file is out of date.
Expand All @@ -105,18 +109,20 @@ jobs:
working-directory: ./src/ispyb/sqlalchemy

- name: Show differences
if: ${{ steps.checkUpdate.needsUpdate == 'true' }}
if: ${{ steps.checkUpdate.outputs.needsUpdate == 'true' }}
run: git diff

- name: Store artifact
if: ${{ steps.checkUpdate.needsUpdate == 'true' }}
if: ${{ steps.checkUpdate.outputs.needsUpdate == 'true' }}
uses: actions/upload-artifact@v4
with:
name: ORM
path: ./src/ispyb/sqlalchemy/_auto_db_schema.py

- name: Create ORM pull request
if: ${{ steps.checkUpdate.needsUpdate == 'true' && github.ref_name == 'main' }}
if: ${{ steps.checkUpdate.outputs.needsUpdate == 'true' }}
run: .github/workflows/scripts/create-orm-update-pull-request
env:
GITHUB_TOKEN: ${{ github.token }}
DATABASE_SCHEMA: ${{ inputs.DATABASE_SCHEMA }}
BRANCH: ${{ github.ref_name }}
4 changes: 2 additions & 2 deletions src/ispyb/sqlalchemy/_auto_db_schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__schema_version__ = "4.2.0"
__schema_version__ = "4.2.1"
# coding: utf-8
from sqlalchemy import (
BINARY,
Expand Down Expand Up @@ -1697,8 +1697,8 @@ class DiffractionPlan(Base):
class LabContact(Base):
__tablename__ = "LabContact"
__table_args__ = (
Index("personAndProposal", "personId", "proposalId", unique=True),
Index("cardNameAndProposal", "cardName", "proposalId", unique=True),
Index("personAndProposal", "personId", "proposalId", unique=True),
)

labContactId = Column(INTEGER(10), primary_key=True)
Expand Down

0 comments on commit e34a02b

Please sign in to comment.