hooks/syncschema.py
branchstable
changeset 5847 51636c991fb4
parent 5846 0337e6870de4
child 5849 9db65b381028
equal deleted inserted replaced
5846:0337e6870de4 5847:51636c991fb4
   363                  'internationalizable': entity.internationalizable}
   363                  'internationalizable': entity.internationalizable}
   364         rdef = self.init_rdef(**props)
   364         rdef = self.init_rdef(**props)
   365         sysource = session.pool.source('system')
   365         sysource = session.pool.source('system')
   366         attrtype = y2sql.type_from_constraints(
   366         attrtype = y2sql.type_from_constraints(
   367             sysource.dbhelper, rdef.object, rdef.constraints)
   367             sysource.dbhelper, rdef.object, rdef.constraints)
   368         # XXX should be moved somehow into lgc.adbh: sqlite doesn't support to
   368         # XXX should be moved somehow into lgdb: sqlite doesn't support to
   369         # add a new column with UNIQUE, it should be added after the ALTER TABLE
   369         # add a new column with UNIQUE, it should be added after the ALTER TABLE
   370         # using ADD INDEX
   370         # using ADD INDEX
   371         if sysource.dbdriver == 'sqlite' and 'UNIQUE' in attrtype:
   371         if sysource.dbdriver == 'sqlite' and 'UNIQUE' in attrtype:
   372             extra_unique_index = True
   372             extra_unique_index = True
   373             attrtype = attrtype.replace(' UNIQUE', '')
   373             attrtype = attrtype.replace(' UNIQUE', '')
   502             if self.values['indexed']:
   502             if self.values['indexed']:
   503                 sysource.create_index(session, table, column)
   503                 sysource.create_index(session, table, column)
   504             else:
   504             else:
   505                 sysource.drop_index(session, table, column)
   505                 sysource.drop_index(session, table, column)
   506         if 'cardinality' in self.values and self.rschema.final:
   506         if 'cardinality' in self.values and self.rschema.final:
   507             adbh = session.pool.source('system').dbhelper
   507             syssource = session.pool.source('system')
   508             if not adbh.alter_column_support:
   508             if not syssource.dbhelper.alter_column_support:
   509                 # not supported (and NOT NULL not set by yams in that case, so
   509                 # not supported (and NOT NULL not set by yams in that case, so
   510                 # no worry)
   510                 # no worry) XXX (syt) then should we set NOT NULL below ??
   511                 return
   511                 return
   512             atype = self.rschema.objects(etype)[0]
   512             atype = self.rschema.objects(etype)[0]
   513             constraints = self.rschema.rdef(etype, atype).constraints
   513             constraints = self.rschema.rdef(etype, atype).constraints
   514             coltype = y2sql.type_from_constraints(adbh, atype, constraints,
   514             coltype = y2sql.type_from_constraints(syssource.dbhelper, atype, constraints,
   515                                                   creating=False)
   515                                                   creating=False)
   516             # XXX check self.values['cardinality'][0] actually changed?
   516             # XXX check self.values['cardinality'][0] actually changed?
   517             notnull = self.values['cardinality'][0] != '1'
   517             syssource.set_null_allowed(self.session, table, column, coltype,
   518             if getattr(adbh, 'alter_table_requires_cursor', False):
   518                                        self.values['cardinality'][0] != '1')
   519                 sql = adbh.sql_set_null_allowed(table, column, coltype, notnull, 
       
   520                                                 self.session.system_sql)
       
   521             else:
       
   522                 sql = adbh.sql_set_null_allowed(table, column, coltype, notnull)
       
   523             session.system_sql(sql)
       
   524         if 'fulltextindexed' in self.values:
   519         if 'fulltextindexed' in self.values:
   525             hook.set_operation(session, 'fti_update_etypes', etype,
   520             hook.set_operation(session, 'fti_update_etypes', etype,
   526                                UpdateFTIndexOp)
   521                                UpdateFTIndexOp)
   527 
   522 
   528 
   523 
   547         table = SQL_PREFIX + str(subjtype)
   542         table = SQL_PREFIX + str(subjtype)
   548         column = SQL_PREFIX + str(rtype)
   543         column = SQL_PREFIX + str(rtype)
   549         # alter the physical schema on size constraint changes
   544         # alter the physical schema on size constraint changes
   550         if newcstr.type() == 'SizeConstraint' and (
   545         if newcstr.type() == 'SizeConstraint' and (
   551             oldcstr is None or oldcstr.max != newcstr.max):
   546             oldcstr is None or oldcstr.max != newcstr.max):
   552             adbh = self.session.pool.source('system').dbhelper
   547             syssource = self.session.pool.source('system')
   553             card = rtype.rdef(subjtype, objtype).cardinality
   548             card = rtype.rdef(subjtype, objtype).cardinality
   554             coltype = y2sql.type_from_constraints(adbh, objtype, [newcstr],
   549             coltype = y2sql.type_from_constraints(syssource.dbhelper, objtype,
   555                                                   creating=False)
   550                                                   [newcstr], creating=False)
   556             if getattr(adbh, 'alter_table_requires_cursor', False):
       
   557                 sql = adbh.sql_change_col_type(table, column, coltype, card[0] != '1',
       
   558                                                self.session.system_sql)
       
   559             else:
       
   560                 sql = adbh.sql_change_col_type(table, column, coltype, card[0] != '1')
       
   561             try:
   551             try:
   562                 session.system_sql(sql, rollback_on_failure=False)
   552                 syssource.change_col_type(session, table, column, coltype, card[0] != '1')
   563                 self.info('altered column %s of table %s: now %s',
   553                 self.info('altered column %s of table %s: now %s',
   564                           column, table, coltype)
   554                           column, table, coltype)
   565             except Exception, ex:
   555             except Exception, ex:
   566                 # not supported by sqlite for instance
   556                 # not supported by sqlite for instance
   567                 self.error('error while altering table %s: %s', table, ex)
   557                 self.error('error while altering table %s: %s', table, ex)
   578         cstrtype = self.cstr.type()
   568         cstrtype = self.cstr.type()
   579         table = SQL_PREFIX + str(self.rdef.subject)
   569         table = SQL_PREFIX + str(self.rdef.subject)
   580         column = SQL_PREFIX + str(self.rdef.rtype)
   570         column = SQL_PREFIX + str(self.rdef.rtype)
   581         # alter the physical schema on size/unique constraint changes
   571         # alter the physical schema on size/unique constraint changes
   582         if cstrtype == 'SizeConstraint':
   572         if cstrtype == 'SizeConstraint':
       
   573             syssource = self.session.pool.source('system')
       
   574             coltype = y2sql.type_from_constraints(syssource.dbhelper,
       
   575                                                   self.rdef.object, [],
       
   576                                                   creating=False)
   583             try:
   577             try:
   584                 adbh = self.session.pool.source('system').dbhelper
   578                 syssource.change_col_type(session, table, column, coltype,
   585                 coltype = y2sql.type_from_constraints(adbh, self.rdef.object, [],
   579                                           self.rdef.cardinality[0] != '1')
   586                                                       creating=False)
       
   587     
       
   588                 if getattr(adbh, 'alter_table_requires_cursor', False):
       
   589                     sql = adbh.sql_change_col_type(table, column, coltype,
       
   590                                                    self.rdef.cardinality[0] != '1',
       
   591                                                    self.session.system_sql)
       
   592                 else:
       
   593                     sql = adbh.sql_change_col_type(table, column, coltype,
       
   594                                                self.rdef.cardinality[0] != '1')
       
   595                 
       
   596                 self.session.system_sql(sql, rollback_on_failure=False)
       
   597                 self.info('altered column %s of table %s: now %s',
   580                 self.info('altered column %s of table %s: now %s',
   598                           column, table, coltype)
   581                           column, table, coltype)
   599             except Exception, ex:
   582             except Exception, ex:
   600                 # not supported by sqlite for instance
   583                 # not supported by sqlite for instance
   601                 self.error('error while altering table %s: %s', table, ex)
   584                 self.error('error while altering table %s: %s', table, ex)