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

feature(tag-db): add tag and study_tag tables to the db (migration) #1923

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions alembic/versions/3c70366b10ea_add_tag_and_study_tag_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Add tag and study_tag tables

Revision ID: 3c70366b10ea
Revises: 1f5db5dfad80
Create Date: 2024-02-02 13:06:47.627554

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "3c70366b10ea"
down_revision = "1f5db5dfad80"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"tag",
sa.Column("label", sa.String(length=40), nullable=False),
sa.Column("color", sa.String(length=20), nullable=True),
sa.PrimaryKeyConstraint("label"),
)
with op.batch_alter_table("tag", schema=None) as batch_op:
batch_op.create_index(batch_op.f("ix_tag_color"), ["color"], unique=False)
batch_op.create_index(batch_op.f("ix_tag_label"), ["label"], unique=False)

op.create_table(
"study_tag",
sa.Column("study_id", sa.String(length=36), nullable=False),
sa.Column("tag_label", sa.String(length=40), nullable=False),
sa.ForeignKeyConstraint(["study_id"], ["study.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["tag_label"], ["tag.label"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("study_id", "tag_label"),
)
with op.batch_alter_table("study_tag", schema=None) as batch_op:
batch_op.create_index(batch_op.f("ix_study_tag_study_id"), ["study_id"], unique=False)
batch_op.create_index(batch_op.f("ix_study_tag_tag_label"), ["tag_label"], unique=False)

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("study_tag", schema=None) as batch_op:
batch_op.drop_index(batch_op.f("ix_study_tag_tag_label"))
batch_op.drop_index(batch_op.f("ix_study_tag_study_id"))

op.drop_table("study_tag")
with op.batch_alter_table("tag", schema=None) as batch_op:
batch_op.drop_index(batch_op.f("ix_tag_label"))
batch_op.drop_index(batch_op.f("ix_tag_color"))

op.drop_table("tag")
# ### end Alembic commands ###
141 changes: 141 additions & 0 deletions antarest/study/css4_colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
"""
CSS Color Module Level 4

[Color keywords](https://www.w3.org/TR/css-color-4/#color-keywords)
"""

COLOR_NAMES = (
"AliceBlue",
"AntiqueWhite",
"Aquamarine",
"Aqua",
"Beige",
"Bisque",
"Black",
"BlanchedAlmond",
"BlueViolet",
"Blue",
"Brown",
"BurlyWood",
"CadetBlue",
"Chartreuse",
"Chocolate",
"Coral",
"CornflowerBlue",
"Cornsilk",
"Crimson",
"DarkBlue",
"DarkGoldenrod",
"DarkGray",
"DarkGreen",
"DarkKhaki",
"DarkOliveGreen",
"DarkOrange",
"DarkOrchid",
"DarkSalmon",
"DarkSeaGreen",
"DarkSlateBlue",
"DarkSlateGray",
"DarkTurquoise",
"DarkViolet",
"DeepPink",
"DeepSkyBlue",
"DimGray",
"DodgerBlue",
"FireBrick",
"ForestGreen",
"Gainsboro",
"Goldenrod",
"Gold",
"Gray",
"GreenYellow",
"Green",
"Honeydew",
"HotPink",
"IndianRed",
"Indigo",
"Ivory",
"Khaki",
"LavenderBlush",
"Lavender",
"LawnGreen",
"LemonChiffon",
"LightBlue",
"LightCoral",
"LightCyan",
"LightGoldenrod",
"LightGray",
"LightGreen",
"LightPink",
"LightSalmon",
"LightSeaGreen",
"LightSkyBlue",
"LightSlateGray",
"LightSteelBlue",
"LightYellow",
"LimeGreen",
"Lime",
"Linen",
"Magenta",
"Maroon",
"MediumAquamarine",
"MediumBlue",
"MediumOrchid",
"MediumPurple",
"MediumSeaGreen",
"MediumSlateBlue",
"MediumSpringGreen",
"MediumTurquoise",
"MediumVioletRed",
"MidnightBlue",
"MintCream",
"MistyRose",
"Moccasin",
"NavajoWhite",
"Navy",
"OldLace",
"OliveDrab",
"Olive",
"OrangeRed",
"Orange",
"Orchid",
"PaleGoldenrod",
"PaleGreen",
"PaleTurquoise",
"PaleVioletRed",
"PapayaWhip",
"PeachPuff",
"Peru",
"Pink",
"Plum",
"PowderBlue",
"Purple",
"Red",
"RosyBrown",
"RoyalBlue",
"SaddleBrown",
"Salmon",
"SandyBrown",
"SeaGreen",
"Seashell",
"Sienna",
"Silver",
"SkyBlue",
"SlateBlue",
"SlateGray",
"Snow",
"SpringGreen",
"SteelBlue",
"Tan",
"Teal",
"Thistle",
"Tomato",
"Turquoise",
"Violet",
"Wheat",
"WhiteSmoke",
"White",
"YellowGreen",
"Yellow",
)
"""List of CSS4 color names used to style tags."""
61 changes: 53 additions & 8 deletions antarest/study/model.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
import dataclasses
import enum
import secrets
import typing as t
import uuid
from datetime import datetime, timedelta
from pathlib import Path

from pydantic import BaseModel
from sqlalchemy import Boolean, Column, DateTime, Enum, ForeignKey, Integer, String, Table # type: ignore
from sqlalchemy import ( # type: ignore
Boolean,
Column,
DateTime,
Enum,
ForeignKey,
Integer,
PrimaryKeyConstraint,
String,
Table,
)
from sqlalchemy.orm import relationship # type: ignore

from antarest.core.exceptions import ShouldNotHappenException
from antarest.core.model import PublicMode
from antarest.core.persistence import Base
from antarest.login.model import Group, GroupDTO, Identity
from antarest.study.css4_colors import COLOR_NAMES

if t.TYPE_CHECKING:
# avoid circular import
from antarest.core.tasks.model import TaskJob

DEFAULT_WORKSPACE_NAME = "default"

groups_metadata = Table(
"group_metadata",
Base.metadata,
Column("group_id", String(36), ForeignKey("groups.id")),
Column("study_id", String(36), ForeignKey("study.id")),
)

STUDY_REFERENCE_TEMPLATES: t.Dict[str, str] = {
"600": "empty_study_613.zip",
"610": "empty_study_613.zip",
Expand All @@ -45,6 +50,44 @@

NEW_DEFAULT_STUDY_VERSION: str = "860"

groups_metadata = Table(
"group_metadata",
Base.metadata,
Column("group_id", String(36), ForeignKey("groups.id")),
Column("study_id", String(36), ForeignKey("study.id")),
)


class StudyTag(Base): # type:ignore
"""
A table to manage the many-to-many relationship between `Study` and `Tag`
"""

__tablename__ = "study_tag"
__table_args__ = (PrimaryKeyConstraint("study_id", "tag_label"),)

study_id: str = Column(String(36), ForeignKey("study.id", ondelete="CASCADE"), index=True, nullable=False)
tag_label: str = Column(String(40), ForeignKey("tag.label", ondelete="CASCADE"), index=True, nullable=False)

def __str__(self) -> str:
return f"[StudyTag] study_id={self.study_id}, tag={self.tag}"


class Tag(Base): # type:ignore
"""
A table to store all tags
"""

__tablename__ = "tag"

label = Column(String(40), primary_key=True, index=True)
color: str = Column(String(20), index=True, default=lambda: secrets.choice(COLOR_NAMES))

studies: t.List["Study"] = relationship("Study", secondary=StudyTag.__table__, back_populates="tags")

def __str__(self) -> str:
return f"[Tag] label={self.label}, css-color-code={self.color}"


class StudyContentStatus(enum.Enum):
VALID = "VALID"
Expand Down Expand Up @@ -106,6 +149,8 @@ class Study(Base): # type: ignore
public_mode = Column(Enum(PublicMode), default=PublicMode.NONE)
owner_id = Column(Integer, ForeignKey(Identity.id), nullable=True, index=True)
archived = Column(Boolean(), default=False, index=True)

tags: t.List[Tag] = relationship(Tag, secondary=StudyTag.__table__, back_populates="studies")
owner = relationship(Identity, uselist=False)
groups = relationship(Group, secondary=lambda: groups_metadata, cascade="")
additional_data = relationship(
Expand Down
2 changes: 1 addition & 1 deletion scripts/rollback.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ CUR_DIR=$(cd "$(dirname "$0")" && pwd)
BASE_DIR=$(dirname "$CUR_DIR")

cd "$BASE_DIR"
alembic downgrade 782a481f3414
alembic downgrade 1f5db5dfad80
cd -
66 changes: 0 additions & 66 deletions tests/study/model.py

This file was deleted.

Loading
Loading