serial.install 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the Serial module.
  5. */
  6. /**
  7. * Implements hook_field_schema().
  8. */
  9. function serial_field_schema(array $field) {
  10. $columns = array();
  11. switch ($field['type']) {
  12. case 'serial':
  13. $columns['value'] = array(
  14. 'type' => 'int',
  15. 'unsigned' => TRUE,
  16. 'not null' => TRUE,
  17. 'sortable' => TRUE,
  18. 'views' => TRUE,
  19. 'index' => TRUE,
  20. );
  21. break;
  22. }
  23. return array('columns' => $columns);
  24. }
  25. /**
  26. * Implements hook_schema().
  27. */
  28. function serial_schema() {
  29. // Get the standard schema:
  30. module_load_include('inc', 'serial');
  31. $table_schema = _serial_get_table_schema();
  32. $schema = array();
  33. foreach (_serial_get_all_fields() as $field) {
  34. $schema[_serial_get_table_name($field->entity_type, $field->bundle, $field->field_name)] = $table_schema;
  35. }
  36. // Return the schema of all the assistant tables (one per field instance).
  37. return $schema;
  38. }
  39. /**
  40. * Upgrade path.
  41. *
  42. * Switches from nids to uniqids.
  43. */
  44. function serial_update_7130() {
  45. module_load_include('inc', 'serial');
  46. $table_schema = _serial_get_table_schema();
  47. // Update the schema of old assistant tables.
  48. foreach (_serial_get_all_fields() as $field) {
  49. // Empty the table.
  50. $table = _serial_get_table_name($field->entity_type, $field->bundle, $field->field_name);
  51. db_delete($table)->execute();
  52. // Drop nid field and key.
  53. db_drop_field($table, 'nid');
  54. db_drop_unique_key($table, 'nid');
  55. // Add uniqid field and key.
  56. db_add_field($table, 'uniqid', $table_schema['fields']['uniqid']);
  57. db_add_unique_key($table, 'uniqid', array('uniqid'));
  58. }
  59. }
  60. /**
  61. * Add 'node_' to all existing serial tables.
  62. *
  63. * Change name:
  64. * from: serial_{content_type}_{field_name}
  65. * to: serial_node_{content_type}_{field_name}
  66. */
  67. function serial_update_7131() {
  68. // All old serial tables are of 'node' entity type.
  69. foreach (db_find_tables('serial_%') as $table) {
  70. db_rename_table($table, preg_replace('/^serial_/', 'serial_node_', $table));
  71. }
  72. }
  73. /**
  74. * Reorganize table names to prevent collisions with long names.
  75. */
  76. function serial_update_7132() {
  77. module_load_include('inc', 'serial');
  78. foreach (db_find_tables('serial_%') as $table) {
  79. // Explode by underscores and match old format.
  80. list(, $entity_type, $bundle, $field_name) = explode('_', $table, 4);
  81. db_rename_table($table, _serial_get_table_name($entity_type, $bundle, $field_name));
  82. }
  83. }