"""Change store.properties to json from pickle

Revision ID: 11bc7a214883
Revises:
Create Date: 2017-05-19 15:42:07.361098

"""
import json
import pickle
from alembic import op


# revision identifiers, used by Alembic.
revision = '11bc7a214883'
down_revision = None
branch_labels = None
depends_on = None

first_agent_version = '17.6.1'


def upgrade():
    engine = op.get_bind()

    for row in engine.execute('select id, properties from stores').fetchall():
        if row['properties']:
            properties = json.dumps(pickle.loads(row['properties']))
            engine.execute('UPDATE stores SET properties=\'{}\' WHERE id=\'{}\''.format(
                properties, row['id']
            ))

def downgrade():
    engine = op.get_bind()

    for row in engine.execute('select id, properties from stores').fetchall():
        if row['properties']:
            properties = pickle.dumps(json.loads(row['properties']))
            engine.execute('UPDATE stores SET properties=\'{}\' WHERE id=\'{}\''.format(
                properties, row['id']
            ))
