Slide 1

Slide 1 text

1 axelerant.com How to write dynamic migration Mohit Aghera, Mitesh Patel

Slide 2

Slide 2 text

2 axelerant.com About Us Mohit Aghera - Back-end developer @Axelerant - Drupal.org: mohit_aghera - Twitter: @mohit_rocks Mitesh Patel - Back-end developer @Axelerant - Known as @miteshmap over internet.

Slide 3

Slide 3 text

3 axelerant.com Dynamic Migrations ● What are dynamic migrations ? ● Why do we need ?

Slide 4

Slide 4 text

4 axelerant.com Migrations : Recap

Slide 5

Slide 5 text

5 axelerant.com Migrations: A brief introduction What Migrate API Provides: ● Migrate Data from source to destination ● Keeps track of record of migrated data ● Provides framework for writing migrations

Slide 6

Slide 6 text

6 axelerant.com Migration Plugins ● Source Plugins ● Process Plugins ● Destination Plugins

Slide 7

Slide 7 text

7 axelerant.com Migration Flow

Slide 8

Slide 8 text

8 axelerant.com Understanding Derivatives ● Drupal 7 has “_info()” hooks ● Wonder how do we generate multiple blocks with single block plugin ? ● Drupal 8 has “Derivatives” ● Allows to generate instances dynamically

Slide 9

Slide 9 text

9 axelerant.com How Derivatives works ? ● Plugin manager uses “Discovery” mechanism ● “DerivativeDiscoveryDecorator” decorator class

Slide 10

Slide 10 text

10 axelerant.com Define along with Plugin

Slide 11

Slide 11 text

11 axelerant.com Deriver Implementation SystemMenuBlock Deriver implementation:

Slide 12

Slide 12 text

12 axelerant.com Understanding Derivers Notable Examples: ● SystemMenuBlock ● BlockContentBlock ● LanguageBlock And many more..

Slide 13

Slide 13 text

13 axelerant.com Possible approaches for migration ● Migration templates ● Config Entities

Slide 14

Slide 14 text

14 axelerant.com Migration as Configuration entities ● Similar to any configurations ● Resides in [module]/config directory ● Typically names are given like “migrate.migration.node_page.yml”

Slide 15

Slide 15 text

15 axelerant.com Migration as Configuration entities Advantages: ● Easy to write and run ● No need to write specific logic to run unlike migration templates Disadvantages: ● Not much flexible ● Hard to change runtime ● Dynamic migration generation is not possible ● Can’t generate based on user inputs

Slide 16

Slide 16 text

16 axelerant.com Migration as templates ● Resides in [module]/migration_templates directory ● Names could match the migration id ● Typically names are given like “node_page.yml”

Slide 17

Slide 17 text

17 axelerant.com Migration as templates Advantages: ● Much more flexible than configuration entities for each variation ● Easy to change runtime ● Ability to generate dynamic migration using reusable templates

Slide 18

Slide 18 text

18 axelerant.com Derivers for Migration ● Works as a derivative for new migrations ● Helps to create migrations dynamically ● Extends DeriverBase class.

Slide 19

Slide 19 text

19 axelerant.com Writing Derivers ● Implement getDerivativeDefinitions() and write your logic.

Slide 20

Slide 20 text

20 axelerant.com Using Derivers Specify in migration template file.

Slide 21

Slide 21 text

21 axelerant.com Running migration

Slide 22

Slide 22 text

22 axelerant.com Use cases ● Migrating large amount of sites with similar architecture but different languages in each site (our use case, https://github.com/miteshmap/drupal_migration_demo) ● Core: Migrating D7 to D8 ● Core: Migrating Workbench Moderation to Content Moderation (https://www.drupal.org/project/wbm2cm) ● Migrating sites from Magento for instance with different attributes for each site ● And many more...

Slide 23

Slide 23 text

23 axelerant.com Thank you … for being here and allowing us to contribute and share our knowledge To summarise… ● We discussed how to make migrations more dynamic and less painful ● We saw an example of how to write derivers ● We saw advantages of doing it, it is done in Core and many contrib modules already

Slide 24

Slide 24 text

24 axelerant.com References Migration API : https://www.drupal.org/docs/8/api/migrate-api/migrate-api-overview Reference module : https://github.com/mohit-rocks/drupalcamp-goa