Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / alembic / versions / 3b62b3f1bf6c_add_attributes_node_.py @ ad9ada51

History | View | Annotate | Download (1.6 kB)

1
"""add attributes node column
2

3
Revision ID: 3b62b3f1bf6c
4
Revises: 4c8ccdc58192
5
Create Date: 2013-07-04 13:11:01.842706
6

7
"""
8

    
9
# revision identifiers, used by Alembic.
10
revision = '3b62b3f1bf6c'
11
down_revision = '4c8ccdc58192'
12

    
13
from alembic import op
14
import sqlalchemy as sa
15
from sqlalchemy.sql import table, column, and_
16

    
17

    
18
def upgrade():
19
    op.add_column('attributes',
20
                  sa.Column('node', sa.Integer, default=0))
21
    op.add_column('attributes',
22
                  sa.Column('is_latest', sa.Boolean, default=True))
23

    
24
    n = table('nodes',
25
              column('node', sa.Integer),
26
              column('latest_version', sa.Integer))
27
    v = table('versions',
28
              column('node', sa.Integer),
29
              column('serial', sa.Integer))
30
    a = table('attributes',
31
              column('serial', sa.Integer),
32
              column('node', sa.Integer),
33
              column('is_latest', sa.Boolean))
34

    
35
    s = sa.select([v.c.node]).where(v.c.serial == a.c.serial)
36
    u = a.update().values({'node': s})
37
    op.execute(u)
38

    
39
    s = sa.select([v.c.serial == n.c.latest_version],
40
                  and_(a.c.node == n.c.node, a.c.serial == v.c.serial))
41
    u = a.update().values({'is_latest': s})
42
    op.execute(u)
43

    
44
    op.alter_column('attributes', 'node', nullable=False)
45
    op.alter_column('attributes', 'is_latest', nullable=False)
46

    
47
    op.create_index('idx_attributes_serial_node', 'attributes',
48
                    ['serial', 'node'])
49

    
50

    
51
def downgrade():
52
    op.drop_index('idx_attributes_serial_node')
53

    
54
    op.drop_column('attributes', 'is_latest')
55
    op.drop_column('attributes', 'node')