From 885b2da30889667db2b4fc7e522d61b1fe34cc69 Mon Sep 17 00:00:00 2001 From: richard-dds Date: Wed, 26 Sep 2018 10:21:05 -0400 Subject: [PATCH] Remove workspaces.name unique constraint - Added some random indexes - Fixed audit_events.request_id foreign key constraint --- ...emove_workspaces_name_unique_constraint.py | 42 +++++++++++++++++++ atst/models/audit_event.py | 2 +- atst/models/workspace.py | 2 +- tests/domain/test_workspaces.py | 6 +++ 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 alembic/versions/903d7c66ff1d_remove_workspaces_name_unique_constraint.py diff --git a/alembic/versions/903d7c66ff1d_remove_workspaces_name_unique_constraint.py b/alembic/versions/903d7c66ff1d_remove_workspaces_name_unique_constraint.py new file mode 100644 index 00000000..539dc01d --- /dev/null +++ b/alembic/versions/903d7c66ff1d_remove_workspaces_name_unique_constraint.py @@ -0,0 +1,42 @@ +"""remove workspaces.name unique constraint + +Revision ID: 903d7c66ff1d +Revises: 7958cca588a1 +Create Date: 2018-09-26 10:19:13.230064 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = '903d7c66ff1d' +down_revision = '7958cca588a1' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_index(op.f('ix_audit_events_request_id'), 'audit_events', ['request_id'], unique=False) + op.create_index(op.f('ix_audit_events_workspace_id'), 'audit_events', ['workspace_id'], unique=False) + op.create_foreign_key(None, 'audit_events', 'requests', ['request_id'], ['id']) + op.alter_column('request_status_events', 'time_created', + existing_type=postgresql.TIMESTAMP(timezone=True), + nullable=False, + existing_server_default=sa.text('now()')) + op.drop_constraint('workspaces_name_key', 'workspaces', type_='unique') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_unique_constraint('workspaces_name_key', 'workspaces', ['name']) + op.alter_column('request_status_events', 'time_created', + existing_type=postgresql.TIMESTAMP(timezone=True), + nullable=True, + existing_server_default=sa.text('now()')) + op.drop_constraint(None, 'audit_events', type_='foreignkey') + op.drop_index(op.f('ix_audit_events_workspace_id'), table_name='audit_events') + op.drop_index(op.f('ix_audit_events_request_id'), table_name='audit_events') + # ### end Alembic commands ### diff --git a/atst/models/audit_event.py b/atst/models/audit_event.py index e402e631..f1ddaf55 100644 --- a/atst/models/audit_event.py +++ b/atst/models/audit_event.py @@ -17,7 +17,7 @@ class AuditEvent(Base, TimestampsMixin): workspace_id = Column(UUID(as_uuid=True), ForeignKey("workspaces.id"), index=True) workspace = relationship("Workspace", backref="audit_events") - request_id = Column(UUID(as_uuid=True), ForeignKey("request.id"), index=True) + request_id = Column(UUID(as_uuid=True), ForeignKey("requests.id"), index=True) resource_type = Column(String(), nullable=False) resource_id = Column(UUID(as_uuid=True), index=True, nullable=False) diff --git a/atst/models/workspace.py b/atst/models/workspace.py index d4f67491..492880c8 100644 --- a/atst/models/workspace.py +++ b/atst/models/workspace.py @@ -12,7 +12,7 @@ class Workspace(Base, mixins.TimestampsMixin, mixins.AuditableMixin): __tablename__ = "workspaces" id = Id() - name = Column(String, unique=True) + name = Column(String) request_id = Column(ForeignKey("requests.id"), nullable=False) projects = relationship("Project", back_populates="workspace") roles = relationship("WorkspaceRole") diff --git a/tests/domain/test_workspaces.py b/tests/domain/test_workspaces.py index 90464b93..0781e91a 100644 --- a/tests/domain/test_workspaces.py +++ b/tests/domain/test_workspaces.py @@ -250,3 +250,9 @@ def test_get_for_update_information(): ccpo = UserFactory.from_atat_role("ccpo") with pytest.raises(UnauthorizedError): Workspaces.get_for_update_information(ccpo, workspace.id) + + +def test_can_create_workspaces_with_matching_names(): + workspace_name = "Great Workspace" + Workspaces.create(RequestFactory.create(), name=workspace_name) + Workspaces.create(RequestFactory.create(), name=workspace_name)