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

[migration] Adding migration to remove empty in/not-in filters #5161

Merged
Merged
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""remove empty filters

Revision ID: afb7730f6a9c
Revises: c5756bec8b47
Create Date: 2018-06-07 09:52:54.535961

"""

# revision identifiers, used by Alembic.
revision = 'afb7730f6a9c'
down_revision = 'c5756bec8b47'

from alembic import op
import json
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, Text

from superset import db

Base = declarative_base()


class Slice(Base):
__tablename__ = 'slices'

id = Column(Integer, primary_key=True)
params = Column(Text)


def upgrade():
bind = op.get_bind()
session = db.Session(bind=bind)

for slc in session.query(Slice).all():
try:
params = json.loads(slc.params)

for key in ('filters', 'having_filters', 'extra_filters'):
value = params.get(key)

# Remove empty in/not-in filters.
if value:
params[key] = [
x for x in value
if not (x['op'] in ('in', 'not in') and not x['val'])
]

slc.params = json.dumps(params, sort_keys=True)
except Exception:
pass

session.commit()
session.close()


def downgrade():
pass