forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #427: Migrate schedule_until into new json schedule field.
- Loading branch information
Marina Samuel
committed
Oct 10, 2018
1 parent
41c41be
commit 1b30056
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |