Skip to content

Commit

Permalink
Merge pull request #750 from MolSSI/next
Browse files Browse the repository at this point in the history
Use computed column for dataset lname
  • Loading branch information
bennybp committed Sep 15, 2023
2 parents e169f94 + 97a69ab commit d06773d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Use computed column for dataset lname
Revision ID: 13cb230def11
Revises: d1ee87a66b71
Create Date: 2023-09-14 18:50:20.067940
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "13cb230def11"
down_revision = "d1ee87a66b71"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.execute(sa.text("ALTER TABLE base_dataset DROP COLUMN lname;"))
op.execute(
sa.text(
"ALTER TABLE base_dataset ADD COLUMN lname CHARACTER VARYING(100) NOT NULL GENERATED ALWAYS AS (lower((name)::text)) stored;"
)
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.execute(sa.text("ALTER TABLE base_dataset DROP COLUMN lname;"))
op.execute(sa.text("ALTER TABLE base_dataset ADD COLUMN lname CHARACTER VARYING(100)"))
op.execute(sa.text("UPDATE base_dataset SET lname = lower(name)"))
op.alter_column("base_dataset", "lname", nullable=False)
op.create_unique_constraint("ux_base_dataset_dataset_type_lname", "base_dataset", ["dataset_type", "lname"])

# ### end Alembic commands ###
3 changes: 2 additions & 1 deletion qcfractal/qcfractal/components/dataset_db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
JSON,
Boolean,
Index,
Computed,
ForeignKey,
ForeignKeyConstraint,
UniqueConstraint,
Expand All @@ -30,7 +31,7 @@ class BaseDatasetORM(BaseORM):
id = Column(Integer, primary_key=True)
dataset_type = Column(String, nullable=False)

lname = Column(String(100), nullable=False)
lname = Column(String(100), Computed("LOWER(name)"), nullable=False)
name = Column(String(100), nullable=False)

tags = Column(JSON, nullable=False)
Expand Down
2 changes: 0 additions & 2 deletions qcfractal/qcfractal/components/dataset_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ def add(
ds_orm = self.dataset_orm(
dataset_type=self.dataset_type,
name=name,
lname=name.lower(),
tagline=tagline,
description=description,
tags=tags,
Expand Down Expand Up @@ -371,7 +370,6 @@ def update_metadata(
raise AlreadyExistsError(f"{self.dataset_type} dataset named '{new_metadata.name}' already exists")

ds.name = new_metadata.name
ds.lname = new_metadata.name.lower()

ds.description = new_metadata.description
ds.tagline = new_metadata.tagline
Expand Down

0 comments on commit d06773d

Please sign in to comment.