Skip to content

Commit

Permalink
Delete create_db.py (#1000)
Browse files Browse the repository at this point in the history
## Fixes issue
#675

## Description of Changes
Removed `create_db.py` file, moved the logic to the Alembic file, and
created constants for the code around my changes.

## Notes for Deployment
If it works in development, it SHOULD work in production. 

## Tests and linting
 - [x] This branch is up-to-date with the `develop` branch.
 - [x] `pytest` passes on my local development environment.
 - [x] `pre-commit` passes on my local development environment.
 - [x] Ran the `make` default command and there were not any issues.

<details><summary>make dev output</summary>

```console
% make dev
docker-compose build
...
WARN[0000] The "APPROVE_REGISTRATIONS" variable is not set. Defaulting to a blank string. 
[+] Running 2/0
 ✔ Container openoversight-postgres-1  Running                                                                                                                                                             0.0s 
 ✔ Container openoversight-web-1       Running                                                                                                                                                             0.0s 
Postgres is up
## Creating database
docker-compose run --rm web alembic --config=./OpenOversight/migrations/alembic.ini stamp head
...
Postgres is up
## Populate database with test data
docker-compose run --rm web python ./test_data.py -p
...
2023-08-01 21:43:54,796 INFO sqlalchemy.engine.Engine [generated in 0.00006s] {'pk_1': 1}
[*] Completed successfully!
% 
```
</details>
  • Loading branch information
michplunkett committed Aug 1, 2023
1 parent 0287352 commit 495a9a0
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ repos:
description: Only executable files should have shebangs (e.g. '#!/usr/bin/env python')
entry: "#!/"
pass_filenames: true
exclude: bin|cli|manage.py|app.py|setup.py|docs|create_db.py|test_data.py
exclude: bin|cli|manage.py|app.py|setup.py|docs|test_data.py
files: \.py$

- repo: https://github.com/ikamensh/flynt/
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ create_db: start
done
@echo "Postgres is up"
## Creating database
docker-compose run --rm web python ./create_db.py
docker-compose run --rm web alembic --config=./OpenOversight/migrations/alembic.ini stamp head

.PHONY: assets
assets:
Expand Down
13 changes: 9 additions & 4 deletions OpenOversight/app/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
User,
db,
)
from OpenOversight.app.utils.constants import ENCODING_UTF_8
from OpenOversight.app.utils.constants import (
ENCODING_UTF_8,
KEY_ENV,
KEY_ENV_PROD,
KEY_ENV_TESTING,
)
from OpenOversight.app.utils.db import get_officer
from OpenOversight.app.utils.general import normalize_gender, prompt_yes_no, str_is_true

Expand Down Expand Up @@ -548,10 +553,10 @@ def bulk_add_officers(filename, no_create, update_by_name, update_static_fields)
create_officer_from_row(row, department_id)

