

def migrate(cr, version):
    cr.execute('alter table payment_order add column total numeric')
    cr.execute(
        'update payment_order '
        'set total=totals.total '
        'from '
        '(select order_id, sum(amount_currency) total '
        'from payment_line group by order_id) totals '
        'where payment_order.id=totals.order_id')



def migrate(cr, version):
    cr.execute(
        'SELECT count(attname) FROM pg_attribute '
        'WHERE attrelid = '
        '( SELECT oid FROM pg_class WHERE relname = %s ) '
        'AND attname = %s',
        ('payment_order', 'total'))
    if cr.fetchone()[0] == 0:
        cr.execute('alter table payment_order add column total numeric')
    cr.execute(
        'update payment_order '
        'set total=totals.total '
        'from '
        '(select order_id, sum(amount_currency) total '
        'from payment_line group by order_id) totals '
        'where payment_order.id=totals.order_id')

Check if column exists before creating it
[FIX] account_banking_payment_export: Check if column exists before creating it

  
  
  def migrate(cr, version):
+     cr.execute(
+         'SELECT count(attname) FROM pg_attribute '
+         'WHERE attrelid = '
+         '( SELECT oid FROM pg_class WHERE relname = %s ) '
+         'AND attname = %s',
+         ('payment_order', 'total'))
+     if cr.fetchone()[0] == 0:
-     cr.execute('alter table payment_order add column total numeric')
+         cr.execute('alter table payment_order add column total numeric')
      cr.execute(
          'update payment_order '
          'set total=totals.total '
          'from '
          '(select order_id, sum(amount_currency) total '
          'from payment_line group by order_id) totals '
          'where payment_order.id=totals.order_id')
  
Check if column exists before creating it
## Code Before:


def migrate(cr, version):
    cr.execute('alter table payment_order add column total numeric')
    cr.execute(
        'update payment_order '
        'set total=totals.total '
        'from '
        '(select order_id, sum(amount_currency) total '
        'from payment_line group by order_id) totals '
        'where payment_order.id=totals.order_id')

## Instruction:
Check if column exists before creating it
## Code After:


def migrate(cr, version):
    cr.execute(
        'SELECT count(attname) FROM pg_attribute '
        'WHERE attrelid = '
        '( SELECT oid FROM pg_class WHERE relname = %s ) '
        'AND attname = %s',
        ('payment_order', 'total'))
    if cr.fetchone()[0] == 0:
        cr.execute('alter table payment_order add column total numeric')
    cr.execute(
        'update payment_order '
        'set total=totals.total '
        'from '
        '(select order_id, sum(amount_currency) total '
        'from payment_line group by order_id) totals '
        'where payment_order.id=totals.order_id')
