[migration] refactor schema migration fix introduced by 5833:d7256ae7c1d1 stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 01 Jul 2010 08:47:24 +0200
branchstable
changeset 5847 51636c991fb4
parent 5846 0337e6870de4
child 5848 b5640328ffad
[migration] refactor schema migration fix introduced by 5833:d7256ae7c1d1 so the code looks better and avoid clutering lgdb api. Depends on lgdb 1.0.5.
__pkginfo__.py
debian/control
hooks/syncschema.py
server/sources/native.py
--- a/__pkginfo__.py	Thu Jul 01 08:32:27 2010 +0200
+++ b/__pkginfo__.py	Thu Jul 01 08:47:24 2010 +0200
@@ -52,7 +52,7 @@
     'Twisted': '',
     # XXX graphviz
     # server dependencies
-    'logilab-database': '>= 1.0.4',
+    'logilab-database': '>= 1.0.5',
     'pysqlite': '>= 2.5.5', # XXX install pysqlite2
     }
 
--- a/debian/control	Thu Jul 01 08:32:27 2010 +0200
+++ b/debian/control	Thu Jul 01 08:47:24 2010 +0200
@@ -33,7 +33,7 @@
 Conflicts: cubicweb-multisources
 Replaces: cubicweb-multisources
 Provides: cubicweb-multisources
-Depends: ${python:Depends}, cubicweb-common (= ${source:Version}), cubicweb-ctl (= ${source:Version}), python-logilab-database (>= 1.0.4), cubicweb-postgresql-support | cubicweb-mysql-support | python-pysqlite2
+Depends: ${python:Depends}, cubicweb-common (= ${source:Version}), cubicweb-ctl (= ${source:Version}), python-logilab-database (>= 1.0.5), cubicweb-postgresql-support | cubicweb-mysql-support | python-pysqlite2
 Recommends: pyro, cubicweb-documentation (= ${source:Version})
 Description: server part of the CubicWeb framework
  CubicWeb is a semantic web application framework.
--- a/hooks/syncschema.py	Thu Jul 01 08:32:27 2010 +0200
+++ b/hooks/syncschema.py	Thu Jul 01 08:47:24 2010 +0200
@@ -365,7 +365,7 @@
         sysource = session.pool.source('system')
         attrtype = y2sql.type_from_constraints(
             sysource.dbhelper, rdef.object, rdef.constraints)
-        # XXX should be moved somehow into lgc.adbh: sqlite doesn't support to
+        # XXX should be moved somehow into lgdb: sqlite doesn't support to
         # add a new column with UNIQUE, it should be added after the ALTER TABLE
         # using ADD INDEX
         if sysource.dbdriver == 'sqlite' and 'UNIQUE' in attrtype:
@@ -504,23 +504,18 @@
             else:
                 sysource.drop_index(session, table, column)
         if 'cardinality' in self.values and self.rschema.final:
-            adbh = session.pool.source('system').dbhelper
-            if not adbh.alter_column_support:
+            syssource = session.pool.source('system')
+            if not syssource.dbhelper.alter_column_support:
                 # not supported (and NOT NULL not set by yams in that case, so
-                # no worry)
+                # no worry) XXX (syt) then should we set NOT NULL below ??
                 return
             atype = self.rschema.objects(etype)[0]
             constraints = self.rschema.rdef(etype, atype).constraints
