search_api_db.install 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. * Re-arranges the $server->options['indexes'] array to be keyed by machine_name
  29. * instead of by id.
  30. */
  31. function search_api_db_update_7101() {
  32. $query = db_select('search_api_server', 's');
  33. $query->addField('s', 'machine_name');
  34. $query->condition('class', 'search_api_db_service');
  35. $index_names = db_select('search_api_index', 'i')
  36. ->fields('i', array('id', 'machine_name'))
  37. ->condition('server', clone $query, 'IN')
  38. ->execute()
  39. ->fetchAllKeyed();
  40. $query->addField('s', 'options');
  41. $servers = $query->execute();
  42. foreach ($servers->fetchAllKeyed() as $name => $options) {
  43. $options = unserialize($options);
  44. if (empty($options['indexes'])) {
  45. continue;
  46. }
  47. $indexes = array();
  48. foreach ($options['indexes'] as $id => $info) {
  49. if (isset($index_names[$id])) {
  50. $indexes[$index_names[$id]] = $info;
  51. }
  52. }
  53. $options['indexes'] = $indexes;
  54. $options = serialize($options);
  55. db_update('search_api_server')
  56. ->fields(array(
  57. 'options' => $options,
  58. ))
  59. ->condition('machine_name', $name)
  60. ->execute();
  61. }
  62. }
  63. /**
  64. * Solves index problems with umlauts, accented characters, etc. on MySQL servers.
  65. */
  66. function search_api_db_update_7102() {
  67. global $databases;
  68. $server_options = db_select('search_api_server', 's')
  69. ->fields('s', array('options'))
  70. ->condition('class', 'search_api_db_service')
  71. ->execute()
  72. ->fetchCol();
  73. foreach ($server_options as $options) {
  74. $options = unserialize($options);
  75. list($key, $target) = explode(':', $options['database'], 2);
  76. $db_driver = $databases[$key][$target]['driver'];
  77. if ($db_driver === 'mysql' && !empty($options['indexes'])) {
  78. $prev_db = db_set_active($key);
  79. foreach ($options['indexes'] as $fields) {
  80. foreach ($fields as $field) {
  81. db_query("ALTER TABLE {{$field['table']}} CONVERT TO CHARACTER SET 'utf8' COLLATE 'utf8_bin'", array(), array('target' => $target));
  82. }
  83. }
  84. db_set_active($prev_db);
  85. }
  86. }
  87. }