server/migractions.py
branchtls-sprint
changeset 1263 01152fffd593
parent 1036 593df4919845
parent 1262 abaadb570626
child 1398 5fe84a5f7035
--- a/server/migractions.py	Mon Apr 06 12:37:45 2009 +0200
+++ b/server/migractions.py	Tue Apr 07 09:30:23 2009 +0200
@@ -34,10 +34,47 @@
 try:
     from cubicweb.server import schemaserial as ss
     from cubicweb.server.utils import manager_userpasswd
-    from cubicweb.server.sqlutils import sqlexec
+    from cubicweb.server.sqlutils import sqlexec, SQL_PREFIX
 except ImportError: # LAX
     pass
 
+def set_sql_prefix(prefix):
+    """3.1.5 migration function: allow to unset/reset SQL_PREFIX"""
+    for module in ('checkintegrity', 'migractions', 'schemahooks',
+                   'sources.rql2sql', 'sources.native'):
+        try:
+            sys.modules['cubicweb.server.%s' % module].SQL_PREFIX = prefix
+            print 'changed SQL_PREFIX for %s' % module
+        except KeyError:
+            pass
+        
+def update_database(repo):
+    """3.1.3 migration function: update database schema by adding SQL_PREFIX to
+    entity type tables and columns
+    """
+    pool = repo._get_pool()
+    source = repo.system_source
+    sqlcu = pool['system']
+    for etype in repo.schema.entities():
+        if etype.is_final():
+            continue
+        try:
+            sqlcu.execute('ALTER TABLE %s RENAME TO cw_%s' % (etype, etype))
+            print 'renamed %s table for source %s' % (etype, uri)
+        except:
+            pass
+        for rschema in etype.subject_relations():
+            if rschema == 'has_text':
+                continue
+            if rschema.is_final() or rschema.inlined:
+                sqlcu.execute('ALTER TABLE cw_%s RENAME %s TO cw_%s'
+                              % (etype, rschema, rschema))
+                print 'renamed %s.%s column for source %s' % (
+                    etype, rschema, uri)
+    pool.commit()
+    repo._free_pool(pool)
+
+        
 class ServerMigrationHelper(MigrationHelper):
     """specific migration helper for server side  migration scripts,
     providind actions related to schema/data migration
@@ -62,7 +99,17 @@
 
     @cached
     def repo_connect(self):
-        self.repo = get_repository(method='inmemory', config=self.config)
+        try:
+            self.repo = get_repository(method='inmemory', config=self.config)
+        except:
+            import traceback
+            traceback.print_exc()
+            print '3.1.5 migration'
+            # XXX 3.1.5 migration
+            set_sql_prefix('')
+            self.repo = get_repository(method='inmemory', config=self.config)
+            update_database(self.repo)
+            set_sql_prefix('cw_')
         return self.repo
     
     def shutdown(self):
@@ -106,7 +153,9 @@
                 if answer == 1: # 1: continue, 2: retry
                     break
             else:
+                from cubicweb.toolsutils import restrict_perms_to_user
                 print 'database backup:', backupfile
+                restrict_perms_to_user(backupfile, self.info)
                 break
         
     def restore_database(self, backupfile, drop=True):
@@ -194,7 +243,10 @@
     @cached
     def rqlcursor(self):
         """lazy rql cursor"""
-        return self.cnx.cursor(self.session)    
+        # should not give session as cnx.cursor(), else we may try to execute
+        # some query while no pool is set on the session (eg on entity attribute
+        # access for instance)
+        return self.cnx.cursor()
     
     def commit(self):
         if hasattr(self, '_cnx'):
@@ -1029,7 +1081,8 @@
         and a sql database
         """
         dbhelper = self.repo.system_source.dbhelper
-        tablesql = eschema2sql(dbhelper, self.repo.schema.eschema(etype))
+        tablesql = eschema2sql(dbhelper, self.repo.schema.eschema(etype),
+                               prefix=SQL_PREFIX)
         for sql in tablesql.split(';'):
             if sql.strip():
                 self.sqlexec(sql)