changeset 1360:1f47adeb67c2 beta

Bumped sqlalchemy version to 0.7, replaced timerproxy with new event system for sqlalchemy. TimerProxy is now longer valid profilling method for sqlalchemy
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 04 Jun 2011 20:34:30 +0200
parents 54fc83f2192c
children 87ca17540603
files development.ini production.ini rhodecode/config/deployment.ini_tmpl rhodecode/config/environment.py rhodecode/lib/__init__.py rhodecode/lib/colored_formatter.py rhodecode/lib/timerproxy.py rhodecode/model/db.py setup.py
diffstat 9 files changed, 79 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/development.ini	Sat Jun 04 01:16:39 2011 +0200
+++ b/development.ini	Sat Jun 04 20:34:30 2011 +0200
@@ -142,9 +142,9 @@
 #########################################################
 ### DB CONFIGS - EACH DB WILL HAVE IT'S OWN CONFIG    ###
 #########################################################
-sqlalchemy.db1.url = sqlite:///%(here)s/rhodecode.db
-#sqlalchemy.db1.url = postgresql://postgres:qwe@localhost/rhodecode
-sqlalchemy.db1.echo = True
+#sqlalchemy.db1.url = sqlite:///%(here)s/rhodecode.db
+sqlalchemy.db1.url = postgresql://postgres:qwe@localhost/rhodecode
+sqlalchemy.db1.echo = false
 sqlalchemy.db1.pool_recycle = 3600
 sqlalchemy.convert_unicode = true
 
@@ -205,13 +205,13 @@
 [handler_console]
 class = StreamHandler
 args = (sys.stderr,)
-level = NOTSET
+level = DEBUG
 formatter = color_formatter
 
 [handler_console_sql]
 class = StreamHandler
 args = (sys.stderr,)
-level = NOTSET
+level = DEBUG
 formatter = color_formatter_sql
 
 ################
--- a/production.ini	Sat Jun 04 01:16:39 2011 +0200
+++ b/production.ini	Sat Jun 04 20:34:30 2011 +0200
@@ -144,7 +144,7 @@
 #########################################################
 #sqlalchemy.db1.url = sqlite:///%(here)s/rhodecode.db
 sqlalchemy.db1.url = postgresql://postgres:qwe@localhost/rhodecode
-sqlalchemy.db1.echo = True
+sqlalchemy.db1.echo = false
 sqlalchemy.db1.pool_recycle = 3600
 sqlalchemy.convert_unicode = true
 
--- a/rhodecode/config/deployment.ini_tmpl	Sat Jun 04 01:16:39 2011 +0200
+++ b/rhodecode/config/deployment.ini_tmpl	Sat Jun 04 20:34:30 2011 +0200
@@ -143,7 +143,7 @@
 ### DB CONFIGS - EACH DB WILL HAVE IT'S OWN CONFIG    ###
 #########################################################
 sqlalchemy.db1.url = sqlite:///%(here)s/rhodecode.db
-sqlalchemy.db1.echo = True
+sqlalchemy.db1.echo = false
 sqlalchemy.db1.pool_recycle = 3600
 sqlalchemy.convert_unicode = true
 
--- a/rhodecode/config/environment.py	Sat Jun 04 01:16:39 2011 +0200
+++ b/rhodecode/config/environment.py	Sat Jun 04 20:34:30 2011 +0200
@@ -65,12 +65,7 @@
 
     #MULTIPLE DB configs
     # Setup the SQLAlchemy database engine
-    if config['debug'] and not test:
-        #use query time debugging.
-        sa_engine_db1 = engine_from_config(config, 'sqlalchemy.db1.',
-                                           proxy=TimerProxy())
-    else:
-        sa_engine_db1 = engine_from_config(config, 'sqlalchemy.db1.')
+    sa_engine_db1 = engine_from_config(config, 'sqlalchemy.db1.')
 
     init_model(sa_engine_db1)
 
--- a/rhodecode/lib/__init__.py	Sat Jun 04 01:16:39 2011 +0200
+++ b/rhodecode/lib/__init__.py	Sat Jun 04 20:34:30 2011 +0200
@@ -161,18 +161,60 @@
 def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs):
     """
     Custom engine_from_config functions that makes sure we use NullPool for
-    file based sqlite databases. This prevents errors on sqlite.
+    file based sqlite databases. This prevents errors on sqlite. This only 
+    applies to sqlalchemy versions < 0.7.0
 
     """
+    import sqlalchemy
     from sqlalchemy import engine_from_config as efc
