def _drop_table(self, cr, uid, ids, context=None):
for model in self.browse(cr, uid, ids, context):
model_pool = self.pool.get(model.model)
- if getattr(model_pool, '_auto', True) and not model.osv_memory:
- cr.execute("DROP table %s cascade" % model_pool._table)
+ # this test should be removed, but check if drop view instead of drop table
+ # just check if table or view exists
+ cr.execute("select relkind from pg_class where relname=%s", (model_pool._table,))
+ result = cr.fetchone()
+ if result and result[0] == 'v':
+ cr.execute("DROP view %s" % (model_pool._table,))
+ elif result and result[0] == 'r':
+ cr.execute("DROP TABLE %s" % (model_pool._table,))
return True
def unlink(self, cr, user, ids, context=None):
# for model in self.browse(cr, user, ids, context):
# if model.state != 'manual':
# raise except_orm(_('Error'), _("You can not remove the model '%s' !") %(model.name,))
- # self._drop_table(cr, user, ids, context)
+ self._drop_table(cr, user, ids, context)
res = super(ir_model, self).unlink(cr, user, ids, context)
pooler.restart_pool(cr.dbname)
return res
]
def _drop_column(self, cr, uid, ids, context=None):
- for field in self.browse(cr, uid, ids, context):
- model = self.pool.get(field.model)
- if not field.model.osv_memory and getattr(model, '_auto', True):
- cr.execute("ALTER table %s DROP column %s" % (model._table, field.name))
- model._columns.pop(field.name, None)
+ field = self.browse(cr, uid, ids, context)
+ model = self.pool.get(field.model)
+ cr.execute("select relkind from pg_class where relname=%s", (model._table,))
+ result = cr.fetchone()[0]
+ cr.execute("SELECT column_name FROM information_schema.columns WHERE table_name ='%s'and column_name='%s'"%(model._table, field.name))
+ column_name = cr.fetchone()
+ if column_name and result == 'r':
+ cr.execute("ALTER table %s DROP column %s cascade" % (model._table, field.name))
+ model._columns.pop(field.name, None)
return True
def unlink(self, cr, user, ids, context=None):
def __init__(self, pool, cr):
osv.osv.__init__(self, pool, cr)
self.doinit = True
-
# also stored in pool to avoid being discarded along with this osv instance
if getattr(pool, 'model_data_reference_ids', None) is None:
self.pool.model_data_reference_ids = {}
def unlink(self, cr, uid, ids, context=None):
""" Regular unlink method, but make sure to clear the caches. """
- self._pre_process_unlink(cr, uid, ids, context)
self._get_id.clear_cache(self)
self.get_object_reference.clear_cache(self)
return super(ir_model_data,self).unlink(cr, uid, ids, context=context)
model_obj = self.pool.get(model)
if not context:
context = {}
-
# records created during module install should result in res.log entries that are already read!
context = dict(context, res_log_read=True)
-
if xml_id and ('.' in xml_id):
assert len(xml_id.split('.'))==2, _("'%s' contains too many dots. XML ids should not contain dots ! These are used to refer to other modules data, as in module.reference_id") % (xml_id)
module, xml_id = xml_id.split('.')
def _pre_process_unlink(self, cr, uid, ids, context=None):
wkf_todo = []
to_unlink = []
+ to_drop_table = []
+ ids.sort()
+ ids.reverse()
for data in self.browse(cr, uid, ids, context):
model = data.model
res_id = data.res_id
model_obj = self.pool.get(model)
- if str(data.name).startswith('constraint_'):
- cr.execute('ALTER TABLE "%s" DROP CONSTRAINT "%s"' % (model_obj._table,data.name[11:]),)
- _logger.info('Drop CONSTRAINT %s@%s', data.name[11:], model)
+ name = data.name
+ if str(name).startswith('foreign_key_'):
+ name = name[12:]
+ # test if constraint exists
+ cr.execute('select conname from pg_constraint where contype=%s and conname=%s',('f', name),)
+ if cr.fetchall():
+ cr.execute('ALTER TABLE "%s" DROP CONSTRAINT "%s"' % (model,name),)
+ _logger.info('Drop CONSTRAINT %s@%s', name, model)
continue
- to_unlink.append((model,res_id))
+
+ if str(name).startswith('table_'):
+ cr.execute("SELECT table_name FROM information_schema.tables WHERE table_name='%s'"%(name[6:]))
+ column_name = cr.fetchone()
+ if column_name:
+ to_drop_table.append(name[6:])
+ continue
+
+ if str(name).startswith('constraint_'):
+ # test if constraint exists
+ cr.execute('select conname from pg_constraint where contype=%s and conname=%s',('u', name),)
+ if cr.fetchall():
+ cr.execute('ALTER TABLE "%s" DROP CONSTRAINT "%s"' % (model_obj._table,name[11:]),)
+ _logger.info('Drop CONSTRAINT %s@%s', name[11:], model)
+ continue
+
+ to_unlink.append((model, res_id))
if model=='workflow.activity':
cr.execute('select res_type,res_id from wkf_instance where id IN (select inst_id from wkf_workitem where act_id=%s)', (res_id,))
wkf_todo.extend(cr.fetchall())
cr.execute("update wkf_transition set condition='True', group_id=NULL, signal=NULL,act_to=act_from,act_from=%s where act_to=%s", (res_id,res_id))
- cr.execute("delete from wkf_transition where act_to=%s", (res_id,))
-
+
for model,res_id in wkf_todo:
wf_service = netsvc.LocalService("workflow")
- wf_service.trg_write(uid, model, res_id, cr)
+ try:
+ wf_service.trg_write(uid, model, res_id, cr)
+ except:
+ _logger.info('Unable to process workflow %s@%s', res_id, model)
+
+ # drop relation .table
+ for model in to_drop_table:
+ cr.execute('DROP TABLE %s cascade'% (model),)
+ _logger.info('Dropping table %s', model)
+
+ for (model, res_id) in to_unlink:
+ if model in ('ir.model','ir.model.fields', 'ir.model.data'):
+ continue
+ model_ids = self.search(cr, uid, [('model', '=', model),('res_id', '=', res_id)])
+ if len(model_ids) > 1:
+ # if others module have defined this record, we do not delete it
+ continue
+ _logger.info('Deleting %s@%s', res_id, model)
+ try:
+ self.pool.get(model).unlink(cr, uid, res_id)
+ except:
+ _logger.info('Unable to delete %s@%s', res_id, model)
+ cr.commit()
+
+ for (model, res_id) in to_unlink:
+ if model not in ('ir.model.fields',):
+ continue
+ model_ids = self.search(cr, uid, [('model', '=', model),('res_id', '=', res_id)])
+ if len(model_ids) > 1:
+ # if others module have defined this record, we do not delete it
+ continue
+ _logger.info('Deleting %s@%s', res_id, model)
+ self.pool.get(model).unlink(cr, uid, res_id)
- #cr.commit()
- if not config.get('import_partial'):
- for (model, res_id) in to_unlink:
- if self.pool.get(model):
- _logger.info('Deleting %s@%s', res_id, model)
- res_ids = self.pool.get(model).search(cr, uid, [('id', '=', res_id)])
- if res_ids:
- self.pool.get(model).unlink(cr, uid, [res_id])
- cr.commit()
-# except Exception:
-# cr.rollback()
-# _logger.warning(
-# 'Could not delete obsolete record with id: %d of model %s\n'
-## 'There should be some relation that points to this resource\n'
-# 'You should manually fix this and restart with --update=module',
-# res_id, model)
+ for (model, res_id) in to_unlink:
+ if model not in ('ir.model',):
+ continue
+ model_ids = self.search(cr, uid, [('model', '=', model),('res_id', '=', res_id)])
+ if len(model_ids) > 1:
+ # if others module have defined this record, we do not delete it
+ continue
+ _logger.info('Deleting %s@%s', res_id, model)
+ self.pool.get(model).unlink(cr, uid, [res_id])
+ cr.commit()
def _process_end(self, cr, uid, modules):
""" Clear records removed from updated module data.
and a module in ir_model_data and noupdate set to false, but not
present in self.loads.
"""
-
-
if not modules:
return True
modules = list(modules)
_logger.info('Deleting %s@%s', res_id, model)
self.pool.get(model).unlink(cr, uid, [res_id])
- # cr.commit()
-
-
ir_model_data()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
# If new class defines a constraint with
# same function name, we let it override
# the old one.
+
new[c2] = c
exist = True
break
update_custom_fields = context.get('update_custom_fields', False)
self._field_create(cr, context=context)
create = not self._table_exist(cr)
-
if getattr(self, '_auto', True):
if create:
return todo_end
-
def _auto_end(self, cr, context=None):
""" Create the foreign keys recorded by _auto_init. """
for t, k, r, d in self._foreign_keys:
cr.execute('ALTER TABLE "%s" ADD FOREIGN KEY ("%s") REFERENCES "%s" ON DELETE %s' % (t, k, r, d))
+ name_id = "foreign_key_"+t+"_"+k+"_fkey"
+ cr.execute('select * from ir_model_data where name=%s and module=%s', (name_id, self._module))
+ if not cr.rowcount:
+ cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module, model) VALUES (%s, now(), now(), %s, %s)", \
+ (name_id, self._module, t)
+ )
cr.commit()
del self._foreign_keys
def _o2m_raise_on_missing_reference(self, cr, f):
# TODO this check should be a method on fields.one2many.
+
other = self.pool.get(f._obj)
if other:
# TODO the condition could use fields_get_keys().
if f._fields_id not in other._inherit_fields.keys():
raise except_orm('Programming Error', ("There is no reference field '%s' found for '%s'") % (f._fields_id, f._obj,))
-
def _m2m_raise_or_create_relation(self, cr, f):
m2m_tbl, col1, col2 = f._sql_names(self)
cr.execute("SELECT relname FROM pg_class WHERE relkind IN ('r','v') AND relname=%s", (m2m_tbl,))
dest_model = self.pool.get(f._obj)
ref = dest_model._table
cr.execute('CREATE TABLE "%s" ("%s" INTEGER NOT NULL, "%s" INTEGER NOT NULL, UNIQUE("%s","%s")) WITH OIDS' % (m2m_tbl, col1, col2, col1, col2))
-
+ #create many2many references
+ name_id = 'table_'+m2m_tbl
+ cr.execute('select * from ir_model_data where name=%s and module=%s', (name_id, self._module))
+ if not cr.rowcount:
+ cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module, model) VALUES (%s, now(), now(), %s, %s)", \
+ (name_id, self._module, self._name)
+ )
+ # self.pool.get('ir.model.data')._update(cr, 1, self._name, self._module, {}, 'table_'+m2m_tbl, store=True, noupdate=False, mode='init', res_id=False, context=None)
# create foreign key references with ondelete=cascade, unless the targets are SQL views
cr.execute("SELECT relkind FROM pg_class WHERE relkind IN ('v') AND relname=%s", (ref,))
if not cr.fetchall():
cr.execute("SELECT conname, pg_catalog.pg_get_constraintdef(oid, true) as condef FROM pg_constraint where conname=%s", (conname,))
existing_constraints = cr.dictfetchall()
-
sql_actions = {
'drop': {
'execute': False,
_schema.debug(sql_action['msg_ok'])
name_id = 'constraint_'+ conname
cr.execute('select * from ir_model_data where name=%s and module=%s', (name_id, module))
- if not cr.rowcount:
+ if not cr.rowcount:
cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module, model) VALUES (%s, now(), now(), %s, %s)", \
(name_id, module, self._name)
)
-#
except:
_schema.warning(sql_action['msg_err'])
cr.rollback()