aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/unit/test_db_migrate_versions_022_add_codebase.py
blob: 95e154f54a77a3a59cd7413815808d8d3c8f8835 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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 datetime
import sqlalchemy as sa
from twisted.trial import unittest
from buildbot.test.util import migration
from buildbot.util import UTC, datetime2epoch

class Migration(migration.MigrateTestMixin, unittest.TestCase):

    def setUp(self):
        return self.setUpMigrateTest()

    def tearDown(self):
        return self.tearDownMigrateTest()

    # create tables as they are before migrating to version 019
    def create_tables_thd(self, conn):
        metadata = sa.MetaData()
        metadata.bind = conn

        self.sourcestamps = sa.Table('sourcestamps', metadata,
            sa.Column('id', sa.Integer,  primary_key=True),
            sa.Column('branch', sa.String(256)),
            sa.Column('revision', sa.String(256)),
            sa.Column('patchid', sa.Integer),
            sa.Column('repository', sa.String(length=512), nullable=False, server_default=''),
            sa.Column('project', sa.String(length=512), nullable=False, server_default=''),
            sa.Column('sourcestampsetid', sa.Integer),
        )
        self.sourcestamps.create(bind=conn)

        self.changes = sa.Table('changes', metadata,
            sa.Column('changeid', sa.Integer,  primary_key=True),
            sa.Column('author', sa.String(256), nullable=False),
            sa.Column('comments', sa.String(1024), nullable=False),
            sa.Column('is_dir', sa.SmallInteger, nullable=False),
            sa.Column('branch', sa.String(256)),
            sa.Column('revision', sa.String(256)),
            sa.Column('revlink', sa.String(256)),
            sa.Column('when_timestamp', sa.Integer, nullable=False),
            sa.Column('category', sa.String(256)),
            sa.Column('repository', sa.String(length=512), nullable=False, server_default=''),
            sa.Column('project', sa.String(length=512), nullable=False, server_default=''),
        )
        self.changes.create(bind=conn)

    def reload_tables_after_migration(self, conn):
        metadata = sa.MetaData()
        metadata.bind = conn
        self.sourcestamps = sa.Table('sourcestamps', metadata, autoload=True)
        self.changes = sa.Table('changes', metadata, autoload=True)
        
    def fill_tables_with_testdata(self, conn, testdata):
        for ssid, repo, codebase, cid in testdata:
            self.insert_sourcestamps_changes(conn, ssid, repo, codebase, cid)

    def insert_sourcestamps_changes(self, conn, sourcestampid, repository, codebase, changeid):
        conn.execute(self.sourcestamps.insert(),
                id=sourcestampid,
                sourcestampsetid=sourcestampid,
                branch='this_branch',
                revision='this_revision',
                patchid = None,
                repository=repository,
                project='',
                codebase=codebase)

        dt_when = datetime.datetime(1978, 6, 15, 12, 31, 15, tzinfo=UTC)
        conn.execute(self.changes.insert(),
                changeid = changeid,
                author = 'develop',
                comments = 'no comment',
                is_dir = 0,
                branch = 'default',
                revision = 'FD56A89',
                revling = None,
                when_timestamp = datetime2epoch(dt_when),
                category = None,
                repository = repository,
                codebase = codebase,
                project = '')

    def test_changes_has_codebase(self):
        changesdata = [(1000, 'https://svn.com/repo_a', 'repo_a', 1)]
        def setup_thd(conn):
            self.create_tables_thd(conn)

        def verify_thd(conn):
            self.reload_tables_after_migration(conn)
            tbl = self.changes
            self.assertTrue(hasattr(tbl.c, 'codebase'), 'Column codebase not found')

            # insert data in the table and new column
            self.fill_tables_with_testdata(conn, changesdata)
            
            res = conn.execute(sa.select([tbl.c.changeid, tbl.c.repository, 
                                          tbl.c.codebase, ]))
            got_changes = res.fetchall()
            self.assertEqual(got_changes, [(1, 'https://svn.com/repo_a', 'repo_a')])

        return self.do_test_migration(21, 22, setup_thd, verify_thd)

    def test_sourcestamps_has_codebase(self):
        changesdata = [(1000, 'https://svn.com/repo_a', 'repo_a', 1)]
        def setup_thd(conn):
            self.create_tables_thd(conn)

        def verify_thd(conn):
            self.reload_tables_after_migration(conn)
            tbl = self.sourcestamps
            self.assertTrue(hasattr(tbl.c, 'codebase'), 'Column codebase not found')

            # insert data in the table and new column
            self.fill_tables_with_testdata(conn, changesdata)
            
            res = conn.execute(sa.select([tbl.c.id, tbl.c.repository, 
                                          tbl.c.codebase,]))
            got_sourcestamps = res.fetchall()
            self.assertEqual(got_sourcestamps, [(1000, 'https://svn.com/repo_a', 'repo_a')])

        return self.do_test_migration(21, 22, setup_thd, verify_thd)