Problem/Motivation
The first version of validation was introduced in #1998658: Creating Custom Block with same name as existing block throws SQL error, you can check commit (https://git.drupalcode.org/project/drupal/-/commit/edcc75602fe1fc3b1d15049bdaec0198ce5aa43a)
At that moment the custom_block module has a custom table with "unique keys", but now it is not a case any more
(core/modules/block/custom_block/custom_block.install)
'unique keys' => array(
'revision_id' => array('revision_id'),
'uuid' => array('uuid'),
'info' => array('info'),
),
Currently custom block entity has constraint UniqueField for info field. I don't see any reason to have it unique.
We can have different content block types and of course they can have not unique info fields.
A little history
This was probably from before we had UUIDs
Back in Drupal 7 these were likely using in hook block info
But now we use the uuid, so this constraint isn't needed.
Steps to reproduce
Add a few block with the same info field value.
Proposed resolution
Get rid off UniqueField constraint.
| Comment | File | Size | Author |
|---|---|---|---|
| #45 | interdiff-41-45.txt | 3.34 KB | lobsterr |
| #45 | 3272969-45.patch | 7.45 KB | lobsterr |
| #41 | interdiff-39-41.txt | 1.03 KB | lobsterr |
| #41 | 3272969-41.patch | 6.59 KB | lobsterr |
| #39 | 3272969-39.patch | 6.49 KB | lobsterr |
Issue fork drupal-3272969
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:
- 3272969-custom-block-should
changes, plain diff MR !2050
Comments
Comment #2
lobsterr commentedComment #3
lobsterr commentedComment #4
bkosborneI recently came to the same conclusion. Why should content blocks be any different than other content entities which don't have such a constraint? Drupal doesn't prevent us from adding duplicate node titles or taxonomy terms.
Comment #5
larowlanThis was probably from before we had UUIDs
Back in Drupal 7 these were likely using in hook block info
But now we use the uuid
Comment #7
lobsterr commentedComment #8
larowlanLooks like there some tests for uniqueness that will need updating.
Comment #9
lobsterr commentedI will check the tests, the schema is created with entity api and constraints don't and any schema changes to dB tables
Comment #10
lobsterr commented@larowlan, I updated the tests
Comment #11
larowlanThanks - it looks like either tests didn't re-run or it's still reporting failures
We'll need an update path test here
You can see an example from another issue here
Comment #13
zipymonkey commentedThanks for your work on this. This fixed the "A custom block with block description BLOCK NAME already exists." errors that.
Comment #14
smustgrave commentedFor the update path test mentioned in #11
Also MR may need to be updated to point to 9.5
Comment #15
smustgrave commentedTook a shot. Using a patch off the 9.5 branch.
Comment #16
smustgrave commentedTagging for accessibility
If we allow for blocks to have the same now could introduce a 508 issue where links have the same description.
Comment #24
larowlanTransferring credit from #3054197: Cannot add a custom block with a block description already used by a non-reusable block
Comment #26
larowlanI don't think we need these local variables, lets just pass the strings to the function, they're only used once
I think we should just remove these lines now, and end the test with the 'Custom Block found in database' assertion
We don't need to test for the absence of a constraint
Same here, let's just remove these
This isn't really testing anything anymore, I think we should just remove the whole class now
can we also assert there are two before the update is run?
thanks
Comment #27
smustgrave commented26.1 updated
26.2 updated test case
26.3 updated test case
26.4 removed the class BlockContentValidationTest
26.5 updated to not use those variables. But not sure I follow
Also updated the dump file to drupal-9.4.0.filled.standard.php.gz
And change the install hook number to be 10100
Comment #28
metasimThe #15 patch does not apply for core 9.5.0-rc1@RC
I tried doing a reroll to apply it on core version 9.5.0-rc1@RC and it doesnt account for changes on comment #26
Comment #29
metasimComment #30
metasimComment #31
smustgrave commentedThis ticket is being targeted for 10.1. If you want to upload a 9.5 patch you still can without changing the ticket. When uploading the patch there is a field "test with" you can select for the 9.5
Comment #32
metasimchanging the function block_content_update_9401() to function block_content_update_9501()
sorry for the spam, this is my first time :(
Comment #33
smustgrave commented0 problem at all. Some tips
When you upload a new patch it's best to provide an interdiff between the two see https://www.drupal.org/docs/develop/git/using-git-to-contribute-to-drupa... for help. This way we can see the changes between two easily.
Also to avoid CI errors you can run ./core/scripts/dev/commit-code-check.sh --cached with your staged files to have them tested.
Also have a great community involvement on the slack channel if you wished to join there also.
Comment #34
larowlanShould we assert the before state (i.e. that the constraint exists)?
Other than that, this looks good to me
Comment #35
lobsterr commentedI have fix conflicts and now we target 9.5. Should maybe switch to 10 ?
I also added check that constraint exists as it was proposed in #34
Comment #36
smustgrave commentedin #27 we started doing a D10 patch. If we are switching back to MRs we should open a separate one for D10. There were additional changes in #27 that should be incorporated, mainly additional testing.
Comment #37
lobsterr commentedok, Let's bring your changes to MR and we will target 10 version
Comment #39
lobsterr commentedI am sorry for the spam related to MR. I have decided eventually to close it.
1) I rerolled the patch again 10.1.x
2) Add check that constraint exists
3) Change the number of update hook
Comment #40
smustgrave commentedThanks for picking this up!
Per #34
Lets add a check before the runUpdates. Count should be 2 before the update.
Comment #41
lobsterr commentedComment #43
lobsterr commentedHm, I spent too much time on it and I couldn't figure out, why test fails and returns only one contraint :(
I see that we get correct list of constraints in normal Functional tests, but not with Update tests. It is the first time I face something like this. Please direct me here. What I am missing ?
Comment #44
smustgrave commentedWas also looking at it the other day and think I see why.
We are using drupal-9.4.0.filled.standard.php.gz but the update hook is 10200() wonder if it's not being called?
I'm not super clear if this change goes under update_hook_n or post_update hook. But if we moved to post_update then it should run fine on D9 and D10 I think.
Haven't tested this myself but just thinking.
Comment #45
lobsterr commentedAfter a deeper investigation: the problem is in drupal-9.4.0.filled.standard.php.gz.
it contains "block_content.field_storage_definitions" definition without any constraints and even if we try to use post_update, it will not work.
Since we want to be sure that our update hook works, I will check if "UniqueField" is there. If it is not there I will set it and then it will be removed in update hook. In the case with drupal-9.4.0.filled.standard.php.gz, it would be ok solution.
Also I updated a bit code to check that UniqueField is there
Comment #46
smustgrave commentedGreat find!
Comment #48
larowlanThis message is shown in the UI at /update.php.
Fixed on commit rather than push back on a simple change.
I checked if there were any database implications here, e.g. indexes to drop etc.
Also checked the status report to make sure there were no entity field definition updates listed, and there were not 🙌
But there are none
Published change record
Comment #50
fantonThe name of the function of the hook_update_N should have been:
block_content_update_10101instead of
block_content_update_10200because the core version is 10.1