changeset 565:78109beea395

Type fixes for Postgres 8.x (we noticed issues on 8.3. Newer versions of Postgres don't auto massage integers from Python to string values in the database. In Bitten, this was causing issues with the 'id' field of bitten_rule, which was actually an integer (coming from the id field of the bitten_platform table.) Without explicitly converting it to a string, you'd see the error reported in #390. Added upgrade steps, tested on Postgres8.3. Closes #390. We may want to rename this field to 'platform' later.
author wbell
date Sat, 16 May 2009 22:39:21 +0000
parents 2145ec6680fd
children e198a8df81e9
files bitten/model.py bitten/upgrades.py
diffstat 2 files changed, 20 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/bitten/model.py
+++ b/bitten/model.py
@@ -188,7 +188,7 @@
             Column('id', auto_increment=True), Column('config'), Column('name')
         ],
         Table('bitten_rule', key=('id', 'propname'))[
-            Column('id'), Column('propname'), Column('pattern'),
+            Column('id', type='int'), Column('propname'), Column('pattern'),
             Column('orderno', type='int')
         ]
     ]
@@ -979,4 +979,4 @@
 
 schema = BuildConfig._schema + TargetPlatform._schema + Build._schema + \
          BuildStep._schema + BuildLog._schema + Report._schema
-schema_version = 8
+schema_version = 9
--- a/bitten/upgrades.py
+++ b/bitten/upgrades.py
@@ -337,6 +337,23 @@
         "When you have confirmed that the migration worked correctly, "
         "you can drop the bitten_log_message table in the database (it remains as a backup)", logs_dir)
 
+def recreate_rule_with_int_id(env, db):
+        """Recreates the bitten_rule table with an integer id column rather than a text one."""
+        from trac.db import Table, Column
+        from bitten.model import TargetPlatform
+        cursor = db.cursor()
+        connector, _ = DatabaseManager(env)._get_connector()
+
+        for table in TargetPlatform._schema:
+            if table.name is 'bitten_rule':
+                env.log.info("Migrating bitten_rule table to integer ids")
+                cursor.execute("CREATE TEMPORARY TABLE old_rule AS SELECT * FROM bitten_rule")
+                cursor.execute("DROP TABLE bitten_rule")
+                for stmt in connector.to_sql(table):
+                    cursor.execute(stmt)
+                cursor.execute("INSERT INTO bitten_rule (id,propname,pattern,orderno) SELECT %s,propname,pattern,orderno FROM old_rule" % db.cast('id', 'int'))
+
+
 map = {
     2: [add_log_table],
     3: [add_recipe_to_config],
@@ -345,4 +362,5 @@
     6: [normalize_file_paths, fixup_generators],
     7: [add_error_table],
     8: [add_filename_to_logs,migrate_logs_to_files],
+    9: [recreate_rule_with_int_id],
 }
Copyright (C) 2012-2017 Edgewall Software