014_Add_Last_XEM_Refresh_Column_To_TVShows_Table.py 1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""Initial migration

Revision ID: 14
Revises:
Create Date: 2017-12-29 14:39:27.854291

"""
import datetime

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = '14'
down_revision = '13'


def upgrade():
    conn = op.get_bind()
    meta = sa.MetaData(bind=conn)
    tv_shows = sa.Table('tv_shows', meta, autoload=True)
    xem_refresh = sa.Table('xem_refresh', meta, autoload=True)

    if not hasattr(tv_shows.c, 'last_xem_refresh'):
        op.add_column('tv_shows', sa.Column('last_xem_refresh', sa.Integer, default=datetime.datetime.now().toordinal()))

    with op.get_context().begin_transaction():
        for row in conn.execute(xem_refresh.select()):
29
            last_xem_refresh = row.last_refreshed or datetime.datetime.now().toordinal()
30
            conn.execute('UPDATE tv_shows SET last_xem_refresh = {} WHERE tv_shows.indexer_id = {}'
31
                         .format(last_xem_refresh, row.indexer_id))
32
33
34
35
36
37

    op.drop_table('xem_refresh')


def downgrade():
    pass