ImportLog.print_logs()
if current_app.config["ENV"] == "testing" or prompt_yes_no(
if current_app.config[KEY_ENV] == KEY_ENV_TESTING or prompt_yes_no(
"Do you want to commit the above changes?"
):
print("Commiting changes.")
print("Committing changes.")
db.session.commit()
else:
print("Aborting changes.")
Expand Down Expand Up @@ -597,7 +602,7 @@ def advanced_csv_import(
See the documentation before running the command.
"""
if force_create and current_app.config["ENV"] == "production":
if force_create and current_app.config[KEY_ENV] == KEY_ENV_PROD:
raise Exception("--force-create cannot be used in production!")

import_csv_files(
Expand Down
17 changes: 11 additions & 6 deletions OpenOversight/app/models/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import os

from OpenOversight.app.utils.constants import (
KEY_DATABASE_URI,
KEY_ENV,
KEY_ENV_DEV,
KEY_ENV_PROD,
KEY_ENV_TESTING,
KEY_OFFICERS_PER_PAGE,
KEY_TIMEZONE,
MEGABYTE,
Expand All @@ -14,7 +19,7 @@ class BaseConfig:
def __init__(self):
# App Settings
self.DEBUG = False
self.ENV = os.environ.get("ENV", "development")
self.ENV = os.environ.get(KEY_ENV, KEY_ENV_DEV)
self.SEED = 666
self.TIMEZONE = os.environ.get(KEY_TIMEZONE, "America/Chicago")
self.TESTING = False
Expand All @@ -24,7 +29,7 @@ def __init__(self):

# DB Settings
self.SQLALCHEMY_TRACK_MODIFICATIONS = False
self.SQLALCHEMY_DATABASE_URI = os.environ.get("SQLALCHEMY_DATABASE_URI")
self.SQLALCHEMY_DATABASE_URI = os.environ.get(KEY_DATABASE_URI)

# Protocol Settings
self.SITEMAP_URL_SCHEME = "http"
Expand Down Expand Up @@ -85,8 +90,8 @@ def __init__(self):


config = {
"development": DevelopmentConfig(),
"testing": TestingConfig(),
"production": ProductionConfig(),
KEY_ENV_DEV: DevelopmentConfig(),
KEY_ENV_TESTING: TestingConfig(),
KEY_ENV_PROD: ProductionConfig(),
}
config["default"] = config.get(os.environ.get("ENV", ""), DevelopmentConfig())
config["default"] = config.get(os.environ.get(KEY_ENV, ""), DevelopmentConfig())
5 changes: 5 additions & 0 deletions OpenOversight/app/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
KEY_TOTAL_OFFICERS = "total_officers"

# Config Key Constants
KEY_DATABASE_URI = "SQLALCHEMY_DATABASE_URI"
KEY_ENV = "ENV"
KEY_ENV_DEV = "development"
KEY_ENV_TESTING = "testing"
KEY_ENV_PROD = "production"
KEY_OFFICERS_PER_PAGE = "OFFICERS_PER_PAGE"
KEY_TIMEZONE = "TIMEZONE"

Expand Down
1 change: 1 addition & 0 deletions OpenOversight/migrations/alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
script_location = ./OpenOversight/migrations/
file_template = %%(year)d-%%(month).2d-%%(day).2d-%%(hour).2d%%(minute).2d_%%(rev)s_%%(slug)s
truncate_slug_length = 40
prepend_sys_path = .

[loggers]
keys = root,sqlalchemy,alembic
Expand Down
26 changes: 16 additions & 10 deletions OpenOversight/migrations/env.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import logging
import os
from logging.config import fileConfig

from alembic import context
from flask import current_app
from sqlalchemy import engine_from_config, pool

from OpenOversight.app import create_app, db
from OpenOversight.app.utils.constants import KEY_DATABASE_URI, KEY_ENV, KEY_ENV_DEV


app = create_app(os.environ.get(KEY_ENV, KEY_ENV_DEV))
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
Expand All @@ -14,12 +19,6 @@
logger = logging.getLogger("alembic.env")


config.set_main_option(
"sqlalchemy.url", current_app.config.get("SQLALCHEMY_DATABASE_URI")
)
target_metadata = current_app.extensions["migrate"].db.metadata


def run_migrations_offline():
"""Run migrations in 'offline' mode.
Expand Down Expand Up @@ -78,7 +77,14 @@ def process_revision_directives(context, revision, directives):
connection.close()


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
with app.app_context():
config.set_main_option("sqlalchemy.url", current_app.config.get(KEY_DATABASE_URI))
target_metadata = current_app.extensions["migrate"].db.metadata

db.app = app
db.create_all()

if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
23 changes: 18 additions & 5 deletions OpenOversight/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
)
from OpenOversight.app.models.database import db as _db
from OpenOversight.app.utils.choices import DEPARTMENT_STATE_CHOICES
from OpenOversight.app.utils.constants import ENCODING_UTF_8
from OpenOversight.app.utils.constants import ENCODING_UTF_8, KEY_ENV_TESTING
from OpenOversight.app.utils.general import merge_dicts
from OpenOversight.tests.routes.route_helpers import ADMIN_EMAIL, ADMIN_PASSWORD

Expand All @@ -51,10 +51,23 @@
class PoliceDepartment:
"""Base Police Department class."""

def __init__(self, name, short_name, state="", unique_internal_identifier_label=""):
def __init__(
self,
name,
short_name,
state="",
unique_internal_identifier_label="",
exclude_state="",
):
self.name = name
self.short_name = short_name
self.state = state if state else random.choice(DEPARTMENT_STATE_CHOICES)[0]
self.state = (
state
if state
else random.choice(
[s for s in DEPARTMENT_STATE_CHOICES if s[0] != exclude_state]
)[0]
)
self.unique_internal_identifier_label = (
unique_internal_identifier_label
if unique_internal_identifier_label
Expand Down Expand Up @@ -84,7 +97,7 @@ def __init__(self, name, short_name, state="", unique_internal_identifier_label=

AC_DEPT = 1
NO_OFFICER_PD = PoliceDepartment("Empty Police Department", "EPD")
OTHER_PD = PoliceDepartment("Chicago Police Department", "CPD")
OTHER_PD = PoliceDepartment("Chicago Police Department", "CPD", exclude_state="IL")
SPRINGFIELD_PD = PoliceDepartment("Springfield Police Department", "SPD", "IL")


Expand Down Expand Up @@ -234,7 +247,7 @@ def assign_faces(officer, images):
@pytest.fixture(scope="session")
def app(request):
"""Session-wide test `Flask` application."""
app = create_app("testing")
app = create_app(KEY_ENV_TESTING)
app.config["WTF_CSRF_ENABLED"] = False

yield app
Expand Down
18 changes: 3 additions & 15 deletions OpenOversight/tests/routes/test_officer_and_department.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@
Unit,
User,
)
from OpenOversight.app.utils.choices import (
DEPARTMENT_STATE_CHOICES,
GENDER_CHOICES,
RACE_CHOICES,
)
from OpenOversight.app.utils.choices import GENDER_CHOICES, RACE_CHOICES
from OpenOversight.app.utils.constants import ENCODING_UTF_8
from OpenOversight.app.utils.db import unit_choices
from OpenOversight.app.utils.forms import add_new_assignment
Expand Down Expand Up @@ -589,11 +585,7 @@ def test_admin_can_edit_police_department(mockdata, client, session):
with current_app.test_request_context():
# Prevent CorrectedPD and MisspelledPD from having the same state
MisspelledPD = PoliceDepartment(
"Misspelled Police Department",
"MPD",
random.choice(
[st[0] for st in DEPARTMENT_STATE_CHOICES if st[0] != CorrectedPD.state]
),
"Misspelled Police Department", "MPD", exclude_state=CorrectedPD.state
)

login_admin(client)
Expand Down Expand Up @@ -1034,11 +1026,7 @@ def test_admin_can_create_department_with_same_name_in_different_state(

# Make sure ExistingPD and ExistingDiffStatePD don't exist in the same state
ExistingDiffStatePD = PoliceDepartment(
"Existing Police Department",
"EPD",
random.choice(
[st[0] for st in DEPARTMENT_STATE_CHOICES if st[0] != ExistingPD.state]
),
"Existing Police Department", "EPD", exclude_state=ExistingPD.state
)

existing_diff_state_form = DepartmentForm(
Expand Down
9 changes: 0 additions & 9 deletions create_db.py

This file was deleted.

2 changes: 1 addition & 1 deletion dockerfiles/web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ RUN test "${DOCKER_BUILD_ENV}" = production || pip3 install --no-cache-dir -r de
COPY package.json /usr/src/app/
RUN yarn

COPY create_db.py test_data.py /usr/src/app/
COPY test_data.py /usr/src/app/
COPY .flake8 /usr/src/app/
COPY mypy.ini /usr/src/app/
EXPOSE 3000
Expand Down
2 changes: 1 addition & 1 deletion dockerfiles/web/Dockerfile-prod
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ RUN pip3 install -r requirements.txt
COPY package.json /usr/src/app/
RUN yarn
# Copy python script to initialize database.
COPY create_db.py /usr/src/app/
COPY /usr/src/app/

WORKDIR /usr/src/app/

Expand Down
3 changes: 2 additions & 1 deletion test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

from OpenOversight.app import create_app
from OpenOversight.app.models.database import db
from OpenOversight.app.utils.constants import KEY_ENV_DEV
from OpenOversight.tests.conftest import add_mockdata


app = create_app("development")
app = create_app(KEY_ENV_DEV)
ctx = app.app_context()
ctx.push()
db.app = app
Expand Down

0 comments on commit 495a9a0

Please sign in to comment.