def increment_views(self): """Increment the number of views in the database. We avoid concurrency issues by incrementing JUST the views and not allowing modified_on to be updated automatically. """ if self.id is None: self.views += 1 return self.views # Don't raise an exception should concurrency problems occur. # Views will not actually be incremented in this case, but thats # relatively unimportant compared to rendering the page for the user. # We may be able to remove this after we improve our triggers to not # issue an UPDATE on media_fulltext unless one of its columns are # actually changed. Even when just media.views is updated, all the # columns in the corresponding media_fulltext row are updated, and # media_fulltext's MyISAM engine must lock the whole table to do so. transaction = DBSession.begin_nested() try: DBSession.query(self.__class__)\ .filter(self.__class__.id == self.id)\ .update({self.__class__.views: self.__class__.views + 1}) transaction.commit() except exc.OperationalError, e: transaction.rollback() # (OperationalError) (1205, 'Lock wait timeout exceeded, try restarting the transaction') if not '1205' in e.message: raise
def increment_views(self): """Increment the number of views in the database. We avoid concurrency issues by incrementing JUST the views and not allowing modified_on to be updated automatically. """ if self.id is None: self.views += 1 return self.views # Don't raise an exception should concurrency problems occur. # Views will not actually be incremented in this case, but thats # relatively unimportant compared to rendering the page for the user. # We may be able to remove this after we improve our triggers to not # issue an UPDATE on media_fulltext unless one of its columns are # actually changed. Even when just media.views is updated, all the # columns in the corresponding media_fulltext row are updated, and # media_fulltext's MyISAM engine must lock the whole table to do so. transaction = DBSession.begin_nested() try: DBSession.query(self.__class__)\ .filter(self.__class__.id == self.id)\ .update({self.__class__.views: self.__class__.views + 1}) transaction.commit() except OperationalError, e: transaction.rollback() # (OperationalError) (1205, 'Lock wait timeout exceeded, try restarting the transaction') if not '1205' in e.message: raise
def insert_settings(defaults): """Insert the given setting if they don't exist yet. XXX: Does not include any support for MultiSetting. This approach won't work for that. We'll need to use sqlalchemy-migrate. :type defaults: list :param defaults: Key and value pairs :rtype: list :returns: Any settings that have just been created. """ inserted = [] existing_settings = set(x[0] for x in DBSession.query(Setting.key) \ .filter(Setting.key \ .in_(key for key, value in defaults))) for key, value in defaults: if key in existing_settings: continue transaction = DBSession.begin_nested() try: s = Setting(key, value) DBSession.add(s) transaction.commit() inserted.append(s) except IntegrityError: transaction.rollback() if inserted: DBSession.commit() return inserted
def insert_settings(defaults): """Insert the given setting if they don't exist yet. XXX: Does not include any support for MultiSetting. This approach won't work for that. We'll need to use sqlalchemy-migrate. :type defaults: list :param defaults: Key and value pairs :rtype: list :returns: Any settings that have just been created. """ inserted = [] try: settings_query = DBSession.query(Setting.key)\ .filter(Setting.key.in_([key for key, value in defaults])) existing_settings = set(x[0] for x in settings_query) except ProgrammingError: # If we are running paster setup-app on a fresh database with a # plugin which tries to use this function every time the # Environment.loaded event fires, the settings table will not # exist and this exception will be thrown, but its safe to ignore. # The settings will be created the next time the event fires, # which will likely be the first time the app server starts up. return inserted for key, value in defaults: if key in existing_settings: continue transaction = DBSession.begin_nested() try: s = Setting(key, value) DBSession.add(s) transaction.commit() inserted.append(s) except IntegrityError: transaction.rollback() if inserted: DBSession.commit() return inserted