-            coltype = y2sql.type_from_constraints(adbh, atype, constraints,
+            coltype = y2sql.type_from_constraints(syssource.dbhelper, atype, constraints,
                                                   creating=False)
             # XXX check self.values['cardinality'][0] actually changed?
-            notnull = self.values['cardinality'][0] != '1'
-            if getattr(adbh, 'alter_table_requires_cursor', False):
-                sql = adbh.sql_set_null_allowed(table, column, coltype, notnull, 
-                                                self.session.system_sql)
-            else:
-                sql = adbh.sql_set_null_allowed(table, column, coltype, notnull)
-            session.system_sql(sql)
+            syssource.set_null_allowed(self.session, table, column, coltype,
+                                       self.values['cardinality'][0] != '1')
         if 'fulltextindexed' in self.values:
             hook.set_operation(session, 'fti_update_etypes', etype,
                                UpdateFTIndexOp)
@@ -549,17 +544,12 @@
         # alter the physical schema on size constraint changes
         if newcstr.type() == 'SizeConstraint' and (
             oldcstr is None or oldcstr.max != newcstr.max):
-            adbh = self.session.pool.source('system').dbhelper
+            syssource = self.session.pool.source('system')
             card = rtype.rdef(subjtype, objtype).cardinality
-            coltype = y2sql.type_from_constraints(adbh, objtype, [newcstr],
-                                                  creating=False)
-            if getattr(adbh, 'alter_table_requires_cursor', False):
-                sql = adbh.sql_change_col_type(table, column, coltype, card[0] != '1',
-                                               self.session.system_sql)
-            else:
-                sql = adbh.sql_change_col_type(table, column, coltype, card[0] != '1')
+            coltype = y2sql.type_from_constraints(syssource.dbhelper, objtype,
+                                                  [newcstr], creating=False)
             try:
-                session.system_sql(sql, rollback_on_failure=False)
+                syssource.change_col_type(session, table, column, coltype, card[0] != '1')
                 self.info('altered column %s of table %s: now %s',
                           column, table, coltype)
             except Exception, ex:
@@ -580,20 +570,13 @@
         column = SQL_PREFIX + str(self.rdef.rtype)
         # alter the physical schema on size/unique constraint changes
         if cstrtype == 'SizeConstraint':
+            syssource = self.session.pool.source('system')
+            coltype = y2sql.type_from_constraints(syssource.dbhelper,
+                                                  self.rdef.object, [],
+                                                  creating=False)
             try:
-                adbh = self.session.pool.source('system').dbhelper
-                coltype = y2sql.type_from_constraints(adbh, self.rdef.object, [],
-                                                      creating=False)
-    
-                if getattr(adbh, 'alter_table_requires_cursor', False):
-                    sql = adbh.sql_change_col_type(table, column, coltype,
-                                                   self.rdef.cardinality[0] != '1',
-                                                   self.session.system_sql)
-                else:
-                    sql = adbh.sql_change_col_type(table, column, coltype,
-                                               self.rdef.cardinality[0] != '1')
-                
-                self.session.system_sql(sql, rollback_on_failure=False)
+                syssource.change_col_type(session, table, column, coltype,
+                                          self.rdef.cardinality[0] != '1')
                 self.info('altered column %s of table %s: now %s',
                           column, table, coltype)
             except Exception, ex:
--- a/server/sources/native.py	Thu Jul 01 08:32:27 2010 +0200
+++ b/server/sources/native.py	Thu Jul 01 08:47:24 2010 +0200
@@ -275,6 +275,8 @@
         if self.dbdriver == 'sqlite':
             self._create_eid = None
             self.create_eid = self._create_eid_sqlite
+        self.binary_to_str = self.dbhelper.dbapi_module.binary_to_str
+
 
     @property
     def _sqlcnx(self):
@@ -672,9 +674,6 @@
 
     # short cut to method requiring advanced db helper usage ##################
 
-    def binary_to_str(self, value):
-        return self.dbhelper.dbapi_module.binary_to_str(value)
-
     def create_index(self, session, table, column, unique=False):
         cursor = LogCursor(session.pool[self.uri])
         self.dbhelper.create_index(cursor, table, column, unique)
@@ -683,6 +682,14 @@
         cursor = LogCursor(session.pool[self.uri])
         self.dbhelper.drop_index(cursor, table, column, unique)
 
+    def change_col_type(self, session, table, column, coltype, null_allowed):
+        cursor = LogCursor(session.pool[self.uri])
+        self.dbhelper.change_col_type(cursor, table, column, coltype, null_allowed)
+
+    def set_null_allowed(self, session, table, column, coltype, null_allowed):
+        cursor = LogCursor(session.pool[self.uri])
+        self.dbhelper.set_null_allowed(cursor, table, column, coltype, null_allowed)
+
     # system source interface #################################################
 
     def eid_type_source(self, session, eid):