Problem/Motivation

We are now double maintaining our Drush commands - Drush 8 and Drush 9. Let's move more logic into a shared CLI service so that duplicate code mostly eliminated.

Proposed resolution

Follow outline in this blog post from Nuvole as recommended by @moshe-weitzman

Remaining tasks

  • Create service for carrying out drush commands so logic can be consolidated in one place.
  • Create stub functions for each migrate task in that service.
  • Prove the service is usable by porting the Drush 9 migrate-import command to it.
  • Adapt the service to be usable by Drush 8 for the same command.
  • Now the approach works, implement this approach for the remaining commands
    • status
    • rollback
    • stop
    • resetStatus
    • messages
    • fieldsSource

User interface changes

None

API changes

None

Data model changes

None

Comments

moshe weitzman created an issue. See original summary.

ressa’s picture

Issue summary: View changes

Fixed the link.

eli-t’s picture

Status: Active » Needs work
StatusFileSize
new12.48 KB

Tentative start to this approach.

This patch provides a service for drush commands based on the approach linked in the summary. It provides a service with name migrate_tools.cli in the class Drupal\migrate_tools\MigrateToolsCliService.

This class has stub methods for each of the commands provided by migrate tools. The method for import() is implemented, and called from the Drush 9 command class. This can run a migration in this way, but does not output to the screen as I haven't passed through the drush logger yet.

Next steps should be to pass through the drush logger for drush 9, and get the same service method called from drush 8. Then fill in the other methods. Then profit.

eli-t’s picture

#3 was missing the constructor to inject the Migration Plugin Manager to the CLI service. This fixes that, and passes the logger Drush injects to the Command class to the CLI service before calling import(). So we get the same output to the screen as we did before refactoring to a service.

heddn’s picture

Some preliminary suggestions:

  1. +++ b/src/MigrateToolsCliService.php
    @@ -0,0 +1,301 @@
    +    array $options = [
    +      'all' => NULL,
    +      'group' => NULL,
    +      'tag' => NULL,
    +      'limit' => NULL,
    +      'feedback' => NULL,
    +      'idlist' => NULL,
    +      'update' => NULL,
    +      'force' => NULL,
    +      'execute-dependencies' => NULL,
    +    ]
    

    Can we do array merging after passing this into import? I really don't like to see functions names span across multiple lines.

  2. +++ b/src/MigrateToolsCliService.php
    @@ -0,0 +1,301 @@
    +    $filter['migration_group'] = $options['group'] ? explode(
    +      ',',
    +      $options['group']
    +    ) : [];
    +    $filter['migration_tags'] = $options['tag'] ? explode(
    +      ',',
    +      $options['tag']
    +    ) : [];
    

    I find it easier to read if these are on a single line.

  3. +++ b/src/MigrateToolsCliService.php
    @@ -0,0 +1,301 @@
    +              $configured_id = (in_array(
    +                $search_value,
    +                $configured_values
    +              )) ? $search_value : 'default';
    ...
    +                if (empty($migration_ids) || in_array(
    +                    Unicode::strtolower($id),
    +                    $migration_ids
    +                  )) {
    

    Same here.

  4. +++ b/src/MigrateToolsCliService.php
    @@ -0,0 +1,301 @@
    +        dt(
    +          '!name Migration - !count failed.',
    +          ['!name' => $migration_id, '!count' => $count]
    +        )
    

    Same line maybe?

eli-t’s picture

The split lines are just inherited from the equivalent existing code in the MigrateToolsCommands class, so I left them as they were so it was easier to eyeball any differences. But that doesn't mean we can't fix them as we move them, and I agree that they are better unsplit.

eli-t’s picture

Just addressing the points from #5 - still lots to do.

quietone’s picture

@Eli-T, can you elaborate on 'still lots to do'? Maybe even some small steps or pieces. I'd like to help but not sure what needs to be done next here.

eli-t’s picture

Issue summary: View changes
eli-t’s picture

@quietone that's awesome! I've just updated the issue summary in the standard template so we can clearly detail the approach I was taking and where it was up to.

The tasks struck off in the summary reflect the status of the patch in #7.

I started to work on the next task - getting Drush 8 to run an import through the service - work in progress in patch uploaded in this comment.
The migrate() method on the MigrateToolsCliService is set to be used by Drush 8, but the MigrateToolsCliService class is attempted to be constructed with the Drush 9 logger, which is NULL, so it falls over.

So next step is to either look at that, or flesh out the other methods in Drush 9 land.

Hope that makes sense, if not, hit me up in Slack and I'll try to clarify.

quietone’s picture

StatusFileSize
new28.72 KB
new14.87 KB

@Eli-T, thanks for the details. It is late so I opted to keep it simple and used the existing migrate-import changes to convert stop, resetStatus, messages and fieldSource.

heddn’s picture

With the work going into https://github.com/drush-ops/drush/issues/2926 and more specifically the plan in https://github.com/drush-ops/drush/pull/3402... I not sure we need to keep working on things here.

eli-t’s picture

Status: Needs work » Postponed

Thanks for the heads up @heddn - I agree, there is little point working on this whilst it looks like it will be removed. Changing status to postponed for now.

marvil07’s picture

Status: Postponed » Needs review

This ticket was posponed pending on a possible drush upstrean inclusion, but the it was decided that will not be the case by drush maintainers.

The last submitted patch, 4: 2921721_4_fix_import_and_make_it_log.patch, failed testing. View results

The last submitted patch, 7: 2921721_7_address_initial_review.patch, failed testing. View results

The last submitted patch, 10: 2921721_10_start_drush_8_import_support.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 11: 2921721_11_start_drush_8_import_support.patch, failed testing. View results

heddn’s picture

Status: Needs work » Closed (outdated)

We no longer have 2 code streams for drush commands. So I think this issue has served its purpose.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.