Skip to content

Commit

Permalink
Restore officer_links table
Browse files Browse the repository at this point in the history
  • Loading branch information
dismantl committed May 5, 2019
1 parent ee68309 commit 3ff9d28
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
13 changes: 10 additions & 3 deletions OpenOversight/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
db = SQLAlchemy()


officer_links = db.Table('officer_links',
db.Column('officer_id', db.Integer, db.ForeignKey('officers.id'), primary_key=True),
db.Column('link_id', db.Integer, db.ForeignKey('links.id'), primary_key=True))

officer_incidents = db.Table('officer_incidents',
db.Column('officer_id', db.Integer, db.ForeignKey('officers.id'), primary_key=True),
db.Column('incident_id', db.Integer, db.ForeignKey('incidents.id'), primary_key=True))
Expand Down Expand Up @@ -98,7 +102,12 @@ class Officer(db.Model):
department_id = db.Column(db.Integer, db.ForeignKey('departments.id'))
department = db.relationship('Department', backref='officers')
unique_internal_identifier = db.Column(db.String(50), index=True, unique=True, nullable=True)
links = db.relationship('Link', back_populates='officer')
# we don't expect to pull up officers via link often so we make it lazy.
links = db.relationship(
'Link',
secondary=officer_links,
lazy='subquery',
backref=db.backref('officers', lazy=True))
notes = db.relationship('Note', back_populates='officer', order_by='Note.date_created')
descriptions = db.relationship('Description', back_populates='officer', order_by='Description.date_created')
salaries = db.relationship('Salary', back_populates='officer', order_by='Salary.year.desc()')
Expand Down Expand Up @@ -332,8 +341,6 @@ class Link(db.Model):
author = db.Column(db.String(255), nullable=True)
creator_id = db.Column(db.Integer, db.ForeignKey('users.id', ondelete='SET NULL'))
creator = db.relationship('User', backref='links', lazy=True)
officer_id = db.Column(db.Integer, db.ForeignKey('officers.id', ondelete='CASCADE'), nullable=True)
officer = db.relationship('Officer', back_populates='links')

@validates('url')
def validate_url(self, key, url):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% if obj.links|length > 0 %}
{% if obj.links|length > 0 or current_user.is_administrator
or (current_user.is_area_coordinator and current_user.ac_department_id == obj.department_id) %}
<div class='row'>
<div class="col-sm-12 col-md-6">
<h3>Links</h3>
Expand Down
21 changes: 3 additions & 18 deletions OpenOversight/migrations/versions/86eb228e4bc0_refactor_links.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"""Refactor links
Revision ID: 86eb228e4bc0
Revises: 2a9064a2507c
Revises: 3015d1dd9eb4
Create Date: 2019-03-15 22:40:10.473917
"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '86eb228e4bc0'
down_revision = '2a9064a2507c'
down_revision = '3015d1dd9eb4'
branch_labels = None
depends_on = None

Expand All @@ -19,27 +18,13 @@ def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint('links_user_id_fkey', 'links', type_='foreignkey')
op.alter_column('links', 'user_id', new_column_name='creator_id')
op.create_foreign_key(None, 'links', 'users', ['creator_id'], ['id'], ondelete='SET NULL')
op.add_column('links', sa.Column('officer_id', sa.Integer(), nullable=True))
op.create_foreign_key(None, 'links', 'officers', ['officer_id'], ['id'], ondelete='CASCADE')
op.execute('UPDATE links SET officer_id = officer_links.officer_id FROM officer_links WHERE officer_links.link_id = links.id')
op.drop_table('officer_links')
op.create_foreign_key('links_creator_id_fkey', 'links', 'users', ['creator_id'], ['id'], ondelete='SET NULL')
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
'officer_links',
sa.Column('officer_id', sa.INTEGER(), autoincrement=False, nullable=False),
sa.Column('link_id', sa.INTEGER(), autoincrement=False, nullable=False),
sa.ForeignKeyConstraint(['link_id'], ['links.id'], name='officer_links_link_id_fkey'),
sa.ForeignKeyConstraint(['officer_id'], ['officers.id'], name='officer_links_officer_id_fkey'),
sa.PrimaryKeyConstraint('officer_id', 'link_id', name='officer_links_pkey'))
op.drop_constraint('links_creator_id_fkey', 'links', type_='foreignkey')
op.drop_constraint('links_officer_id_fkey', 'links', type_='foreignkey')
op.alter_column('links', 'creator_id', new_column_name='user_id')
op.create_foreign_key('links_user_id_fkey', 'links', 'users', ['user_id'], ['id'])
op.execute('INSERT INTO officer_links (officer_id, link_id) SELECT officer_id, id FROM links')
op.drop_column('links', 'officer_id')
# ### end Alembic commands ###

0 comments on commit 3ff9d28

Please sign in to comment.