changeset 846:ee95915922dd

Add checks that (PostgreSQL) sequences have ended up in the correct state at the end of the upgrade scripts. Test currently fails on PostgreSQL as a result of #632.
author hodgestar
date Tue, 12 Oct 2010 21:23:28 +0000
parents a14913740f53
children f606ee47b8ca
files bitten/tests/upgrades.py bitten/upgrades.py
diffstat 2 files changed, 34 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/bitten/tests/upgrades.py
+++ b/bitten/tests/upgrades.py
@@ -35,7 +35,7 @@
             # Trac gained support for testing against different databases in 0.11.5
             # If this support is available, we copy the test db uri configuration
             # into the main test config so it can be picked up by
-            # upgrades._parse_scheme()
+            # upgrades.parse_scheme()
             self.env.config.set('trac', 'database', self.env.dburi)
         self.env.path = tempfile.mkdtemp()
         logs_dir = self.env.config.get("bitten", "logs_dir", "log/bitten")
@@ -166,10 +166,10 @@
         'bitten_report_item',
         'bitten_error',
         'old_step',
-        'old_config',
+        'old_config_v2',
         'old_log_v5',
         'old_log_v8',
-        'old_rule',
+        'old_rule_v9',
         'old_build_v11',
     ]
 
@@ -230,8 +230,6 @@
 
     def _check_basic_upgrade(self):
         """Check the results of an upgrade of basic data."""
-        db = self.env.get_db_cnx()
-
         configs = list(model.BuildConfig.select(self.env,
             include_inactive=True))
         platforms = list(model.TargetPlatform.select(self.env))
@@ -265,6 +263,33 @@
         log_file = logs[0].get_log_file(logs[0].filename)
         self.assertEqual(file(log_file, "rU").read(), "line1\nline2\n")
 
+        # check final sequences
+        for tbl, col in [
+            ('bitten_build', 'id'),
+            ('bitten_log', 'id'),
+            ('bitten_platform', 'id'),
+            ('bitten_report', 'id'),
+        ]:
+            self._check_sequence(tbl, col)
+
+    def _check_sequence(self, tbl, col):
+        scheme = upgrades.parse_scheme(self.env)
+        if scheme == "postgres":
+            self._check_postgres_sequence(tbl, col)
+
+    def _check_postgres_sequence(self, tbl, col):
+        """Check a PostgreSQL sequence for the given table and column."""
+        seq = '%s_%s_seq' % (tbl, col)
+        cursor = self.env.get_db_cnx().cursor()
+        cursor.execute("SELECT MAX(%s) FROM %s" % (col, tbl))
+        current_max = cursor.fetchone()[0] or 0 # if currently None
+        cursor.execute("SELECT nextval('%s')" % (seq,))
+        current_seq = cursor.fetchone()[0] - 1
+        self.assertEqual(current_max, current_seq,
+            "On %s (col: %s) expected column max (%d) "
+            "and sequence value (%d) to match"
+            % (tbl, col, current_max, current_seq))
+
     def test_null_upgrade(self):
         self._do_upgrade()
 
--- a/bitten/upgrades.py
+++ b/bitten/upgrades.py
@@ -25,7 +25,7 @@
 
 # database abstraction functions
 
-def _parse_scheme(env):
+def parse_scheme(env):
     """Retrieve the environment database scheme."""
     connection_uri = DatabaseManager(env).connection_uri
     parts = connection_uri.split(':', 1)
@@ -36,7 +36,7 @@
     """Update a sequence associated with an autoincrement column."""
     # Hopefully Trac will eventually implement its own version
     # of this function.
-    scheme = _parse_scheme(env)
+    scheme = parse_scheme(env)
     if scheme == "postgres":
         seq = '%s_%s_seq' % (tbl, col)
         cursor = db.cursor()
@@ -47,7 +47,7 @@
     """Drop an index associated with a table."""
     # Hopefully Trac will eventually implement its own version
     # of this function.
-    scheme = _parse_scheme(env)
+    scheme = parse_scheme(env)
     cursor = db.cursor()
     if scheme == "mysql":
         cursor.execute("DROP INDEX %s ON %s" % (idx, tbl))
@@ -593,7 +593,7 @@
 
     if not duplicates_exist:
         cursor = db.cursor()
-        scheme = _parse_scheme(env)
+        scheme = parse_scheme(env)
         if scheme == "mysql":
             # 111 = 333 / len(columns in index) -- this is the Trac default
             cursor.execute("CREATE UNIQUE INDEX bitten_build_config_rev_platform_idx ON bitten_build (config(111), rev(111), platform)")
Copyright (C) 2012-2017 Edgewall Software