Skip to content

Instantly share code, notes, and snippets.

@pierrenel
Created October 3, 2012 11:26
Show Gist options
  • Save pierrenel/3826472 to your computer and use it in GitHub Desktop.
Save pierrenel/3826472 to your computer and use it in GitHub Desktop.
apachesolr update 7012
/**
* Rename some variables and update the database tables
*/
function apachesolr_update_7012() {
module_load_include('inc', 'apachesolr', 'apachesolr.index');
$env_id = apachesolr_default_environment();
// Variable changed from integer to array with environment integers
$stored = variable_get('apachesolr_index_last', array());
if (isset($stored['apachesolr'])) {
$stored[$env_id]['node']['last_entity_id'] = $stored['apachesolr']['last_nid'];
$stored[$env_id]['node']['last_changed'] = $stored['apachesolr']['last_change'];
unset($stored['apachesolr']);
variable_set('apachesolr_index_last', $stored);
}
$last = variable_get('apachesolr_index_updated', NULL);
if (isset($last)) {
variable_set('apachesolr_index_updated', array($env_id => (int) $last));
}
// Change namespace to environment id
$excluded_types = apachesolr_environment_variable_get('apachesolr', 'apachesolr_search_excluded_types', array());
apachesolr_environment_variable_set($env_id, 'apachesolr_search_excluded_types', $excluded_types);
apachesolr_environment_variable_del('apachesolr', 'apachesolr_search_excluded_types');
// Install the new schema
//Predefine an amount of types that get their own table
$types = array(
'other' => 'apachesolr_index_entities',
'node' => 'apachesolr_index_entities_node',
);
foreach ($types as $type) {
$schema[$type] = array(
'description' => t('Stores a record of when an entity changed to determine if it needs indexing by Solr.'),
'fields' => array(
'entity_type' => array(
'description' => t('The type of entity.'),
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
),
'entity_id' => array(
'description' => t('The primary identifier for an entity.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'bundle' => array(
'description' => t('The bundle to which this entity belongs.'),
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
),
'status' => array(
'description' => t('Boolean indicating whether the entity is visible to non-administrators (eg, published for nodes).'),
'type' => 'int',
'not null' => TRUE,
'default' => 1,
),
'changed' => array(
'description' => t('The Unix timestamp when an entity was changed.'),
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'indexes' => array(
'changed' => array('bundle', 'status', 'changed'),
),
'primary key' => array('entity_id'),
);
if ($type == 'other') {
// Need the entity type also in the pkey for multiple entities in one table.
$schema[$type]['primary key'][] = 'entity_type';
}
// Create the table
db_create_table($type, $schema[$type]);
}
$schema['apachesolr_index_bundles'] = array(
'description' => t('Records what bundles we should be indexing for a given environment.'),
'fields' => array(
'env_id' => array(
'description' => t('The name of the environment.'),
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
),
'entity_type' => array(
'description' => t('The type of entity.'),
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
),
'bundle' => array(
'description' => t('The bundle to index.'),
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
),
),
'primary key' => array('env_id', 'entity_type', 'bundle'),
);
db_create_table('apachesolr_index_bundles', $schema['apachesolr_index_bundles']);
// Move the data from apachesolr_search_node to apachesolr_index_entities_node
$select = db_select('apachesolr_search_node', 'asn');
$select->join('node', 'n', 'asn.nid = n.nid');
$select->addField('n', 'nid', 'entity_id');
$select->addField('n', 'type', 'bundle');
$select->addField('asn', 'status', 'status');
$select->addField('asn', 'changed', 'changed');
$select->addExpression("'node'", 'entity_type');
$return_value = db_insert('apachesolr_index_entities_node')
->fields(array('entity_id', 'bundle', 'status', 'changed', 'entity_type'))
->from($select)
->execute();
// Drop the table apachesolr_search_node
db_drop_table('apachesolr_search_node');
// For all environments get the excluded types
$environments = apachesolr_load_all_environments();
foreach ($environments as $env_id => $environment) {
$excluded_types = apachesolr_environment_variable_get($env_id, 'apachesolr_search_excluded_types', array());
// Get indexable entity types
$options = array();
foreach (entity_get_info() as $entity_type => $entity_info) {
if (!empty($entity_info['apachesolr']['indexable'])) {
foreach ($entity_info['bundles'] as $key => $info) {
// See if it was excluded & only of entity node. We will not enable
// other entity types by default
if (empty($excluded_types[$key]) && $entity_type == 'node') {
$options[$entity_type][$key] = $key;
}
}
}
}
// Set all except the excluded types
foreach ($options as $entity_type => $bundles) {
apachesolr_index_set_bundles($env_id, $entity_type, $bundles);
}
// Remove the excluded types
apachesolr_environment_variable_del($env_id, 'apachesolr_search_excluded_types');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment