Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 17 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 @@ -215,6 +216,20 @@ class _HasTable:
_HasTable = object


# 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.
#
# NOTE: If editing, also update model.migrations.utils.DbObjectNames.
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 get_uuid(uuid: Optional[Union[UUID, str]] = None) -> UUID:
if isinstance(uuid, UUID):
return uuid
Expand All @@ -225,8 +240,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
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 @@ -7,6 +7,7 @@
"""
from galaxy.model.migrations.util import (
create_index,
DbObjectNames,
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 = DbObjectNames.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 @@ -7,6 +7,7 @@
"""
from galaxy.model.migrations.util import (
create_index,
DbObjectNames,
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 = DbObjectNames.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 @@ -18,6 +18,7 @@
create_index,
create_table,
create_unique_constraint,
DbObjectNames,
drop_column,
drop_constraint,
drop_index,
Expand All @@ -31,6 +32,10 @@
branch_labels = None
depends_on = None

old_index_name = DbObjectNames.index("default_quota_association", "type")
new_index_name = DbObjectNames.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 @@ -12,6 +12,7 @@

from galaxy.model.migrations.util import (
add_column,
DbObjectNames,
drop_column,
drop_index,
transaction,
Expand All @@ -27,6 +28,7 @@
# database object names used in this revision
table_name = "api_keys"
column_name = "deleted"
index_name = DbObjectNames.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)
23 changes: 23 additions & 0 deletions lib/galaxy/model/migrations/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,26 @@ def transaction():
except OperationalError:
op.execute("ROLLBACK")
raise


class DbObjectNames:
"""
Helper methods for generating names for database constraints and indexes.
This format is based on the naming convention specified in the galaxy.model module.
"""

@staticmethod
def foreign_key(table_name, column_name):
return f"{table_name}_{column_name}_fkey"

@staticmethod
def unique_constraint(table_name, column_name):
return f"{table_name}_{column_name}_key"

@staticmethod
def check_constraint(table_name, column_name):
return f"{table_name}_{column_name}_check"

@staticmethod
def index(table_name, column_name):
return f"ix_{table_name}_{column_name}"