id summary reporter owner description type status priority milestone component version severity resolution keywords cc branch changelog apichanges internalchanges 11098 env.get_version() returns the wrong value if row is missing from system table ethan.jucovy@… ethan.jucovy@… "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: {{{ #!python >>> 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: {{{ #!python 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`." defect closed high 1.0.2 general 1.0dev normal fixed Fix `Environment.get_version` regression from 0.12.x, effectively return `False` for databases older than Trac 0.11.