Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add v0 data models #6

Merged
merged 10 commits into from
May 4, 2023
Prev Previous commit
Next Next commit
Use Association classes for M2M. Make Family-Environment relationship…
… M2M
nadzyah committed May 3, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 108e3ee40bc46b869018a626d276449e475587bf
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
"""Add basic v0 models

Revision ID: 5f106e482c7e
Revision ID: a3637f5027ec
Revises:
Create Date: 2023-05-02 13:47:10.755876+00:00
Create Date: 2023-05-03 12:22:45.061264+00:00

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '5f106e482c7e'
revision = 'a3637f5027ec'
down_revision = None
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('artefact',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=200), nullable=False),
sa.Column('source', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
sa.Column('status', sa.Enum('Approved', 'Marked as Failed', name='artefact_status_enum'), nullable=True),
sa.Column('is_archived', sa.Boolean(), server_default=sa.text('false'), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_artefact_name'), 'artefact', ['name'], unique=True)
op.create_table('artefact_group',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=200), nullable=False),
@@ -48,28 +37,53 @@ def upgrade() -> None:
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_family_name'), 'family', ['name'], unique=True)
op.create_table('expected_environment',
sa.Column('artefact_group_id', sa.Integer(), nullable=False),
sa.Column('environment_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['artefact_group_id'], ['artefact_group.id'], ),
sa.ForeignKeyConstraint(['environment_id'], ['environment.id'], ),
sa.PrimaryKeyConstraint('artefact_group_id', 'environment_id')
)
op.create_table('family_environment',
sa.Column('family_id', sa.Integer(), nullable=False),
sa.Column('environment_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['environment_id'], ['environment.id'], ),
sa.ForeignKeyConstraint(['family_id'], ['family.id'], ),
sa.PrimaryKeyConstraint('family_id', 'environment_id')
)
op.create_table('stage',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('position', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('position')
sa.Column('family_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['family_id'], ['family.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_stage_name'), 'stage', ['name'], unique=True)
op.create_table('expected_environment',
sa.Column('artefact_group_id', sa.Integer(), nullable=False),
sa.Column('environment_id', sa.Integer(), nullable=False),
op.create_index(op.f('ix_stage_position'), 'stage', ['position'], unique=False)
op.create_table('artefact',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=200), nullable=False),
sa.Column('source', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
sa.Column('status', sa.Enum('Approved', 'Marked as Failed', name='artefact_status_enum'), nullable=True),
sa.Column('family_id', sa.Integer(), nullable=True),
sa.Column('stage_id', sa.Integer(), nullable=True),
sa.Column('artefact_group_id', sa.Integer(), nullable=True),
sa.Column('is_archived', sa.Boolean(), server_default=sa.text('false'), nullable=False),
sa.ForeignKeyConstraint(['artefact_group_id'], ['artefact_group.id'], ),
sa.ForeignKeyConstraint(['environment_id'], ['environment.id'], ),
sa.PrimaryKeyConstraint('artefact_group_id', 'environment_id')
sa.ForeignKeyConstraint(['family_id'], ['family.id'], ),
sa.ForeignKeyConstraint(['stage_id'], ['stage.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_artefact_name'), 'artefact', ['name'], unique=True)
op.create_table('test_execution',
sa.Column('artefact_id', sa.Integer(), nullable=False),
sa.Column('environment_id', sa.Integer(), nullable=False),
sa.Column('jenkins_link', sa.String(length=200), nullable=True),
sa.Column('c3_link', sa.String(length=200), nullable=True),
sa.Column('update_at', sa.DateTime(), nullable=True),
sa.Column('status', sa.Enum('Not Started', 'In Progress', 'Passed', 'Failed', 'Error', 'Not Tested', name='test_status_enum'), server_default='Not Started', nullable=True),
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
sa.Column('status', sa.Enum('Not Started', 'In Progress', 'Passed', 'Failed', 'Not Tested', name='test_status_enum'), server_default='Not Started', nullable=False),
sa.ForeignKeyConstraint(['artefact_id'], ['artefact.id'], ),
sa.ForeignKeyConstraint(['environment_id'], ['environment.id'], ),
sa.PrimaryKeyConstraint('artefact_id', 'environment_id')
@@ -80,15 +94,17 @@ def upgrade() -> None:
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('test_execution')
op.drop_table('expected_environment')
op.drop_index(op.f('ix_artefact_name'), table_name='artefact')
op.drop_table('artefact')
op.drop_index(op.f('ix_stage_position'), table_name='stage')
op.drop_index(op.f('ix_stage_name'), table_name='stage')
op.drop_table('stage')
op.drop_table('family_environment')
op.drop_table('expected_environment')
op.drop_index(op.f('ix_family_name'), table_name='family')
op.drop_table('family')
op.drop_index(op.f('ix_environment_name'), table_name='environment')
op.drop_table('environment')
op.drop_index(op.f('ix_artefact_group_name'), table_name='artefact_group')
op.drop_table('artefact_group')
op.drop_index(op.f('ix_artefact_name'), table_name='artefact')
op.drop_table('artefact')
# ### end Alembic commands ###
144 changes: 93 additions & 51 deletions backend/src/data_access/models.py
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@
DateTime,
)
from sqlalchemy.sql import func, expression
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.dialects.postgresql import JSONB, ARRAY
from sqlalchemy.orm import (
DeclarativeBase,
MappedAsDataclass,
@@ -77,22 +77,11 @@ class Stage(Base):

id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(100), unique=True, index=True)
position: Mapped[int] = mapped_column(unique=True)
position: Mapped[int] = mapped_column(index=True)
# Relationships
family_id = mapped_column(ForeignKey("family.id"))
family: Mapped[Family] = relationship(back_populates="stages")
arterfacts: Mapped[List["Artefact"]] = relationship(back_populates="stage")


# A table to represent the expected envoronments.
# It's the M2M relationship between ArtefactGroup and Environment tables
expected_environment = Table(
"expected_environment",
Base.metadata,
Column(
"artefact_group_id", ForeignKey("artefact_group.id"), primary_key=True
),
Column("environment_id", ForeignKey("environment.id"), primary_key=True),
)
artefacts: Mapped[List["Artefact"]] = relationship(back_populates="stage")


class ArtefactGroup(Base):
@@ -107,35 +96,9 @@ class ArtefactGroup(Base):
artefacts: Mapped[List["Artefact"]] = relationship(
back_populates="artefact_group"
)
environments: Mapped[List["Environment"]] = relationship(
secondary=expected_environment, back_populates="artefact_groups"
)


# A table to represent the result of test execution.
# It's the M2M relationship between Artefact and Environment tables
test_execution = Table(
"test_execution",
Base.metadata,
Column("artefact_id", ForeignKey("artefact.id"), primary_key=True),
Column("environment_id", ForeignKey("environment.id"), primary_key=True),
Column("jenkins_link", String(200), nullable=True),
Column("c3_link", String(200), nullable=True),
Column("update_at", DateTime, onupdate=func.now()),
Column(
"status",
Enum(
"Not Started",
"In Progress",
"Passed",
"Failed",
"Error",
"Not Tested",
name="test_status_enum",
),
server_default="Not Started",
),
)
environments: Mapped[List["ExpectedEnvironment"]] = relationship(
back_populates="artefact_group"
)


class Artefact(Base):
@@ -152,13 +115,16 @@ class Artefact(Base):
nullable=True,
)
# Relationships
family_id = mapped_column(ForeignKey("family.id"))
family: Mapped[Family] = relationship(back_populates="artefacts")
stage_id = mapped_column(ForeignKey("stage.id"))
stage: Mapped[Stage] = relationship(back_populates="artefacts")
artefact_group_id = mapped_column(ForeignKey("artefact_group.id"))
artefact_group: Mapped[ArtefactGroup] = relationship(
back_populates="artefacts"
)
environments: Mapped[List["Environment"]] = relationship(
secondary=test_execution, back_populates="artefacts"
environments: Mapped[List["TestExecution"]] = relationship(
back_populates="artefact"
)
# Default fields
is_archived: Mapped[bool] = mapped_column(
@@ -177,10 +143,86 @@ class Environment(Base):
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(200), unique=True, index=True)
# Relationships
family: Mapped[Family] = relationship(back_populates="environments")
artefacts: Mapped[List["Artefact"]] = relationship(
secondary=test_execution, back_populates="environments"
families: Mapped[List["FamilyEnvironment"]] = relationship(
back_populates="environment"
)
artefacts: Mapped[List["TestExecution"]] = relationship(
back_populates="environment"
)
artefact_groups: Mapped[List["ExpectedEnvironment"]] = relationship(
back_populates="environment"
)


class TestExecution(Base):
"""
A table to represent the result of test execution.
It's the M2M relationship between Artefact and Environment tables
"""

__tablename__ = "test_execution"

artefact_id: Mapped[int] = mapped_column(
ForeignKey("artefact.id"), primary_key=True
)
environment_id: Mapped[int] = mapped_column(
ForeignKey("environment.id"), primary_key=True
)
jenkins_link: Mapped[str] = mapped_column(String(200), nullable=True)
c3_link: Mapped[str] = mapped_column(String(200), nullable=True)
updated_at: Mapped[timestamp] = mapped_column(onupdate=func.now())
status: Mapped[str] = mapped_column(
Enum(
"Not Started",
"In Progress",
"Passed",
"Failed",
"Not Tested",
name="test_status_enum",
),
server_default="Not Started",
)
artefact: Mapped["Artefact"] = relationship(back_populates="environments")
environment: Mapped["Environment"] = relationship(
back_populates="artefacts"
)


class ExpectedEnvironment(Base):
"""
A table to represent the expected envoronments.
It's the M2M relationship between ArtefactGroup and Environment tables
"""

__tablename__ = "expected_environment"

artefact_group_id: Mapped[int] = mapped_column(
ForeignKey("artefact_group.id"), primary_key=True
)
environment_id: Mapped[int] = mapped_column(
ForeignKey("environment.id"), primary_key=True
)
artefact_group: Mapped["ArtefactGroup"] = relationship(
back_populates="environments"
)
environment: Mapped["Environment"] = relationship(
back_populates="artefact_groups"
)


class FamilyEnvironment(Base):
"""
A table to represent the M2M relationship between Family and
Environment tables
"""

__tablename__ = "family_environment"

family_id: Mapped[int] = mapped_column(
ForeignKey("family.id"), primary_key=True
)
artefact_groups: Mapped[List["ArtefactGroup"]] = relationship(
secondary=expected_environment, back_populates="environments"
environment_id: Mapped[int] = mapped_column(
ForeignKey("environment.id"), primary_key=True
)
family: Mapped["Family"] = relationship(back_populates="environments")
environment: Mapped["Environment"] = relationship(back_populates="family")