Skip to content

Commit

Permalink
Use raw SQL to validate installation attributes
Browse files Browse the repository at this point in the history
We can't use declarative model here, because model may change and
SQLAlchemy will generate query that doesn't match target schema. In this
case we need query that matches release 25.

Signed-off-by: Martin Styk <[email protected]>
  • Loading branch information
StykMartin committed Dec 23, 2023
1 parent 8012be0 commit 321a1dc
Showing 1 changed file with 54 additions and 17 deletions.
71 changes: 54 additions & 17 deletions IntegrationTests/src/bkr/inttest/server/test_database_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,10 @@ def test_scheduled_recipe_has_installation_column_after_upgrade(self):
"VALUES ('machine_recipe', 1, 1, 'Scheduled', FALSE)")
recipe = self.migration_session.query(Recipe).get(1)
# Recipe has been missed by past migration and has no installation row
self.assertIsNone(recipe.installation)
row_count = self.migration_session.execute(
"SELECT COUNT(*) FROM installation WHERE recipe_id = :recipe_id", {"recipe_id": recipe.id}
).scalar()
self.assertEqual(0, row_count)
self.migration_session.close()

# This migration has become a NOOP
Expand All @@ -1574,13 +1577,27 @@ def test_scheduled_recipe_has_installation_column_after_upgrade(self):
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')
row = self.migration_session.execute(
"""SELECT
i.distro_tree_id,
i.variant,
i.distro_name,
i.osmajor,
i.osminor,
a.arch
FROM installation i
JOIN arch a ON i.arch_id = a.id
WHERE i.recipe_id = :recipe_id
""", {"recipe_id": recipe.id}
).fetchone()

self.assertIsNotNone(row)
self.assertEqual(row['distro_tree_id'], 1)
self.assertEqual(row['arch'], u'i386')
self.assertEqual(row['variant'], u'Server')
self.assertEqual(row['distro_name'], u'RHEL6')
self.assertEqual(row['osmajor'], u'RedHatEnterpriseLinux6')
self.assertEqual(row['osminor'], u'9')
with self.migration_metadata.bind.connect() as connection:
self.assertEqual(1,
connection.scalar('SELECT count(*) FROM installation;'))
Expand Down Expand Up @@ -1632,24 +1649,44 @@ def test_scheduled_recipe_has_installation_column_after_upgrade_when_cancelled(s
"VALUES (6, 'machine_recipe', 1, 1, 'Scheduled', FALSE)")
r1, r2, r3, r4, r5, r6 = self.migration_session.query(Recipe).all()
for recipe in [r1, r2, r4, r5, r6]:
self.assertIsNone(recipe.installation)
self.assertIsNotNone(r3.installation)
row_count = self.migration_session.execute(
"SELECT COUNT(*) FROM installation WHERE recipe_id = :recipe_id", {"recipe_id": recipe.id}
).scalar()
self.assertEqual(0, row_count)

row_count = self.migration_session.execute(
"SELECT COUNT(*) FROM installation WHERE recipe_id = :recipe_id", {"recipe_id": r3.id}
).scalar()
self.assertEqual(1, row_count)
self.migration_session.close()

migration = DataMigration(name=u'insert-installation-row-for-recipes-before-25-take-2')
finished = migration.migrate_one_batch(self.migration_metadata.bind)
self.assertTrue(finished)

for recipe in self.migration_session.query(Recipe).all():
self.assertIsNotNone(recipe.installation,
row = self.migration_session.execute(
"""SELECT
i.distro_tree_id,
i.variant,
i.distro_name,
i.osmajor,
i.osminor,
a.arch
FROM installation i
JOIN arch a ON i.arch_id = a.id
WHERE i.recipe_id = :recipe_id
""", {"recipe_id": recipe.id}
).fetchone()
self.assertIsNotNone(row,
'Installation row for recipeid %s (%s) should exist' % (
recipe.id, recipe.status))
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')
self.assertEqual(row['distro_tree_id'], 1)
self.assertEqual(row['arch'], u'i386')
self.assertEqual(row['variant'], u'Server')
self.assertEqual(row['distro_name'], u'RHEL6')
self.assertEqual(row['osmajor'], u'RedHatEnterpriseLinux6')
self.assertEqual(row['osminor'], u'9')
with self.migration_metadata.bind.connect() as connection:
self.assertEqual(6,
connection.scalar('SELECT count(*) FROM installation;'))

0 comments on commit 321a1dc

Please sign in to comment.