Chris@0
|
1 INTRODUCTION
|
Chris@0
|
2 ------------
|
Chris@0
|
3 The migrate_example module demonstrates how to implement custom migrations
|
Chris@0
|
4 for Drupal 8. It includes a group of "beer" migrations demonstrating a complete
|
Chris@0
|
5 simple migration scenario.
|
Chris@0
|
6
|
Chris@0
|
7 THE BEER SITE
|
Chris@0
|
8 -------------
|
Chris@0
|
9 In this scenario, we have a beer aficionado site which stores its data in MySQL
|
Chris@0
|
10 tables - there are content items for each beer on the site, user accounts with
|
Chris@0
|
11 profile data, categories to classify the beers, and user-generated comments on
|
Chris@0
|
12 the beers. We want to convert this site to Drupal with just a few modifications
|
Chris@0
|
13 to the basic structure.
|
Chris@0
|
14
|
Chris@0
|
15 To make the example as simple as to run as possible, the source data is placed
|
Chris@0
|
16 in tables directly in your Drupal database - in most real-world scenarios, your
|
Chris@0
|
17 source data will be in an external database. The migrate_example_setup submodule
|
Chris@0
|
18 creates and populates these tables, as well as configuring your Drupal 8 site
|
Chris@0
|
19 (creating a node type, vocabulary, fields, etc.) to receive the data.
|
Chris@0
|
20
|
Chris@0
|
21 STRUCTURE
|
Chris@0
|
22 ---------
|
Chris@0
|
23 There are two primary components to this example:
|
Chris@0
|
24
|
Chris@4
|
25 1. Migration configuration, in the migrations and config/install directories.
|
Chris@4
|
26 These YAML files describe the migration process and provide the mappings from
|
Chris@4
|
27 the source data to Drupal's destination entities. The difference between the
|
Chris@4
|
28 two possible directories:
|
Chris@4
|
29
|
Chris@4
|
30 a. Files in the migrations directory provide configuration directly for the
|
Chris@4
|
31 migration plugins. The filenames are of the form <migration ID>.yml. This
|
Chris@4
|
32 approach is recommended when your migration configuration is fully hardcoded
|
Chris@4
|
33 and does not need to be overridden (e.g., you don't need to change the URL to
|
Chris@4
|
34 a source web service through an admin UI). While developing migrations,
|
Chris@4
|
35 changes to these files require at most a 'drush cr' to load your changes.
|
Chris@4
|
36
|
Chris@4
|
37 b. Files in the config/install directory provide migration configuration as
|
Chris@4
|
38 configuration entities, and have names of the form
|
Chris@4
|
39 migrate_plus.migration.<migration ID>.yml ("migration" because they define
|
Chris@4
|
40 entities of the "migration" type, and "migrate_plus" because that is the
|
Chris@4
|
41 module which implements the "migration" type). Migrations defined in this way
|
Chris@4
|
42 may have their configuration modified (in particular, through a web UI) by
|
Chris@4
|
43 loading the configuration entity, modifying its configuration, and saving the
|
Chris@4
|
44 entity. When developing, to get edits to the .yml files in config/install to
|
Chris@4
|
45 take effect in active configuration, use the config_devel module.
|
Chris@4
|
46
|
Chris@4
|
47 Configuration in either type of file is identical - the only differences are
|
Chris@4
|
48 the directories and filenames.
|
Chris@0
|
49
|
Chris@0
|
50 2. Source plugins, in src/Plugin/migrate/source. These are referenced from the
|
Chris@0
|
51 configuration files, and provide the source data to the migration processing
|
Chris@0
|
52 pipeline, as well as manipulating that data where necessary to put it into
|
Chris@0
|
53 a canonical form for migrations.
|
Chris@0
|
54
|
Chris@0
|
55 UNDERSTANDING THE MIGRATIONS
|
Chris@0
|
56 ----------------------------
|
Chris@0
|
57 The YAML and PHP files are copiously documented in-line. To best understand
|
Chris@0
|
58 the concepts described in a more-or-less narrative form, it is recommended you
|
Chris@0
|
59 read the files in the following order:
|
Chris@0
|
60
|
Chris@0
|
61 1. migrate_plus.migration_group.beer.yml
|
Chris@0
|
62 2. migrate_plus.migration.beer_term.yml
|
Chris@0
|
63 3. BeerTerm.php
|
Chris@0
|
64 4. migrate_plus.migration.beer_user.yml
|
Chris@0
|
65 5. BeerUser.php
|
Chris@0
|
66 6. migrate_plus.migration.beer_node.yml
|
Chris@0
|
67 7. BeerNode.php
|
Chris@4
|
68 8. beer_comment.yml
|
Chris@0
|
69 9. BeerComment.php
|
Chris@0
|
70
|
Chris@0
|
71 RUNNING THE MIGRATIONS
|
Chris@0
|
72 ----------------------
|
Chris@0
|
73 The migrate_tools module (https://www.drupal.org/project/migrate_tools) provides
|
Chris@0
|
74 the tools you need to perform migration processes. At this time, the web UI only
|
Chris@0
|
75 provides status information - to perform migration operations, you need to use
|
Chris@0
|
76 the drush commands.
|
Chris@0
|
77
|
Chris@0
|
78 # Enable the tools and the example module if you haven't already.
|
Chris@0
|
79 drush en -y migrate_tools,migrate_example
|
Chris@0
|
80
|
Chris@0
|
81 # Look at the migrations. Just look at them. Notice that they are displayed in
|
Chris@0
|
82 # the order they will be run, which reflects their dependencies. For example,
|
Chris@0
|
83 # because the node migration references the imported terms and users, it must
|
Chris@0
|
84 # run after those migrations have been run.
|
Chris@0
|
85 drush ms # Abbreviation for migrate-status
|
Chris@0
|
86
|
Chris@0
|
87 # Run the import operation for all the beer migrations.
|
Chris@0
|
88 drush mi --group=beer # Abbreviation for migrate-import
|
Chris@0
|
89
|
Chris@0
|
90 # Look at what you've done! Also, visit the site and see the imported content,
|
Chris@0
|
91 # user accounts, etc.
|
Chris@0
|
92 drush ms
|
Chris@0
|
93
|
Chris@0
|
94 # Look at the duplicate username message.
|
Chris@0
|
95 drush mmsg beer_user # Abbreviation for migrate-messages
|
Chris@0
|
96
|
Chris@0
|
97 # Run the rollback operation for all the migrations (removing all the imported
|
Chris@0
|
98 # content, user accounts, etc.). Note that it will rollback the migrations in
|
Chris@0
|
99 # the opposite order as they were imported.
|
Chris@0
|
100 drush mr --group=beer # Abbreviation for migrate-rollback
|
Chris@0
|
101
|
Chris@0
|
102 # You can import specific migrations.
|
Chris@0
|
103 drush mi beer_term,beer_user
|
Chris@0
|
104 # At this point, go look at your content listing - you'll see beer nodes named
|
Chris@0
|
105 # "Stub", generated from the user's favbeers references.
|
Chris@0
|
106
|
Chris@0
|
107 drush mi beer_node,beer_comment
|
Chris@0
|
108 # Refresh your content listing - the stub nodes have been filled with real beer!
|
Chris@0
|
109
|
Chris@0
|
110 # You can rollback specific migrations.
|
Chris@0
|
111 drush mr beer_comment,beer_node
|