search_api_db.install 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Implements hook_uninstall().
  4. */
  5. function search_api_db_uninstall() {
  6. if (module_exists('search_api')) {
  7. db_delete('search_api_server')
  8. ->condition('class', 'search_api_db_service')
  9. ->execute();
  10. }
  11. foreach (db_find_tables(Database::getConnection()->prefixTables('{search_api_db}') . '%') as $table) {
  12. if (preg_match('/search_api_db_.*$/', $table, $matches)) {
  13. db_drop_table($matches[0]);
  14. }
  15. }
  16. }
  17. /**
  18. * Implements hook_update_dependencies().
  19. */
  20. function search_api_db_update_dependencies() {
  21. // This update should run after primary IDs have been changed to machine names in the framework.
  22. $dependencies['search_api_db'][7101] = array(
  23. 'search_api' => 7102,
  24. );
  25. return $dependencies;
  26. }
  27. /**
  28. * Implements hook_update_N().
  29. *
  30. * Re-arranges the $server->options['indexes'] array to be keyed by machine_name
  31. * instead of by id.
  32. */
  33. function search_api_db_update_7101() {
  34. $query = db_select('search_api_server', 's');
  35. $query->addField('s', 'machine_name');
  36. $query->condition('class', 'search_api_db_service');
  37. $index_names = db_select('search_api_index', 'i')
  38. ->fields('i', array('id', 'machine_name'))
  39. ->condition('server', clone $query, 'IN')
  40. ->execute()
  41. ->fetchAllKeyed();
  42. $query->addField('s', 'options');
  43. $servers = $query->execute();
  44. foreach ($servers->fetchAllKeyed() as $name => $options) {
  45. $options = unserialize($options);
  46. if (empty($options['indexes'])) {
  47. continue;
  48. }
  49. $indexes = array();
  50. foreach ($options['indexes'] as $id => $info) {
  51. if (isset($index_names[$id])) {
  52. $indexes[$index_names[$id]] = $info;
  53. }
  54. }
  55. $options['indexes'] = $indexes;
  56. $options = serialize($options);
  57. db_update('search_api_server')
  58. ->fields(array(
  59. 'options' => $options,
  60. ))
  61. ->condition('machine_name', $name)
  62. ->execute();
  63. }
  64. }