77
88# Database version identifier for upgrades.
99db_version = 3
10+ db_version_key = 'code_comments_schema_version'
1011
1112# Database schema
1213schema = {
3031 Column ('user' ),
3132 Column ('type' ),
3233 Column ('path' ),
33- Column ('repos' ),
3434 Column ('rev' ),
3535 Column ('notify' , type = 'bool' ),
3636 Index (['user' ]),
3939}
4040
4141
42- def to_sql (env , table ):
43- """ Convenience function to get the to_sql for the active connector."""
44- dc = DatabaseManager (env )._get_connector ()[0 ]
45- return dc .to_sql (table )
46-
47-
48- def create_tables (env , db ):
49- cursor = db .cursor ()
50- for table_name in schema :
51- for stmt in to_sql (env , schema [table_name ]):
52- cursor .execute (stmt )
53- cursor .execute (
54- "INSERT INTO system VALUES ('code_comments_schema_version', %s)" ,
55- str (db_version ))
56-
57-
5842# Upgrades
5943
60- def upgrade_from_1_to_2 (env , db ):
61- # Add the new column "type"
62- @env .with_transaction ()
63- def add_type_column (db ):
64- cursor = db .cursor ()
65- cursor .execute ('ALTER TABLE code_comments ADD COLUMN type TEXT' )
66-
67- # Convert all the current comments to the new schema
68- @env .with_transaction ()
69- def convert_comments (db ):
70- comments = {}
71- cursor = db .cursor ()
72- cursor .execute ('SELECT id, path FROM code_comments' )
73- comments = cursor .fetchall ()
44+ def upgrade_from_1_to_2 (env ):
45+ with env .db_transaction as db :
46+ # Add the new column "type"
47+ db ('ALTER TABLE code_comments ADD COLUMN type TEXT' )
48+ # Convert all the current comments to the new schema
7449 # options:
7550 # 1: comment on file (path != "" && path != "attachment")
7651 # 2: comment on changeset (path == "")
7752 # 3: comment on attachment (path == "attachment")
78- for comment in comments :
53+ for comment in db ("""
54+ SELECT id, path FROM code_comments
55+ """ ):
7956 path = comment [1 ]
8057 is_comment_to_attachment = path .startswith ('attachment' )
8158 is_comment_to_file = not is_comment_to_attachment and '' != path
8259 is_comment_to_changeset = '' == path
83- cursor = db .cursor ()
8460 update = 'UPDATE code_comments SET type={0} WHERE id={1}'
8561 sql = ''
8662
@@ -91,16 +67,13 @@ def convert_comments(db):
9167 elif is_comment_to_file :
9268 sql = update .format ("'browser'" , str (comment [0 ]))
9369
94- cursor . execute (sql )
70+ db (sql )
9571
9672
97- def upgrade_from_2_to_3 (env , db ):
73+ def upgrade_from_2_to_3 (env ):
9874 # Add the new table
99- @env .with_transaction ()
100- def add_subscriptions_table (db ):
101- cursor = db .cursor ()
102- for stmt in to_sql (env , schema ['code_comments_subscriptions' ]):
103- cursor .execute (stmt )
75+ dbm = DatabaseManager (env )
76+ dbm .create_tables ((schema ['code_comments_subscriptions' ],))
10477
10578
10679upgrade_map = {
@@ -118,40 +91,25 @@ def environment_created(self):
11891 """Called when a new Trac environment is created."""
11992 pass
12093
121- def environment_needs_upgrade (self , db ):
94+ def environment_needs_upgrade (self ):
12295 """
12396 Called when Trac checks whether the environment needs to be upgraded.
12497 Returns `True` if upgrade is needed, `False` otherwise.
12598 """
126- return self ._get_version (db ) != db_version
99+ dbm = DatabaseManager (self .env )
100+ return dbm .get_database_version (db_version_key ) != db_version
127101
128- def upgrade_environment (self , db ):
102+ def upgrade_environment (self ):
129103 """
130104 Actually perform an environment upgrade, but don't commit as
131105 that is done by the common upgrade procedure when all plugins are done.
132106 """
133- current_ver = self ._get_version (db )
107+ dbm = DatabaseManager (self .env )
108+ current_ver = dbm .get_database_version (db_version_key )
134109 if current_ver == 0 :
135- create_tables (self . env , db )
110+ dbm . create_tables (schema . values () )
136111 else :
137112 while current_ver + 1 <= db_version :
138- upgrade_map [current_ver + 1 ](self .env , db )
113+ upgrade_map [current_ver + 1 ](self .env )
139114 current_ver += 1
140- cursor = db .cursor ()
141- cursor .execute ("""
142- UPDATE system SET value=%s
143- WHERE name='code_comments_schema_version'
144- """ , str (db_version ))
145-
146- def _get_version (self , db ):
147- cursor = db .cursor ()
148- try :
149- cursor .execute ("""
150- SELECT value FROM system
151- WHERE name='code_comments_schema_version'
152- """ )
153- for row in cursor :
154- return int (row [0 ])
155- return 0
156- except :
157- return 0
115+ dbm .set_database_version (db_version , db_version_key )
0 commit comments