Files
synapse/.github/workflows/schema_diff.yml
T
Eric Eastwood 1b0fd0abc6 Pin Python versions in CI (#20531)
Part of https://github.com/element-hq/backend-internal/issues/238

Also discussed in [meeting to improve security release
process](https://docs.google.com/document/d/1UUShIXcuhmDX53-auQIEkeuEaLDYedkFfiE6NbSzOuE/edit?tab=t.0#bookmark=id.c2cmusfqw6lc)

Spawning from seeing the CI failing in an old LTS branch because it's
choosing the newest Python 3.15 which we're not compatible with yet
(https://github.com/element-hq/synapse/issues/20340), let alone the old
LTS. We also experienced this here in Synapse FOSS codebase which is why
https://github.com/element-hq/synapse/pull/20386 was made.
2026-11-18 17:14:21 -06:00

149 lines
6.1 KiB
YAML

name: Schema Diff # If this is changed, need to update `schema_diff_comment.yml` as well
on:
pull_request:
paths:
- synapse/storage/schema/*/delta/**
- synapse/storage/schema/*/full_schemas/**
- .github/workflows/schema_diff.yml
env:
LATEST_SUPPORTED_PYTHON_VERSION: "3.14"
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
jobs:
# Posts a GitHub PR comment that shows what the effective change to the schema is.
# Provides an excuse to run the `make_full_schema.sh` script in CI (so we keep it working)
# and can act as a review aid for schema changes, letting you easily see the diff of the
# end result, even when background updates or complex schema deltas are present.
show-schema-diff:
name: Show schema diff
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Start postgres with a faked clock
background: true
id: postgres
# Use faketime here for schema deltas that are wall-clock sensitive under Postgres
# For SQLite, faketime is used when invoking `make_full_schema.sh` within the script
run: |
# Build a docker image with faketime
mkdir /tmp/postgres-faketime
cat > /tmp/postgres-faketime/Dockerfile <<'EOF'
FROM postgres:14-alpine
RUN apk add --no-cache libfaketime
# It seems like it could be harmful to fake the monotonic timer
# as it might prevent deadlock detection, etc.
# But not sure, just doing out of precaution.
ENV FAKETIME_DONT_FAKE_MONOTONIC=1
ENTRYPOINT ["faketime", "-f", "2001-05-25 12:42:42", "docker-entrypoint.sh"]
CMD ["postgres"]
EOF
docker build -t localhost/postgres-faketime /tmp/postgres-faketime
# Run it in the background
docker run -d --name postgres -p 5432:5432 \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_INITDB_ARGS="--lc-collate C --lc-ctype C --encoding UTF8" \
--health-cmd pg_isready --health-interval 10s \
--health-timeout 5s --health-retries 5 \
localhost/postgres-faketime
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
- name: Add PostgreSQL apt repository
# We need a version of pg_dump that can handle the version of
# PostgreSQL being tested against. The Ubuntu package repository lags
# behind new releases, so we have to use the PostreSQL apt repository.
# Steps taken from https://www.postgresql.org/download/linux/ubuntu/
run: |
sudo install -d /usr/share/postgresql-common/pgdg
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
. /etc/os-release
sudo sh -c "echo 'deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $VERSION_CODENAME-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
sudo apt-get update
# Install `pg_dump` and ensure it's up to date. If it's older than the
# server version, it can cause mismatches in the schema dumps.
# (Specifically, we've seen spurious `pg_attver` changes in the schema
# dumps)
- name: Install PostgreSQL client and faketime
run: |
sudo apt-get -qq install postgresql-client faketime
sudo apt-get -qq install --only-upgrade 'postgresql-client-*'
- uses: matrix-org/setup-python-poetry@5bbf6603c5c930615ec8a29f1b5d7d258d905aa4 # v2.0.0
with:
python-version: ${{ env.LATEST_SUPPORTED_PYTHON_VERSION }}
poetry-version: "2.4.1"
extras: "postgres"
- name: Wait for Postgres to be up
run: |
until [ "$(docker inspect -f '{{.State.Health.Status}}' postgres)" = healthy ]; do sleep 2; done
- name: Generate schema diff
id: schema_diff
env:
PGHOST: localhost
PGUSER: postgres
PGPASSWORD: postgres
run: |
poetry run python .ci/scripts/schema_diff.py \
--base origin/develop \
> "${{ runner.temp }}/schema_diff.md"
- name: Stop postgres
cancel: postgres
# If the generation step failed, write an error message so the sticky
# comment step still has a file to read.
- name: Ensure output file exists on failure
if: always() && steps.schema_diff.outcome == 'failure'
run: |
echo "⚠️ Schema diff generation failed. See job logs for details." \
> "${{ runner.temp }}/schema_diff.md"
# Post a comment.
#
# For same-repo PRs, we can do this directly and we are done.
- name: Post sticky PR comment
uses: marocchino/sticky-pull-request-comment@28ad303d6acb4ccfc50f20c0044188e7e294b8c4 # v3
# Only run for same-repo PRs
if: always() && github.event.pull_request.head.repo.full_name == github.repository
with:
header: schema-diff
path: ${{ runner.temp }}/schema_diff.md
# For PRs from forks, we only have a read-only `GITHUB_TOKEN` here and need to work around
# by triggering a `workflow_run` onto the main repo.
#
# We upload an artifact and the `schema_diff_comment.yml` workflow will be triggered
# on the main repo by this workflow exiting.
- name: Save PR number for forked PRs
# Only run for fork PRs
if: always() && github.event.pull_request.head.repo.full_name != github.repository
run: echo "${{ github.event.pull_request.number }}" > "${{ runner.temp }}/pr_number"
- name: Upload schema diff artifact for forked PRs
# Only run for fork PRs
if: always() && github.event.pull_request.head.repo.full_name != github.repository
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: schema-diff
path: |
${{ runner.temp }}/schema_diff.md
${{ runner.temp }}/pr_number
retention-days: 1