-    from sqlalchemy.pool import NullPool
+    import logging
+
+    if int(sqlalchemy.__version__.split('.')[1]) < 7:
+
+        # This solution should work for sqlalchemy < 0.7.0, and should use
+        # proxy=TimerProxy() for execution time profiling
+
+        from sqlalchemy.pool import NullPool
+        url = configuration[prefix + 'url']
 
-    url = configuration[prefix + 'url']
+        if url.startswith('sqlite'):
+            kwargs.update({'poolclass': NullPool})
+        return efc(configuration, prefix, **kwargs)
+    else:
+        import time
+        from sqlalchemy import event
+        from sqlalchemy.engine import Engine
+
+        log = logging.getLogger('timerproxy')
+        BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38)
+        engine = efc(configuration, prefix, **kwargs)
 
-    if url.startswith('sqlite'):
-        kwargs.update({'poolclass': NullPool})
+        def color_sql(sql):
+            COLOR_SEQ = "\033[1;%dm"
+            COLOR_SQL = YELLOW
+            normal = '\x1b[0m'
+            return ''.join([COLOR_SEQ % COLOR_SQL, sql, normal])
+
+        if configuration['debug']:
+            #attach events only for debug configuration
 
-    return efc(configuration, prefix, **kwargs)
+            def before_cursor_execute(conn, cursor, statement,
+                                    parameters, context, executemany):
+                context._query_start_time = time.time()
+                log.info(color_sql(">>>>> STARTING QUERY >>>>>"))
+
+
+            def after_cursor_execute(conn, cursor, statement,
+                                    parameters, context, executemany):
+                total = time.time() - context._query_start_time
+                log.info(color_sql("<<<<< TOTAL TIME: %f <<<<<" % total))
+
+            event.listen(engine, "before_cursor_execute",
+                         before_cursor_execute)
+            event.listen(engine, "after_cursor_execute",
+                         after_cursor_execute)
+
+    return engine
 
 
 def age(curdate):
--- a/rhodecode/lib/colored_formatter.py	Sat Jun 04 01:16:39 2011 +0200
+++ b/rhodecode/lib/colored_formatter.py	Sat Jun 04 20:34:30 2011 +0200
@@ -62,7 +62,7 @@
         def_record = logging.Formatter.format(self, record)
         end = RESET_SEQ
 
-        colored_record = start + def_record + end
+        colored_record = ''.join([start, def_record, end])
         return colored_record
 
 
@@ -81,5 +81,5 @@
         def_record = format_sql(logging.Formatter.format(self, record))
         end = RESET_SEQ
 
-        colored_record = start + def_record + end
+        colored_record = ''.join([start, def_record, end])
         return colored_record
--- a/rhodecode/lib/timerproxy.py	Sat Jun 04 01:16:39 2011 +0200
+++ b/rhodecode/lib/timerproxy.py	Sat Jun 04 20:34:30 2011 +0200
@@ -10,7 +10,7 @@
     COLOR_SEQ = "\033[1;%dm"
     COLOR_SQL = YELLOW
     normal = '\x1b[0m'
-    return COLOR_SEQ % COLOR_SQL + sql + normal
+    return ''.join([COLOR_SEQ % COLOR_SQL, sql, normal])
 
 
 class TimerProxy(ConnectionProxy):
--- a/rhodecode/model/db.py	Sat Jun 04 01:16:39 2011 +0200
+++ b/rhodecode/model/db.py	Sat Jun 04 20:34:30 2011 +0200
@@ -50,7 +50,7 @@
 
 class RhodeCodeSettings(Base):
     __tablename__ = 'rhodecode_settings'
