Files
notifications-api/migrations/versions/0007_template_history.py
Nicholas Staples cec582e73d Fix migration.
2016-04-25 13:57:55 +01:00

71 lines
3.3 KiB
Python

"""empty message
Revision ID: 0007_template_history
Revises: 0006_api_keys_history
Create Date: 2016-04-22 09:51:55.615891
"""
# revision identifiers, used by Alembic.
revision = '0007_template_history'
down_revision = '0006_api_keys_history'
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.create_table('templates_history',
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('template_type', sa.String(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.Column('content', sa.Text(), nullable=False),
sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('subject', sa.Text(), nullable=True),
sa.Column('created_by_id', postgresql.UUID(as_uuid=True), nullable=True),
sa.Column('version', sa.Integer(), autoincrement=False, nullable=False),
sa.PrimaryKeyConstraint('id', 'version')
)
op.create_index(op.f('ix_templates_history_created_by_id'), 'templates_history', ['created_by_id'], unique=False)
op.create_index(op.f('ix_templates_history_service_id'), 'templates_history', ['service_id'], unique=False)
op.add_column('templates', sa.Column('created_by_id', postgresql.UUID(as_uuid=True), nullable=True))
op.add_column('templates', sa.Column('version', sa.Integer(), nullable=True))
op.get_bind()
op.execute('UPDATE templates SET created_by_id = (SELECT user_id FROM user_to_service WHERE templates.service_id = user_to_service.service_id LIMIT 1)')
op.execute('UPDATE templates SET version = 1, created_at = now()')
op.execute((
'INSERT INTO templates_history (id, name, template_type, created_at, updated_at, content, service_id, subject, version)'
' SELECT id, name, template_type, created_at, updated_at, content, service_id, subject, version FROM templates'))
op.alter_column('templates', 'created_at', nullable=False)
op.alter_column('templates', 'created_by_id', nullable=False)
op.alter_column('templates', 'version', nullable=False)
op.create_index(op.f('ix_templates_created_by_id'), 'templates', ['created_by_id'], unique=False)
op.create_foreign_key("fk_templates_created_by_id", 'templates', 'users', ['created_by_id'], ['id'])
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_constraint("fk_templates_created_by_id", 'templates', type_='foreignkey')
op.drop_index(op.f('ix_templates_created_by_id'), table_name='templates')
op.drop_column('templates', 'version')
op.drop_column('templates', 'created_by_id')
op.alter_column('api_keys_history', 'created_by_id',
existing_type=postgresql.UUID(),
nullable=True)
op.alter_column('api_keys_history', 'created_at',
existing_type=postgresql.TIMESTAMP(),
nullable=True)
op.drop_index(op.f('ix_templates_history_service_id'), table_name='templates_history')
op.drop_index(op.f('ix_templates_history_created_by_id'), table_name='templates_history')
op.drop_table('templates_history')
### end Alembic commands ###