migrate_example.install 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * @file
  4. * Set up the migration example module.
  5. */
  6. require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'migrate_example') .
  7. '/beer.install.inc';
  8. require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'migrate_example') .
  9. '/wine.install.inc';
  10. function migrate_example_schema() {
  11. $schema = migrate_example_beer_schema();
  12. $schema += migrate_example_wine_schema();
  13. return $schema;
  14. }
  15. function migrate_example_install() {
  16. migrate_example_beer_install();
  17. migrate_example_wine_install();
  18. // A simple format for testing migration of format
  19. $example_format = array(
  20. 'format' => 'migrate_example',
  21. 'name' => 'Migrate example format',
  22. 'weight' => 20,
  23. 'filters' => array(
  24. // Escape all HTML.
  25. 'filter_html_escape' => array(
  26. 'weight' => 0,
  27. 'status' => 1,
  28. ),
  29. ),
  30. );
  31. $example_format = (object) $example_format;
  32. filter_format_save($example_format);
  33. }
  34. function migrate_example_uninstall() {
  35. migrate_example_beer_uninstall();
  36. migrate_example_wine_uninstall();
  37. if ($format = filter_format_load('migrate_example')) {
  38. filter_format_disable($format);
  39. }
  40. }
  41. function migrate_example_disable() {
  42. migrate_example_beer_disable();
  43. migrate_example_wine_disable();
  44. }
  45. /**
  46. * Convert modificationdate datetime field to modificationdatetime int field.
  47. */
  48. function migrate_example_update_7001() {
  49. $ret = array();
  50. db_add_field('migrate_example_beer_legacy_urls', 'modificationdatetime', array(
  51. 'type' => 'int',
  52. 'unsigned' => TRUE,
  53. 'not null' => FALSE,
  54. )
  55. );
  56. $result = db_select('migrate_example_beer_legacy_urls', 'ms')
  57. ->fields('ms', array('machine_name', 'modificationdate'))
  58. ->execute();
  59. foreach ($result as $row) {
  60. $modificationdatetime = strtotime($row->modificationdate);
  61. db_update('migrate_example_beer_legacy_urls')
  62. ->fields(array('modificationdatetime' => $modificationdatetime))
  63. ->condition('machine_name', $row->machineName)
  64. ->execute();
  65. }
  66. db_drop_field('migrate_example_beer_legacy_urls', 'modificationdate');
  67. $ret[] = t('Converted modificationdate datetime field to modificationdatetime int field');
  68. return $ret;
  69. }
  70. /**
  71. * Add image alt/title/description columns.
  72. */
  73. function migrate_example_update_7002() {
  74. $ret = array();
  75. db_add_field('migrate_example_beer_node', 'image_alt', array(
  76. 'type' => 'varchar',
  77. 'length' => 255,
  78. 'not null' => FALSE,
  79. 'description' => 'Image ALT',
  80. )
  81. );
  82. db_add_field('migrate_example_beer_node', 'image_title', array(
  83. 'type' => 'varchar',
  84. 'length' => 255,
  85. 'not null' => FALSE,
  86. 'description' => 'Image title',
  87. )
  88. );
  89. db_add_field('migrate_example_beer_node', 'image_description', array(
  90. 'type' => 'varchar',
  91. 'length' => 255,
  92. 'not null' => FALSE,
  93. 'description' => 'Image description',
  94. )
  95. );
  96. db_update('migrate_example_beer_node')
  97. ->fields(array(
  98. 'image_alt' => 'Heinekin alt',
  99. 'image_title' => 'Heinekin title',
  100. 'image_description' => 'Heinekin description',
  101. ))
  102. ->condition('bid', 99999999)
  103. ->execute();
  104. $ret[] = t('Added image_alt, image_title, and image_description fields.');
  105. return $ret;
  106. }
  107. /**
  108. * Add data for remote file examples.
  109. */
  110. function migrate_example_update_7003() {
  111. $ret = array();
  112. db_create_table('migrate_example_wine_files', migrate_example_wine_schema_files());
  113. migrate_example_wine_data_files();
  114. db_add_field('migrate_example_wine_account', 'imageid', array(
  115. 'type' => 'int',
  116. 'unsigned' => TRUE,
  117. 'not null' => FALSE,
  118. 'description' => 'Image ID.',
  119. )
  120. );
  121. db_update('migrate_example_wine_account')
  122. ->fields(array('imageid' => 1))
  123. ->condition('accountid', 9)
  124. ->execute();
  125. $ret[] = t('Added migrate_example_wine_files table.');
  126. $ret[] = t('Added imageid field to migrate_example_wine_account table.');
  127. return $ret;
  128. }
  129. /**
  130. * Add sample data for file fields. And, make the image field multi-value.
  131. */
  132. function migrate_example_update_7004() {
  133. $ret = array();
  134. db_update('migrate_example_wine')
  135. ->fields(array('image' => 'http://cyrve.com/files/penguin.jpeg'))
  136. ->condition('wineid', 1)
  137. ->execute();
  138. db_update('migrate_example_wine')
  139. ->fields(array('image' => 'http://cyrve.com/files/rioja.jpeg|http://cyrve.com/files/boutisse_0.jpeg'))
  140. ->condition('wineid', 2)
  141. ->execute();
  142. $field = field_info_field('field_migrate_example_image');
  143. if ($field) {
  144. $field['cardinality'] = -1; // Unlimited
  145. field_update_field($field);
  146. }
  147. else {
  148. migrate_example_beer_image();
  149. migrate_example_wine_fields();
  150. }
  151. $ret[] = t('Added sample data for file fields.');
  152. $ret[] = t('Made field_migrate_example_image multi-value');
  153. return $ret;
  154. }
  155. /**
  156. * Expand file field example data.
  157. */
  158. function migrate_example_update_7005() {
  159. $ret = array();
  160. // Easiest to just start over from scratch
  161. if (db_table_exists('migrate_example_wine_files')) {
  162. db_drop_table('migrate_example_wine_files');
  163. }
  164. db_create_table('migrate_example_wine_files', migrate_example_wine_schema_files());
  165. migrate_example_wine_data_files();
  166. // Moved this data to migrate_example_wine_files
  167. if (db_field_exists('migrate_example_wine', 'image')) {
  168. db_drop_field('migrate_example_wine', 'image');
  169. }
  170. $ret[] = t('Reconfigured sample data for file fields.');
  171. return $ret;
  172. }
  173. /**
  174. * Sample data for table destinations..
  175. */
  176. function migrate_example_update_7006() {
  177. $ret = array();
  178. db_create_table('migrate_example_wine_table_source', migrate_example_wine_schema_table_source());
  179. db_create_table('migrate_example_wine_table_dest', migrate_example_wine_schema_table_dest());
  180. migrate_example_wine_data_table_source();
  181. $ret[] = t('Added sample data for table destinations.');
  182. return $ret;
  183. }
  184. /**
  185. * Add data for testing/demonstrating roles.
  186. */
  187. function migrate_example_update_7007() {
  188. $ret = array();
  189. db_add_field('migrate_example_wine_account', 'positions', array(
  190. 'type' => 'varchar',
  191. 'length' => 255,
  192. 'not null' => FALSE,
  193. 'description' => 'Positions held',
  194. )
  195. );
  196. $query = db_update('migrate_example_wine_account')
  197. ->fields(array('positions' => '5'))
  198. ->condition('accountid', 1)
  199. ->execute();
  200. db_update('migrate_example_wine_account')
  201. ->fields(array('positions' => '18'))
  202. ->condition('accountid', 3)
  203. ->execute();
  204. db_update('migrate_example_wine_account')
  205. ->fields(array('positions' => '5,18'))
  206. ->condition('accountid', 9)
  207. ->execute();
  208. $ret[] = t('Added positions field to migrate_example_wine_account table.');
  209. return $ret;
  210. }