MailArchiveExt: fixed AttributeErrors from environment_needs_upgrade() with Trac 1.0
@@ -150,36 +150,17 @@ | ||
150 | 150 | import traceback |
151 | 151 | traceback.print_exc(e) |
152 | 152 | db.rollback() |
153 | - | |
153 | + | |
154 | 154 | def is_no_table(self, db): |
155 | 155 | # check for database table |
156 | - cursor = db.cursor() | |
157 | - try: | |
158 | - cursor.execute("SELECT id FROM mailarc WHERE id='1'") | |
159 | - cursor.execute("SELECT category FROM mailarc_category WHERE category='1'") | |
160 | - except Exception, e: | |
161 | -# import traceback | |
162 | -# traceback.print_exc(e) | |
163 | - return True | |
164 | - return False | |
165 | - | |
156 | + tables = set(self.get_table_names(db)) | |
157 | + return 'mailarc' not in tables or 'mailarc_category' not in tables | |
158 | + | |
166 | 159 | def is_old_schema(self, db): |
167 | - cursor = db.cursor() | |
168 | - try: | |
169 | - cursor.execute("INSERT INTO mailarc (" | |
170 | - "category, messageid) " | |
171 | - "VALUES (%s,%s)", ('testcategory', 'testid')) | |
172 | - cursor.execute("SELECT id from mailarc WHERE messageid = 'testid'") | |
173 | - id = cursor.fetchone()[0] | |
174 | - if id is None: | |
175 | - db.rollback() | |
176 | - return True | |
177 | - except : | |
178 | - db.rollback() | |
179 | - return True | |
180 | - | |
181 | - db.rollback() | |
182 | - return False | |
160 | + columns = set(self.get_column_names(db, 'mailarc')) | |
161 | + return 'id' not in columns or \ | |
162 | + 'category' not in columns or \ | |
163 | + 'messageid' not in columns | |
183 | 164 | |
184 | 165 | def create_table(self, db, table): |
185 | 166 | db_connector, _ = DatabaseManager(self.env)._get_connector() |
@@ -194,3 +175,45 @@ | ||
194 | 175 | cur.execute(sql, params) |
195 | 176 | except Exception, e: |
196 | 177 | pass |
178 | + | |
179 | + @property | |
180 | + def connection_uri(self): | |
181 | + return DatabaseManager(self.env).connection_uri | |
182 | + | |
183 | + def get_table_names(self, db): | |
184 | + cursor = db.cursor() | |
185 | + scheme = self.connection_uri.split(':')[0] | |
186 | + if scheme == 'sqlite': | |
187 | + cursor.execute("SELECT name FROM sqlite_master WHERE type='table'") | |
188 | + return [row[0] for row in cursor] | |
189 | + if scheme == 'postgres': | |
190 | + cursor.execute(""" | |
191 | + SELECT table_name FROM information_schema.tables | |
192 | + WHERE table_schema=%s""", (db.schema,)) | |
193 | + return [row[0] for row in cursor] | |
194 | + if schema == 'mysql': | |
195 | + cursor.execute(""" | |
196 | + SELECT table_name FROM information_schema.tables | |
197 | + WHERE table_schema=%s""", (db.schema,)) | |
198 | + return [row[0] for row in cursor] | |
199 | + raise ValueError('Unknown scheme %s' % scheme) | |
200 | + | |
201 | + def get_column_names(self, db, table): | |
202 | + cursor = db.cursor() | |
203 | + scheme = self.connection_uri.split(':')[0] | |
204 | + if scheme == 'sqlite': | |
205 | + cursor.execute("PRAGMA table_info(%s)" % db.quote(table)) | |
206 | + return [row[1] for row in cursor] | |
207 | + if scheme == 'postgres': | |
208 | + cursor.execute(""" | |
209 | + SELECT column_name FROM information_schema.columns | |
210 | + WHERE table_schema=%s AND table_name=%s | |
211 | + """, (db.schema, table)) | |
212 | + return [row[0] for row in cursor] | |
213 | + if schema == 'mysql': | |
214 | + cursor.execute(""" | |
215 | + SELECT column_name FROM information_schema.columns | |
216 | + WHERE table_schema=%s AND table_name=%s | |
217 | + """, (db.schema, table)) | |
218 | + return [row[0] for row in cursor] | |
219 | + raise ValueError('Unknown scheme %s' % scheme) |
@@ -4,7 +4,7 @@ | ||
4 | 4 | name='TracMailArchive', |
5 | 5 | author='wadahiro', |
6 | 6 | author_email='wadahiro@gmail.com', |
7 | - version='0.4.2', | |
7 | + version='0.4.3', | |
8 | 8 | license = "New BSD", |
9 | 9 | url='http://sourceforge.jp/projects/shibuya-trac/wiki/plugins%2FMailArchiveExtPlugin', |
10 | 10 | packages=find_packages(exclude=['*.tests*']), |