Edgewall Software

Opened 11 years ago

Last modified 11 years ago

#11098 closed defect

env.get_version() returns the wrong value if row is missing from system table — at Initial Version

Reported by: ethan.jucovy@… Owned by:
Priority: high Milestone: 1.0.2
Component: general Version: 1.0dev
Severity: normal Keywords:
Cc: Branch:
Release Notes:
API Changes:
Internal Changes:

Description

The docstring for Environment.get_version in trac/env.py states:

Return the current version of the database. If the optional argument initial is set to True, the version of the database used at the time of creation will be returned. In practice, for database created before 0.11, this will return False which is "older" than any db version number.

This is actually not correct. For databases that lack an "initial_database_version" row in their system table, this function appears to return [] (an empty list) which is interpreted as newer than any db version number, at least on my version of cpython 2.6.5:

>>> False > 0
False
>>> [] > 0
True

The function will also return [] (instead of False) when it is called with initial=False against a database whose "database_version" entry has not yet been populated.

(This situation should never occur in normal usage of Trac, but I ran into it today while trying to manually create a new Trac database for an environment that already existed — I ran DatabaseManager(env).init_db() but neglected to then populate the tables with data.)

This then causes unrecoverable (and strange!) errors when running trac-admin commands, because the EnvironmentSetup component's environment_needs_upgrade function will raise the following error:

        elif dbver > db_default.db_version:
            raise TracError(_('Database newer than Trac version'))

The function returns [] because its final line is: return rows and int(rows[0][0]). When there are no matching rows found from the database query, rows will be an empty list. This is considered False for boolean purposes, so the and statement terminates early; evaluates to the value of rows; and returns the empty list rows.

Change History (1)

by ethan.jucovy@…, 11 years ago

Attachment: get_version.patch added
Note: See TracTickets for help on using tickets.