Skip to content

Commit e9f2862

Browse files
committed
#510 6th PoC - fix DB Alembic upgrade mgnt with Flask-Migrate latest
1 parent 422a697 commit e9f2862

7 files changed

Lines changed: 80 additions & 54 deletions

File tree

.github/workflows/main.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ jobs:
3737
- name: Load Fixtures Test Data ⚙️
3838
run: pixi run -e dev load-data tests/data/fixtures.json
3939

40+
- name: Test DB downgrade to previous schema (via Alembic) ⚙️
41+
run: pixi run -e dev db-downgrade
42+
43+
- name: Test DB upgrade back to current schema (via Alembic) ⚙️
44+
run: pixi run -e dev db-upgrade
45+
4046
- name: Run Probes ⚙️
4147
run: pixi run -e dev run-healthchecks
4248

GeoHealthCheck/manage.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,36 @@
3737
# optional arguments:
3838
# -h, --help show this help message and exit
3939

40-
from flask_script import Manager
41-
from flask_migrate import Migrate, MigrateCommand
40+
from flask_migrate import (Migrate, upgrade, downgrade, current,
41+
migrate, history, revision)
4242
from init import App
43+
import sys
4344

4445
DB = App.get_db()
4546
APP = App.get_app()
4647

47-
migrate = Migrate(APP, DB)
48-
49-
manager = Manager(APP)
50-
manager.add_command('db', MigrateCommand)
48+
Migrate(APP, DB)
5149

5250
if __name__ == '__main__':
53-
manager.run()
51+
action = ''
52+
if len(sys.argv) > 1:
53+
action = sys.argv[1]
54+
55+
with APP.app_context():
56+
if action == 'current':
57+
current()
58+
elif action == 'upgrade':
59+
# Upgrade to latest version
60+
upgrade()
61+
elif action == 'downgrade':
62+
# Downgrade one version back
63+
downgrade()
64+
elif action == 'migrate':
65+
# Generate revision
66+
migrate()
67+
elif action == 'history':
68+
# Show revisions
69+
history()
70+
elif action == 'revision':
71+
# Create new revision
72+
revision()

GeoHealthCheck/migrations/README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Database upgrade support
22

33
This dir contains various files for developing database upgrades.
4-
Upgrades are supported using Alembic via Flask-Migrate
5-
and Flask-Script.
6-
Users should be able to upgrade existing installs via:
4+
Upgrades are supported using Alembic via Flask-Migrate.
5+
Users should be able to upgrade existing installations via `pixi`:
76

87
# In top dir of installation
9-
invoke upgrade
8+
pixi run db-upgrade
9+
# or the equivalent
10+
python manage.py upgrade
1011

1112
The `versions` dir contains the various upgrades. These were
1213
initially created using the Alembic `autogenerate` facility
@@ -20,12 +21,12 @@ for various DB management tasks related to migrations and upgrading.
2021
Whenever a change in the database schema or table content
2122
conventions has changed a new migration should be created via the command.
2223

23-
python3 manage.py db migrate
24+
python manage.py migrate
2425

2526
Where `migrate` is an alias for `revision --autogenerate`.
2627
Alternatively if the autogeneration does not work, create an empty migration:
2728

28-
python3 manage.py db revision
29+
python manage.py revision
2930

3031
In both cases this will create a new revision and a `<revision_number>_.py` file
3132
under `versions/` to upgrade
@@ -37,9 +38,9 @@ to check various DB metadata.
3738

3839
Subsequently the upgrade can be performed using:
3940

40-
python3 manage.py db upgrade
41-
# or the equivalent (for users)
42-
invoke upgrade
41+
pixi run db-upgrade
42+
# or the equivalent
43+
python manage.py upgrade
4344

4445
## Revisions
4546

docker/scripts/run-web.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export PYTHONPATH=/GeoHealthCheck/GeoHealthCheck:$PYTHONPATH
1616
pushd /GeoHealthCheck || exit 1
1717

1818
# pixi shell -e prod
19-
pixi run -e prod upgrade-db
19+
pixi run -e prod db-upgrade
2020

2121
# SCRIPT_NAME should not have value '/'
2222
[ "${SCRIPT_NAME}" = '/' ] && export SCRIPT_NAME="" && echo "make SCRIPT_NAME empty from /"

pixi.lock

Lines changed: 22 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ dependencies = [
2222
"Flask==1.1.1",
2323
"Flask-Babel==0.12.2",
2424
"Flask-Login==0.4.1",
25-
"Flask-Migrate==2.5.2",
26-
"Flask-Script==2.0.6",
25+
"Flask-Migrate==4.1.0",
2726
"SQLAlchemy==1.3.8",
2827
"Flask-SQLAlchemy==2.4.0",
2928
"itsdangerous==1.1.0",
@@ -82,7 +81,8 @@ run = "python GeoHealthCheck/app.py"
8281
docs = "invoke refresh-docs"
8382
clean = "invoke clean"
8483
runner-daemon = "invoke runner-daemon"
85-
upgrade-db = "invoke upgrade"
84+
db-upgrade = "invoke db-upgrade"
85+
db-downgrade = "invoke db-downgrade"
8686
run-healthchecks = "invoke run-healthchecks"
8787
run-tests = "invoke run-tests"
8888

tasks.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,22 @@ def create_hash(c, password):
206206

207207

208208
@task
209-
def upgrade(c):
209+
def db_upgrade(c):
210210
"""upgrade database if changed; be sure to backup first!"""
211211

212212
print('Upgrading database...')
213213
os.chdir(BASEDIR / 'GeoHealthCheck')
214-
c.run('python manage.py db upgrade')
214+
c.run('python manage.py upgrade')
215+
os.chdir(BASEDIR)
216+
217+
218+
@task
219+
def db_downgrade(c):
220+
"""downgrade database to previous version; be sure to backup first!"""
221+
222+
print('Downgrading database...')
223+
os.chdir(BASEDIR / 'GeoHealthCheck')
224+
c.run('python manage.py downgrade')
215225
os.chdir(BASEDIR)
216226

217227

0 commit comments

Comments
 (0)