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

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

# This file is part of Buildbot.  Buildbot is free software: you can 

# redistribute it and/or modify it under the terms of the GNU General Public 

# License as published by the Free Software Foundation, version 2. 

# 

# This program is distributed in the hope that it will be useful, but WITHOUT 

# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 

# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 

# details. 

# 

# You should have received a copy of the GNU General Public License along with 

# this program; if not, write to the Free Software Foundation, Inc., 51 

# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 

# 

# Copyright Buildbot Team Members 

 

import sqlalchemy as sa 

from migrate.changeset import constraint 

from buildbot.util import sautils 

 

def upgrade(migrate_engine): 

 

    metadata = sa.MetaData() 

    metadata.bind = migrate_engine 

 

    sourcestamps_table = sa.Table('sourcestamps', metadata, autoload=True) 

    buildsets_table = sa.Table('buildsets', metadata, autoload=True) 

 

    # Create the sourcestampset table 

    # that defines a sourcestampset 

    sourcestampsets_table = sa.Table("sourcestampsets", metadata, 

        sa.Column("id", sa.Integer, primary_key=True), 

    ) 

    sourcestampsets_table.create() 

 

    # All current sourcestampid's are migrated to sourcestampsetid 

    # Insert all sourcestampid's as setid's into sourcestampsets table 

    sourcestampsetids = sa.select([sourcestamps_table.c.id]) 

    # this doesn't seem to work without str() -- verified in sqla 0.6.0 - 0.7.1 

    migrate_engine.execute(str(sautils.InsertFromSelect(sourcestampsets_table, sourcestampsetids))) 

 

    # rename the buildsets table column 

    buildsets_table.c.sourcestampid.alter(name='sourcestampsetid') 

 

    metadata.remove(buildsets_table) 

    buildsets_table = sa.Table('buildsets', metadata, autoload=True) 

 

    cons = constraint.ForeignKeyConstraint([buildsets_table.c.sourcestampsetid], [sourcestampsets_table.c.id]) 

    cons.create() 

 

    # Add sourcestampsetid including index to sourcestamps table 

    ss_sourcestampsetid = sa.Column('sourcestampsetid', sa.Integer) 

    ss_sourcestampsetid.create(sourcestamps_table) 

 

    # Update the setid to the same value as sourcestampid 

    migrate_engine.execute(str(sourcestamps_table.update().values(sourcestampsetid=sourcestamps_table.c.id))) 

    ss_sourcestampsetid.alter(nullable=False) 

 

    # Data is up to date, now force integrity 

    cons = constraint.ForeignKeyConstraint([sourcestamps_table.c.sourcestampsetid], [sourcestampsets_table.c.id]) 

    cons.create() 

 

    # Add index for performance reasons to find all sourcestamps in a set quickly 

    idx = sa.Index('sourcestamps_sourcestampsetid', sourcestamps_table.c.sourcestampsetid, unique=False) 

    idx.create()