Answers.Body: <p>Here is an example of one I have used:</p>
  
  <pre><code> public function upgrade_1002() {
      $this-&gt;ctx-&gt;log-&gt;info('Applying update 1002 (add country)id to civicrm_project table and renaming manager_id in civicrm_programme table)');
      if (CRM_Core_DAO::checkTableExists('civicrm_project')) {
        if (!CRM_Core_DAO::checkFieldExists('civicrm_project', 'country_id')) {
          CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_project ADD COLUMN country_id INT(11) DEFAULT NULL');
        }
        if (CRM_Core_DAO::checkFieldExists('civicrm_programme' , 'contact_id_manager')) {
          CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_programme CHANGE contact_id_manager manager_id INT(11)');
        }
      }
      return TRUE;
    }
  </code></pre>
  
  <p>Does this explain it a little? And obviously you also have to make sure your install query is fixed as this will be used when installing, not the upgrades.</p>
  
  <p>And another one with the sql in a separate file:</p>
  
  <pre><code>  /**
     * Upgrade 1003 - add table civicrm_case_project
     */
    public function upgrade_1003() {
      $this-&gt;ctx-&gt;log-&gt;info('Applying update 1003 (create civicrm_case_project table');
      if (!CRM_Core_DAO::checkTableExists('civicrm_case_project')) {
        $this-&gt;executeSqlFile('sql/createCaseProject.sql');
      }
      return TRUE;
    }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-29T14:23:41.650Z
Answers.Text: Thanks Eric, where do those upgrade_xxxx functions sit in an extension? How does CiviCRM know they need doing for a given extension? And finally, when you say about install/upgrade: I think you're saying upgrade_xxx functions are not run on install (so the install functions need to be up-to-date), is that right?
Answers.UserDisplayName: artfulrobot
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-29T15:20:33.623Z
Answers.Text: If you do civix generate:upgrader the appropriate classes will be created for you, the function sit in the CRM/<your namespace>/Upgrader class. And yes, the upgrade functions do not run on install
Answers.UserDisplayName: ErikH - CiviCooP
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-29T14:20:06.813Z
Answers.LastActivityDate: 2016-04-29T14:20:06.813Z
Answers.UserDisplayName: ErikH - CiviCooP
Body: <p>I'm developing an upgrade to an extension and when this version is installed it's going to need to update the database.</p>
  
  <p>The module extension has been made with civix and I notice there's a <code>CRM_XXX_Upgrader_Base</code> class that looks useful but I can't figure out where I'd put the code I need to run.</p>
  
  <p>Are there any good examples of extension upgrade hooks that I could learn from?</p>
  
  <p>I've looked at a couple of popular extensions, but they don't seem to have used/needed them, so I'm stabbing in the dark a bit.</p>
  
Tags: <extension><upgrade><developer>
Title: How to use CiviX upgrade hooks?
ThreadText: # How to use CiviX upgrade hooks?
  
  (Asked by: username_0 on 2016-04-29)
  
  I'm developing an upgrade to an extension and when this version is installed it's going to need to update the database.
  
  The module extension has been made with civix and I notice there's a `CRM_XXX_Upgrader_Base` class that looks useful but I can't figure out where I'd put the code I need to run.
  
  Are there any good examples of extension upgrade hooks that I could learn from?
  
  I've looked at a couple of popular extensions, but they don't seem to have used/needed them, so I'm stabbing in the dark a bit.
  
  **Tags:** extension, upgrade, developer
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-04-29. Score: 1 | Accepted answer
  
  Here is an example of one I have used:
   public function upgrade_1002() {
      $this->ctx->log->info('Applying update 1002 (add country)id to civicrm_project table and renaming manager_id in civicrm_programme table)');
      if (CRM_Core_DAO::checkTableExists('civicrm_project')) {
        if (!CRM_Core_DAO::checkFieldExists('civicrm_project', 'country_id')) {
          CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_project ADD COLUMN country_id INT(11) DEFAULT NULL');
        }
        if (CRM_Core_DAO::checkFieldExists('civicrm_programme' , 'contact_id_manager')) {
          CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_programme CHANGE contact_id_manager manager_id INT(11)');
        }
      }
      return TRUE;
    }
  
  Does this explain it a little? And obviously you also have to make sure your install query is fixed as this will be used when installing, not the upgrades.
  And another one with the sql in a separate file:
    /**
     * Upgrade 1003 - add table civicrm_case_project
     */
    public function upgrade_1003() {
      $this->ctx->log->info('Applying update 1003 (create civicrm_case_project table');
      if (!CRM_Core_DAO::checkTableExists('civicrm_case_project')) {
        $this->executeSqlFile('sql/createCaseProject.sql');
      }
      return TRUE;
    }
  
  > **Comments:**
  > **username_0**: Thanks Eric, where do those upgrade_xxxx functions sit in an extension? How does CiviCRM know they need doing for a given extension? And finally, when you say about install/upgrade: I think you're saying upgrade_xxx functions are not run on install (so the install functions need to be up-to-date), is that right?  
  > **username_1**: If you do civix generate:upgrader the appropriate classes will be created for you, the function sit in the CRM/<your namespace>/Upgrader class. And yes, the upgrade functions do not run on install  
  
  ---
  
  