source: mauRepo/MolesManager/trunk/src/MolesManager/molesSessionMiddleware.py @ 8229

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/src/MolesManager/molesSessionMiddleware.py@8229
Revision 8229, 3.5 KB checked in by mnagni, 9 years ago (diff)

Handles correctly the SQLAlchemy logging

Line 
1'''
2Created on 9 Jan 2012
3
4@author: mnagni
5'''
6from MolesManager.moles3epb import Moles3EPB
7from libs.migration.client import MigrationThread
8from MolesManager.settings import RUN_MIGRATION, MIGRATION_INTERVAL,\
9    MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT, MOLES3_DB_CONNECTION,\
10    MOLES3_DB_SCRIPT, EVENTS_DB, INFO_DB_CONNECTION
11from libs.commons_db import DbManager
12from libs.migration.MigrationEPB import MigrationEPB
13from libs.migration.InfodbEPB import InfodbEPB
14import logging
15
16
17
18class MolesSessionMiddleware(object):
19   
20    #This attribute should be not here.
21    #unfortunately I cannot find any start/stop signals from Django
22    _migrationThread = MigrationThread(interval = MIGRATION_INTERVAL)
23    _epbInitialized = False
24   
25
26    def _initSearchIndexes(self, db_manager):
27        #To Be Done - CHECK IF THE COLUMN ALREADY EXISTS!
28        # We don't want sqlalchemy to know about this column so we add it externally.
29        try:
30            db_manager._engine.execute("alter table md_identifier add column code_search_vector tsvector")                 
31
32            # This indexes the tsvector column
33
34            db_manager._engine.execute("create index md_identifier_code_search_index on md_identifier using gin(code_search_vector)")
35
36            # This sets up the trigger that keeps the tsvector column up to date.
37            db_manager._engine.execute("create trigger md_identifier_code_search_update before update or insert on md_identifier \
38                for each row execute procedure tsvector_update_trigger('code_search_vector', 'pg_catalog.english', 'code')")                       
39        except Exception as e:
40            pass
41
42    def _getNewMolesSession(self):
43        session = Moles3EPB.getNewMolesSession() 
44        EVENTS_DB(session)
45        return session
46   
47    def _doInitialization(self):
48        infoDB = DbManager(INFO_DB_CONNECTION)
49        InfodbEPB.overrrideDBManager(infoDB)
50           
51        migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT)
52        MigrationEPB.overrrideDBManager(migrationDB)       
53
54        molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT)
55        Moles3EPB.overrrideDBManager(molesDB)
56        MolesSessionMiddleware._epbInitialized = True           
57        self._initSearchIndexes(molesDB)
58   
59    def _getMolesSession(self):
60        if not MolesSessionMiddleware._epbInitialized:
61            self._doInitialization()
62
63        return self._getNewMolesSession()
64   
65    def _migration(self, runMigration = RUN_MIGRATION):
66        if runMigration and not MolesSessionMiddleware._migrationThread.isAlive():
67            #t.setDaemon(False)
68            MolesSessionMiddleware._migrationThread.start()
69           
70    """
71        Represents the access to the Moles database.
72        Creates from the existing db connections pools a new session
73        to be used by all the DB operations involved in the actual HTTPRequest
74    """ 
75   
76    def process_request(self, request):       
77        self._migration() #see the note on  MolesSessionMiddleware._migration
78       
79        request.moles_session = self._getMolesSession()
80
81
82    def process_response(self, request, response):
83        if hasattr(request, 'moles_session'):
84            request.moles_session.close()
85
86        return response
87
88
89    def process_exception(self, request, exception):
90        try:
91            session = request.moles_session           
92        except AttributeError:
93            return
94        session.rollback()
95        session.close()       
Note: See TracBrowser for help on using the repository browser.