1# This file is part of Buildbot. Buildbot is free software: you can 2# redistribute it and/or modify it under the terms of the GNU General Public 3# License as published by the Free Software Foundation, version 2. 4# 5# This program is distributed in the hope that it will be useful, but WITHOUT 6# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 7# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 8# details. 9# 10# You should have received a copy of the GNU General Public License along with 11# this program; if not, write to the Free Software Foundation, Inc., 51 12# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 13# 14# Copyright Buildbot Team Members 15 16 17import os 18 19import sqlalchemy as sa 20from alembic.runtime.migration import MigrationContext 21 22from twisted.internet import defer 23from twisted.python import log 24 25from buildbot.db import connector 26from buildbot.test.fake import fakemaster 27from buildbot.test.util import db 28from buildbot.test.util import dirs 29from buildbot.test.util import querylog 30from buildbot.test.util.misc import TestReactorMixin 31from buildbot.util import sautils 32 33# test_upgrade vs. migration tests 34# 35# test_upgrade is an integration test -- it tests the whole upgrade process, 36# including the code in model.py. Migrate tests are unit tests, and test a 37# single db upgrade script. 38 39 40class MigrateTestMixin(TestReactorMixin, db.RealDatabaseMixin, dirs.DirsMixin): 41 42 @defer.inlineCallbacks 43 def setUpMigrateTest(self): 44 self.setUpTestReactor() 45 self.basedir = os.path.abspath("basedir") 46 self.setUpDirs('basedir') 47 48 yield self.setUpRealDatabase() 49 50 master = fakemaster.make_master(self) 51 self.db = connector.DBConnector(self.basedir) 52 yield self.db.setServiceParent(master) 53 self.db.pool = self.db_pool 54 55 def tearDownMigrateTest(self): 56 self.tearDownDirs() 57 return self.tearDownRealDatabase() 58 59 @defer.inlineCallbacks 60 def do_test_migration(self, base_revision, target_revision, 61 setup_thd_cb, verify_thd_cb): 62 63 def setup_thd(conn): 64 metadata = sa.MetaData() 65 table = sautils.Table( 66 'alembic_version', metadata, 67 sa.Column("version_num", sa.String(32), nullable=False), 68 ) 69 table.create(bind=conn) 70 conn.execute(table.insert(), version_num=base_revision) 71 setup_thd_cb(conn) 72 yield self.db.pool.do(setup_thd) 73 74 alembic_scripts = self.alembic_get_scripts() 75 76 def upgrade_thd(engine): 77 with querylog.log_queries(): 78 with sautils.withoutSqliteForeignKeys(engine): 79 with engine.connect() as conn: 80 81 def upgrade(rev, context): 82 log.msg(f'Upgrading from {rev} to {target_revision}') 83 return alembic_scripts._upgrade_revs(target_revision, rev) 84 85 context = MigrationContext.configure(conn, opts={'fn': upgrade}) 86 87 with context.begin_transaction(): 88 context.run_migrations() 89 90 yield self.db.pool.do_with_engine(upgrade_thd) 91 92 def check_table_charsets_thd(engine): 93 # charsets are only a problem for MySQL 94 if engine.dialect.name != 'mysql': 95 return 96 dbs = [r[0] for r in engine.execute("show tables")] 97 for tbl in dbs: 98 r = engine.execute("show create table {}".format(tbl)) 99 create_table = r.fetchone()[1] 100 self.assertIn('DEFAULT CHARSET=utf8', create_table, 101 "table {} does not have the utf8 charset".format(tbl)) 102 yield self.db.pool.do(check_table_charsets_thd) 103 104 def verify_thd(engine): 105 with sautils.withoutSqliteForeignKeys(engine): 106 verify_thd_cb(engine) 107 108 yield self.db.pool.do(verify_thd) 109