Problem/Motivation

When a dynamic entity reference field is used as a base field, no index is added to the corresponding target_id_int column. This may be an issue for non-base fields as well, I haven't verified.

Proposed resolution

Add the index to the int column (and probably write an update hook).

Remaining tasks

User interface changes

API changes

Data model changes

Comments

jhedstrom created an issue. See original summary.

nick.james’s picture

I started to create a test for the update hook, however, I realized a new data snapshot would need to be generated after dynamic_entity_reference_update_8201 was run. The reason is the DER fields in the current snapshot do not have a target_id_int column in the database. Once dynamic_entity_reference_update_8201 is run, the field is added and the fix in this patch works as expected by adding an index on the new column. However, it prevents us from doing an isolated test to confirm that the update hook in this patch works as expected and creates the index on any existing DER fields that already have the target_id_int column.

I'm also not sure about all the test cases that need to covered for this change. But I thought I would at least provide the solution I have so far.

jhedstrom’s picture

+++ b/src/Storage/IntColumnHandler.php
@@ -78,8 +78,14 @@ abstract class IntColumnHandler implements IntColumnHandlerInterface {
+        $schema->addIndex($table, $column_int, [$column_int], $full_spec);

Should this index instead of just being on the `_int` column, be on the entity type and the int column (as the normal index for this field is on the target ID and on the entity type column)?

nick.james’s picture

StatusFileSize
new8.4 KB
nick.james’s picture

StatusFileSize
new7.68 KB
jibran’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 4: dynamic_entity_reference-add-index-for-int-columns-2927218-3.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

nick.james’s picture

jibran’s picture

Thanks, for working on this. We need tests for this change and we also need upgrade path tests for this.

+++ b/dynamic_entity_reference.install
@@ -123,3 +123,62 @@ function dynamic_entity_reference_update_8201() {
+          $spec = [
+            'fields' => [
+              $column => [
+                'type' => 'int',
+                'unsigned' => TRUE,
+                'not null' => FALSE,
+              ],
+              $index_column => $schema_info['columns']['target_type']
+            ]
+          ];

Can we please move the static part of this array outside the loops?

jhedstrom’s picture

jhedstrom’s picture

Status: Needs work » Needs review
StatusFileSize
new1.59 KB
new4.93 KB
new114.41 KB

This adds an update test, as well as 2 tests under the normal kernel tests to verify the index is created. I've attached those as a test-only patch as well. The interdiff does not contain the new starting db. (Note, to test the actual update hook, a new starting db was needed since if we started with the current update db, that actually adds the _int column, so the index is created during that update instead).

The last submitted patch, 11: 2927218-11-TEST-ONLY.patch, failed testing. View results

jibran’s picture

+++ b/tests/src/Functional/Update/DerUpdate8202Test.php
@@ -0,0 +1,52 @@
+ * Tests DER update path from 8201 and on.

Thanks, for this comment. Should we add assert in 8201 tests as well?

jibran’s picture

Can you please share the DB generation script as well?

jhedstrom’s picture

This is the db test script (I copied it from the other issue https://www.drupal.org/node/2555027#comment-11307815). The steps are the same:

  1. Install testing profile
  2. drush scr test.php
  3. php ./core/scripts/dump-database-d8-mysql.php --no-ansi > modules/dynamic_entity_reference/tests/fixtures/update/update_test_8202.php
  4. gzip modules/dynamic_entity_reference/tests/fixtures/update/update_test_8202.php
jhedstrom’s picture

jhedstrom’s picture

StatusFileSize
new2.25 KB
jhedstrom’s picture

StatusFileSize
new1.2 KB
new115.61 KB

This adds those assertions to the previous update test.

jhedstrom’s picture

StatusFileSize
new784 bytes
new115.74 KB

Oops, I missed one.

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, I'll have a look at FieldStorageSubscriber in detail after these changes and commit it.

.

  • jibran committed 2c71d8e on 8.x-2.x
    Issue #2927218 by jhedstrom, nick.james, jibran: No index is added for...
jibran’s picture

Status: Reviewed & tested by the community » Fixed
StatusFileSize
new2.88 KB

I reviewed the changes thank you @nick.james and @jhedstrom for finding and fixing this. Committed and pushed to 8.x-2.x. Fixed following CS issues on commit. I'll create a new release soon.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.