You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import logging
  5. from psycopg2.extensions import AsIs
  6. _logger = logging.getLogger(__name__)
  7. def column_exists(cr, table, column):
  8. cr.execute("""
  9. SELECT column_name
  10. FROM information_schema.columns
  11. WHERE table_name = %s AND column_name = %s""", (table, column))
  12. return bool(cr.fetchall())
  13. def column_add_with_value(cr, table, column, field_type, value):
  14. if not column_exists(cr, table, column):
  15. cr.execute("""
  16. ALTER TABLE %s
  17. ADD COLUMN %s %s""", (AsIs(table), AsIs(column), AsIs(field_type)))
  18. cr.execute("""
  19. UPDATE %s SET %s = %s""", (AsIs(table), AsIs(column), value))
  20. def pre_init_hook(cr):
  21. _logger.info("Creating res.partner.tracking_emails_count column "
  22. "with value 0")
  23. column_add_with_value(
  24. cr, "res_partner", "tracking_emails_count", "integer", 0)
  25. _logger.info("Creating res.partner.email_score column "
  26. "with value 50.0")
  27. column_add_with_value(
  28. cr, "res_partner", "email_score", "double precision", 50.0)