Refactor migrations adding indexes

This commit is contained in:
Nicola Soranzo
2019-04-09 00:29:47 +01:00
parent 60c357ecdf
commit 9498ae5d4e
3 changed files with 45 additions and 36 deletions
@@ -3,9 +3,15 @@
import logging
import sys
from sqlalchemy import Index, MetaData, Table
from sqlalchemy import (
MetaData,
Table
)
from galaxy.model.migrate.versions.util import engine_false
from galaxy.model.migrate.versions.util import (
add_index,
engine_false
)
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
@@ -23,18 +29,8 @@ def upgrade(migrate_engine):
metadata.reflect()
User_table = Table("galaxy_user", metadata, autoload=True)
HistoryDatasetAssociation_table = Table("history_dataset_association", metadata, autoload=True)
# Add 2 indexes to the galaxy_user table
i = Index('ix_galaxy_user_deleted', User_table.c.deleted)
try:
i.create()
except Exception:
log.exception("Adding index 'ix_galaxy_user_deleted' to galaxy_user table failed.")
i = Index('ix_galaxy_user_purged', User_table.c.purged)
try:
i.create()
except Exception:
log.exception("Adding index 'ix_galaxy_user_purged' to galaxy_user table failed.")
add_index('ix_galaxy_user_deleted', User_table, 'deleted')
add_index('ix_galaxy_user_purged', User_table, 'purged')
# Set the default data in the galaxy_user table, but only for null values
cmd = "UPDATE galaxy_user SET deleted = %s WHERE deleted is null" % engine_false(migrate_engine)
try:
@@ -46,12 +42,9 @@ def upgrade(migrate_engine):
migrate_engine.execute(cmd)
except Exception:
log.exception("Setting default data for galaxy_user.purged column failed.")
# Add 1 index to the history_dataset_association table
i = Index('ix_hda_copied_from_library_dataset_dataset_association_id', HistoryDatasetAssociation_table.c.copied_from_library_dataset_dataset_association_id)
try:
i.create()
except Exception:
log.exception("Adding index 'ix_hda_copied_from_library_dataset_dataset_association_id' to history_dataset_association table failed.")
add_index('ix_hda_copied_from_library_dataset_dataset_association_id',
'history_dataset_association',
'copied_from_library_dataset_dataset_association_id', metadata)
def downgrade(migrate_engine):
@@ -6,7 +6,8 @@ from __future__ import print_function
import logging
from sqlalchemy import Index, MetaData, Table
from sqlalchemy.engine import reflection
from galaxy.model.migrate.versions.util import add_index
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
@@ -54,18 +55,9 @@ def upgrade(migrate_engine):
print(__doc__)
metadata.bind = migrate_engine
metadata.reflect()
insp = reflection.Inspector.from_engine(migrate_engine)
# Create missing indexes
for ix, table, col in indexes:
try:
log.debug("Creating index '%s' on column '%s' in table '%s'" % (ix, col, table))
t = Table(table, metadata, autoload=True)
if ix not in [ins_ix.get('name', None) for ins_ix in insp.get_indexes(table)]:
Index(ix, t.c[col]).create()
else:
pass # Index already exists, don't recreate.
except Exception:
log.exception("Unable to create index '%s'.", ix)
add_index(ix, table, col, metadata)
def downgrade(migrate_engine):
+28 -4
View File
@@ -1,6 +1,9 @@
import logging
from sqlalchemy import Table
from sqlalchemy import (
Index,
Table
)
log = logging.getLogger(__name__)
@@ -68,7 +71,7 @@ def add_column(column, table, metadata=None, **kwds):
assert metadata is not None
table = Table(table, metadata, autoload=True)
column.create(table, **kwds)
assert column is getattr(table.c, column.name)
assert column is table.c[column.name]
except Exception:
log.exception("Adding column '%s' to table '%s' failed.", column, table)
@@ -85,7 +88,7 @@ def alter_column(column_name, table, metadata=None, **kwds):
if not isinstance(table, Table):
assert metadata is not None
table = Table(table, metadata, autoload=True)
column = getattr(table.c, column_name)
column = table.c[column_name]
column.alter(**kwds)
except Exception:
log.exception("Modifying column '%s' of table '%s' failed.", column_name, table)
@@ -103,7 +106,28 @@ def drop_column(column_name, table, metadata=None):
if not isinstance(table, Table):
assert metadata is not None
table = Table(table, metadata, autoload=True)
column = getattr(table.c, column_name)
column = table.c[column_name]
column.drop()
except Exception:
log.exception("Dropping column '%s' from table '%s' failed.", column_name, table)
def add_index(index_name, table, column_name, metadata=None):
"""
:param table: Table to drop the column from
:type table: :class:`Table` or str
:param metadata: Needed only if ``table`` is a table name
:type metadata: :class:`Metadata`
"""
try:
if not isinstance(table, Table):
assert metadata is not None
table = Table(table, metadata, autoload=True)
if index_name not in [ix.name for ix in table.indexes]:
index = Index(index_name, table.c[column_name])
index.create()
else:
log.debug("Index '%s' on column '%s' in table '%s' already exists.", index_name, column_name, table)
except Exception:
log.exception("Adding index '%s' on column '%s' to table '%s' failed.", index_name, column_name, table)