Skip to content

Commit

Permalink
just dump it all
Browse files Browse the repository at this point in the history
  • Loading branch information
Erutis committed Aug 28, 2024
1 parent da69c17 commit a21c306
Show file tree
Hide file tree
Showing 7 changed files with 1,751 additions and 10 deletions.
106 changes: 106 additions & 0 deletions .github/workflows/db-changes-var1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: DB Change Check var1

on:
pull_request:
branches:
- main

jobs:
check-sql-schema-changes:
runs-on: ubuntu-latest
steps:
- name: Checkout Current Branch
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- name: Check For Changes
id: changed-db-files
uses: tj-actions/changed-files@v44
with:
files: "app/**"

- name: Check Conditions
id: check-conditions
run: |
echo "DB_CHANGE=${{ steps.changed-db-files.outputs.any_changed }}" >> $GITHUB_ENV
run-alembic:
needs: check-sql-schema-changes
if: ${{ needs.check-sql-schema-changes.DB_CHANGE == 'true' }}
runs-on: ubuntu-latest

services:
postgres:
image: postgres:14-bullseye
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_HOST: localhost
POSTGRES_DRIVERNAME: postgresql
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:
- name: Set up Python
id: setup-python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Generate envs
id: generate-envs
run: |
DATABASE_URL="postgresql://postgres:postgres@localhost:5432"
echo "DATABASE_URL=$DATABASE_URL" >> $GITHUB_ENV
- name: Install alembic
id: install-alembic
run: |
python -m pip install --upgrade pip
pip install alembic # version?
- name: Run alembic script autogeneration
id: run-alembic-script-autogen
env:
DATABASE_URL: ${{ env.DATABASE_URL }}
run: |
alembic -c alembic.ini revision --autogenerate -m "test_script"
- name: (Try) Run alembic upgrade
id: run-alembic-upgrade
env:
DATABASE_URL: ${{ env.DATABASE_URL }}
run: |
alembic -c migrations/starter-kit/alembic.ini upgrade head"
# - name: Checkout new branch
# id: checkout-new-branch
# run: |
# git config --global user.email "github-actions[bot]@users.noreply.github.com"
# git config --global user.name "github-actions[bot]"
# git checkout -b ${{ env.BRANCH_NAME }}
# git push origin ${{ env.BRANCH_NAME }}

# - name: Add alembic script to new branch
# id: add-alembic-script-to-new-branch
# run: |
# git add .
# git commit -m "Add new Alembic migration"
# git push origin HEAD:${{ github.ref }}

# - name: Create PR
# id: create-pull-request
# uses: peter-evans/create-pull-request@v5
# with:
# branch: ${{ env.BRANCH_NAME }}
# base: main
# title: "BotPR: {{ env.BRANCH_NAME }}"
# author: "github-actions[bot] <github-actions[bot]@users.noreply.github.com>"

99 changes: 99 additions & 0 deletions .github/workflows/db-changes-var2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: DB Change Check var2

on:
pull_request:
branches:
- main

jobs:
check-sql-schema-changes:
runs-on: ubuntu-latest
steps:
- name: Checkout Current Branch
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Check For DB Schema Change
id: db-schema-changes
uses: tj-actions/changed-files@v45
with:
files: "app/**"

- name: Check For Migration File
id: migration-changes
uses: tj-actions/changed-files@v45
with:
files: 'migrations/**'

- name: Determine Outcome
id: determine-outcome
run: |
echo "DB_CHANGE=${{ steps.db-schema-changes.outputs.any_changed }}" >> $GITHUB_ENV
echo "MIGRATION_CHANGE=${{ steps.migration-changes.outputs.any_changed }}" >> $GITHUB_ENV
- name: Check Conditions
id: check-conditions
run: |
if [[ "${{ env.DB_CHANGE }}" == "true" && "${{ env.MIGRATION_CHANGE }}" == "true" ]]; then
echo "Conditions met. Continue to the next job."
echo "true" > continue.txt
elif [[ "${{ env.DB_CHANGE }}" == "true" || "${{ env.MIGRATION_CHANGE }}" == "true" ]]; then
echo "Only one condition is true. Failing the job."
exit 1
else
echo "Neither condition is true. Completing successfully."
echo "true" > continue.txt
fi
- name: Set Continue Output
id: set-continue-output
run: echo "continue=$(cat continue.txt)" >> $GITHUB_OUTPUT

run-alembic:
needs: check-sql-schema-changes
if: ${{ needs.check-sql-schema-changes.outputs.continue == 'true' }}
runs-on: ubuntu-latest

services:
postgres:
image: postgres:14-bullseye
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_HOST: localhost
POSTGRES_DRIVERNAME: postgresql
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:
- name: Set up Python
id: setup-python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Generate envs
id: generate-envs
run: |
DATABASE_URL="postgresql://postgres:postgres@localhost:5432"
echo "DATABASE_URL=$DATABASE_URL" >> $GITHUB_ENV
- name: Install alembic
run: |
python -m pip install --upgrade pip
pip install alembic # version?
- name: (Try) Run alembic upgrade
id: run-alembic-upgrade
env:
DATABASE_URL: ${{ env.DATABASE_URL }}
run: |
alembic -c migrations/starter-kit/alembic.ini upgrade head"
20 changes: 10 additions & 10 deletions docker/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ services:
ports:
- "5432:5432"

db-setup: # create extensions & schemas
container_name: db-setup
environment: *common-variables
depends_on:
db:
condition: service_started
restart: true
build:
context: ..
dockerfile: docker/db_setup.Dockerfile
# db-setup: # create extensions & schemas
# container_name: db-setup
# environment: *common-variables
# depends_on:
# db:
# condition: service_started
# restart: true
# build:
# context: ..
# dockerfile: docker/db_setup.Dockerfile

# alembic:
# container_name: alembic-setup
Expand Down
1 change: 1 addition & 0 deletions migrations/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generic single-database configuration.
78 changes: 78 additions & 0 deletions migrations/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context

from app.tables import FeedItem, Trajectory, metadata

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = [metadata]

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)

with context.begin_transaction():
context.run_migrations()


def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)

with context.begin_transaction():
context.run_migrations()


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
26 changes: 26 additions & 0 deletions migrations/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}


def upgrade() -> None:
${upgrades if upgrades else "pass"}


def downgrade() -> None:
${downgrades if downgrades else "pass"}
Loading

0 comments on commit a21c306

Please sign in to comment.