Ejemplo n.º 1
0
 /**
  * Change index and add missing constraints for civicrm_contribution_recur.
  */
 public function addMissingConstraints(CRM_Queue_TaskContext $ctx)
 {
     $query = "SHOW KEYS FROM `civicrm_contribution_recur` WHERE key_name = 'UI_contrib_payment_instrument_id'";
     $dao = CRM_Core_DAO::executeQuery($query);
     if ($dao->N) {
         CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_contribution_recur DROP INDEX UI_contrib_payment_instrument_id');
         CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_contribution_recur ADD INDEX UI_contribution_recur_payment_instrument_id (payment_instrument_id)');
     }
     $constraintArray = array('contact_id' => " ADD CONSTRAINT `FK_civicrm_contribution_recur_contact_id` FOREIGN KEY (`contact_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE CASCADE ", 'payment_processor_id' => " ADD CONSTRAINT `FK_civicrm_contribution_recur_payment_processor_id` FOREIGN KEY (`payment_processor_id`) REFERENCES `civicrm_payment_processor` (`id`) ON DELETE SET NULL ", 'financial_type_id' => " ADD CONSTRAINT `FK_civicrm_contribution_recur_financial_type_id` FOREIGN KEY (`financial_type_id`) REFERENCES `civicrm_financial_type` (`id`) ON DELETE SET NULL ", 'campaign_id' => " ADD CONSTRAINT `FK_civicrm_contribution_recur_campaign_id` FOREIGN KEY (`campaign_id`) REFERENCES `civicrm_campaign` (`id`) ON DELETE SET NULL ");
     $constraint = array();
     foreach ($constraintArray as $constraintKey => $value) {
         $foreignKeyExists = CRM_Core_DAO::checkFKConstraintInFormat('civicrm_contribution_recur', $constraintKey);
         if (!$foreignKeyExists) {
             $constraint[] = $value;
         }
     }
     if (!empty($constraint)) {
         $query = "ALTER TABLE civicrm_contribution_recur " . implode(' , ', $constraint);
         CRM_Core_DAO::executeQuery($query);
     }
     return TRUE;
 }
