Skip to content

Commit

Permalink
Closes #427: Migrate schedule_until into new json schedule field.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marina Samuel committed Oct 10, 2018
1 parent 41c41be commit 1b30056
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions migrations/versions/b8a479422596_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
Migrate schedule_until to schedule.until
Revision ID: b8a479422596
Revises: 640888ce445d
Create Date: 2018-10-10 14:53:20.042470
"""
from datetime import datetime
from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import table

from redash.models import MutableDict, PseudoJSON


# revision identifiers, used by Alembic.
revision = 'b8a479422596'
down_revision = '640888ce445d'
branch_labels = None
depends_on = None


def upgrade():
queries = table(
'queries',
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('schedule', MutableDict.as_mutable(PseudoJSON)),
sa.Column('schedule_until', sa.DateTime(True), nullable=True))

conn = op.get_bind()
for query in conn.execute(queries.select()):
if query.schedule_until is None:
continue

schedule_json = query.schedule
schedule_json['until'] = query.schedule_until.strftime('%Y-%m-%d')

conn.execute(
queries
.update()
.where(queries.c.id == query.id)
.values(schedule=MutableDict(schedule_json)))

op.drop_column('queries', 'schedule_until')


def downgrade():
op.add_column('queries', sa.Column('schedule_until', sa.DateTime(True), nullable=True))

queries = table(
'queries',
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('schedule', MutableDict.as_mutable(PseudoJSON)),
sa.Column('schedule_until', sa.DateTime(True), nullable=True))

conn = op.get_bind()
for query in conn.execute(queries.select()):
if query.schedule['until'] is None:
continue

scheduleUntil = datetime.strptime(query.schedule['until'], '%Y-%m-%d')

conn.execute(
queries
.update()
.where(queries.c.id == query.id)
.values(schedule_until=scheduleUntil))

0 comments on commit 1b30056

Please sign in to comment.