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

ENH: add alembic migration #777

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
ENH: add alembic revision
  • Loading branch information
genematx committed Aug 9, 2024
commit 341b11c90ae3de7a56f03b3c8072212222ea7e6a
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Change 'path' to 'dataset' in HDF5 assets

Revision ID: 562203c724c7
Revises: ed3a4223a600
Create Date: 2024-08-09 10:13:36.384838

"""
from alembic import op
import sqlalchemy as sa
from tiled.catalog.orm import JSONVariant

# revision identifiers, used by Alembic.
revision = '562203c724c7'
down_revision = 'ed3a4223a600'
branch_labels = None
depends_on = None

def upgrade():
connection = op.get_bind()
nodes = sa.Table(
"nodes",
sa.MetaData(),
sa.Column("id", sa.Integer),
sa.Column("metadata", JSONVariant),
)

# Loop over all nodes that have 'parameters' field, choose and update those related to hdf5 files
condition = sa.text("CAST(nodes.metadata->>'parameters' AS TEXT) != ''")
cursor = connection.execute(sa.select(nodes.c.id, nodes.c.metadata).filter(condition).select_from(nodes))
for _id, _md in cursor:
if 'hdf5' in (_md.get('mimetype', '') + _md.get('spec', '')).lower():
if isinstance(_md['parameters'], dict) and ('path' in _md['parameters'].keys()):
_md['parameters']['dataset'] = _md['parameters'].pop('path')
connection.execute(nodes.update().where(nodes.c.id == _id).values(metadata=_md))

def downgrade():
# This _could_ be implemented but we will wait for a need since we are
# still in alpha releases.
raise NotImplementedError
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def upgrade():
connection = op.get_bind()

if connection.engine.dialect.name == "postgresql":
# This change must be committed befor ethe new 'table' enum value can be used.
# This change must be committed before the new 'table' enum value can be used.
with op.get_context().autocommit_block():
op.execute(
sa.text(
Expand Down