← Back to team overview

savoirfairelinux-openerp team mailing list archive

Re: lp:~savoirfairelinux-openerp/partner-contact-management/partner_isp into lp:partner-contact-management

 

Review: Disapprove

Yes, changing types that don't convert to each other (like in the birthdate case) make you lose your data on every update.

I would love to see a module that does *just* that (change birthdate's type), but this involves some trickery for the first installation and the update process. I once solved this problem this way:

    def _auto_init(self, cr, context=None):
        cr.execute("""
            SELECT typname FROM pg_class c,pg_attribute a, pg_type t
            WHERE c.relname=%s AND a.attname = %s
            AND c.oid=a.attrelid AND a.atttypid=t.oid
            AND c.oid=a.attrelid""", (self._table, 'birthdate'))
        type_before = cr.fetchone()[0]
        result = super(res_partner, self)._auto_init(cr, context=context)
        if type_before == 'varchar':
            self._copy_from_latest_of_type(cr, 'birthdate', 'date')
        return result

    def _copy_from_latest_of_type(self, cr, name, typename, drop_moved=True):
        values = {
                'table': self._table,
                'name': name,
                'name_moved': name + '_moved',
                'name_moved_like': name + '_moved%',
                'typename': typename
                }
        cr.execute("""
            SELECT attname
            FROM pg_class c,pg_attribute a, pg_type t
            WHERE c.relname=%(table)s AND a.attname like %(name_moved_like)s
            AND c.oid=a.attrelid AND a.atttypid=t.oid
            AND t.typname=%(typename)s
            order by
                substring(attname from length(%(name_moved)s) + 1)::int
                desc
            """, values)
        row = cr.fetchall()[0]
        if row:
            cr.execute('update %(table)s set %(name)s=%(name_moved)s' % dict(
                values, name_moved=row[0]))
        else:
            return
        if drop_moved:
            cr.execute("""
                SELECT attname
                FROM pg_class c,pg_attribute a, pg_type t
                WHERE c.relname=%(table)s
                AND a.attname like %(name_moved_like)s
                AND c.oid=a.attrelid AND a.atttypid=t.oid""", values)
            columns = [row[0] for row in cr.fetchall()]
            cr.execute(('alter table %(table)s ' + ','.join(
                ['drop column ' + column for column in columns])) % values)
            cr.commit()


Further, I also think this module binds together pretty unrelated properties and shouldn't be included as it is.
-- 
https://code.launchpad.net/~savoirfairelinux-openerp/partner-contact-management/partner_isp/+merge/185095
Your team Savoir-faire Linux' OpenERP is subscribed to branch lp:~savoirfairelinux-openerp/partner-contact-management/partner_isp.


References