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

@@ -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)