Skip to content

Commit

Permalink
Making it more reliable
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgliss committed Jun 24, 2024
1 parent c85e11a commit fafd35d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 37 deletions.
35 changes: 4 additions & 31 deletions .github/workflows/lint-migrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
with:
fetch-depth: 0
- name: Set up Python 3.11
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: 3.11.2
- uses: actions/cache@v2
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
Expand All @@ -35,36 +35,9 @@ jobs:
chmod +x eugene
- name: Generate sql files
run: |
# Navigate to the git repository root, if not already there
cd "$(git rev-parse --show-toplevel)" || exit
# Perform git diff to check for changes in the specified directory
changed_files=$(git diff --name-only -- src/dispatch/database/revisions/tenant/versions)
# Filter for newly added files (*.py)
new_files=$(echo "$changed_files" | grep -E "^src/dispatch/database/revisions/tenant/versions/2024-06-12_.*\.py$")
# Check if there is exactly one new file and handle errors for multiple or no files
if [[ $(echo "$new_files" | wc -l) -ne 1 ]]; then
echo "Error: There should be exactly one new revision file. Found: $(echo "$new_files" | wc -l)"
exit 1
fi
new_file_path=$(echo "$new_files" | head -n 1)
new_revision_id=$(basename "$new_file_path" .py | cut -d'_' -f2)
# Get the most recent revision ID excluding the newly added one
old_file=$(git ls-files src/dispatch/database/revisions/tenant/versions | grep -oP '2024-06-12_\K[^.]+(?=\.py)' | grep -v "$new_revision_id" | sort -rV | head -n 1)
if [[ -z "$old_file" ]]; then
echo "Error: No old revision ID found"
exit 1
fi
old_revision_id=$(echo "$old_file")
dispatch database upgrade --revision-type tenant --revision="${old_revision_id}:${new_revision_id}" --sql
dispatch database upgrade --revision-type tenant --sql
- name: Lint files
run: ./eugene lint --git-diff origin/main migration-scripts -f md --accept-failures > lint.md
run: ./eugene lint alembic_output.sql -f md --accept-failures > lint.md
- name: Post Comment
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Check out Git repository
uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: 3.11
- uses: actions/setup-node@v3
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ jobs:
- name: Check out Git repository
uses: actions/checkout@v2
- name: Set up Python 3.11
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: 3.11.2
- uses: actions/cache@v2
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
Expand Down
8 changes: 7 additions & 1 deletion src/dispatch/database/revisions/core/env.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from alembic import context
from alembic import context, script
from sqlalchemy import engine_from_config, pool

from dispatch.logging import logging
Expand Down Expand Up @@ -85,20 +85,26 @@ def run_migrations_offline():
# Get the URL from the config
url = config.get_main_option("sqlalchemy.url")

alembic_script = script.ScriptDirectory.from_config(config)

_, previous = list(alembic_script.walk_revisions(base='base', head='heads'))[:2]

# Set the migration options
context.configure(
url=url,
target_metadata=target_metadata,
include_schemas=True,
include_object=include_object,
literal_binds=True, # Binds parameters with their string values
starting_rev=previous.revision,
)

# Start a transaction and run migrations
with context.begin_transaction():
context.run_migrations()



if context.is_offline_mode():
run_migrations_offline()
else:
Expand Down
9 changes: 7 additions & 2 deletions src/dispatch/database/revisions/tenant/env.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from alembic import context
from alembic import context, script

from sqlalchemy import engine_from_config, pool, inspect


Expand Down Expand Up @@ -98,13 +98,18 @@ def run_migrations_offline():
# Get the URL from the config
url = config.get_main_option("sqlalchemy.url")

alembic_script = script.ScriptDirectory.from_config(config)

_, previous = list(alembic_script.walk_revisions(base='base', head='heads'))[:2]

# Set the migration options
context.configure(
url=url,
target_metadata=target_metadata,
include_schemas=True,
include_object=include_object,
literal_binds=True, # Binds parameters with their string values
starting_rev=previous.revision,
)

# Start a transaction and run migrations
Expand Down

0 comments on commit fafd35d

Please sign in to comment.