diff --git a/alembic/versions/f064247f2988_projects_and_environments.py b/alembic/versions/f064247f2988_projects_and_environments.py new file mode 100644 index 00000000..5efa590b --- /dev/null +++ b/alembic/versions/f064247f2988_projects_and_environments.py @@ -0,0 +1,47 @@ +"""projects and environments + +Revision ID: f064247f2988 +Revises: a2b499a1dd62 +Create Date: 2018-08-17 11:30:53.684954 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = 'f064247f2988' +down_revision = 'a2b499a1dd62' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('projects', + sa.Column('time_created', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.Column('time_updated', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('uuid_generate_v4()'), nullable=False), + sa.Column('name', sa.String(), nullable=False), + sa.Column('description', sa.String(), nullable=False), + sa.Column('workspace_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.ForeignKeyConstraint(['workspace_id'], ['workspaces.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('environments', + sa.Column('time_created', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.Column('time_updated', sa.TIMESTAMP(timezone=True), server_default=sa.text('now()'), nullable=False), + sa.Column('id', postgresql.UUID(as_uuid=True), server_default=sa.text('uuid_generate_v4()'), nullable=False), + sa.Column('name', sa.String(), nullable=False), + sa.Column('project_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.ForeignKeyConstraint(['project_id'], ['projects.id'], ), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('environments') + op.drop_table('projects') + # ### end Alembic commands ### diff --git a/atst/models/__init__.py b/atst/models/__init__.py index 129064eb..9accc290 100644 --- a/atst/models/__init__.py +++ b/atst/models/__init__.py @@ -11,3 +11,5 @@ from .workspace_role import WorkspaceRole from .pe_number import PENumber from .task_order import TaskOrder from .workspace import Workspace +from .project import Project +from .environment import Environment diff --git a/atst/models/environment.py b/atst/models/environment.py new file mode 100644 index 00000000..8e812635 --- /dev/null +++ b/atst/models/environment.py @@ -0,0 +1,16 @@ +from sqlalchemy import Column, ForeignKey, String +from sqlalchemy.orm import relationship + +from atst.models import Base +from atst.models.types import Id +from atst.models.mixins import TimestampsMixin + + +class Environment(Base, TimestampsMixin): + __tablename__ = "environments" + + id = Id() + name = Column(String, nullable=False) + + project_id = Column(ForeignKey("projects.id")) + project = relationship("Project") diff --git a/atst/models/project.py b/atst/models/project.py new file mode 100644 index 00000000..05ee853e --- /dev/null +++ b/atst/models/project.py @@ -0,0 +1,18 @@ +from sqlalchemy import Column, ForeignKey, String +from sqlalchemy.orm import relationship + +from atst.models import Base +from atst.models.types import Id +from atst.models.mixins import TimestampsMixin + + +class Project(Base, TimestampsMixin): + __tablename__ = "projects" + + id = Id() + name = Column(String, nullable=False) + description = Column(String, nullable=False) + + workspace_id = Column(ForeignKey("workspaces.id"), nullable=False) + workspace = relationship("Workspace") + projects = relationship("Environment", back_populates="project") diff --git a/atst/models/workspace.py b/atst/models/workspace.py index b0051093..31476254 100644 --- a/atst/models/workspace.py +++ b/atst/models/workspace.py @@ -10,11 +10,11 @@ class Workspace(Base, TimestampsMixin): __tablename__ = "workspaces" id = Id() + name = Column(String, unique=True) request_id = Column(ForeignKey("requests.id"), nullable=False) request = relationship("Request") - - name = Column(String, unique=True) + projects = relationship("Project", back_populates="workspace") @property def owner(self): diff --git a/tests/domain/test_workspaces.py b/tests/domain/test_workspaces.py index fc52a719..d72be57e 100644 --- a/tests/domain/test_workspaces.py +++ b/tests/domain/test_workspaces.py @@ -42,3 +42,10 @@ def test_creating_workspace_adds_owner(): workspace_user = WorkspaceUsers.get(workspace.id, user.id) assert workspace_user.workspace_role + +def test_workspace_has_timestamps(): + request = RequestFactory.create() + workspace = Workspaces.create(request) + assert workspace.request == request + assert workspace.name == request.id + assert workspace.time_created == workspace.time_updated