uuid.install 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the uuid module.
  5. */
  6. define('UUID_UPGRADE_VAR', 'uuid_upgrade_in_progress');
  7. /**
  8. * Include some helper functions for the Entity API.
  9. */
  10. module_load_include('inc', 'uuid', 'uuid.entity');
  11. /**
  12. * Helper function that returns a schema field definition for UUID fields.
  13. *
  14. * @see uuid_schema_alter()
  15. * @see uuid_install()
  16. */
  17. function uuid_schema_field_definition() {
  18. return array(
  19. 'type' => 'char',
  20. 'length' => 36,
  21. 'not null' => TRUE,
  22. 'default' => '',
  23. 'description' => 'The Universally Unique Identifier.',
  24. );
  25. }
  26. /**
  27. * Implements of hook_schema_alter().
  28. */
  29. function uuid_schema_alter(&$schema = array()) {
  30. $field = uuid_schema_field_definition();
  31. foreach (uuid_get_core_entity_info() as $entity_type => $info) {
  32. $schema[$info['base table']]['fields'][$info['entity keys']['uuid']] = $field;
  33. if (!empty($info['revision table']) && !empty($info['entity keys']['revision uuid'])) {
  34. $schema[$info['revision table']]['fields'][$info['entity keys']['revision uuid']] = $field;
  35. }
  36. }
  37. }
  38. /**
  39. * Implements hook_install().
  40. */
  41. function uuid_install() {
  42. _uuid_install_uuid_fields();
  43. uuid_sync_all();
  44. }
  45. /**
  46. * Install the 'uuid' and 'vuuid' fields into Drupal core entity tables where needed.
  47. *
  48. * IMPORTANT: This function is called both at install and update time. If this method
  49. * is modified to add additional fields in the future, the update strategy must be
  50. * considered. See the comment in uuid_update_7102.
  51. */
  52. function _uuid_install_uuid_fields() {
  53. $field = uuid_schema_field_definition();
  54. foreach (uuid_get_core_entity_info() as $entity_type => $info) {
  55. if (!db_field_exists($info['base table'], $info['entity keys']['uuid'])) {
  56. db_add_field($info['base table'], $info['entity keys']['uuid'], $field);
  57. db_add_index($info['base table'], $info['entity keys']['uuid'], array($info['entity keys']['uuid']));
  58. }
  59. if (!empty($info['revision table']) && !empty($info['entity keys']['revision uuid'])) {
  60. if (!db_field_exists($info['revision table'], $info['entity keys']['revision uuid'])) {
  61. db_add_field($info['revision table'], $info['entity keys']['revision uuid'], $field);
  62. db_add_index($info['revision table'], $info['entity keys']['revision uuid'], array($info['entity keys']['revision uuid']));
  63. }
  64. }
  65. }
  66. }
  67. /**
  68. * Implements hook_uninstall().
  69. */
  70. function uuid_uninstall() {
  71. foreach (uuid_get_core_entity_info() as $entity_type => $info) {
  72. if (db_field_exists($info['base table'], $info['entity keys']['uuid'])) {
  73. db_drop_field($info['base table'], $info['entity keys']['uuid']);
  74. db_drop_index($info['base table'], $info['entity keys']['uuid']);
  75. }
  76. if (!empty($info['revision table']) && !empty($info['entity keys']['revision uuid'])) {
  77. if (db_field_exists($info['revision table'], $info['entity keys']['revision uuid'])) {
  78. db_drop_field($info['revision table'], $info['entity keys']['revision uuid']);
  79. db_drop_index($info['revision table'], $info['entity keys']['revision uuid']);
  80. }
  81. }
  82. }
  83. }
  84. /**
  85. * Implements hook_modules_installed().
  86. */
  87. function uuid_modules_installed($modules) {
  88. // Run the installation hook. This makes sure that the schema for all
  89. // supported core entity types is set correct.
  90. uuid_install();
  91. }
  92. /**
  93. * Create uuid_vocabulary and uuid_term_data tables.
  94. */
  95. function uuid_update_6001() {
  96. $ret = array();
  97. db_create_table($ret, 'uuid_vocabulary', uuid_table_schema('vocabulary', 'vid'));
  98. db_create_table($ret, 'uuid_term_data', uuid_table_schema('term_data', 'tid'));
  99. return $ret;
  100. }
  101. /**
  102. * For each of out tables, drop the indexe on the UUID column and add a unique
  103. * key on that column.
  104. */
  105. function uuid_update_6002() {
  106. $ret = array();
  107. foreach (uuid_schema() as $table => $schema) {
  108. db_drop_index($ret, $table, $table . '_uuid_idx');
  109. db_add_unique_key($ret, $table, $table . '_uuid_key', array('uuid'));
  110. }
  111. return $ret;
  112. }
  113. /**
  114. * Create uuid_comment table.
  115. */
  116. function uuid_update_6003() {
  117. $ret = array();
  118. db_create_table($ret, 'uuid_comments', uuid_table_schema('comments', 'cid'));
  119. return $ret;
  120. }
  121. /**
  122. * Fix the column definitions for uuid columns in all tables
  123. * to use the more efficient char spec.
  124. */
  125. function uuid_update_6004() {
  126. $ret = array();
  127. // Use what's in uuid_table_schema in order to be consistent.
  128. $tables = uuid_schema();
  129. $spec = $tables['uuid_node']['fields']['uuid'];
  130. foreach ($tables as $tablename => $schema) {
  131. if (db_table_exists($tablename)) {
  132. db_change_field($ret, $tablename, 'uuid', 'uuid', $spec);
  133. }
  134. }
  135. return $ret;
  136. }
  137. /**
  138. * Modify existing uuid_node_revisions table to support revision deletion, and
  139. * add in as much legacy data as possible.
  140. */
  141. function uuid_update_6005() {
  142. $ret = array();
  143. if (db_table_exists('uuid_node_revisions')) {
  144. // Use what's already defined in uuid schema in order to be consistent.
  145. $schema = uuid_schema();
  146. $spec = $schema['uuid_node_revisions']['fields']['nid'];
  147. db_add_field($ret, 'uuid_node_revisions', 'nid', $spec);
  148. // Add node ids to the new column, for revisions that exist, but now have a
  149. // default value of 0 in uuid_node_revisions.
  150. $result = db_query('SELECT nr.nid, nr.vid FROM {node_revisions} AS nr LEFT JOIN {uuid_node_revisions} AS unr ON unr.vid=nr.vid WHERE unr.nid=%d', 0);
  151. while ($item = db_fetch_object($result)) {
  152. $ret[] = update_sql('UPDATE {uuid_node_revisions} SET nid=' . (int) $item->nid . ' WHERE vid=' . (int) $item->vid);
  153. }
  154. // Add uuid_node_revision rows for rows that don't exist, but should.
  155. $result = db_query('SELECT nr.nid, nr.vid FROM {node_revisions} AS nr LEFT JOIN {uuid_node_revisions} AS unr ON unr.vid=nr.vid WHERE unr.nid IS NULL');
  156. while ($item = db_fetch_object($result)) {
  157. $ret[] = update_sql(sprintf("INSERT INTO {uuid_node_revisions} (vid, uuid, nid) VALUES(%d, '%s', %d)", $item->vid, uuid_uuid(), $item->nid));
  158. }
  159. // Delete any orphaned revision vid, uuid pairs.
  160. $ret[] = update_sql('DELETE FROM {uuid_node_revisions} WHERE nid=0');
  161. }
  162. return $ret;
  163. }
  164. /**
  165. * Remove old variables.
  166. */
  167. function uuid_update_7100() {
  168. $types = array(
  169. 'nodes',
  170. 'users',
  171. 'taxonomy',
  172. 'comments',
  173. );
  174. foreach ($types as $type) {
  175. variable_del('uuid_automatic_for_' . $type);
  176. }
  177. return array();
  178. }
  179. /**
  180. * Clear cache for installations that used alpha1. Modules that previously was
  181. * enabled in uuid_update_7100() doesn't exist anymore.
  182. */
  183. function uuid_update_7101() {
  184. drupal_flush_all_caches();
  185. }
  186. /**
  187. * Insure that the uuid and vuuid fields are added where needed.
  188. *
  189. * Note that update 7102 calls _uuid_install_uuid_fields(), which is an
  190. * idempotent function. If _uuid_install_uuid_fields() is changed at some
  191. * point in the future (but remains idempotent), then some uuid users
  192. * will have run update 7102, and some will not. A new uuid_update_7103()
  193. * function would would therefore be necessary to update all users to
  194. * the latest schema. At the same time, uuid_update_7102() could become
  195. * an empty function, as it would not be necessary to call _uuid_install_uuid_fields()
  196. * twice.
  197. */
  198. function uuid_update_7102() {
  199. // If the user have disabled the UUID module during upgrade (as UPGRADE.txt
  200. // instructs), some functions will be missing. So include the module file.
  201. module_load_include('module', 'uuid', 'uuid');
  202. _uuid_install_uuid_fields();
  203. uuid_sync_all();
  204. }