Ejemplo n.º 2
0
 function verifyPreDBState(&$errorMessage)
 {
     $config =& CRM_Core_Config::singleton();
     // Let's first update the config defaults
     require_once "CRM/Core/DAO/Domain.php";
     $domain =& new CRM_Core_DAO_Domain();
     $domain->selectAdd();
     $domain->selectAdd('config_backend');
     $domain->find(true);
     if ($domain->config_backend) {
         $defaults = unserialize($domain->config_backend);
         // reset components
         $defaults['enableComponents'] = array('CiviContribute', 'CiviPledge', 'CiviMember', 'CiviEvent', 'CiviMail');
         $defaults['enableComponentIDs'] = array(1, 6, 2, 3, 4);
         $defaults['moneyvalueformat'] = '%!i';
         $defaults['fieldSeparator'] = ',';
         $defaults['fatalErrorTemplate'] = 'CRM/common/fatal.tpl';
         // update cms-version
         if ($config->userFramework == 'Joomla') {
             $defaults['userFrameworkVersion'] = '1.5';
             if (class_exists('JVersion')) {
                 $version =& new JVersion();
                 $defaults['userFrameworkVersion'] = $version->getShortVersion();
             }
         } else {
             if ($config->userFramework == 'Drupal') {
                 $defaults['userFrameworkVersion'] = '6.3';
                 if (defined('VERSION')) {
                     $defaults['userFrameworkVersion'] = VERSION;
                 }
             }
         }
         // serialise settings
         require_once "CRM/Core/BAO/Setting.php";
         CRM_Core_BAO_Setting::add($defaults);
         $config =& CRM_Core_Config::singleton();
     }
     // check if log file is writable
     if (!is_writable($config->uploadDir . 'CiviCRM.log')) {
         $errorMessage = ts('Log file CiviCRM.log is not writable. Make sure files directory is writable.', array(1 => $config->uploadDir));
         return false;
     }
     $errorMessage = ts('Database check failed - the current database is not v2.0.');
     $is20db = true;
     // abort if partial upgraded db found.
     if ($this->checkVersion('2.01') || $this->checkVersion('2.02') || $this->checkVersion('2.03')) {
         $errorMessage = ts('Corrupt / Partial Upgraded database found. Looks like upgrade wizard failed to complete all the required steps to convert your database to v2.1. Please fix any errors and start the upgrade process again with a clean v2.0 database.');
         return false;
     }
     // abort if already 2.1
     if ($this->checkVersion('2.1')) {
         $errorMessage = ts('Database check failed - looks like you have already upgraded to the latest version (v2.1) of the database.');
         return false;
     }
     // check if 2.0 version
     if (!$this->checkVersion('2.0')) {
         $is20db = false;
     }
     // check if 2.0 tables exists
     if (!CRM_Core_DAO::checkTableExists('civicrm_activity') || !CRM_Core_DAO::checkTableExists('civicrm_activity_assignment') || !CRM_Core_DAO::checkTableExists('civicrm_activity_target') || !CRM_Core_DAO::checkTableExists('civicrm_address') || !CRM_Core_DAO::checkTableExists('civicrm_address') || !CRM_Core_DAO::checkTableExists('civicrm_case') || !CRM_Core_DAO::checkTableExists('civicrm_case_activity') || !CRM_Core_DAO::checkTableExists('civicrm_component') || !CRM_Core_DAO::checkTableExists('civicrm_contribution_widget') || !CRM_Core_DAO::checkTableExists('civicrm_grant') || !CRM_Core_DAO::checkTableExists('civicrm_group_nesting') || !CRM_Core_DAO::checkTableExists('civicrm_group_organization') || !CRM_Core_DAO::checkTableExists('civicrm_loc_block') || !CRM_Core_DAO::checkTableExists('civicrm_openid') || !CRM_Core_DAO::checkTableExists('civicrm_openid_associations') || !CRM_Core_DAO::checkTableExists('civicrm_openid_nonces') || !CRM_Core_DAO::checkTableExists('civicrm_preferences_date') || !CRM_Core_DAO::checkTableExists('civicrm_tell_friend') || !CRM_Core_DAO::checkTableExists('civicrm_timezone') || !CRM_Core_DAO::checkTableExists('civicrm_worldregion')) {
         // db is not 2.0
         $errorMessage .= ' Few 2.0 tables were found missing.';
         $is20db = false;
     }
     // check fields which MUST be present if a proper 2.0 db
     if ($is20db) {
         if (!CRM_Core_DAO::checkFieldExists('civicrm_activity', 'source_record_id') || !CRM_Core_DAO::checkFieldExists('civicrm_activity', 'activity_date_time') || !CRM_Core_DAO::checkFieldExists('civicrm_activity', 'status_id') || !CRM_Core_DAO::checkFieldExists('civicrm_contact', 'first_name') || !CRM_Core_DAO::checkFieldExists('civicrm_contact', 'last_name') || !CRM_Core_DAO::checkFieldExists('civicrm_contact', 'gender_id') || !CRM_Core_DAO::checkFieldExists('civicrm_contact', 'mail_to_household_id') || !CRM_Core_DAO::checkFieldExists('civicrm_contact', 'user_unique_id') || !CRM_Core_DAO::checkFieldExists('civicrm_contact', 'household_name') || !CRM_Core_DAO::checkFieldExists('civicrm_contact', 'organization_name') || !CRM_Core_DAO::checkFieldExists('civicrm_contribution', 'honor_type_id') || !CRM_Core_DAO::checkFieldExists('civicrm_contribution_page', 'is_pay_later') || !CRM_Core_DAO::checkFieldExists('civicrm_contribution_page', 'pay_later_text') || !CRM_Core_DAO::checkFieldExists('civicrm_country', 'region_id') || !CRM_Core_DAO::checkFieldExists('civicrm_custom_field', 'column_name') || !CRM_Core_DAO::checkFieldExists('civicrm_custom_field', 'option_group_id') || !CRM_Core_DAO::checkFieldExists('civicrm_custom_group', 'table_name') || !CRM_Core_DAO::checkFieldExists('civicrm_custom_group', 'is_multiple') || !CRM_Core_DAO::checkFieldExists('civicrm_domain', 'version') || !CRM_Core_DAO::checkFieldExists('civicrm_domain', 'loc_block_id') || !CRM_Core_DAO::checkFieldExists('civicrm_email', 'contact_id') || !CRM_Core_DAO::checkFieldExists('civicrm_email', 'location_type_id') || !CRM_Core_DAO::checkFieldExists('civicrm_email', 'is_billing') || !CRM_Core_DAO::checkFieldExists('civicrm_entity_tag', 'contact_id') || !CRM_Core_DAO::checkFieldExists('civicrm_event', 'participant_listing_id') || !CRM_Core_DAO::checkFieldExists('civicrm_event', 'loc_block_id') || !CRM_Core_DAO::checkFieldExists('civicrm_event', 'receipt_text') || !CRM_Core_DAO::checkFieldExists('civicrm_event_page', 'is_pay_later') || !CRM_Core_DAO::checkFieldExists('civicrm_event_page', 'pay_later_text') || !CRM_Core_DAO::checkFieldExists('civicrm_financial_trxn', 'contribution_id') || !CRM_Core_DAO::checkFieldExists('civicrm_im', 'contact_id') || !CRM_Core_DAO::checkFieldExists('civicrm_im', 'location_type_id') || !CRM_Core_DAO::checkFieldExists('civicrm_membership_payment', 'contribution_id') || !CRM_Core_DAO::checkFieldExists('civicrm_membership_type', 'receipt_text_signup') || !CRM_Core_DAO::checkFieldExists('civicrm_membership_type', 'receipt_text_renewal') || !CRM_Core_DAO::checkFieldExists('civicrm_option_value', 'component_id') || !CRM_Core_DAO::checkFieldExists('civicrm_participant_payment', 'contribution_id') || !CRM_Core_DAO::checkFieldExists('civicrm_payment_processor', 'url_api') || !CRM_Core_DAO::checkFieldExists('civicrm_payment_processor_type', 'url_api_default') || !CRM_Core_DAO::checkFieldExists('civicrm_phone', 'contact_id') || !CRM_Core_DAO::checkFieldExists('civicrm_phone', 'location_type_id') || !CRM_Core_DAO::checkFieldExists('civicrm_uf_match', 'uf_name')) {
             // db looks to have stuck somewhere between 2.0 & 2.1
             $errorMessage .= ' Few important fields were found missing in some of the tables.';
             $is20db = false;
         }
     }
     if (!$is20db) {
         // check if the db is 2.1
         if (CRM_Core_DAO::checkTableExists('civicrm_cache') && CRM_Core_DAO::checkFieldExists('civicrm_cache', 'group_name') && CRM_Core_DAO::checkFieldExists('civicrm_cache', 'created_date') && CRM_Core_DAO::checkFieldExists('civicrm_cache', 'expired_date') && CRM_Core_DAO::checkTableExists('civicrm_group_contact_cache') && CRM_Core_DAO::checkFieldExists('civicrm_group_contact_cache', 'contact_id') && CRM_Core_DAO::checkTableExists('civicrm_menu') && CRM_Core_DAO::checkFieldExists('civicrm_menu', 'path_arguments') && CRM_Core_DAO::checkFieldExists('civicrm_menu', 'is_exposed') && CRM_Core_DAO::checkFieldExists('civicrm_menu', 'page_type') && CRM_Core_DAO::checkTableExists('civicrm_discount') && CRM_Core_DAO::checkFieldExists('civicrm_discount', 'option_group_id') && CRM_Core_DAO::checkFieldExists('civicrm_discount', 'end_date')) {
             $errorMessage = ts('Database check failed - it looks like you have already upgraded to the latest version (v2.1) of the database.');
         }
         return false;
     }
     // check tables which should not exist for v2.x
     if (CRM_Core_DAO::checkTableExists('civicrm_custom_option') || CRM_Core_DAO::checkTableExists('civicrm_custom_value') || CRM_Core_DAO::checkTableExists('civicrm_email_history') || CRM_Core_DAO::checkTableExists('civicrm_geo_coord') || CRM_Core_DAO::checkTableExists('civicrm_individual') || CRM_Core_DAO::checkTableExists('civicrm_location') || CRM_Core_DAO::checkTableExists('civicrm_meeting') || CRM_Core_DAO::checkTableExists('civicrm_organization') || CRM_Core_DAO::checkTableExists('civicrm_phonecall') || CRM_Core_DAO::checkTableExists('civicrm_sms_history') || CRM_Core_DAO::checkTableExists('civicrm_validation')) {
         // table(s) found in the db which are no longer required
         // for v2.x, though would not do any harm it's recommended
         // to remove them.
         CRM_Core_Session::setStatus(ts("Table(s) found in your db which are no longer required for v2.x, though would not do any harm it's recommended to remove them"));
     }
     // show error if any of the tables, use 'MyISAM' storage engine.
     // just check the first 10 civicrm tables, rather than checking all 106!
     if (CRM_Core_DAO::isDBMyISAM(10)) {
         $errorMessage = ts('Your database is configured to use the MyISAM database engine. CiviCRM  requires InnoDB. You will need to convert any MyISAM tables in your database to InnoDB before proceeding.');
         return false;
     }
     // check FK constraint names are in valid format.
     if (!CRM_Core_DAO::checkFKConstraintInFormat('civicrm_contact', 'domain_id') || !CRM_Core_DAO::checkFKConstraintInFormat('civicrm_contribution_page', 'domain_id') || !CRM_Core_DAO::checkFKConstraintInFormat('civicrm_contribution_recur', 'domain_id') || !CRM_Core_DAO::checkFKConstraintInFormat('civicrm_membership_status', 'domain_id') || !CRM_Core_DAO::checkFKConstraintInFormat('civicrm_contribution', 'contact_id') || !CRM_Core_DAO::checkFKConstraintInFormat('civicrm_contribution', 'contribution_type_id')) {
         $errorMessage = ts('Database consistency check failed. FK constraint names not in the required format. Please rebuild your 2.0 database to ensure schema integrity.');
         return false;
     }
     return true;
 }
