migrate_example_setup.install 11.4 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5 6
 * Install file for migrate example module.
 *
7 8 9 10 11
 * Set up source data and destination configuration for the migration example
 * module. We do this in a separate module so migrate_example itself is a pure
 * migration module.
 */

12 13 14
/**
 * Implements hook_schema().
 */
15
function migrate_example_setup_schema() {
16 17 18 19 20 21
  $schema['migrate_example_beer_account'] = migrate_example_beer_schema_account();
  $schema['migrate_example_beer_node'] = migrate_example_beer_schema_node();
  $schema['migrate_example_beer_comment'] = migrate_example_beer_schema_comment();
  $schema['migrate_example_beer_topic'] = migrate_example_beer_schema_topic();
  $schema['migrate_example_beer_topic_node'] = migrate_example_beer_schema_topic_node();

22 23 24
  return $schema;
}

25 26 27
/**
 * Implements hook_install().
 */
28
function migrate_example_setup_install() {
29 30 31 32 33 34 35 36
  // Populate our tables.
  migrate_example_beer_data_account();
  migrate_example_beer_data_node();
  migrate_example_beer_data_comment();
  migrate_example_beer_data_topic();
  migrate_example_beer_data_topic_node();
}

37 38 39 40 41 42
/**
 * The hook_schema definition for node.
 *
 * @return array
 *   The schema definition.
 */
43
function migrate_example_beer_schema_node() {
44
  return [
45
    'description' => 'Beers of the world.',
46 47
    'fields' => [
      'bid'  => [
48 49 50
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Beer ID.',
51 52
      ],
      'name'  => [
53 54 55
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
56 57
      ],
      'body' => [
58 59 60 61
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Full description of the beer.',
62 63
      ],
      'excerpt' => [
64 65 66 67
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Abstract for this beer.',
68 69
      ],
      'countries' => [
70 71 72 73
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Countries of origin. Multiple values, delimited by pipe',
74 75
      ],
      'aid' => [
76 77 78
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Account Id of the author.',
79 80
      ],
      'image' => [
81 82 83 84
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Image path',
85 86
      ],
      'image_alt' => [
87 88 89 90
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Image ALT',
91 92
      ],
      'image_title' => [
93 94 95 96
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Image title',
97 98
      ],
      'image_description' => [
99 100 101 102
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Image description',
103 104 105 106
      ],
    ],
    'primary key' => ['bid'],
  ];
107 108
}

109 110 111 112 113 114
/**
 * The hook_schema definition for topic.
 *
 * @return array
 *   The schema definition.
 */
115
function migrate_example_beer_schema_topic() {
116
  return [
117
    'description' => 'Categories',
118 119
    'fields' => [
      'style'  => [
120 121 122
        'type' => 'varchar_ascii',
        'length' => 255,
        'not null' => TRUE,
123 124
      ],
      'details' => [
125 126 127
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
128 129
      ],
      'style_parent' => [
130 131 132 133
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Parent topic, if any',
134 135
      ],
      'region' => [
136 137 138 139
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Region first associated with this style',
140 141
      ],
      'hoppiness' => [
142 143 144 145
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Relative hoppiness of the beer',
146 147 148 149
      ],
    ],
    'primary key' => ['style'],
  ];
150 151
}

152 153 154 155 156 157
/**
 * The hook_schema definition for topic node.
 *
 * @return array
 *   The schema definition.
 */
158
function migrate_example_beer_schema_topic_node() {
159
  return [
160
    'description' => 'Beers topic pairs.',
161 162
    'fields' => [
      'bid'  => [
163 164 165
        'type' => 'int',
        'not null' => TRUE,
        'description' => 'Beer ID.',
166 167
      ],
      'style'  => [
168 169 170 171
        'type' => 'varchar_ascii',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Topic name',
172 173 174 175
      ],
    ],
    'primary key' => ['style', 'bid'],
  ];
176 177
}

178 179 180 181 182 183
/**
 * The hook_schema definition for comment.
 *
 * @return array
 *   The schema definition.
 */
184
function migrate_example_beer_schema_comment() {
185
  return [
186
    'description' => 'Beers comments.',
187 188
    'fields' => [
      'cid'  => [
189 190 191
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Comment ID.',
192 193
      ],
      'bid'  => [
194 195 196
        'type' => 'int',
        'not null' => TRUE,
        'description' => 'Beer ID that is being commented upon',
197 198
      ],
      'cid_parent' => [
199 200 201
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Parent comment ID in case of comment replies.',
202 203
      ],
      'subject' => [
204 205 206 207
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Comment subject',
208 209
      ],
      'body' => [
210 211 212 213
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Comment body',
214 215
      ],
      'name' => [
216 217 218 219
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Comment name (if anon)',
220 221
      ],
      'mail' => [
222 223 224 225
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Comment email (if anon)',
226 227
      ],
      'aid' => [
228 229 230
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Account ID (if any).',
231 232 233 234
      ],
    ],
    'primary key' => ['cid'],
  ];
235 236
}

237 238 239 240 241 242
/**
 * The hook_schema definition for account.
 *
 * @return array
 *   The schema definition.
 */
243
function migrate_example_beer_schema_account() {
244
  return [
245
    'description' => 'Beers accounts.',
246 247
    'fields' => [
      'aid'  => [
248
        'type' => 'serial',
249
        'not null' => TRUE,
250
        'description' => 'Account ID',
251 252
      ],
      'status'  => [
253 254 255
        'type' => 'int',
        'not null' => TRUE,
        'description' => 'Blocked_Allowed',
256 257
      ],
      'registered' => [
258 259 260 261
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Registration date',
262 263
      ],
      'username' => [
264 265 266 267
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Account name (for login)',
268 269
      ],
      'nickname' => [
270 271 272 273
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Account name (for display)',
274 275
      ],
      'password' => [
276 277 278 279
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Account password (raw)',
280 281
      ],
      'email' => [
282 283 284 285
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Account email',
286 287
      ],
      'sex' => [
288 289 290
        'type' => 'int',
        'not null' => FALSE,
        'description' => 'Gender (0 for male, 1 for female)',
291 292
      ],
      'beers' => [
293 294 295 296
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'Favorite Beers',
297 298 299 300
      ],
    ],
    'primary key' => ['aid'],
  ];
301 302
}

303 304 305
/**
 * Populate node table.
 */
306
function migrate_example_beer_data_node() {
307 308 309 310 311 312 313 314 315 316 317 318
  $fields = [
    'bid',
    'name',
    'body',
    'excerpt',
    'countries',
    'aid',
    'image',
    'image_alt',
    'image_title',
    'image_description',
  ];
319
  $query = db_insert('migrate_example_beer_node')
320
    ->fields($fields);
321
  // Use high bid numbers to avoid overwriting an existing node id.
322
  $data = [
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    // Comes with migrate_example project.
    [
      99999999,
      'Heineken',
      'Blab Blah Blah Green',
      'Green',
      'Netherlands|Belgium',
      0,
      'heineken.jpg',
      'Heinekin alt',
      'Heinekin title',
      'Heinekin description',
    ],
    [
      99999998,
      'Miller Lite',
      'We love Miller Brewing',
      'Tasteless',
      'USA|Canada',
      1,
      NULL,
      NULL,
      NULL,
      NULL,
    ],
    [
      99999997,
      'Boddington',
      'English occasionally get something right',
      'A treat',
      'United Kingdom',
      1,
      NULL,
      NULL,
      NULL,
      NULL,
    ],
360
  ];
361 362 363 364 365 366
  foreach ($data as $row) {
    $query->values(array_combine($fields, $row));
  }
  $query->execute();
}

367
/**
368 369
 * Populate account table.
 *
370
 * Note that alice has duplicate username. Exercises dedupe_entity plugin.
371
 * TODO duplicate email also.
372
 */
373
function migrate_example_beer_data_account() {
374 375 376 377 378 379 380 381 382 383
  $fields = [
    'status',
    'registered',
    'username',
    'nickname',
    'password',
    'email',
    'sex',
    'beers',
  ];
384 385
  $query = db_insert('migrate_example_beer_account')
    ->fields($fields);
386
  $data = [
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    [
      1,
      '2010-03-30 10:31:05',
      'alice',
      'alice in beerland',
      'alicepass',
      'alice@example.com',
      '1',
      '99999999|99999998|99999997',
    ],
    [
      1,
      '2010-04-04 10:31:05',
      'alice',
      'alice in aleland',
      'alicepass',
      'alice2@example.com',
      '1',
      '99999999|99999998|99999997',
    ],
    [
      0,
      '2007-03-15 10:31:05',
      'bob',
      'rebob',
      'bobpass',
      'bob@example.com',
      '0',
      '99999999|99999997',
    ],
    [
      1,
      '2004-02-29 10:31:05',
      'charlie',
      'charlie chocolate',
      'mykids',
      'charlie@example.com',
      '0',
      '99999999|99999998',
    ],
427
  ];
428 429 430 431 432 433
  foreach ($data as $row) {
    $query->values(array_combine($fields, $row));
  }
  $query->execute();
}

434 435 436
/**
 * Populate comment table.
 */
437
function migrate_example_beer_data_comment() {
438
  $fields = ['bid', 'cid_parent', 'subject', 'body', 'name', 'mail', 'aid'];
439 440
  $query = db_insert('migrate_example_beer_comment')
    ->fields($fields);
441 442 443 444 445
  $data = [
    [99999998, NULL, 'im first', 'full body', 'alice', 'alice@example.com', 0],
    [99999998, NULL, 'im second', 'aromatic', 'alice', 'alice@example.com', 0],
    [99999999, NULL, 'im parent', 'malty', 'alice', 'alice@example.com', 0],
    [99999999, 1, 'im child', 'cold body', 'bob', NULL, 1],
446 447 448 449 450 451 452 453 454
    [
      99999999,
      4,
      'im grandchild',
      'bitter body',
      'charlie@example.com',
      NULL,
      1,
    ],
455
  ];
456 457 458 459 460 461
  foreach ($data as $row) {
    $query->values(array_combine($fields, $row));
  }
  $query->execute();
}

462 463 464
/**
 * Populate topic table.
 */
465
function migrate_example_beer_data_topic() {
466
  $fields = ['style', 'details', 'style_parent', 'region', 'hoppiness'];
467 468
  $query = db_insert('migrate_example_beer_topic')
    ->fields($fields);
469 470 471
  $data = [
    ['ale', 'traditional', NULL, 'Medieval British Isles', 'Medium'],
    ['red ale', 'colorful', 'ale', NULL, NULL],
472 473 474 475 476 477 478
    [
      'pilsner',
      'refreshing',
      NULL,
      'Pilsen, Bohemia (now Czech Republic)',
      'Low',
    ],
479
  ];
480 481 482 483 484 485
  foreach ($data as $row) {
    $query->values(array_combine($fields, $row));
  }
  $query->execute();
}

486 487 488
/**
 * Populate topic node table.
 */
489
function migrate_example_beer_data_topic_node() {
490
  $fields = ['bid', 'style'];
491 492
  $query = db_insert('migrate_example_beer_topic_node')
    ->fields($fields);
493 494 495 496 497
  $data = [
    [99999999, 'pilsner'],
    [99999999, 'red ale'],
    [99999998, 'red ale'],
  ];
498 499 500 501
  foreach ($data as $row) {
    $query->values(array_combine($fields, $row));
  }
  $query->execute();
502
}