Problem/Motivation

Import doesn't overwrite files. Because it doesn't overwrite, it leads to files proliferating like rabbits. And when later running drush decm example_module, all the exported files are obviously disambiguated with a sequential number. i.e. public://2021-02/officer_1.jpg when originally it was just officer.jpg. And I have to git checkout content/file/*.

Steps to reproduce

See problem description.

Proposed resolution

When importing files, always force overwrite.

Remaining tasks

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

heddn created an issue. See original summary.

herved made their first commit to this issue’s fork.

herved’s picture

Hi, here's a first attempt which should fix the issue.
Should we add tests?

PS: Sorry I'm a bit lost with d.org patches vs merge requests.
Are all projects using gitlab's merge requests now?

heddn’s picture

Status: Active » Reviewed & tested by the community

This works. And there's test coverage. So LGTM.

kevinfunk’s picture

Thanks @herved. Tested and fixes the issues with files not being overridden.

lisa.rae’s picture

andrea.cividini’s picture

StatusFileSize
new1.85 KB

I'm just attaching the patch here for those who may need it.

heddn’s picture

Title: Import doesn't overwrite files » Import should overwrite files
socialnicheguru’s picture

is this a duplicate of this issue as it is overwriting also, https://www.drupal.org/project/default_content/issues/2698425 ?

heddn’s picture

This is pretty tightly scoped to something different. It isn't about overwriting entities at all. You could even drop the DB and still experience this issue. Which is how I normally see it. I drop the db and reinstall the site and import default content. Without this patch, the files do not overwrite and I start seeing file_1.txt, file_2.txt, etc. This means that when I run the drush command to update the default content, the file entities always result in "changes" and all the files are named with some high numeric suffix.

#2698425: Do not re-import existing entities is at the entity level and is specific to re-running the installation of default on the same DB.

Rajeshreeputra made their first commit to this issue’s fork.

rajeshreeputra’s picture

+1 RTBC

andypost’s picture

Status: Reviewed & tested by the community » Needs review

There's new patch after RTBC and MR

Can we make it configurable?

rajeshreeputra’s picture

sure, will test this again and post the results

omarlopesino made their first commit to this issue’s fork.

xurizaemon’s picture

Status: Needs review » Reviewed & tested by the community

AFAICT the proposed changes here are identical to the changes proposed as of #5, at which point there was test coverage and the behaviour was more consistent. I think this is good to merge.

This appears to work as it says on the tin, which is good - it eliminates a "round trip" issue where executing drush dcim example will generate a new filename for any files which already exist in the destination location, after which a drush dcem example will update the default content exports to append a _0 suffix ... then _1, then ...

👍

berdir’s picture

Status: Reviewed & tested by the community » Needs work

To early to require 10.3.

grimreaper made their first commit to this issue’s fork.

grimreaper’s picture

StatusFileSize
new5.14 KB

Hi,

MR rebased to have a patch appliable on the 2.0.0-alpha3.

grimreaper changed the visibility of the branch 3200212-rebased-2.0.0-alpha3 to hidden.

grimreaper changed the visibility of the branch 3200212-rebased-2.0.0-alpha3 to active.

grimreaper’s picture

Hum, strange, MR is rebased, I also tried another one with only one commit. But patch is not applicable

Idea, I think it is because of changes in .info.yml

grimreaper’s picture

StatusFileSize
new2.2 KB

Now applicable.

byrond’s picture

I don't think this sufficiently tests that additional files were not created:

    // Assert the files, since a file already existed at that location, one has
    // been overwritten.
    $files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['filename' => 'test-file.txt']);
    $this->assertCount(1, $files);

The file is created during the test with file_put_contents(), so an entity for it won't exist in the database. To confirm this, I removed all but the test from the patch, and it didn't fail.

Never mind. I tried on a clean site, and the patch does fail when the patch is not present and passes when it is. I misunderstood how the test was working.

stephane aimar’s picture

StatusFileSize
new2.29 KB

2.0.0-beta1 patch compatibility

rosk0 made their first commit to this issue’s fork.

rosk0’s picture

Status: Needs work » Reviewed & tested by the community

I believe it is fine to require 10.3 now, so back to RTBC based on #17.