Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 4 additions & 1 deletion lib/galaxy/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
inspect,
Integer,
join,
MetaData,
not_,
Numeric,
or_,
Expand Down Expand Up @@ -122,6 +123,7 @@
TrimmedString,
UUIDType,
)
from galaxy.model.database_object_names import NAMING_CONVENTION
from galaxy.model.item_attrs import (
get_item_annotation_str,
UsesAnnotations,
Expand Down Expand Up @@ -225,8 +227,9 @@ def get_uuid(uuid: Optional[Union[UUID, str]] = None) -> UUID:

class Base(_HasTable, metaclass=DeclarativeMeta):
__abstract__ = True
metadata = MetaData(naming_convention=NAMING_CONVENTION)
mapper_registry.metadata = metadata
registry = mapper_registry
metadata = mapper_registry.metadata
__init__ = mapper_registry.constructor

@classmethod
Expand Down
44 changes: 44 additions & 0 deletions lib/galaxy/model/database_object_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Naming convention and helper functions for generating names of database
constraints and indexes.
"""
from typing import (
List,
Union,
)

from galaxy.util import listify

# Naming convention applied to database constraints and indexes.
# All except "ix" are conventions used by PostgreSQL by default.
# We keep the "ix" template consistent with historical Galaxy usage.
NAMING_CONVENTION = {
"pk": "%(table_name)s_pkey",
"fk": "%(table_name)s_%(column_0_name)s_fkey",
"uq": "%(table_name)s_%(column_0_name)s_key",
"ck": "%(table_name)s_%(column_0_name)s_check",
"ix": "ix_%(table_name)s_%(column_0_name)s",
}


def foreign_key(table_name: str, column_names: Union[str, List]) -> str:
columns = _as_str(column_names)
return f"{table_name}_{columns}_fkey"


def unique_constraint(table_name: str, column_names: Union[str, List]) -> str:
columns = _as_str(column_names)
return f"{table_name}_{columns}_key"


def check_constraint(table_name: str, column_name: str) -> str:
return f"{table_name}_{column_name}_check"


def index(table_name: str, column_names: Union[str, List]) -> str:
columns = _as_str(column_names)
return f"ix_{table_name}_{columns}"


def _as_str(column_names: Union[str, List]) -> str:
return "_".join(listify(column_names))
3 changes: 2 additions & 1 deletion lib/galaxy/model/migrations/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
from alembic.script.base import Script
from sqlalchemy import create_engine

from galaxy.model import Base
from galaxy.model.migrations import (
GXY,
ModelId,
TSI,
)

config = context.config
target_metadata = None # Not implemented: used for autogenerate, which we don't use here.
target_metadata = Base.metadata
log = logging.getLogger(__name__)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2023-03-07 15:06:55.682273

"""
from galaxy.model.database_object_names import index
from galaxy.model.migrations.util import (
create_index,
drop_index,
Expand All @@ -17,12 +18,12 @@
depends_on = None

table_name = "workflow_request_step_states"
columns = ["workflow_invocation_id"]
index_name = "ix_workflow_request_step_states_workflow_invocation_id"
column_name = "workflow_invocation_id"
index_name = index(table_name, column_name)


def upgrade():
create_index(index_name, table_name, columns)
create_index(index_name, table_name, [column_name])


def downgrade():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create Date: 2023-03-07 15:10:44.943542

"""
from galaxy.model.database_object_names import index
from galaxy.model.migrations.util import (
create_index,
drop_index,
Expand All @@ -18,12 +19,12 @@


table_name = "workflow_request_input_parameters"
columns = ["workflow_invocation_id"]
index_name = "ix_workflow_request_input_parameters_workflow_invocation_id"
column_name = "workflow_invocation_id"
index_name = index(table_name, column_name)


def upgrade():
create_index(index_name, table_name, columns)
create_index(index_name, table_name, [column_name])


def downgrade():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
String,
)

from galaxy.model.database_object_names import index
from galaxy.model.migrations.util import (
add_column,
create_index,
Expand All @@ -31,6 +32,10 @@
branch_labels = None
depends_on = None

old_index_name = index("default_quota_association", "type")
new_index_name = index("quota", "quota_source_label")
unique_constraint_name = "uqsu_unique_label_per_user" # leave unchanged


def upgrade():
with transaction():
Expand All @@ -43,17 +48,15 @@ def upgrade():
# user had an index on disk_usage - does that make any sense? -John
Column("disk_usage", Numeric(15, 0)),
)
create_unique_constraint(
"uqsu_unique_label_per_user", "user_quota_source_usage", ["user_id", "quota_source_label"]
)
drop_index("ix_default_quota_association_type", "default_quota_association")
create_index("ix_quota_quota_source_label", "quota", ["quota_source_label"])
create_unique_constraint(unique_constraint_name, "user_quota_source_usage", ["user_id", "quota_source_label"])
drop_index(old_index_name, "default_quota_association")
create_index(new_index_name, "quota", ["quota_source_label"])


def downgrade():
with transaction():
drop_index("ix_quota_quota_source_label", "quota")
create_index("ix_default_quota_association_type", "default_quota_association", ["type"], unique=True)
drop_constraint("uqsu_unique_label_per_user", "user_quota_source_usage")
drop_index(new_index_name, "quota")
create_index(old_index_name, "default_quota_association", ["type"], unique=True)
drop_constraint(unique_constraint_name, "user_quota_source_usage")
drop_table("user_quota_source_usage")
drop_column("quota", "quota_source_label")
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Column,
)

from galaxy.model.database_object_names import index
from galaxy.model.migrations.util import (
add_column,
drop_column,
Expand All @@ -27,6 +28,7 @@
# database object names used in this revision
table_name = "api_keys"
column_name = "deleted"
index_name = index(table_name, column_name)


def upgrade():
Expand All @@ -35,5 +37,5 @@ def upgrade():

def downgrade():
with transaction():
drop_index("ix_api_keys_deleted", table_name)
drop_index(index_name, table_name)
drop_column(table_name, column_name)
32 changes: 32 additions & 0 deletions test/unit/data/model/test_database_object_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import galaxy.model.database_object_names as names


def test_foreign_key_single_column():
assert names.foreign_key("foo", "bar_id") == "foo_bar_id_fkey"


def test_foreign_key_composite():
assert names.foreign_key("foo", ["bar_id", "buz_id"]) == "foo_bar_id_buz_id_fkey"
assert names.foreign_key("foo", ["bar_id", "buz_id", "bam_id"]) == "foo_bar_id_buz_id_bam_id_fkey"


def test_unique_constraint_single_column():
assert names.unique_constraint("foo", "bar") == "foo_bar_key"


def test_unique_constraint_composite():
assert names.unique_constraint("foo", ["bar", "buz"]) == "foo_bar_buz_key"
assert names.unique_constraint("foo", ["bar", "buz", "bam"]) == "foo_bar_buz_bam_key"


def test_check_constraint():
assert names.check_constraint("foo", "bar") == "foo_bar_check"


def test_index_single_column():
assert names.index("foo", "bar") == "ix_foo_bar"


def test_index_composite():
assert names.index("foo", ["bar", "buz"]) == "ix_foo_bar_buz"
assert names.index("foo", ["bar", "buz", "bam"]) == "ix_foo_bar_buz_bam"