Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add allegations table #1131

Draft
wants to merge 16 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions OpenOversight/app/models/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,28 @@ class Incident(BaseModel, TrackUpdates):
)


class Allegation(BaseModel, TrackUpdates):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, I cannot get alembic to generate a migration for this table addition. Is there anything that looks out of place, @sea-kelp?

Copy link
Collaborator

@sea-kelp sea-kelp Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried a number of things with the LPL OO alembic but couldn't get it to generate the revision. I was however able to cherry pick the commits (613489b..f2977e4) into our OpenOversight repo and generate the revision there. I'm wondering if there's maybe a change that we haven't ported over yet that's causing the issue?
OrcaCollective@cadea71

Obviously, our models might be slightly different from yours so I wouldn't recommend directly using this generated migration

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember seeing some comments that each individual models needs to be imported into Alembic's env.py file. I'll take a look at the Orca one and see what the differences are.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's super hacky, but I added the table without it creating a migration and then removed the table and it created a migration. I'm just going to reverse the code on upgrade and downgrade, and take that result. I'm not super happy with how it ended up working, but I'll take it at this point.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I downgrade and then upgrade the flask db command, it will error since the table will already be created when the application starts out.

I then continually used the flask db downgrade command and we get errors for other downgrades as well. I think this means we are consistent, but there are certainly some difficulties between the ORM and flask to maybe look into at some point.

__tablename__ = "allegations"

id = db.Column(db.Integer, primary_key=True)
incident_id = db.Column(
db.Integer, db.ForeignKey("incidents.id", name="allegations_incident_id_fkey")
)
officer_id = db.Column(
db.Integer, db.ForeignKey("officers.id", name="allegations_officer_id_fkey")
)
disposition = db.Column(db.Text(), nullable=False)
discipline = db.Column(db.Text(), nullable=True)
type = db.Column(db.Text(), nullable=False)
finding = db.Column(db.Text(), nullable=True)
officer = db.relationship(
"Officer", backref=db.backref("allegations", cascade_backrefs=False), lazy=True
)
incident = db.relationship(
"Incident", backref=db.backref("allegations", cascade_backrefs=False), lazy=True
)


class User(UserMixin, BaseModel):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""make allegations table

Revision ID: 8895cbef5743
Revises: 99c50fc8d294
Create Date: 2024-10-29 17:55:02.500162

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql


revision = "8895cbef5743"
down_revision = "99c50fc8d294"


def upgrade():
op.create_table(
"allegations",
sa.Column(
"created_at",
postgresql.TIMESTAMP(timezone=True),
server_default=sa.text("now()"),
autoincrement=False,
nullable=False,
),
sa.Column(
"last_updated_at",
postgresql.TIMESTAMP(timezone=True),
server_default=sa.text("now()"),
autoincrement=False,
nullable=False,
),
sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column("incident_id", sa.INTEGER(), autoincrement=False, nullable=True),
sa.Column("officer_id", sa.INTEGER(), autoincrement=False, nullable=True),
sa.Column("disposition", sa.TEXT(), autoincrement=False, nullable=False),
sa.Column("discipline", sa.TEXT(), autoincrement=False, nullable=True),
sa.Column("type", sa.TEXT(), autoincrement=False, nullable=False),
sa.Column("finding", sa.TEXT(), autoincrement=False, nullable=True),
sa.Column("created_by", sa.INTEGER(), autoincrement=False, nullable=True),
sa.Column("last_updated_by", sa.INTEGER(), autoincrement=False, nullable=True),
sa.ForeignKeyConstraint(
["created_by"],
["users.id"],
name="allegations_created_by_fkey",
ondelete="SET NULL",
),
sa.ForeignKeyConstraint(
["incident_id"], ["incidents.id"], name="allegations_incident_id_fkey"
),
sa.ForeignKeyConstraint(
["last_updated_by"],
["users.id"],
name="allegations_last_updated_by_fkey",
ondelete="SET NULL",
),
sa.ForeignKeyConstraint(
["officer_id"], ["officers.id"], name="allegations_officer_id_fkey"
),
sa.PrimaryKeyConstraint("id", name="allegations_pkey"),
)


def downgrade():
op.drop_table("allegations")
Loading