From e7fe27fa226db33a68a6bdd2bcb6ac9c48c6acd2 Mon Sep 17 00:00:00 2001 From: Adam Coldrick Date: Thu, 7 Mar 2019 16:31:12 +0000 Subject: [PATCH] Add a table mapping Teams to Projects Change-Id: I4821d4d204a35abab6b43e1037424bdc5c9606a1 --- ..._associate_multiple_projects_with_teams.py | 51 +++++++++++++++++++ storyboard/db/models.py | 10 +++- 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 storyboard/db/migration/alembic_migrations/versions/065_associate_multiple_projects_with_teams.py diff --git a/storyboard/db/migration/alembic_migrations/versions/065_associate_multiple_projects_with_teams.py b/storyboard/db/migration/alembic_migrations/versions/065_associate_multiple_projects_with_teams.py new file mode 100644 index 00000000..d66a5c70 --- /dev/null +++ b/storyboard/db/migration/alembic_migrations/versions/065_associate_multiple_projects_with_teams.py @@ -0,0 +1,51 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# + +"""Associate multiple projects with teams + +Revision ID: 065 +Revises: 064 +Create Date: 2019-03-07 11:25:31.308033 + +""" + +# revision identifiers, used by Alembic. +revision = '065' +down_revision = '064' + + +from alembic import op +import sqlalchemy as sa + + +def upgrade(active_plugins=None, options=None): + op.create_table('project_teams', + sa.Column('project_id', sa.Integer(), nullable=False), + sa.Column('team_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['project_id'], ['projects.id'], ), + sa.ForeignKeyConstraint(['team_id'], ['teams.id'], ) + ) + dialect = op.get_bind().engine.dialect + if dialect.supports_alter: + op.drop_constraint('projects_ibfk_1', 'projects', type_='foreignkey') + op.drop_column('projects', 'team_id') + + +def downgrade(active_plugins=None, options=None): + dialect = op.get_bind().engine.dialect + if dialect.supports_alter: + op.add_column('projects', sa.Column( + 'team_id', sa.Integer(), autoincrement=False, nullable=True)) + op.create_foreign_key( + 'projects_ibfk_1', 'projects', 'teams', ['team_id'], ['id']) + op.drop_table('project_teams') diff --git a/storyboard/db/models.py b/storyboard/db/models.py index c7d60d82..6e9e4f42 100644 --- a/storyboard/db/models.py +++ b/storyboard/db/models.py @@ -217,6 +217,13 @@ class Team(ModelBuilder, Base): backref="teams") +project_teams = Table( + 'project_teams', Base.metadata, + Column('project_id', Integer, ForeignKey('projects.id'), nullable=False), + Column('team_id', Integer, ForeignKey('teams.id'), nullable=False) +) + + project_group_mapping = Table( 'project_group_mapping', Base.metadata, Column('project_id', Integer, ForeignKey('projects.id')), @@ -267,8 +274,7 @@ class Project(FullText, ModelBuilder, Base): name = Column(String(CommonLength.top_middle_length)) description = Column(UnicodeText()) - team_id = Column(Integer, ForeignKey('teams.id')) - team = relationship(Team, primaryjoin=team_id == Team.id) + teams = relationship(Team, secondary="project_teams", backref="projects") tasks = relationship('Task', backref='project') branches = relationship('Branch', backref='project') repo_url = Column(String(CommonLength.top_large_length), default=None)