Skip to content

Commit cc0d0dd

Browse files
committed
#510 6th PoC - fixes and improvements DB actions manage.py and friends
1 parent ecdb24f commit cc0d0dd

8 files changed

Lines changed: 42 additions & 47 deletions

File tree

.github/workflows/main.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ 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
40+
- name: Test DB downgrade to previous schema (via Alembic) ⚙️
41+
run: pixi run -e dev db-action downgrade
42+
43+
- name: Test DB upgrade back to current schema (via Alembic) ⚙️
44+
run: pixi run -e dev db-action upgrade
4545

4646
- name: Run Probes ⚙️
4747
run: pixi run -e dev run-healthchecks

Dockerfile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,16 @@ RUN \
9191
&& echo "For ${TZ} date=$(date)" && echo "Locale=$(locale)" \
9292
&& adduser --disabled-password --shell /bin/bash --home ${GHC_HOME} --gecos "User" ghc \
9393
&& chmod +x /*.sh \
94-
&& echo "pixi install --locked -e prod" && pixi install --locked -e prod \
95-
&& echo "pixi run -e prod setup" && pixi run -e prod setup \
96-
&& echo "cp /config_site.py " && cp /config_site.py ${GHC_HOME}/instance/config_site.py \
97-
&& echo "copy plugins.." && if [ -d /plugins ]; then cp -ar /plugins/* ${GHC_HOME}/GeoHealthCheck/plugins/; fi && rm -rf /plugins \
94+
&& pixi install --locked -e prod \
95+
&& pixi run -e prod setup \
96+
&& cp /config_site.py ${GHC_HOME}/instance/config_site.py \
97+
&& if [ -d /plugins ]; then cp -ar /plugins/* ${GHC_HOME}/GeoHealthCheck/plugins/; fi && rm -rf /plugins \
9898
&& apt-get remove --purge -y ${DEB_BUILD_DEPS} \
9999
&& apt-get clean \
100100
&& apt autoremove -y \
101101
&& rm -rf /var/lib/apt/lists/*
102102

103+
# For later: run as user 'ghc'
103104
# USER ghc
104105

105106
# For SQLite

GeoHealthCheck/manage.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,16 @@
77
# Usage:
88
#
99
# $ python3 manage.py --help
10-
# usage: manage.py [-h] {shell,db,runserver} ...
11-
#
12-
# positional arguments:
13-
# {shell,db,runserver}
14-
# shell Runs a Python shell inside Flask application context.
15-
# db Perform database migrations
16-
# runserver Runs the Flask development server i.e. app.run()
10+
# usage: manage.py action
1711
#
1812
# optional arguments:
1913
# -h, --help show this help message and exit
2014
#
2115
# For DB management:
22-
# $ python3 manage.py db --help
16+
# $ python manage.py upgrade
2317
# usage: Perform database migrations
2418
#
25-
# positional arguments:
19+
# action arguments:
2620
# {upgrade,migrate,current,stamp,init,downgrade,history,revision}
2721
# upgrade Upgrade to a later version
2822
# migrate Alias for 'revision --autogenerate'
@@ -37,20 +31,28 @@
3731
# optional arguments:
3832
# -h, --help show this help message and exit
3933

34+
import sys
35+
import os
4036
from flask_migrate import (Migrate, upgrade, downgrade, current,
4137
migrate, history, revision)
4238
from init import App
43-
import sys
4439

4540
DB = App.get_db()
4641
APP = App.get_app()
47-
48-
Migrate(APP, DB)
42+
workdir_path = os.path.dirname(__file__)
43+
migrations_path = os.path.join(workdir_path, 'migrations')
44+
Migrate(APP, DB, directory=migrations_path)
45+
ACTIONS = ['current', 'upgrade', 'downgrade', 'migrate', 'history', 'revision']
4946

5047
if __name__ == '__main__':
51-
action = ''
52-
if len(sys.argv) > 1:
53-
action = sys.argv[1]
48+
if len(sys.argv) < 1 or sys.argv[1] not in ACTIONS:
49+
print(f'Invalid action, valid values: {ACTIONS}')
50+
sys.exit(1)
51+
52+
# Valid action name
53+
action = sys.argv[1]
54+
55+
os.chdir(workdir_path)
5456

5557
with APP.app_context():
5658
if action == 'current':

GeoHealthCheck/migrations/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Upgrades are supported using Alembic via Flask-Migrate.
55
Users should be able to upgrade existing installations via `pixi`:
66

77
# In top dir of installation
8-
pixi run db-upgrade
8+
pixi run db-action upgrade
99
# or the equivalent
1010
python manage.py upgrade
1111

@@ -38,7 +38,7 @@ to check various DB metadata.
3838

3939
Subsequently the upgrade can be performed using:
4040

41-
pixi run db-upgrade
41+
pixi run db-action upgrade
4242
# or the equivalent
4343
python manage.py upgrade
4444

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 db-upgrade
19+
pixi run -e prod db-action upgrade
2020

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

docs/install.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ An existing GHC database installation can be upgraded with:
105105
.. code-block:: bash
106106
107107
# In the top directory (e.g. the topdir cloned from github)
108-
invoke upgrade
108+
invoke db-action upgrade
109109
110110
# Notice any output, in particular errors
111111
@@ -177,10 +177,10 @@ were added:
177177
* OGC 3DTiles Probe (by SpotInfo)
178178
* MapBox TileJSON Probe (by SpotInfo)
179179
* additional WMTS Probes (by SpotInfo)
180-
* use official OGC naming for OAFeat Probes (includes DB-upgrade)
180+
* use official OGC naming for OAFeat Probes (includes db-action upgrade)
181181
* many bugfixes and security updates
182182

183-
Only a single DB-upgrade is required and only if your installation (DB) contains
183+
Only a single db-action upgrade is required and only if your installation (DB) contains
184184
OGC OAFeat Resources and Probes, formerly called "WFS3".
185185

186186
See `closed issues/merged PRs for related Milestone 0.9.0 <https://github.com/geopython/GeoHealthCheck/milestone/10?closed=1>`_.

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ license = { text = "MIT" }
88
authors = [
99
{ name = "Just van den Broecke", email = "just@justobjects.nl" },
1010
{ name = "Tom Kralidis", email = "tomkralidis@gmail.com" },
11+
{ name = "Francesco Bartoli" },
1112
]
1213
# Complete runtime dependency set, readable by pip and Dependabot.
1314
# Hybrid sourcing: the native/geo libs listed here AND in
@@ -81,8 +82,7 @@ run = "python GeoHealthCheck/app.py"
8182
docs = "invoke refresh-docs"
8283
clean = "invoke clean"
8384
runner-daemon = "invoke runner-daemon"
84-
db-upgrade = "invoke db-upgrade"
85-
db-downgrade = "invoke db-downgrade"
85+
db-action = "invoke db-action"
8686
run-healthchecks = "invoke run-healthchecks"
8787
run-tests = "invoke run-tests"
8888

tasks.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,15 @@ def create_hash(c, password):
206206

207207

208208
@task
209-
def db_upgrade(c):
210-
"""upgrade database if changed; be sure to backup first!"""
211-
212-
print('Upgrading database...')
213-
os.chdir(BASEDIR / 'GeoHealthCheck')
214-
c.run('python manage.py upgrade')
215-
os.chdir(BASEDIR)
209+
def db_action(c, action):
210+
"""Execute database action, e.g. upgrade, downgrade"""
216211

212+
if action is None:
213+
print('Usage: db-action (action), or try help')
214+
return
217215

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')
225-
os.chdir(BASEDIR)
216+
print(f'Database action={action}')
217+
c.run(f'python GeoHealthCheck/manage.py {action}')
226218

227219

228220
@task

0 commit comments

Comments
 (0)