hooks/syncschema.py
changeset 9463 d62e13eba033
parent 9450 af4b93bc38a5
child 9469 032825bbacab
equal deleted inserted replaced
9462:375fc1868b11 9463:d62e13eba033
    79         session.exception('error while adding column %s to table %s',
    79         session.exception('error while adding column %s to table %s',
    80                           table, column)
    80                           table, column)
    81     # create index before alter table which may expectingly fail during test
    81     # create index before alter table which may expectingly fail during test
    82     # (sqlite) while index creation should never fail (test for index existence
    82     # (sqlite) while index creation should never fail (test for index existence
    83     # is done by the dbhelper)
    83     # is done by the dbhelper)
    84     session.cnxset.source('system').create_index(session, table, column)
    84     session.repo.system_source.create_index(session, table, column)
    85     session.info('added index on %s(%s)', table, column)
    85     session.info('added index on %s(%s)', table, column)
    86 
    86 
    87 
    87 
    88 def insert_rdef_on_subclasses(session, eschema, rschema, rdefdef, props):
    88 def insert_rdef_on_subclasses(session, eschema, rschema, rdefdef, props):
    89     # XXX 'infered': True/False, not clear actually
    89     # XXX 'infered': True/False, not clear actually
   242         schema = session.vreg.schema
   242         schema = session.vreg.schema
   243         etype = ybo.EntityType(eid=entity.eid, name=entity.name,
   243         etype = ybo.EntityType(eid=entity.eid, name=entity.name,
   244                                description=entity.description)
   244                                description=entity.description)
   245         eschema = schema.add_entity_type(etype)
   245         eschema = schema.add_entity_type(etype)
   246         # create the necessary table
   246         # create the necessary table
   247         tablesql = y2sql.eschema2sql(session.cnxset.source('system').dbhelper,
   247         tablesql = y2sql.eschema2sql(session.repo.system_source.dbhelper,
   248                                      eschema, prefix=SQL_PREFIX)
   248                                      eschema, prefix=SQL_PREFIX)
   249         for sql in tablesql.split(';'):
   249         for sql in tablesql.split(';'):
   250             if sql.strip():
   250             if sql.strip():
   251                 session.system_sql(sql)
   251                 session.system_sql(sql)
   252         # add meta relations
   252         # add meta relations
   285 
   285 
   286     def rename(self, oldname, newname):
   286     def rename(self, oldname, newname):
   287         self.session.vreg.schema.rename_entity_type(oldname, newname)
   287         self.session.vreg.schema.rename_entity_type(oldname, newname)
   288         # we need sql to operate physical changes on the system database
   288         # we need sql to operate physical changes on the system database
   289         sqlexec = self.session.system_sql
   289         sqlexec = self.session.system_sql
   290         dbhelper= self.session.cnxset.source('system').dbhelper
   290         dbhelper = self.session.repo.system_source.dbhelper
   291         sql = dbhelper.sql_rename_table(SQL_PREFIX+oldname,
   291         sql = dbhelper.sql_rename_table(SQL_PREFIX+oldname,
   292                                         SQL_PREFIX+newname)
   292                                         SQL_PREFIX+newname)
   293         sqlexec(sql)
   293         sqlexec(sql)
   294         self.info('renamed table %s to %s', oldname, newname)
   294         self.info('renamed table %s to %s', oldname, newname)
   295         sqlexec('UPDATE entities SET type=%(newname)s WHERE type=%(oldname)s',
   295         sqlexec('UPDATE entities SET type=%(newname)s WHERE type=%(oldname)s',
   430                  'fulltextindexed': entity.fulltextindexed,
   430                  'fulltextindexed': entity.fulltextindexed,
   431                  'internationalizable': entity.internationalizable}
   431                  'internationalizable': entity.internationalizable}
   432         # update the in-memory schema first
   432         # update the in-memory schema first
   433         rdefdef = self.init_rdef(**props)
   433         rdefdef = self.init_rdef(**props)
   434         # then make necessary changes to the system source database
   434         # then make necessary changes to the system source database
   435         syssource = session.cnxset.source('system')
   435         syssource = session.repo.system_source
   436         attrtype = y2sql.type_from_constraints(
   436         attrtype = y2sql.type_from_constraints(
   437             syssource.dbhelper, rdefdef.object, rdefdef.constraints)
   437             syssource.dbhelper, rdefdef.object, rdefdef.constraints)
   438         # XXX should be moved somehow into lgdb: sqlite doesn't support to
   438         # XXX should be moved somehow into lgdb: sqlite doesn't support to
   439         # add a new column with UNIQUE, it should be added after the ALTER TABLE
   439         # add a new column with UNIQUE, it should be added after the ALTER TABLE
   440         # using ADD INDEX
   440         # using ADD INDEX
   605         rdef = self.rdef = self.rschema.rdefs[self.rdefkey]
   605         rdef = self.rdef = self.rschema.rdefs[self.rdefkey]
   606         # update the in-memory schema first
   606         # update the in-memory schema first
   607         self.oldvalues = dict( (attr, getattr(rdef, attr)) for attr in self.values)
   607         self.oldvalues = dict( (attr, getattr(rdef, attr)) for attr in self.values)
   608         rdef.update(self.values)
   608         rdef.update(self.values)
   609         # then make necessary changes to the system source database
   609         # then make necessary changes to the system source database
   610         syssource = session.cnxset.source('system')
   610         syssource = session.repo.system_source
   611         if 'indexed' in self.values:
   611         if 'indexed' in self.values:
   612             syssource.update_rdef_indexed(session, rdef)
   612             syssource.update_rdef_indexed(session, rdef)
   613             self.indexed_changed = True
   613             self.indexed_changed = True
   614         if 'cardinality' in self.values and (rdef.rtype.final or
   614         if 'cardinality' in self.values and (rdef.rtype.final or
   615                                              rdef.rtype.inlined) \
   615                                              rdef.rtype.inlined) \
   623         if self.rdef is None:
   623         if self.rdef is None:
   624             return
   624             return
   625         # revert changes on in memory schema
   625         # revert changes on in memory schema
   626         self.rdef.update(self.oldvalues)
   626         self.rdef.update(self.oldvalues)
   627         # revert changes on database
   627         # revert changes on database
   628         syssource = self.session.cnxset.source('system')
   628         syssource = self.session.repo.system_source
   629         if self.indexed_changed:
   629         if self.indexed_changed:
   630             syssource.update_rdef_indexed(self.session, self.rdef)
   630             syssource.update_rdef_indexed(self.session, self.rdef)
   631         if self.null_allowed_changed:
   631         if self.null_allowed_changed:
   632             syssource.update_rdef_null_allowed(self.session, self.rdef)
   632             syssource.update_rdef_null_allowed(self.session, self.rdef)
   633 
   633 
   651         # in-place modification of in-memory schema first
   651         # in-place modification of in-memory schema first
   652         _set_modifiable_constraints(rdef)
   652         _set_modifiable_constraints(rdef)
   653         rdef.constraints.remove(self.oldcstr)
   653         rdef.constraints.remove(self.oldcstr)
   654         # then update database: alter the physical schema on size/unique
   654         # then update database: alter the physical schema on size/unique
   655         # constraint changes
   655         # constraint changes
   656         syssource = session.cnxset.source('system')
   656         syssource = session.repo.system_source
   657         cstrtype = self.oldcstr.type()
   657         cstrtype = self.oldcstr.type()
   658         if cstrtype == 'SizeConstraint':
   658         if cstrtype == 'SizeConstraint':
   659             syssource.update_rdef_column(session, rdef)
   659             syssource.update_rdef_column(session, rdef)
   660             self.size_cstr_changed = True
   660             self.size_cstr_changed = True
   661         elif cstrtype == 'UniqueConstraint':
   661         elif cstrtype == 'UniqueConstraint':
   667         if self.newcstr is not None:
   667         if self.newcstr is not None:
   668             self.rdef.constraints.remove(self.newcstr)
   668             self.rdef.constraints.remove(self.newcstr)
   669         if self.oldcstr is not None:
   669         if self.oldcstr is not None:
   670             self.rdef.constraints.append(self.oldcstr)
   670             self.rdef.constraints.append(self.oldcstr)
   671         # revert changes on database
   671         # revert changes on database
   672         syssource = self.session.cnxset.source('system')
   672         syssource = self.session.repo.system_source
   673         if self.size_cstr_changed:
   673         if self.size_cstr_changed:
   674             syssource.update_rdef_column(self.session, self.rdef)
   674             syssource.update_rdef_column(self.session, self.rdef)
   675         if self.unique_changed:
   675         if self.unique_changed:
   676             syssource.update_rdef_unique(self.session, self.rdef)
   676             syssource.update_rdef_unique(self.session, self.rdef)
   677 
   677 
   698         if oldcstr is not None:
   698         if oldcstr is not None:
   699             rdef.constraints.remove(oldcstr)
   699             rdef.constraints.remove(oldcstr)
   700         rdef.constraints.append(newcstr)
   700         rdef.constraints.append(newcstr)
   701         # then update database: alter the physical schema on size/unique
   701         # then update database: alter the physical schema on size/unique
   702         # constraint changes
   702         # constraint changes
   703         syssource = session.cnxset.source('system')
   703         syssource = session.repo.system_source
   704         if cstrtype == 'SizeConstraint' and (oldcstr is None or
   704         if cstrtype == 'SizeConstraint' and (oldcstr is None or
   705                                              oldcstr.max != newcstr.max):
   705                                              oldcstr.max != newcstr.max):
   706             syssource.update_rdef_column(session, rdef)
   706             syssource.update_rdef_column(session, rdef)
   707             self.size_cstr_changed = True
   707             self.size_cstr_changed = True
   708         elif cstrtype == 'UniqueConstraint' and oldcstr is None:
   708         elif cstrtype == 'UniqueConstraint' and oldcstr is None:
   717         session = self.session
   717         session = self.session
   718         prefix = SQL_PREFIX
   718         prefix = SQL_PREFIX
   719         entity = self.entity
   719         entity = self.entity
   720         table = '%s%s' % (prefix, entity.constraint_of[0].name)
   720         table = '%s%s' % (prefix, entity.constraint_of[0].name)
   721         cols = ['%s%s' % (prefix, r.name) for r in entity.relations]
   721         cols = ['%s%s' % (prefix, r.name) for r in entity.relations]
   722         dbhelper = session.cnxset.source('system').dbhelper
   722         dbhelper = session.repo.system_source.dbhelper
   723         sqls = dbhelper.sqls_create_multicol_unique_index(table, cols, entity.name)
   723         sqls = dbhelper.sqls_create_multicol_unique_index(table, cols, entity.name)
   724         for sql in sqls:
   724         for sql in sqls:
   725             session.system_sql(sql)
   725             session.system_sql(sql)
   726 
   726 
   727     def postcommit_event(self):
   727     def postcommit_event(self):
   737 
   737 
   738     def precommit_event(self):
   738     def precommit_event(self):
   739         session = self.session
   739         session = self.session
   740         prefix = SQL_PREFIX
   740         prefix = SQL_PREFIX
   741         table = '%s%s' % (prefix, self.entity.type)
   741         table = '%s%s' % (prefix, self.entity.type)
   742         dbhelper = session.cnxset.source('system').dbhelper
   742         dbhelper = session.repo.system_source.dbhelper
   743         cols = ['%s%s' % (prefix, c) for c in self.cols]
   743         cols = ['%s%s' % (prefix, c) for c in self.cols]
   744         sqls = dbhelper.sqls_drop_multicol_unique_index(table, cols, self.cstrname)
   744         sqls = dbhelper.sqls_drop_multicol_unique_index(table, cols, self.cstrname)
   745         for sql in sqls:
   745         for sql in sqls:
   746             session.system_sql(sql)
   746             session.system_sql(sql)
   747 
   747