aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/util/migration.py
blob: 42cef8709cb5a7378e2e1c751ead5c70f9cc6182 (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
# 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 os
from twisted.python import log
from twisted.internet import defer
import sqlalchemy as sa
import migrate
import migrate.versioning.api
from buildbot.db import connector
from buildbot.test.util import db, dirs, querylog
from buildbot.test.fake import fakemaster


# test_upgrade vs. migration tests
#
# test_upgrade is an integration test -- it tests the whole upgrade process,
# including the code in model.py.  Migrate tests are unit tests, and test a
# single db upgrade script.

class MigrateTestMixin(db.RealDatabaseMixin, dirs.DirsMixin):

    def setUpMigrateTest(self):
        self.basedir = os.path.abspath("basedir")
        self.setUpDirs('basedir')

        d = self.setUpRealDatabase()
        def make_dbc(_):
            master = fakemaster.make_master()
            self.db = connector.DBConnector(master, self.basedir)
            self.db.pool = self.db_pool
        d.addCallback(make_dbc)
        return d

    def tearDownMigrateTest(self):
        self.tearDownDirs()
        return self.tearDownRealDatabase()

    def do_test_migration(self, base_version, target_version,
                            setup_thd_cb, verify_thd_cb):
        d = defer.succeed(None)
        def setup_thd(conn):
            metadata = sa.MetaData()
            table = sa.Table('migrate_version', metadata,
                    sa.Column('repository_id', sa.String(250),
                                        primary_key=True),
                    sa.Column('repository_path', sa.Text),
                    sa.Column('version', sa.Integer))
            table.create(bind=conn)
            conn.execute(table.insert(),
                    repository_id='Buildbot',
                    repository_path=self.db.model.repo_path,
                    version=base_version)
            setup_thd_cb(conn)
        d.addCallback(lambda _ : self.db.pool.do(setup_thd))

        def upgrade_thd(engine):
            querylog.log_from_engine(engine)
            schema = migrate.versioning.schema.ControlledSchema(engine,
                                                    self.db.model.repo_path)
            changeset = schema.changeset(target_version)
            for version, change in changeset:
                log.msg('upgrading to schema version %d' % (version+1))
                schema.runchange(version, change, 1)
        d.addCallback(lambda _ : self.db.pool.do_with_engine(upgrade_thd))

        d.addCallback(lambda _ : self.db.pool.do(verify_thd_cb))
        return d