Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Since #2304849: Stop excluding local ID and revision fields from HAL output HAL exports include the entity ID from the original site. This can lead to exceptions when importing using Default Content (similar to those seen where a UUID already exists).
See also the change record at Entity local ID and revision ID are now included in HAL responses.
Proposed resolution
Entity IDs should be stripped out during the import.
Comment | File | Size | Author |
---|---|---|---|
#21 | default_content-ignore_entity_ids_for_import-2689069-21.patch | 1.19 KB | awm |
| |||
#14 | interdiff.txt | 8.1 KB | AaronBauman |
#14 | default_content-ignore_entity_ids_for_import-2689069-14.patch | 8.79 KB | AaronBauman |
Comments
Comment #2
andypostAnother case here when entity has no canonical URL...
Comment #3
Arla#2... as in #2700723: Paragraphs and Default Content
Comment #4
AaronBaumanJust got bit by this myself.
Patch should be pretty simple.
Instead of relying on "enforceIsNew", we can simply unset the id and revision keys, as applicable, immediately before saving entities during ::importContent
If we're actually importing duplicate content, we'll get a UUID collision, so intended behavior remains intact.
Comment #6
AaronBaumanIn this patch:
- Updated to fix failing tests (missing getDefinition() call on entityManager)
- New test coverage for conflicting ids.
Comment #8
AaronBauman- and forgot the ->save() call
Comment #10
AaronBaumanEntity::create is fluent.
Entity::save is not.
Simpletest is all but impossible to work with locally.
Comment #11
andypostbtw But what about to use
if ($entity->validate()) save(); else solve();
instead of save?Comment #12
larowlanEnglish needs some work here - will conflict will?
drupalCreateUser has a third argument 'admin user' which you can use instead here
Should this get saved?
I'd like to see a similar test with a user, where the username or email already exists. Because there are unique constraints on those fields, we need to prevent them from being created/prevent the SQL error. Which would align with @andypost's comment about calling
::validate()
Comment #13
AaronBaumancreateNode() (via NodeCreationTrait) calls save(). There's an analogous TermCreationTrait in ctools, but I figured better not to add a dependency.
Are you saying, in other words, only unset the entity keys if we fail validation?
I agree with this sentiment, but do we just try/catch around entity save()?
Feels like it's out of scope to try and resolve email or username collision.
Comment #14
AaronBaumanin this patch:
- new "user" directory in default_content_test
- user2.json and user2fail.json:
- moved admin account creation and login into DefaultContentTest::setUp
- add test that user2.json uid != 2 (even though it's specified in the json file)
- add test that user2fail.json doesn't get imported (because of conflicting email address)
- add validate check prior to saving entity during DefaultContentManager::importContent
- add skip entities which don't validate
- add exception handling around entity::save() during ::importContent
open questions:
- do we actually want to catch entity save exceptions during import?
-- if so, do we need a new ImportException event or similar?
- do we need similar accommodation for validate failure?
Comment #16
fagoPatch has some test-fails, but works fine in my manual test.
I'm not sure about the approach taken though. I'd be cautious with changing all entity-key values - let's better hard-code the changing to IDs and revision-IDs only. Other entity keys should not be changed.
Comment #17
AaronBaumanSorry I didn't make it clear in the description, but the only entity keys getting ignored are id and revision:
I'm trying to get the tests to pass, but simpletest is killing me.
I'll keep plugging, but anyone else feel free to take a stab
Comment #18
kalpaitch CreditAttribution: kalpaitch as a volunteer and commentedThis is kinda now covered by #2698425-42: Do not reimport existing entities. Suggest moving credits and closing as duplicate...?
Comment #19
kalpaitch CreditAttribution: kalpaitch as a volunteer and commentedIMO anyway.
Comment #20
nedjoWorking on a related patch in Better Normalizers: #2913001: Add option to exclude local ID and revision ID values. Rather than ignoring on import, this would remove local ID values on export.
Comment #21
awm CreditAttribution: awm at Johnson & Johnson commentedI am not sure why this is closed but I recently ran into the same issue and " #2698425-42: Do not reimport existing entities. " does not address this issue when you already have nodes with the same IDs exported from a different env. For example, export nodes locally, and one node would have an ID = 123. If this id already exist on a different en then the import fails.
Below is a patch a re-relled based on patch #14:
Comment #22
awm CreditAttribution: awm at Johnson & Johnson commentedComment #23
malcomio CreditAttribution: malcomio at Capgemini commentedAs suggested in #20, and in the workaround mentioned on #2969631: NID / VID / TID conflicts in branches, would it make more sense to prevent the entity IDs being included in the export?
Comment #24
malcomio CreditAttribution: malcomio at Capgemini commentedComment #25
BerdirI've recently created the 2.0.x branch, see the project page on all the improvements in the 2.0.x branch. Testing that and providing feedback would be very welcome. The 1.x branch isn't actively maintained and won't receive new features anymore, so I'm closing this and other issues as won't fix.
2.0.x uses a completely different normalization implementation that does not depend on hal.module anymore and skips all ID fields.