Do not reimport existing entities

Created on 1 April 2016, over 8 years ago
Updated 30 August 2024, 3 months ago

Reinstalling modules containing default content results in a database constraint error on the entities' UUID.
I believe this could be avoided with the following patch, resulting in only updating already existing entities, not trying to save them as new ones.

Adding reference to previously mentioned entity revisions issue

🐛 Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

🇭🇺Hungary Karsa

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

  • First commit to issue fork.
  • First commit to issue fork.
  • 🇳🇱Netherlands eelkeblok Netherlands 🇳🇱

    Thanks for all the work everyone. While trying to get a project moved over to default content 2.x and tinkering around with this issue as well as Allow manual imports Postponed , I found that the code for this issue basically completely ignores the legacy json files, because the logic has been implemented in the denormalize() method of the content entity normalizer, which exclusively deals with YAML files. The code branch for json is higher op, in the importContent method of the importer class. I tried looking through this issue whether that was a deliberate decision, but I couldn't find any mention of it. To me this "smells" a little, it should really matter what format the input is in for this functionality to do its thing. Of course, when json support is completely removed this might all go away... Thoughts?

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    17 pass
  • Status changed to Needs review over 1 year ago
  • 🇧🇬Bulgaria pfrenssen Sofia

    Replying to the comment from @eelkeblok in #2698425-187: Do not reimport existing entities :

    I found that the code for this issue basically completely ignores the legacy json files, because the logic has been implemented in the denormalize() method of the content entity normalizer, which exclusively deals with YAML files. The code branch for json is higher op, in the importContent method of the importer class. I tried looking through this issue whether that was a deliberate decision, but I couldn't find any mention of it. To me this "smells" a little, it should really matter what format the input is in for this functionality to do its thing. Of course, when json support is completely removed this might all go away... Thoughts?

    This issue got raised before. It was suggested by @berdir in comment #2698425-161: Do not reimport existing entities that the old JSON format is basically unsupported and developers should update to YAML format when updating to 2.x.

    default_content 8.x-1.x is also basically unsupported and 2.0.x uses a custom serialization format that does not use the serialization API at all and I strongly suggest to update to that.

    Since there never was support for updating existing entities in 1.x and there never will be, there is no real reason to include JSON support in this issue. I agree that it would help to make the upgrade process smoother for people who were already using an early version of this patch for 1.x, but that shouldn't block adoption of this issue.

    All remarks have been addressed, setting back to needs review.

  • 🇧🇪Belgium johanvdr

    Should we not check for existing entities and update them instead of recreating all?

  • 🇨🇭Switzerland Mistrae

    Patch #173 works well. On the contrary with MR 14, if I import 2 different nodes with same images and medias on 2 different modules, one node has no image.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    13 pass
  • 🇧🇬Bulgaria pfrenssen Sofia

    @Mistrae thanks for the report. Could you possibly provide a test case that shows the bug? Or more details on how this can be replicated?

  • Could not apply the patch after upgrading to 2.0@alpha. Is it because the HAL module is made optional in the new version?

  • Status changed to Needs work 9 months ago
  • Thanks for patch #175, we have tested it on drupal 9.5. And it works correctly.

    Be careful with changing ID during import.

  • 🇧🇯Benin delacosta456

    hi
    also thanks for #175 .. it's working on D10.2.5 with php8.2

  • 🇪🇸Spain unstatu

    Hello everyone,

    I have been struggling with the same problem that was mentioned by stamina in https://www.drupal.org/project/default_content/issues/2698425#mr14-note2... 🐛 Do not reimport existing entities Needs review .

    The way to reproduce the problem is:

    - Create a layout
    - Place an inline block_content with an entity reference field to one or several media images
    - Export the node where the layouts was created (including its dependencies)
    - If you try to import the node in a different environment, the block_content loses its references to the media entities. Media entities are created correctly but the entity_reference field in the block_content entity is empty.

    I have created a patch file based on the current status of the MR!14 adding the fix that he suggested.

  • 🇻🇳Vietnam thanhdo1991

    Some cases, we need to call importContent method without config directory. This patch file make more parameters for custom path

  • 🇧🇯Benin delacosta456

    hi @unstatu
    thanks for the patch that applied correctly on D10.2 +php8.2
    However on my own side when updating the node's section (in layout builder), changes will not be reflected in another env until i delete the node and import again by enabling the custom module.

    This looks to be also the case for other entities like menu_link_content.(Ex have menu link X in both environment Adev and Bprod. on Adev, change the label and disable the menu link, and finally export. there will be no change )

    it' looks like #175 approach is the only one that is successfully uptating an entity

  • 🇧🇯Benin delacosta456

    hi all
    @andypost
    @lammensj
    @eelkeblok
    @kalpaitch
    @nedjo
    @pfrenssen
    @jomsy

    Can somebody please help me know how to use the $update_existing variable (setting it to true ) when re-enabling DC-custom-module to fire entities update?

    Thanks

  • First commit to issue fork.
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7
    last update 5 months ago
    14 pass
  • 🇪🇨Ecuador jwilson3

    Confirmed #196 worked for me. #197 untested.

  • Status changed to RTBC 5 months ago
  • 🇵🇱Poland bronismateusz

    I confirm, the import problems were solved by the patch from #196. 🐛 Do not reimport existing entities Needs review

  • Status changed to Needs work 3 months ago
  • 🇨🇭Switzerland berdir Switzerland
    +++ b/src/Normalizer/ContentEntityNormalizer.php
    @@ -178,18 +179,45 @@ class ContentEntityNormalizer implements ContentEntityNormalizerInterface {
    +        return $entity;
    +      }
    +      // Adding support for Entity Reference Revisions entities. This is used by
    +      // popular contrib modules such as Paragraphs and Commerce.
    +      // @todo Drop the method_exists() call once
    +      //   https://www.drupal.org/project/entity_reference_revisions/issues/3336752
    +      //   is fixed.
    +      elseif ($entity instanceof EntityNeedsSaveInterface && method_exists($entity, 'setNeedsSave')) {
    +        $entity->setNeedsSave(TRUE);
    +      }
    

    the issue is now committed, would conflict if an older version is used, one option would be to add a conflict to composer.json, not sure if that's worth it.

    The created merge request is for #197, not #196. Please create the correct MR so that tests on CI can run with this.

    Looks like a version of this is already in the core importer API.

  • First commit to issue fork.
  • Pipeline finished with Success
    3 months ago
    Total: 226s
    #268195
  • 🇦🇺Australia silverham

    [Do not commit]
    Uploading patch file for Acquia starter kit since it depended on a merge request patch which was updated but I believe it should of depended on uploaded patch file instead to avoid the issue in the future.

  • 🇦🇺Australia silverham

    [Do not commit - for version 2.0.0-alpha3 only]
    Uploading patch file 2698425-do-not-re-import-existing-entities--acquia-starter-kit--2-0-0alpha3-2024-08-30.patch for Acquia starter kit since it depended on a merge request patch which was updated but I believe it should of depended on uploaded patch file instead to avoid the issue in the future.

  • First commit to issue fork.
  • Pipeline finished with Success
    3 months ago
    Total: 171s
    #269266
Production build 0.71.5 2024