| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | """ |
|---|
| 3 | DirectoryAuthPlugin :: database management part. |
|---|
| 4 | |
|---|
| 5 | License: BSD |
|---|
| 6 | |
|---|
| 7 | (c) 2012 branson matheson branson-dot-matheson-at-nasa-dot-gov |
|---|
| 8 | """ |
|---|
| 9 | |
|---|
| 10 | from trac.core import Component, implements |
|---|
| 11 | from trac.db.schema import Table, Column, Index |
|---|
| 12 | from trac.env import IEnvironmentSetupParticipant |
|---|
| 13 | |
|---|
| 14 | __all__ = ['DirectoryAuthPluginSetup'] |
|---|
| 15 | |
|---|
| 16 | # Database version identifier for upgrades. |
|---|
| 17 | db_version = 1 |
|---|
| 18 | |
|---|
| 19 | # Database schema |
|---|
| 20 | schema = [ |
|---|
| 21 | # Blog posts |
|---|
| 22 | Table('dir_cache', key='id')[ |
|---|
| 23 | Column('id', type='varchar(32)'), |
|---|
| 24 | Column('lut', type='int'), |
|---|
| 25 | Column('data', type='blob'), |
|---|
| 26 | Index(['id'])], |
|---|
| 27 | ] |
|---|
| 28 | |
|---|
| 29 | schemaPostgres = [ |
|---|
| 30 | # Blog posts |
|---|
| 31 | Table('dir_cache', key='id')[ |
|---|
| 32 | Column('id', type='varchar(32)'), |
|---|
| 33 | Column('lut', type='int'), |
|---|
| 34 | Column('data', type='bytea'), |
|---|
| 35 | Index(['id'])], |
|---|
| 36 | ] |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | upgrade_map = {} |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | def to_sql(env, table): |
|---|
| 43 | """ Convenience function to get the to_sql for the active connector.""" |
|---|
| 44 | from trac.db.api import DatabaseManager |
|---|
| 45 | dc = DatabaseManager(env)._get_connector()[0] |
|---|
| 46 | return dc.to_sql(table) |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | def create_tables(env, db): |
|---|
| 50 | """ Creates the basic tables as defined by schema. |
|---|
| 51 | using the active database connector. """ |
|---|
| 52 | cursor = db.cursor() |
|---|
| 53 | usedSchema = schema |
|---|
| 54 | if env.config.get('trac', 'database').startswith('postgres'): |
|---|
| 55 | usedSchema = schemaPostgres |
|---|
| 56 | for table in usedSchema: |
|---|
| 57 | for stmt in to_sql(env, table): |
|---|
| 58 | cursor.execute(stmt) |
|---|
| 59 | cursor.execute("INSERT into system values ('dirauthplugin_version', %s)", |
|---|
| 60 | (db_version,)) |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | class DirectoryAuthPluginSetup(Component): |
|---|
| 64 | |
|---|
| 65 | implements(IEnvironmentSetupParticipant) |
|---|
| 66 | |
|---|
| 67 | def environment_created(self): |
|---|
| 68 | pass |
|---|
| 69 | |
|---|
| 70 | def environment_needs_upgrade(self, db): |
|---|
| 71 | return self._get_version(db) != db_version |
|---|
| 72 | |
|---|
| 73 | def upgrade_environment(self, db): |
|---|
| 74 | current_ver = self._get_version(db) |
|---|
| 75 | if current_ver == 0: |
|---|
| 76 | create_tables(self.env, db) |
|---|
| 77 | else: |
|---|
| 78 | while current_ver + 1 <= db_version: |
|---|
| 79 | upgrade_map[current_ver + 1](self.env, db) |
|---|
| 80 | current_ver += 1 |
|---|
| 81 | cursor = db.cursor() |
|---|
| 82 | cursor.execute(""" |
|---|
| 83 | UPDATE system SET value=%s WHERE name='dirauthplugin_version' |
|---|
| 84 | """, db_version) |
|---|
| 85 | |
|---|
| 86 | def _get_version(self, db): |
|---|
| 87 | cursor = db.cursor() |
|---|
| 88 | try: |
|---|
| 89 | cursor.execute(""" |
|---|
| 90 | SELECT value FROM system WHERE name='dirauthplugin_version' |
|---|
| 91 | """) |
|---|
| 92 | for row in cursor: |
|---|
| 93 | return int(row[0]) |
|---|
| 94 | return 0 |
|---|
| 95 | except: |
|---|
| 96 | return 0 |
|---|