summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRĂ³man Joost <rjoost@redhat.com>2018-04-17 17:03:42 +1000
committerRoman Joost <rjoost@redhat.com>2018-04-18 23:15:06 +0000
commit1cd6d8ad9db219e1871cb9f6b630dc483e6d5a33 (patch)
treefbae219cebcb0b33afcaa805ba75d813c8f071bd
parentdc844d467b09c78c4cda836886771b2d472071a4 (diff)
Adds installation migration for scheduled jobs
This adds a migration for scheduled jobs which have been affected by the Beaker 24 -> 25.1 upgrade, but have been missed by the migration which should have taken care of them. Bug: 1568224 Change-Id: I44ce7fa7ac57c4b43da63779b80e3bdcd60b7f67
-rw-r--r--IntegrationTests/src/bkr/inttest/server/test_database_migration.py47
-rw-r--r--Server/bkr/server/data-migrations/insert-installation-row-for-scheduled-recipes-before-25.py45
2 files changed, 92 insertions, 0 deletions
diff --git a/IntegrationTests/src/bkr/inttest/server/test_database_migration.py b/IntegrationTests/src/bkr/inttest/server/test_database_migration.py
index 12a2291..bccc111 100644
--- a/IntegrationTests/src/bkr/inttest/server/test_database_migration.py
+++ b/IntegrationTests/src/bkr/inttest/server/test_database_migration.py
@@ -1429,3 +1429,50 @@ class MigrationTest(unittest.TestCase):
with self.migration_metadata.bind.connect() as connection:
self.assertEqual(2,
connection.scalar('SELECT count(*) FROM installation;'))
+
+ # https://bugzilla.redhat.com/show_bug.cgi?id=1568224
+ def test_scheduled_recipe_has_installation_column_after_upgrade(self):
+ with self.migration_metadata.bind.connect() as connection:
+ connection.execute(pkg_resources.resource_string('bkr.inttest.server', 'database-dumps/25.sql'))
+ # add a queued job
+ connection.execute(
+ "INSERT INTO osmajor (id, osmajor) "
+ "VALUES (1, 'RedHatEnterpriseLinux6')")
+ connection.execute(
+ "INSERT INTO osversion (osmajor_id, osminor) "
+ "VALUES (1, 9)")
+ connection.execute(
+ "INSERT INTO distro (name, osversion_id) "
+ "VALUES ('RHEL6', 1)")
+ connection.execute(
+ "INSERT INTO distro_tree (distro_id, arch_id, variant) "
+ "VALUES (1, 1, 'Server')")
+ connection.execute(
+ "INSERT INTO job (owner_id, retention_tag_id, status, dirty_version, clean_version) "
+ "VALUES (1, 1, 'Scheduled', '', '2017-07-27 14:28:20')")
+ connection.execute(
+ "INSERT INTO recipe_set (job_id, status) "
+ "VALUES (1, 'Scheduled')")
+ connection.execute(
+ "INSERT INTO recipe (type, recipe_set_id, distro_tree_id, status) "
+ "VALUES ('machine_recipe', 1, 1, 'Scheduled')")
+ recipe = self.migration_session.query(Recipe).get(1)
+ # Recipe has been missed by past migration and has no installation row
+ self.assertIsNone(recipe.installation)
+ self.migration_session.close()
+
+ migration = DataMigration(name=u'insert-installation-row-for-scheduled-recipes-before-25')
+ finished = migration.migrate_one_batch(self.migration_metadata.bind)
+ self.assertTrue(finished)
+
+ recipe = self.migration_session.query(Recipe).get(1)
+ self.assertIsNotNone(recipe.installation)
+ self.assertEqual(recipe.installation.distro_tree_id, 1)
+ self.assertEqual(recipe.installation.arch.arch, u'i386')
+ self.assertEqual(recipe.installation.variant, u'Server')
+ self.assertEqual(recipe.installation.distro_name, u'RHEL6')
+ self.assertEqual(recipe.installation.osmajor, u'RedHatEnterpriseLinux6')
+ self.assertEqual(recipe.installation.osminor, u'9')
+ with self.migration_metadata.bind.connect() as connection:
+ self.assertEqual(1,
+ connection.scalar('SELECT count(*) FROM installation;'))
diff --git a/Server/bkr/server/data-migrations/insert-installation-row-for-scheduled-recipes-before-25.py b/Server/bkr/server/data-migrations/insert-installation-row-for-scheduled-recipes-before-25.py
new file mode 100644
index 0000000..2219379
--- /dev/null
+++ b/Server/bkr/server/data-migrations/insert-installation-row-for-scheduled-recipes-before-25.py
@@ -0,0 +1,45 @@
+# This program 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; either version 2 of the License, or
+# (at your option) any later version.
+
+"""
+Alembic revision 3ba776df4c76 was supposed to INSERT rows into the
+'installation' table for recipes that were not yet provisioned at the time when
+Beaker was upgraded to 25. It originally had a bug where it was ignoring
+recipes in the 'Scheduled' state. That migration is fixed now, but we also
+repeat the same INSERT statement here, for Beaker sites which were already
+upgraded using the buggy version of the Alembic migration.
+"""
+
+import logging
+
+
+logger = logging.getLogger(__name__)
+
+
+def migrate_one_batch(engine):
+ with engine.begin() as connection:
+ result = connection.execute("""
+ INSERT INTO installation
+ (created, distro_tree_id, recipe_id, arch_id, distro_name, osmajor, osminor, variant)
+ SELECT UTC_TIMESTAMP(),
+ recipe.distro_tree_id,
+ recipe.id,
+ distro_tree.arch_id,
+ distro.name,
+ osmajor.osmajor,
+ osversion.osminor,
+ distro_tree.variant
+ FROM recipe
+ INNER JOIN distro_tree ON distro_tree.id = recipe.distro_tree_id
+ INNER JOIN distro ON distro.id = distro_tree.distro_id
+ INNER JOIN osversion ON osversion.id = distro.osversion_id
+ INNER JOIN osmajor ON osmajor.id = osversion.osmajor_id
+ LEFT OUTER JOIN installation ON installation.recipe_id = recipe.id
+ WHERE recipe.status = 'Scheduled'
+ AND installation.recipe_id is NULL;
+ """)
+
+ logger.info('Created installation row for %d recipes', result.rowcount)
+ return True