Ejemplo n.º 3
0
 /**
  * @param $rev
  *
  * @return bool
  */
 public function upgrade_4_4_4($rev)
 {
     $fkConstraint = array();
     if (!CRM_Core_DAO::checkFKConstraintInFormat('civicrm_activity_contact', 'activity_id')) {
         $fkConstraint[] = "ADD CONSTRAINT `FK_civicrm_activity_contact_activity_id` FOREIGN KEY (`activity_id`) REFERENCES `civicrm_activity` (`id`) ON DELETE CASCADE";
     }
     if (!CRM_Core_DAO::checkFKConstraintInFormat('civicrm_activity_contact', 'contact_id')) {
         $fkConstraint[] = "ADD CONSTRAINT `FK_civicrm_activity_contact_contact_id` FOREIGN KEY (`contact_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE CASCADE;\n";
     }
     if (!empty($fkConstraint)) {
         $fkConstraint = implode(',', $fkConstraint);
         $sql = "ALTER TABLE `civicrm_activity_contact`\n{$fkConstraint}\n";
         // CRM-14036 : delete entries of un-mapped contacts
         CRM_Core_DAO::executeQuery("DELETE ac FROM civicrm_activity_contact ac\nLEFT JOIN civicrm_contact c\nON c.id = ac.contact_id\nWHERE c.id IS NULL;\n");
         // delete entries of un-mapped activities
         CRM_Core_DAO::executeQuery("DELETE ac FROM civicrm_activity_contact ac\nLEFT JOIN civicrm_activity a\nON a.id = ac.activity_id\nWHERE a.id IS NULL;\n");
         CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS=0;");
         CRM_Core_DAO::executeQuery($sql);
         CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS=1;");
     }
     // task to process sql
     $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.4')), 'runSql', $rev);
     // CRM-13892 : add `name` column to dashboard schema
     $query = "\nALTER TABLE civicrm_dashboard\n    ADD name varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Internal name of dashlet.' AFTER domain_id ";
     CRM_Core_DAO::executeQuery($query, array(), TRUE, NULL, FALSE, FALSE);
     $dashboard = new CRM_Core_DAO_Dashboard();
     $dashboard->find();
     while ($dashboard->fetch()) {
         $urlElements = explode('/', $dashboard->url);
         if ($urlElements[1] == 'dashlet') {
             $url = explode('&', $urlElements[2]);
             $name = $url[0];
         } elseif ($urlElements[1] == 'report') {
             $url = explode('&', $urlElements[3]);
             $name = 'report/' . $url[0];
         }
         $values .= "\n      WHEN {$dashboard->id} THEN '{$name}'\n      ";
     }
     $query = "\n     UPDATE civicrm_dashboard\n  SET name = CASE id\n  {$values}\n  END;\n    ";
     CRM_Core_DAO::executeQuery($query, array(), TRUE, NULL, FALSE, FALSE);
     // CRM-13998 : missing alter statements for civicrm_report_instance
     $this->addTask(ts('Confirm civicrm_report_instance sql table for upgrades'), 'updateReportInstanceTable');
     return TRUE;
 }