On Mon, Sep 17, 2012 at 10:28 AM, Christopher Nelson <
chris.nelson.1...@gmail.com> wrote:

> So, I need to create my private tables.  I got a pointer to
> IEnvironmentSetupParticipant [1] and the code in Trac that creates the
> database [2].  The former leads me to more detail on creating tables
> [3].  I find a schema [4], but that module doesn't reference the
> schema.  Presumably there's some indirection I'm not following that
> uses a DatabaseManager or something.  What do I need to do in my
> SetupParticipant to use schema to drive table creation?
>

That schema[4] that you found is used during `trac-admin /path initenv`;
once you trace through trac/admin/console.py the relevant code ends up
being http://trac.edgewall.org/browser/trunk/trac/db/api.py#L247 and the
db-backend-specific implementations in e.g.
http://trac.edgewall.org/browser/trunk/trac/db/sqlite_backend.py#L208

For setting up database tables in plugins, I've used TracHoursPlugin as an
example/template/thing-to-cargo-cult-from.  It uses a helper library
(TracSqlHelperScript) that abstracts out a create_table function, but if
you don't feel like making that a dependency of your plugin, it's only a
few lines of code that you can copy over; your code will end up looking
something like:

{{{
from trac.db import Table, Column, Index, DatabaseManager
class MySetupParticipant(Component):
    [...]

    def upgrade_environment(self, db):
        if i_should_not_create_tables(): return

        repo_version_table = Table('repository_version', key=('id'))[
            Column('id', auto_increment=True),
            Column('repo'),
            Column('version'),
            ]

        db_connector, _ = DatabaseManager(self.env)._get_connector()
        stmts = db_connector.to_sql(repo_version_table)
        cursor = db.cursor()
        for stmt in stmts:
            cursor.execute(stmt)
}}}

(Untested and probably includes some stupid typos.)

I'm not aware of any more formal core API for executing CREATE TABLE
statements but would love to be wrong about that.

-Ethan


> [1]
> http://trac.edgewall.org/wiki/TracDev/PluginDevelopment/ExtensionPoints/trac.env.IEnvironmentSetupParticipant
>
> [2] http://trac.edgewall.org/browser/trunk/trac/env.py#L556
>
> [3] http://trac.edgewall.org/browser/trunk/trac/db_default.py
>
> [4] http://trac.edgewall.org/browser/trunk/trac/db_default.py#L36

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

Reply via email to