Add organisation_id to Service.

This is the second commit in the series to add organisation_id to Service.
- Data migration to update services.organisation_id from data in organisation_to_service
 (The rollback will lose any updates to organisation unless the script is updated to set organistion_to_service from service.organisation_id )
- Update Service.organisation relationship to a ForeignKey relationship to Organisation.
- Update Organisation.services to a backref relationship to Service.
This commit is contained in:
Rebecca Law
2019-08-13 17:25:30 +01:00
parent 515d6602c1
commit 55dc7184cc
6 changed files with 36 additions and 20 deletions

View File

@@ -99,7 +99,6 @@ def dao_add_service_to_organisation(service, organisation_id):
id=organisation_id id=organisation_id
).one() ).one()
organisation.services.append(service)
service.organisation_id = organisation_id service.organisation_id = organisation_id
service.organisation_type = organisation.organisation_type service.organisation_type = organisation.organisation_type
service.crown = organisation.crown service.crown = organisation.crown

View File

@@ -293,7 +293,7 @@ def dao_create_service(
insert_service_sms_sender(service, current_app.config['FROM_NUMBER']) insert_service_sms_sender(service, current_app.config['FROM_NUMBER'])
if organisation: if organisation:
service.organisation = organisation # service.organisation = organisation
service.organisation_id = organisation.id service.organisation_id = organisation.id
service.organisation_type = organisation.organisation_type service.organisation_type = organisation.organisation_type
if organisation.email_branding: if organisation.email_branding:

View File

@@ -352,8 +352,8 @@ class Organisation(db.Model):
services = db.relationship( services = db.relationship(
'Service', 'Service',
secondary='organisation_to_service', uselist=True,
uselist=True) backref=db.backref('services'))
agreement_signed = db.Column(db.Boolean, nullable=True) agreement_signed = db.Column(db.Boolean, nullable=True)
agreement_signed_at = db.Column(db.DateTime, nullable=True) agreement_signed_at = db.Column(db.DateTime, nullable=True)
@@ -480,11 +480,7 @@ class Service(db.Model, Versioned):
go_live_at = db.Column(db.DateTime, nullable=True) go_live_at = db.Column(db.DateTime, nullable=True)
organisation_id = db.Column(UUID(as_uuid=True), db.ForeignKey('organisation.id'), index=True, nullable=True) organisation_id = db.Column(UUID(as_uuid=True), db.ForeignKey('organisation.id'), index=True, nullable=True)
organisation = db.relationship( organisation = db.relationship('Organisation', foreign_keys=[organisation_id])
'Organisation',
secondary=organisation_to_service,
uselist=False,
single_parent=True)
email_branding = db.relationship( email_branding = db.relationship(
'EmailBranding', 'EmailBranding',

View File

@@ -0,0 +1,30 @@
"""
Revision ID: 0303_populate_services_org_id
Revises: 0302_add_org_id_to_services
Create Date: 2019-08-06 09:43:57.993510
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
revision = '0303_populate_services_org_id'
down_revision = '0302_add_org_id_to_services'
def upgrade():
sql = """
UPDATE services
SET organisation_id = (SELECT organisation_id from organisation_to_service
where organisation_to_service.service_id = services.id)
"""
op.execute(sql)
def downgrade():
sql = """
UPDATE services
SET organisation_id = null
"""
op.execute(sql)

View File

@@ -178,17 +178,6 @@ def test_add_service_to_organisation(sample_service, sample_organisation):
assert sample_service.organisation_id == sample_organisation.id assert sample_service.organisation_id == sample_organisation.id
def test_add_service_to_multiple_organisation_raises_error(sample_service, sample_organisation):
another_org = create_organisation()
dao_add_service_to_organisation(sample_service, sample_organisation.id)
with pytest.raises(IntegrityError):
dao_add_service_to_organisation(sample_service, another_org.id)
assert len(sample_organisation.services) == 1
assert sample_organisation.services[0] == sample_service
def test_get_organisation_services(sample_service, sample_organisation): def test_get_organisation_services(sample_service, sample_organisation):
another_service = create_service(service_name='service 2') another_service = create_service(service_name='service 2')
another_org = create_organisation() another_org = create_organisation()

View File

@@ -58,6 +58,7 @@ from app.models import (
INTERNATIONAL_SMS_TYPE, INTERNATIONAL_SMS_TYPE,
LETTER_TYPE, LETTER_TYPE,
user_folder_permissions, user_folder_permissions,
Organisation
) )
from tests.app.db import ( from tests.app.db import (
create_ft_billing, create_ft_billing,
@@ -124,6 +125,7 @@ def test_create_service_with_organisation(notify_db_session):
dao_create_service(service, user) dao_create_service(service, user)
assert Service.query.count() == 1 assert Service.query.count() == 1
service_db = Service.query.one() service_db = Service.query.one()
organisation = Organisation.query.get(organisation.id)
assert service_db.name == "service_name" assert service_db.name == "service_name"
assert service_db.id == service.id assert service_db.id == service.id
assert service_db.email_from == 'email_from' assert service_db.email_from == 'email_from'