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.

35 lines
1.2 KiB

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