Skip to content

Commit aab0cc6

Browse files
Revert "Delete create_db.py (#1000)" (#1152)
## Fixes issue #1000 unfortunately broke alembic's functionality to create migrations that include new tables (Using `flask db migrate`). The issue is that before alembic has a change to analyze the difference between existing database and schema from database.py the `db.create_all()` command automatically creates the tables making it so that they are not registered for the migration. I could not come up with a way to reasonably work around this issue, so I think the best course of action is to revert the removal of the create_db.py file. ## Description of Changes This reverts that breaking commit. It is not a clean revert of #1000, since that PR also included unrelated changes to tests and constants. Those changes were not reverted. ## Notes for Deployment I have verified the changes in my local environment and the tests are passing, but I have not tested that the changes work as expected in a completely fresh environment (which would use the create_db file) ## Tests and Linting - [x] This branch is up-to-date with the `develop` branch. - [ ] (N/A) Ran `make create_db_diagram` command. - [x] `pytest` passes on my local development environment. - [x] `pre-commit` passes on my local development environment. --------- Co-authored-by: michplunkett <5885605+michplunkett@users.noreply.github.com>
1 parent ae30e6c commit aab0cc6

File tree

8 files changed

+25
-20
lines changed

8 files changed

+25
-20
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ repos:
5252
description: Only executable files should have shebangs (e.g. '#!/usr/bin/env python')
5353
entry: "#!/"
5454
pass_filenames: true
55-
exclude: bin|cli|manage.py|app.py|setup.py|docs|test_data.py
55+
exclude: bin|cli|docs|test_data.py
5656
files: \.py$
5757

5858
- repo: https://github.com/ikamensh/flynt/

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ create_db: start
2929
done
3030
@echo "Postgres is up"
3131
## Creating database
32-
docker compose run --rm web flask db stamp head
32+
docker compose run --rm web python ./create_db.py
3333

3434
.PHONY: create_db_diagram
3535
create_db_diagram:

OpenOversight/migrations/alembic.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
script_location = ./OpenOversight/migrations/
33
file_template = %%(year)d-%%(month).2d-%%(day).2d-%%(hour).2d%%(minute).2d_%%(rev)s_%%(slug)s
44
truncate_slug_length = 40
5-
prepend_sys_path = .
65

76
[loggers]
87
keys = root,sqlalchemy,alembic

OpenOversight/migrations/env.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import logging
2-
import os
32
from logging.config import fileConfig
43

54
from alembic import context
65
from flask import current_app
76
from sqlalchemy import engine_from_config, pool
87

9-
from OpenOversight.app import create_app, db
10-
from OpenOversight.app.utils.constants import KEY_DATABASE_URI, KEY_ENV, KEY_ENV_DEV
8+
from OpenOversight.app.utils.constants import KEY_DATABASE_URI
119

1210

13-
app = create_app(os.environ.get(KEY_ENV, KEY_ENV_DEV))
1411
# this is the Alembic Config object, which provides
1512
# access to the values within the .ini file in use.
1613
config = context.config
@@ -19,6 +16,10 @@
1916
logger = logging.getLogger("alembic.env")
2017

2118

19+
config.set_main_option("sqlalchemy.url", current_app.config.get(KEY_DATABASE_URI))
20+
target_metadata = current_app.extensions["migrate"].db.metadata
21+
22+
2223
def run_migrations_offline():
2324
"""Run migrations in 'offline' mode.
2425
@@ -77,14 +78,7 @@ def process_revision_directives(context, revision, directives):
7778
connection.close()
7879

7980

80-
with app.app_context():
81-
config.set_main_option("sqlalchemy.url", current_app.config.get(KEY_DATABASE_URI))
82-
target_metadata = current_app.extensions["migrate"].db.metadata
83-
84-
db.app = app
85-
db.create_all()
86-
87-
if context.is_offline_mode():
88-
run_migrations_offline()
89-
else:
90-
run_migrations_online()
81+
if context.is_offline_mode():
82+
run_migrations_offline()
83+
else:
84+
run_migrations_online()

create_db.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from OpenOversight.app import create_app
2+
from OpenOversight.app.models.database import db
3+
from OpenOversight.app.utils.constants import KEY_ENV_DEV
4+
5+
6+
app = create_app(KEY_ENV_DEV)
7+
with app.app_context():
8+
db.app = app
9+
db.create_all()

dockerfiles/web/Dockerfile-dev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ RUN apt-get update && apt-get install -y xvfb libpq-dev python3-dev graphviz-dev
1515
COPY requirements.txt /usr/src/app/
1616
RUN pip3 install --no-cache-dir -r requirements.txt
1717

18-
COPY test_data.py /usr/src/app/
18+
COPY create_db.py test_data.py /usr/src/app/
1919

2020
ENV SECRET_KEY 4Q6ZaQQdiqtmvZaxP1If
2121
ENV SQLALCHEMY_DATABASE_URI postgresql://openoversight:terriblepassword@postgres/openoversight-dev

dockerfiles/web/Dockerfile-prod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ COPY requirements.txt /usr/src/app/
2828
# Install required python packages with pip.
2929
RUN pip3 install -r requirements.txt
3030

31+
# Copy python script to initialize database.
32+
COPY create_db.py /usr/src/app/
33+
3134
WORKDIR /usr/src/app/
3235

3336
FROM base as production

dockerfiles/web/Dockerfile-test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ RUN curl https://sh.rustup.rs -sSf | sh -s -- -y && \
3030
pip3 install --no-cache-dir -r requirements.txt && \
3131
pip3 install --no-cache-dir -r dev-requirements.txt
3232

33-
COPY test_data.py /usr/src/app/
33+
COPY create_db.py test_data.py /usr/src/app/
3434

3535
ENV PATH="/usr/src/app/geckodriver:${PATH}"
3636
ENV SECRET_KEY 4Q6ZaQQdiqtmvZaxP1If

0 commit comments

Comments
 (0)