Hello All,

  I'm trying to get my head around how roll backs are performed with the
0.13 DB API.  Here is the platform (windows xp x64)

Trac0.13dev-r10865Docutils0.8Genshi0.7dev-r1177Pygments1.4pysqlite2.6.0
Python2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]pytz
2010osetuptools0.6SQLite3.6.21jQuery:1.5.1

  The ContactsPlugin does not work out of the box.  I wanted to do
something similar so I started to look at it and I modified some of the
code until it worked.  I was confused because different errors were coming
up each time.  The plugin had a try/catch block, and a db.rollback() was
performed inside the exception.  However, it seems that the db.rollback()
only rolled back the last cursor.execute() statement.

   In reading http://trac.edgewall.org/wiki/TracDev/DatabaseApi, under the
0.13 API it seems that if an exception is raised in the with block, then
the transactions in the with block will be rolled back.  That doesn't seem
to be the case.  Here is the upgrade method.

    def do_db_upgrade(self):
        with self.env.db_transaction as db:
            cursor = db.cursor()

            if self.db_installed_version < 1: # previously set in the
__init__ method of the component
                contacts_table = Table('contact', key=('id',))[
                        Column('id', type='int', auto_increment=True),
                        Column('first'),
                        Column('last'),
                        Column('position'),
                        Column('email'),
                        Column('phone'),
                        Index(['id'],unique=True)
                        ]
                print 'Adding Contacts Table'
                for stmt in to_sql(self.env, contacts_table):
                    print "Executing %s" % stmt
                    cursor.execute(stmt)
                print "Updating System Table"
                print "Executing INSERT into system values
('contacts_version', %s)"%(str(self.db_version),)
                cursor.execute("INSERT into system values
('contacts_version', %s)",str(self.db_version))
                raise Exception("Intentional Exception")

So this is the output I get when I run trac-admin upgrade on the
environment:

No installed contacts table.
Contacts needs an upgrade
 * Upgrading db
Adding Contacts Table
Executing CREATE TABLE contact (
    id integer PRIMARY KEY,
    first text,
    last text,
    position text,
    email text,
    phone text
);
Executing CREATE UNIQUE INDEX contact_id_idx ON contact (id);
Updating System Table
Executing INSERT into system values ('contacts_version', 1)
Exception: Intentional Exception

And when I run trac-admin upgrade directly subsequent on the same
environment.

No installed contacts table.
Contacts needs an upgrade
 * Upgrading db
Adding Contacts Table
Executing CREATE TABLE contact (
    id integer PRIMARY KEY,
    first text,
    last text,
    position text,
    email text,
    phone text
);
OperationalError: table contact already exists


So the system table was the only execute statement to be rolled back in the
with block.  Otherwise, we wouldn't be in this method.  So is that
intentional?  I would think I would get the same output on each execution.
 Do you have to pass all of your SQL statements into a single cursor
execution?  I'm sorry if that is a newb question.  I've tried to get a feel
by reading the sqllite documentation, but I guess  it's over me.  Thank you
very much for any input.

-Nelson

-- 
You received this message because you are subscribed to the Google Groups "Trac 
Development" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/trac-dev?hl=en.

Reply via email to