storyboard/storyboard/db/migration/alembic_migrations/versions/017_timeline_events.py

130 lines
4.3 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
"""Add timeline events. Migrate comments under a new event each.
Revision ID: 017
Revises: 016
Create Date: 2014-04-17 13:05:26.572216
"""
# revision identifiers, used by Alembic.
revision = '017'
down_revision = '016'
from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql.expression import table
from sqlalchemy import MetaData, Table
from storyboard.common import event_types
MYSQL_ENGINE = 'InnoDB'
MYSQL_CHARSET = 'utf8'
def upgrade(active_plugins=None, options=None):
### commands auto generated by Alembic - please adjust! ###
op.create_table(
'events',
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('story_id', sa.Integer(), nullable=True),
sa.Column('comment_id', sa.Integer(), nullable=True),
sa.Column('author_id', sa.Integer(), nullable=True),
sa.Column('event_type', sa.Unicode(length=100), nullable=False),
sa.Column('event_info', sa.UnicodeText(), nullable=True),
sa.PrimaryKeyConstraint('id'),
mysql_engine=MYSQL_ENGINE,
mysql_charset=MYSQL_CHARSET)
bind = op.get_bind()
comments = list(bind.execute(
sa.select(columns=['*'], from_obj=Table('comments', MetaData()))))
events_table = table(
'events',
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('story_id', sa.Integer(), nullable=True),
sa.Column('comment_id', sa.Integer(), nullable=True),
sa.Column('author_id', sa.Integer(), nullable=True),
sa.Column('event_type', sa.Unicode(length=100), nullable=False)
)
for comment in comments:
bind.execute(events_table.insert().values(
created_at=comment['created_at'],
event_type=event_types.USER_COMMENT,
comment_id=comment['id'],
story_id=comment['story_id'],
author_id=comment['author_id']
))
op.drop_constraint('comments_ibfk_1', 'comments', type_='foreignkey')
op.drop_constraint('comments_ibfk_2', 'comments', type_='foreignkey')
op.drop_column(u'comments', u'action')
op.drop_column(u'comments', u'story_id')
op.drop_column(u'comments', u'author_id')
op.drop_column(u'comments', u'comment_type')
### end Alembic commands ###
def downgrade(active_plugins=None, options=None):
### commands auto generated by Alembic - please adjust! ###
op.add_column(
u'comments',
sa.Column(u'comment_type', sa.Unicode(length=20), nullable=True))
op.add_column(
u'comments',
sa.Column(u'author_id', sa.Integer(), nullable=True))
op.add_column(
u'comments',
sa.Column(u'story_id', sa.Integer(), nullable=True))
op.add_column(
u'comments',
sa.Column(u'action', sa.Unicode(length=150), nullable=True))
op.create_foreign_key('comments_ibfk_1', 'comments',
'users', ['author_id'], ['id'])
op.create_foreign_key('comments_ibfk_2', 'comments',
'stories', ['story_id'], ['id'])
bind = op.get_bind()
events = list(bind.execute(
sa.select(columns=['*'], from_obj=Table('events', MetaData()))))
comments_table = table(
'comments',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('story_id', sa.Integer(), nullable=True),
sa.Column('author_id', sa.Integer(), nullable=True),
)
for event in events:
bind.execute(comments_table.update()
.where(comments_table.c.id == event['comment_id'])
.values(story_id=event['story_id'],
author_id=event['author_id']))
op.drop_table('events')
### end Alembic commands ###