Skip to content

Commit

Permalink
global: Integer type compatibility fix
Browse files Browse the repository at this point in the history
* Changes Integer in models because from SQLAlchemy>=1.0 it's arised a
  exceptions on use LegacyInteger. (closes inveniosoftware#3037)

* PEP8/257 code style improvements.

Signed-off-by: Leonardo Rossi <[email protected]>
  • Loading branch information
Leonardo Rossi committed Apr 22, 2015
1 parent 67cf626 commit 9442900
Show file tree
Hide file tree
Showing 19 changed files with 630 additions and 478 deletions.
49 changes: 27 additions & 22 deletions invenio/ext/logging/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2011, 2012, 2014 CERN.
# Copyright (C) 2011, 2012, 2014, 2015 CERN.
#
# Invenio is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
Expand All @@ -17,20 +17,18 @@
# along with Invenio; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

"""
errorlib database models.
"""
"""errorlib database models."""

# General imports.
from datetime import datetime

from invenio.base.globals import cfg
from invenio.ext.sqlalchemy import db

from sqlalchemy.dialects import mysql


def _is_pow_of_2(n):
"""
Return True if n is a power of 2
"""
"""Return True if n is a power of 2."""
while n > 1:
if n % 2:
return False
Expand All @@ -39,28 +37,35 @@ def _is_pow_of_2(n):


class HstEXCEPTION(db.Model):

"""Represents a HstEXCEPTION record."""

__tablename__ = 'hstEXCEPTION'
id = db.Column(db.Integer(15, unsigned=True), nullable=False,
primary_key=True, autoincrement=True)
id = db.Column(
db.Integer().with_variant(mysql.INTEGER(15, unsigned=True), 'mysql'),
nullable=False, primary_key=True, autoincrement=True)
name = db.Column(db.String(50), nullable=False)
filename = db.Column(db.String(255), nullable=True)
line = db.Column(db.Integer(9), nullable=True)
line = db.Column(
db.Integer().with_variant(mysql.INTEGER(9), 'mysql'),
nullable=True)
last_seen = db.Column(db.DateTime, nullable=False,
server_default='1900-01-01 00:00:00', index=True)
last_notified = db.Column(db.DateTime, nullable=False,
server_default='1900-01-01 00:00:00', index=True)
counter = db.Column(db.Integer(15), nullable=False,
server_default='0')
total = db.Column(db.Integer(15), nullable=False,
server_default='0', index=True)
counter = db.Column(
db.Integer().with_variant(mysql.INTEGER(15), 'mysql'),
nullable=False, server_default='0')
total = db.Column(
db.Integer().with_variant(mysql.INTEGER(15), 'mysql'),
nullable=False, server_default='0', index=True)

__table_args__ = (db.Index('name', name, filename, line, unique=True),
db.Model.__table_args__)

@classmethod
def get_or_create(cls, name, filename, line):
"""Finds or create exception log."""
"""Find or create exception log."""
try:
log = cls.query.filter_by(name=name, filename=filename,
line=line).one()
Expand All @@ -72,7 +77,7 @@ def get_or_create(cls, name, filename, line):
'counter': counter,
'total': log.total + 1}, synchronize_settion=False)
db.session.add(log)
except:
except Exception:
log = HstEXCEPTION(name=name,
filename=filename,
line=line,
Expand All @@ -83,16 +88,18 @@ def get_or_create(cls, name, filename, line):
db.session.add(log)
try:
db.session.commit()
except:
except Exception:
db.session.rollback()
return log

@property
def exception_should_be_notified(self):
"""Exception should be notified."""
return _is_pow_of_2(self.counter)

@property
def pretty_notification_info(self):
"""Pretty notification info."""
return ("This exception has already been seen %s times\n "
"last time it was seen: %s\n "
"last time it was notified: %s\n" % (
Expand All @@ -102,14 +109,12 @@ def pretty_notification_info(self):

@classmethod
def get_pretty_notification_info(cls, name, filename, line):
"""
Return a sentence describing when this exception was already seen.
"""
"""Return a sentence describing when this exception was already seen."""
try:
return cls.query.filter_by(
name=name, filename=filename, line=line
).one().pretty_notification_info
except:
except Exception:
return "It is the first time this exception has been seen.\n"


Expand Down
17 changes: 8 additions & 9 deletions invenio/ext/sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@

"""Initialization and configuration for `flask_sqlalchemy`."""

import sqlalchemy
import sqlalchemy.dialects.postgresql

from flask_registry import ModuleAutoDiscoveryRegistry, RegistryProxy

from flask_sqlalchemy import SQLAlchemy as FlaskSQLAlchemy

import sqlalchemy
import sqlalchemy.dialects.postgresql
from sqlalchemy import event, types
from sqlalchemy.ext.compiler import compiles
from sqlalchemy import event
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.pool import Pool

from sqlalchemy_utils import JSONType

from invenio.ext.sqlalchemy.types import LegacyBigInteger, LegacyInteger, \
from invenio.ext.sqlalchemy.types import LegacyBigInteger, \
LegacyMediumInteger, LegacySmallInteger, LegacyTinyInteger

from .expressions import AsBINARY
Expand Down Expand Up @@ -60,19 +60,18 @@ def _include_sqlalchemy(obj, engine=None):
setattr(obj, 'Char', engine_types.CHAR)
try:
setattr(obj, 'TinyText', engine_types.TINYTEXT)
except:
except Exception:
setattr(obj, 'TinyText', engine_types.TEXT)
setattr(obj, 'hybrid_property', hybrid_property)
try:
setattr(obj, 'Double', engine_types.DOUBLE)
except:
except Exception:
setattr(obj, 'Double', engine_types.FLOAT)
setattr(obj, 'Binary', sqlalchemy.types.LargeBinary)
setattr(obj, 'iBinary', sqlalchemy.types.LargeBinary)
setattr(obj, 'iLargeBinary', sqlalchemy.types.LargeBinary)
setattr(obj, 'iMediumBinary', sqlalchemy.types.LargeBinary)
setattr(obj, 'UUID', GUID)
setattr(obj, 'Integer', LegacyInteger)
setattr(obj, 'MediumInteger', LegacyMediumInteger)
setattr(obj, 'SmallInteger', LegacySmallInteger)
setattr(obj, 'TinyInteger', LegacyTinyInteger)
Expand Down Expand Up @@ -117,7 +116,7 @@ def autocommit_on_checkin(dbapi_con, con_record):
"""Call autocommit on raw mysql connection for fixing bug in MySQL 5.5."""
try:
dbapi_con.autocommit(True)
except:
except Exception:
pass
# FIXME
# from invenio.ext.logging import register_exception
Expand Down
12 changes: 6 additions & 6 deletions invenio/ext/sqlalchemy/types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2014 CERN.
# Copyright (C) 2014, 2015 CERN.
#
# Invenio is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
Expand All @@ -20,15 +20,15 @@
"""Implement various custom column types."""

from .guid import GUID
from .marshal_binary import MarshalBinary
from .pickle_binary import PickleBinary
from .legacyinteger import LegacyInteger
from .legacybiginteger import LegacyBigInteger
from .legacyinteger import LegacyInteger # Don't remove, required!
from .legacymediuminteger import LegacyMediumInteger
from .legacysmallinteger import LegacySmallInteger
from .legacytinyinteger import LegacyTinyInteger
from .legacybiginteger import LegacyBigInteger
from .marshal_binary import MarshalBinary
from .pickle_binary import PickleBinary


__all__ = ('GUID', 'MarshalBinary', 'PickleBinary',
'LegacyInteger', 'LegacyMediumInteger', 'LegacySmallInteger',
'LegacyMediumInteger', 'LegacySmallInteger',
'LegacyTinyInteger', 'LegacyBigInteger')
Loading

0 comments on commit 9442900

Please sign in to comment.