Skip to content

Commit

Permalink
API - break out ownership and person in the db (#204)
Browse files Browse the repository at this point in the history
* API - break out ownership and person in the db

Signed-off-by: Kial Jinnah <[email protected]>

* tweak

Signed-off-by: Kial Jinnah <[email protected]>

* fix UI test

Signed-off-by: Kial Jinnah <[email protected]>

* UI test / lint fixes

Signed-off-by: Kial Jinnah <[email protected]>

* Updated pm tests, added statement id interest conncected people mapping

Signed-off-by: Kial Jinnah <[email protected]>

* PR comment update

Signed-off-by: Kial Jinnah <[email protected]>

---------

Signed-off-by: Kial Jinnah <[email protected]>
  • Loading branch information
kialj876 authored Sep 26, 2024
1 parent bea6a4f commit 9eb1065
Show file tree
Hide file tree
Showing 30 changed files with 1,219 additions and 353 deletions.
10 changes: 10 additions & 0 deletions btr-api/migrations/versions/20230821_191f8d1ddc48_.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,13 @@ def downgrade():

op.drop_table('users')
# ### end Alembic commands ###
op.execute(f"""
DELETE
FROM pg_enum
WHERE enumtypid = (SELECT oid FROM pg_type WHERE typname='submissiontype')
""")
op.execute(f"""
DELETE
FROM pg_type
WHERE typname='submissiontype'
""")
192 changes: 192 additions & 0 deletions btr-api/migrations/versions/20240924_0922_8d8279a86def_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
"""empty message
Revision ID: 8d8279a86def
Revises: 8850fb7d527b
Create Date: 2024-09-24 09:22:07.352266
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '8d8279a86def'
down_revision = '8850fb7d527b'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('person',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('statement_id', sa.Uuid(), nullable=False),
sa.Column('person_json', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column('birthdate', sa.Date(), nullable=True),
sa.Column('is_public', sa.Boolean(), nullable=False),
sa.Column('last_notification_type', sa.Enum('ADDING_ADULT', 'ADDING_MINOR', 'UPDATING_MINOR_PUBLIC', name='emailtype'), nullable=True),
sa.Column('last_notification_datetime', sa.DateTime(), nullable=True),
sa.Column('version', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('statement_id'),
sqlite_autoincrement=True
)
op.create_table('person_history',
sa.Column('id', sa.Integer(), autoincrement=False, nullable=False),
sa.Column('statement_id', sa.Uuid(), autoincrement=False, nullable=False),
sa.Column('person_json', postgresql.JSONB(astext_type=sa.Text()), autoincrement=False, nullable=False),
sa.Column('birthdate', sa.Date(), autoincrement=False, nullable=True),
sa.Column('is_public', sa.Boolean(), autoincrement=False, nullable=False),
sa.Column('last_notification_type', sa.Enum('ADDING_ADULT', 'ADDING_MINOR', 'UPDATING_MINOR_PUBLIC', name='emailtype'), autoincrement=False, nullable=True),
sa.Column('last_notification_datetime', sa.DateTime(), autoincrement=False, nullable=True),
sa.Column('version', sa.Integer(), autoincrement=False, nullable=False),
sa.Column('changed', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id', 'version'),
sqlite_autoincrement=True
)
op.create_table('ownership',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('statement_id', sa.Uuid(), nullable=False),
sa.Column('ownership_json', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column('person_id', sa.Integer(), nullable=True),
sa.Column('submission_id', sa.Integer(), nullable=True),
sa.Column('version', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['person_id'], ['person.id'], ),
sa.ForeignKeyConstraint(['submission_id'], ['submission.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('statement_id'),
sqlite_autoincrement=True
)
op.create_table('ownership_history',
sa.Column('id', sa.Integer(), autoincrement=False, nullable=False),
sa.Column('statement_id', sa.Uuid(), autoincrement=False, nullable=False),
sa.Column('ownership_json', postgresql.JSONB(astext_type=sa.Text()), autoincrement=False, nullable=False),
sa.Column('person_id', sa.Integer(), autoincrement=False, nullable=True),
sa.Column('submission_id', sa.Integer(), autoincrement=False, nullable=True),
sa.Column('version', sa.Integer(), autoincrement=False, nullable=False),
sa.Column('changed', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['person_id'], ['person.id'], ),
sa.ForeignKeyConstraint(['submission_id'], ['submission.id'], ),
sa.PrimaryKeyConstraint('id', 'version'),
sqlite_autoincrement=True
)
with op.batch_alter_table('submission', schema=None) as batch_op:
batch_op.alter_column('type',
existing_type=postgresql.ENUM('other', 'standard', name='submissiontype'),
nullable=False)
batch_op.alter_column('submitted_datetime',
existing_type=postgresql.TIMESTAMP(timezone=True),
type_=sa.DateTime(),
nullable=False,
existing_server_default=sa.text('now()'))
batch_op.alter_column('submitted_payload',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
nullable=False)
batch_op.alter_column('submitter_id',
existing_type=sa.INTEGER(),
nullable=False)
batch_op.drop_column('payload')

with op.batch_alter_table('submission_history', schema=None) as batch_op:
batch_op.alter_column('type',
existing_type=postgresql.ENUM('other', 'standard', name='submissiontype'),
nullable=False,
autoincrement=False)
batch_op.alter_column('submitted_datetime',
existing_type=postgresql.TIMESTAMP(timezone=True),
type_=sa.DateTime(),
nullable=False,
autoincrement=False)
batch_op.alter_column('submitted_payload',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
nullable=False,
autoincrement=False)
batch_op.alter_column('submitter_id',
existing_type=sa.INTEGER(),
nullable=False,
autoincrement=False)
batch_op.drop_column('payload')

with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.alter_column('username',
existing_type=sa.VARCHAR(length=1000),
nullable=False)
batch_op.alter_column('sub',
existing_type=sa.VARCHAR(length=36),
nullable=False)
batch_op.alter_column('iss',
existing_type=sa.VARCHAR(length=1024),
nullable=False)
batch_op.alter_column('idp_userid',
existing_type=sa.VARCHAR(length=256),
nullable=False)
batch_op.alter_column('creation_date',
existing_type=postgresql.TIMESTAMP(timezone=True),
type_=sa.DateTime(),
nullable=False)

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('users', schema=None) as batch_op:
batch_op.alter_column('creation_date',
existing_type=sa.DateTime(),
type_=postgresql.TIMESTAMP(timezone=True),
nullable=True)
batch_op.alter_column('idp_userid',
existing_type=sa.VARCHAR(length=256),
nullable=True)
batch_op.alter_column('iss',
existing_type=sa.VARCHAR(length=1024),
nullable=True)
batch_op.alter_column('sub',
existing_type=sa.VARCHAR(length=36),
nullable=True)
batch_op.alter_column('username',
existing_type=sa.VARCHAR(length=1000),
nullable=True)

with op.batch_alter_table('submission_history', schema=None) as batch_op:
batch_op.add_column(sa.Column('payload', postgresql.JSONB(astext_type=sa.Text()), autoincrement=False, nullable=True))
batch_op.alter_column('submitter_id',
existing_type=sa.INTEGER(),
nullable=True,
autoincrement=False)
batch_op.alter_column('submitted_payload',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
nullable=True,
autoincrement=False)
batch_op.alter_column('submitted_datetime',
existing_type=sa.DateTime(),
type_=postgresql.TIMESTAMP(timezone=True),
nullable=True,
autoincrement=False)
batch_op.alter_column('type',
existing_type=postgresql.ENUM('other', 'standard', name='submissiontype'),
nullable=True,
autoincrement=False)

with op.batch_alter_table('submission', schema=None) as batch_op:
batch_op.add_column(sa.Column('payload', postgresql.JSONB(astext_type=sa.Text()), autoincrement=False, nullable=True))
batch_op.alter_column('submitter_id',
existing_type=sa.INTEGER(),
nullable=True)
batch_op.alter_column('submitted_payload',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
nullable=True)
batch_op.alter_column('submitted_datetime',
existing_type=sa.DateTime(),
type_=postgresql.TIMESTAMP(timezone=True),
nullable=True,
existing_server_default=sa.text('now()'))
batch_op.alter_column('type',
existing_type=postgresql.ENUM('other', 'standard', name='submissiontype'),
nullable=True)

op.drop_table('ownership_history')
op.drop_table('ownership')
op.drop_table('person_history')
op.drop_table('person')
# ### end Alembic commands ###
23 changes: 19 additions & 4 deletions btr-api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions btr-api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ flask-jwt-oidc = "^0.3.0"
gunicorn = "^21.2.0"
jsonschema = {extras = ["format"], version = "^4.20.0"}
pycountry = "^23.12.11"
flask-caching = "^2.3.0"

[tool.poetry.group.test.dependencies]
freezegun = "^1.2.2"
Expand Down
7 changes: 7 additions & 0 deletions btr-api/src/btr_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ class Config: # pylint: disable=too-few-public-methods

LEGISLATIVE_TIMEZONE = os.getenv('LEGISLATIVE_TIMEZONE', 'America/Vancouver')

# Cache stuff
CACHE_TYPE = os.getenv('CACHE_TYPE', 'SimpleCache')
try:
CACHE_DEFAULT_TIMEOUT = int(os.getenv('CACHE_DEFAULT_TIMEOUT', '300'))
except (TypeError, ValueError):
CACHE_DEFAULT_TIMEOUT = 300


class Production(Config): # pylint: disable=too-few-public-methods
"""Production class configuration that should override vars for production.
Expand Down
4 changes: 4 additions & 0 deletions btr-api/src/btr_api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
# POSSIBILITY OF SUCH DAMAGE.
"""This exports all of the models and schemas used by the application."""
from .db import db # noqa: I001
from .ownership import Ownership
from .person import Person
from .submission import Submission
from .submission import SubmissionType
from .user import User
Expand All @@ -41,6 +43,8 @@

__all__ = (
"db",
"Ownership",
"Person",
"Submission",
"SubmissionType",
"User",
Expand Down
63 changes: 63 additions & 0 deletions btr-api/src/btr_api/models/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright © 2024 Province of British Columbia
#
# Licensed under the BSD 3 Clause License, (the "License");
# you may not use this file except in compliance with the License.
# The template for the license can be found here
# https://opensource.org/license/bsd-3-clause/
#
# Redistribution and use in source and binary forms,
# with or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""Base Model."""

from .db import db


class Base(db.Model):
"""Base model class for db tables."""

__abstract__ = True

def save(self):
"""Save and commit to the session."""
db.session.add(self)
db.session.commit()

def save_to_session(self):
"""Save to the session. Does not commit."""
db.session.add(self)

return self

@staticmethod
def commit():
"""Commit the session."""
db.session.commit()

@staticmethod
def rollback():
"""RollBack the session."""
db.session.rollback()
Loading

0 comments on commit 9eb1065

Please sign in to comment.