-    __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True})
+    __table_args__ = (UniqueConstraint('app_settings_name'), {'extend_existing':True})
     app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
     app_settings_name = Column("app_settings_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
     app_settings_value = Column("app_settings_value", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
@@ -99,7 +99,7 @@
 
 class RhodeCodeUi(Base):
     __tablename__ = 'rhodecode_ui'
-    __table_args__ = {'useexisting':True}
+    __table_args__ = {'extend_existing':True}
     ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
     ui_section = Column("ui_section", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
     ui_key = Column("ui_key", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
@@ -114,7 +114,7 @@
 
 class User(Base):
     __tablename__ = 'users'
-    __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True})
+    __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'extend_existing':True})
     user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
     username = Column("username", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
     password = Column("password", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
@@ -173,10 +173,10 @@
 
 class UserLog(Base):
     __tablename__ = 'user_logs'
-    __table_args__ = {'useexisting':True}
+    __table_args__ = {'extend_existing':True}
     user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
     user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
-    repository_id = Column("repository_id", Integer(length=None, convert_unicode=False, assert_unicode=None), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
+    repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
     repository_name = Column("repository_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
     user_ip = Column("user_ip", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
     action = Column("action", UnicodeText(length=1200000, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
@@ -192,7 +192,7 @@
 
 class UsersGroup(Base):
     __tablename__ = 'users_groups'
-    __table_args__ = {'useexisting':True}
+    __table_args__ = {'extend_existing':True}
 
     users_group_id = Column("users_group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
     users_group_name = Column("users_group_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
@@ -216,7 +216,7 @@
 
 class UsersGroupMember(Base):
     __tablename__ = 'users_groups_members'
-    __table_args__ = {'useexisting':True}
+    __table_args__ = {'extend_existing':True}
 
     users_group_member_id = Column("users_group_member_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
     users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
@@ -231,7 +231,7 @@
 
 class Repository(Base):
     __tablename__ = 'repositories'
-    __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},)
+    __table_args__ = (UniqueConstraint('repo_name'), {'extend_existing':True},)
     __mapper_args__ = {'extension':RepositoryMapper()}
 
     repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
@@ -302,7 +302,7 @@
 class Group(Base):
     __tablename__ = 'groups'
     __table_args__ = (UniqueConstraint('group_name', 'group_parent_id'),
-                      CheckConstraint('group_id != group_parent_id'), {'useexisting':True},)
+                      CheckConstraint('group_id != group_parent_id'), {'extend_existing':True},)
     __mapper_args__ = {'order_by':'group_name'}
 
     group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
@@ -361,7 +361,7 @@
 
 class Permission(Base):
     __tablename__ = 'permissions'
-    __table_args__ = {'useexisting':True}
+    __table_args__ = {'extend_existing':True}
     permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
     permission_name = Column("permission_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
     permission_longname = Column("permission_longname", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
@@ -376,7 +376,7 @@
 
 class RepoToPerm(Base):
     __tablename__ = 'repo_to_perm'
-    __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True})
+    __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'extend_existing':True})
     repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
     user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
     permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
@@ -388,7 +388,7 @@
 
 class UserToPerm(Base):
     __tablename__ = 'user_to_perm'
-    __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True})
+    __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'extend_existing':True})
     user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
     user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
     permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
@@ -433,7 +433,7 @@
 
 class UsersGroupRepoToPerm(Base):
     __tablename__ = 'users_group_repo_to_perm'
-    __table_args__ = (UniqueConstraint('users_group_id', 'permission_id'), {'useexisting':True})
+    __table_args__ = (UniqueConstraint('users_group_id', 'permission_id'), {'extend_existing':True})
     users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
     users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
     permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
@@ -494,7 +494,7 @@
 
 class GroupToPerm(Base):
     __tablename__ = 'group_to_perm'
-    __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'useexisting':True})
+    __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'extend_existing':True})
 
     group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
     user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
@@ -507,7 +507,7 @@
 
 class Statistics(Base):
     __tablename__ = 'statistics'
-    __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True})
+    __table_args__ = (UniqueConstraint('repository_id'), {'extend_existing':True})
     stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
     repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None)
     stat_on_revision = Column("stat_on_revision", Integer(), nullable=False)
@@ -521,7 +521,7 @@
     __tablename__ = 'user_followings'
     __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'),
                       UniqueConstraint('user_id', 'follows_user_id')
-                      , {'useexisting':True})
+                      , {'extend_existing':True})
 
     user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
     user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
@@ -542,7 +542,7 @@
 
 class CacheInvalidation(Base):
     __tablename__ = 'cache_invalidation'
-    __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True})
+    __table_args__ = (UniqueConstraint('cache_key'), {'extend_existing':True})
     cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
     cache_key = Column("cache_key", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
     cache_args = Column("cache_args", String(length=255, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
@@ -560,7 +560,7 @@
 
 class DbMigrateVersion(Base):
     __tablename__ = 'db_migrate_version'
-    __table_args__ = {'useexisting':True}
+    __table_args__ = {'extend_existing':True}
     repository_id = Column('repository_id', String(250), primary_key=True)
     repository_path = Column('repository_path', Text)
     version = Column('version', Integer)
--- a/setup.py	Sat Jun 04 01:16:39 2011 +0200
+++ b/setup.py	Sat Jun 04 20:34:30 2011 +0200
@@ -12,12 +12,12 @@
 requirements = [
         "Pylons==1.0.0",
         "WebHelpers>=1.2",
-        "SQLAlchemy>=0.6.6",
+        "SQLAlchemy>=0.7.0",
         "Mako>=0.4.0",
         "vcs>=0.2.1",
         "pygments>=1.4",
         "mercurial>=1.8.1",
-        "whoosh>=1.8.0",
+        "whoosh>=1.8.4",
         "celery>=2.2.5",
         "babel",
         "python-dateutil>=1.5.0,<2.0.0",