Add command option to avoid purge or to just append the fixtures datas

Created on 9 November 2022, about 2 years ago
Updated 16 March 2023, almost 2 years ago

Problem/Motivation

I think the module is a bit radical when you execute the command 'drush content-fixtures:load' as it purges all datas prior to load the fixtures.

Maybe I'm thinking wrong here as I am new to the fixtures concept. From what I understand is to create stable content to run tests. I'm trying to implement it on an existing Drupal Website that has a lot of content and entity types. What's the best practice here ? Have an empty version of the same website to run tests with fixtures ?

Proposed resolution

Here is just a version with purge at true or false. But we could also have an option like 'append' to just append content.

  /**
   * Load all fixtures.
   *
   * @command content-fixtures:load
   * @aliases content-fixtures-load
   * @option groups
   * @option purge
   * @usage drush content-fixtures:load
   *   Loads all fixtures.
   * @usage drush content-fixtures:load --groups=group1,group2,group3
   *   Loads fixtures belonging to three groups.
   *
   * @validate-module-enabled content_fixtures
   */
  public function load($options = ['groups' => InputOption::VALUE_REQUIRED, 'purge' => TRUE]) {

    if (!empty($options['groups'])) {
      $groups = explode(',', $options['groups']);
      $this->output()->writeln('Selected groups: ' . implode(', ', $groups));
    }

    if ($options['purge'] === TRUE) {
      if (!$this->io()->confirm('Are you sure you want to delete all existing content and load fixtures?', FALSE)) {
        return;
      }

      $this->output()->writeln('Deleting existing content.');
      $this->purger->purge();
    }

    $fixtures = $this->fixturesLoader->getFixtures();

    if (empty($fixtures)) {
      $this->output()->writeln('No fixtures found.');
      return;
    }

    foreach ($fixtures as $fixture) {
      if (isset($groups) && (!$fixture instanceof FixtureGroupInterface || empty(array_intersect($groups, $fixture->getGroups())))) {
        continue;
      }

      $this->output()->writeln('Loading fixture: ' . \get_class($fixture));
      $fixture->load();
    }
    $this->output()->writeln('Done.');
  }
💬 Support request
Status

Postponed: needs info

Version

3.1

Component

Code

Created by

🇫🇷France pbonnefoi

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇵🇱Poland Luke_Nuke

    I apologize for a very late respond, but better late than never. Nids absolutely can be static. You just need to set ID of the entity before saving it, and it will be saved under this ID.

Production build 0.71.5 2024