Problem/Motivation

The current implementation of views handler for dblog present several problems:

  1. wid and uid fields of the watchdog table are using numeric fields handlers: When a site has more than 1000 logs, the Logs id could use a separator to format the logs, for example 1,200. If this field is used in a link to link a log event this breaks the link. See: #2849797: dblog link broken with > 1000 watchdog entries
  2. The use of the in_operator filter handler for type filter makes impossible deprecate _dblog_get_message_types the (See #2458191: Provide a storage backend for dblog module)
  3. .

  4. The relationship between watchdog table and the user table is incorrect. This makes impossible to get for example the username of the author name that triggered an error.

The mentioned problems blocks the progress on several patches:

Proposed resolution

  • Update the numeric fields form the watchdog table (wid, and uid) to use the standard plugins.
  • Update the relationship of uid to join the users_field_data for more flexibility in watchdog based views.
  • Update the watchdog.types filter from in_operator with a callback to a custom filter.

Also the dblog module don't have a good test coverage for views integration (for example filters are not tested). It would be nice to include some integral test coverage for views.

The upgrade path required for this patch includes two steps.

  1. Use a hook_update_N to update all the views that use the watchdog as base table.
  2. Provide a hook_entity_type_presave hook to update contrib modules that provide their own watchdog based views. (See #2870724: Define and document best practices for configuration entity updates/bc layers)

Remaining tasks

  • Write the upgrade path. done
  • Write tests for the upgrade path. done
  • Extend functional tests for views done

User interface changes

wid and uid fields will not provide anymore settings related to numeric formatting.

API changes

None.

Data model changes

None.

CommentFileSizeAuthor
#102 interdiff-2851293-93-102.txt2.17 KBdagmar
#102 2851293-102.patch38.66 KBdagmar
#99 2851293-99.patch16.83 KBakashkrishnan01
#93 interdiff-2851293-86-93.txt610 bytesdagmar
#93 2851293-93.patch38.69 KBdagmar
#92 screenshot-d8.dev-2017-04-30-09-40-18.png564.22 KBjibran
#86 2851293-85.patch38.97 KBdagmar
#85 interdiff-2851293-80-85.patch.txt5.16 KBdagmar
#85 interdiff-2851293-80-85.patch.txt5.16 KBdagmar
#80 2851293-80-hook_update_n.patch35.75 KBdagmar
#80 2851293-80-post-update.patch35.65 KBdagmar
#80 interdiff-2851293-80-hook_update_N-hook_post_update.txt2.32 KBdagmar
#75 interdiff-2851293-71-75.txt615 bytesdagmar
#75 2851293-75.patch35.75 KBdagmar
#71 interdiff-2851293-68-71.txt2.31 KBdagmar
#71 2851293-71.patch35.68 KBdagmar
#68 interdiff-2851293-66-68.txt597 bytesdagmar
#68 2851293-68.patch35.31 KBdagmar
#66 interdiff-2851293-58-60.txt2.48 KBdagmar
#66 2851293-60.patch35.32 KBdagmar
#58 2851293-58.patch34.81 KBdagmar
#58 interdiff-2851293-56-58.txt1.25 KBdagmar
#56 2851293-53.patch34.87 KBdagmar
#56 interdiff-2851293-49-53.txt3.72 KBdagmar
#49 interdiff-2851293-47-49.txt466 bytesdagmar
#49 2851293-49.patch34.85 KBdagmar
#47 2851293-47.patch34.86 KBdagmar
#47 interdiff-2851293-45-47.txt6.01 KBdagmar
#45 40-45-interdiff.txt3.3 KBalexpott
#45 2851293-45.patch33.7 KBalexpott
#40 interdiff-2851293-37-40.txt1.23 KBdagmar
#40 2851293-40.patch33.67 KBdagmar
#37 interdiff36_37.txt851 bytesMunavijayalakshmi
#37 2851293-37.patch33.65 KBMunavijayalakshmi
#36 interdiff-2851293-34-36.txt889 bytesdagmar
#36 2851293-36.patch33.65 KBdagmar
#34 interdiff-2851293-32-34.txt2.74 KBdagmar
#34 2851293-34.patch33.76 KBdagmar
#32 interdiff-2851293-29-32.txt1.63 KBdagmar
#32 2851293-32.patch33.59 KBdagmar
#29 2851293-29.patch33.72 KBdagmar
#29 interdiff-2851293-26-29.txt2.4 KBdagmar
#26 interdiff-2851293-25-26.txt610 bytesdagmar
#26 2851293-26.patch33.3 KBdagmar
#25 interdiff-2851293-22-25.txt624 bytesdagmar
#25 2851293-25.patch32.89 KBdagmar
#22 interdiff-2851293-20-22.txt579 bytesdagmar
#22 2851293-22.patch33.79 KBdagmar
#21 interdiff-2851293-18-20.txt684 bytesdagmar
#20 2851293-20.patch33.82 KBdagmar
#18 interdiff-2851293-16-18.txt1.53 KBdagmar
#18 2851293-18.patch33.78 KBdagmar
#16 2851293-16.patch33.84 KBdagmar
#13 interdiff-2851293-11-13.txt880 bytesdagmar
#13 2851293-13.patch46.35 KBdagmar
#11 2851293-11.patch46.35 KBdagmar
#5 2851293-5.patch23.59 KBdagmar
#5 interdiff-2851293-2-5.txt4.77 KBdagmar
#2 2851293-2.patch24.96 KBdagmar
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dagmar created an issue. See original summary.

dagmar’s picture

Status: Active » Needs review
FileSize
24.96 KB

An initial patch.

dagmar’s picture

Title: Upgrade views handlers and relathionships for dblog » Upgrade views handlers and relationships for dblog

Status: Needs review » Needs work

The last submitted patch, 2: 2851293-2.patch, failed testing.

dagmar’s picture

Status: Needs work » Needs review
FileSize
4.77 KB
23.59 KB
-    // The uid relathionship should now join to the {users} table.
-    $tables = array_keys($view->getBaseTables());
-    $this->assertTrue(in_array('users', $tables));
-    $this->assertTrue(in_array('watchdog', $tables));
-
     $this->runUpdates();

It seems we cannot test the view with the previous values defined in the hook_views_data. Some advice from the views maintainers here?

Lendude’s picture

@dagmar since the relationship isn't changed in the post_update hook there is no need to test the change in the post_update test. A normal (non-update) test that checks that relationship might be nice.

bit of quick nitpicking:

  1. +++ b/core/modules/dblog/dblog.post_update.php
    @@ -0,0 +1,77 @@
    +/**
    + * @file
    + * Post update functions for the Database Logging module.
    + */
    

    I don't think we do @file anymore?

  2. +++ b/core/modules/dblog/dblog.post_update.php
    @@ -0,0 +1,77 @@
    + * Change the table relationship of user for watchdog. Use standard plugin for
    

    The relationship isn't changed in the post update hook.

  3. +++ b/core/modules/dblog/dblog.post_update.php
    @@ -0,0 +1,77 @@
    +          // We are only interested in wid and uid fields from the watchdog
    +          // table that still use the numeric id
    

    missing period at the end

  4. +++ b/core/modules/dblog/dblog.views.inc
    @@ -39,9 +47,8 @@ function dblog_views_data() {
    -    'help' => t('The user ID of the user on which the log entry was written..'),
    

    do we want to remove this?

jibran’s picture

Issue tags: +VDC
dawehner’s picture

Issue tags: -VDC

It seems we cannot test the view with the previous values defined in the hook_views_data. Some advice from the views maintainers here?

To be honest I'm personally not convinced by testing things before running update.php. Accessing stuff can cause side effects, like maybe some caching of schema in this issue.

xjm’s picture

I think the removal of the VDC tag was an xpost. Tagging for subsystem maintainer review also to make sure @dawehner's comments in #8 are taken into consideration before this might be committed.

Lendude’s picture

Accessing stuff can cause side effects, like maybe some caching of schema in this issue.

Uhh if running update.php doesn't clear those caches, isn't that something we would want to detect? Cause that sounds like trouble to me :)

dagmar’s picture

Thanks everyone. I added the code to cover the change in filters.

@dagmar since the relationship isn't changed in the post_update hook there is no need to test the change in the post_update test. A normal (non-update) test that checks that relationship might be nice.

Included in this patch.

To be honest I'm personally not convinced by testing things before running update.php. Accessing stuff can cause side effects, like maybe some caching of schema in this issue.

I removed that code, we can add it again after more discussion.

Status: Needs review » Needs work

The last submitted patch, 11: 2851293-11.patch, failed testing.

dagmar’s picture

Status: Needs work » Needs review
FileSize
46.35 KB
880 bytes
dagmar’s picture

Issue tags: +blocker
dagmar’s picture

Assigned: Unassigned » dagmar
Status: Needs review » Needs work

We need to include the conversion of the types into a new custom handler. See #2458191-66: Provide a storage backend for dblog module

dagmar’s picture

Title: Upgrade views handlers and relationships for dblog » Upgrade dblog views integration
Status: Needs work » Needs review
Parent issue: » #2015149: Replace dblog recent log entries with a view
Related issues: +#2458191: Provide a storage backend for dblog module
FileSize
33.84 KB

No interdiff sorry I renamed a lot of files.

This patch do 3 things:

This patch makes really simple changes, the complexity is on the tests (and maybe in the upgrade path).

Status: Needs review » Needs work

The last submitted patch, 16: 2851293-16.patch, failed testing.

dagmar’s picture

Status: Needs work » Needs review
FileSize
33.78 KB
1.53 KB

Status: Needs review » Needs work

The last submitted patch, 18: 2851293-18.patch, failed testing.

dagmar’s picture

Status: Needs work » Needs review
FileSize
33.82 KB

I tested this manually, this should work.

dagmar’s picture

FileSize
684 bytes

I forgot the interdiff.

dagmar’s picture

And this should remove the other error from #18.

The last submitted patch, 20: 2851293-20.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 22: 2851293-22.patch, failed testing.

dagmar’s picture

Status: Needs work » Needs review
FileSize
32.89 KB
624 bytes

Re-rolled after the array() -> [] conversion.

The interdiff shows the relevant change.

dagmar’s picture

Woops, I forgot a part of a file.

The last submitted patch, 25: 2851293-25.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 26: 2851293-26.patch, failed testing.

dagmar’s picture

Status: Needs work » Needs review
FileSize
2.4 KB
33.72 KB
dagmar’s picture

Issue summary: View changes
dagmar’s picture

dagmar’s picture

Lendude’s picture

Manually tested this and the changes are good and the update works nicely.

  1. +++ b/core/modules/dblog/dblog.views.inc
    @@ -39,9 +47,9 @@ function dblog_views_data() {
    +    'help' => t('The user ID of the user on which the log entry was written.'),
    

    Unrelated change

  2. +++ b/core/modules/dblog/src/Plugin/views/filter/DblogTypes.php
    @@ -0,0 +1,24 @@
    +      $this->valueOptions = _dblog_get_message_types();
    

    This seems very strange to me. This is great for the exposed filter, but for a non-exposed filter this makes no sense. This means when I'm building the View, the type of message I want to filter on, needs to be present in the database at that time. But it seems that was how this previously worked too, so this is not the place to change this behaviour probably.

  3. +++ b/core/modules/dblog/src/Tests/Update/DblogUpgrade2851293.php
    @@ -0,0 +1,47 @@
    +class DblogUpgrade2851293 extends UpdatePathTestBase {
    

    Class name can be a bit more descriptive and needs to end in Test (I think)

  4. +++ b/core/modules/dblog/src/Tests/Update/DblogUpgrade2851293.php
    @@ -0,0 +1,47 @@
    +    // type should use the dblog_types plugin now.
    

    Start with a capital letter or quote type.

  5. +++ b/core/modules/dblog/tests/src/Kernel/Views/ViewsIntegrationTest.php
    @@ -40,19 +41,137 @@ protected function setUp($import_test_views = TRUE) {
    +    $entries = $this->createRandomLogEntries();
    ...
    +    $this->createRandomLogEntries();
    ...
    +  protected function createRandomLogEntries() {
    

    Why random? why not use fixed test parameters?

  6. +++ b/core/modules/dblog/tests/src/Kernel/Views/ViewsIntegrationTest.php
    @@ -40,19 +41,137 @@ protected function setUp($import_test_views = TRUE) {
    +    $this->container->get('router.builder')->rebuild();
    

    not needed

  7. +++ b/core/modules/dblog/tests/src/Kernel/Views/ViewsIntegrationTest.php
    @@ -40,19 +41,137 @@ protected function setUp($import_test_views = TRUE) {
    +    $this->container->get('router.builder')->rebuild();
    

    not needed

dagmar’s picture

Thanks for the review @Lendude!

1. Agree, but in my opinion is ok to fix this here. We are modifying a lots of entries of the same file and that will leave the hook in good shape.

2. Logs types are dynamic, it seems this is the only way to proceed.

3. I renamed it to DblogFiltersAndFieldsUpgradeTest.

4. Fixed.

5. The code of that method was originally present in the test, I abstracted that part of the code into a new method. There are a lot of work to improve the log generation for tests (See #2848914: Move DbLogTest::generateLogEntries() into a Trait]) For now I just renamed the method to createLogEntries.

6/7. Fixed.

Lendude’s picture

1. Yeah I think it's fine sneaking it in here.

2. For the non-exposed filter a string filter would make more sense to me, but since we can't vary by exposed/non-exposed and this was how it was working before, so yeah lets stick with this.

5. Yeah that follow up sounds like the way to go, so this looks good for now.

6/7:

+++ b/core/modules/dblog/tests/src/Kernel/Views/ViewsIntegrationTest.php
@@ -18,41 +18,162 @@
+    $this->container->get('router.builder')->rebuild();
...
+    $this->container->get('router.builder')->rebuild();

You got one that I missed, and missed two that I pointed to :)

So just 6/7 and then this looks ready to me.

dagmar’s picture

@Lendude Thanks!

Munavijayalakshmi’s picture

+++ b/core/modules/dblog/tests/src/Kernel/Views/ViewsIntegrationTest.php
@@ -76,41 +195,28 @@ public function testIntegration() {
+    // Setup a watchdog entry with severity WARNING
...
+    // Setup a watchdog entry with a different module

Comments should (noramlly) begin with a capital letter and end with a full stop / period .

Fixed and attached new patch.

Lendude’s picture

Status: Needs review » Reviewed & tested by the community

This looks ready to me. Nice work!

tstoeckler’s picture

Status: Reviewed & tested by the community » Needs work

Coder is not happy with this one:

core/modules/dblog/dblog.post_update.php
line 1 Missing file doc comment
core/modules/dblog/tests/src/Kernel/Views/ViewsIntegrationTest.php
30 Line indented incorrectly; expected 2 spaces, found 3

Also:

+++ b/core/modules/dblog/dblog.post_update.php
@@ -0,0 +1,72 @@
+ * Use standard plugin for wid and uid fields for watchdog table, and the
+ * dblog_types plugin for type filter.

This should be shortened so it fits in one line.

dagmar’s picture

Status: Needs work » Needs review
FileSize
33.67 KB
1.23 KB

Thanks @tstoeckler.

tstoeckler’s picture

Status: Needs review » Reviewed & tested by the community

Thank you, perfect!

jibran’s picture

Some coding improvements. Should we wait for @dawehner here so that we can remove the subsystem review tag?

  1. +++ b/core/modules/dblog/dblog.post_update.php
    @@ -0,0 +1,76 @@
    +            $save = TRUE;
    ...
    +            $save = TRUE;
    

    Why not $save |= TRUE?

  2. +++ b/core/modules/dblog/dblog.post_update.php
    @@ -0,0 +1,76 @@
    +            // Delete all the attributes related to numeric fields.
    +            foreach ($numeric_attributes_to_delete as $key) {
    +              if (isset($new_value[$key])) {
    +                unset($new_value[$key]);
    +              }
    

    We don't need if here. Just unset($new_value[$key]); is fine.
    This can all be done in one line. unset($arr[key1], $arr[key2], ...);

  3. +++ b/core/modules/dblog/src/Plugin/views/filter/DblogTypes.php
    @@ -0,0 +1,24 @@
    +      $this->valueOptions = _dblog_get_message_types();
    

    Let's inject the query service and execute the query here.

dagmar’s picture

Thanks @jibran.

Should we wait for @dawehner here so that we can remove the subsystem review tag?

In #11 I addressed the concerns from @dawehner. So I think is ok to remove the Needs subsystem maintainer review tag.

Let's inject the query service and execute the query here.

Actually, this will be replaced by the new dblog StorageBackend when #2458191: Provide a storage backend for dblog module be ready. So, this is not the right approach. We are trying to avoid sql queries from outside the StorageBackend.

alexpott’s picture

Well @Lendude is also a subsystem maintainer but yeah only a subsystem maintainer should remove that tag. Given that @Lendude rtbc-d the patch I assume he is happy with it but I'd still rather wait for confirmation.

alexpott’s picture

  1. Some tiny nits that would be nice if they were addressed. Not going to needs work for this. Nits patch attached
  2. +++ b/core/modules/dblog/tests/src/Kernel/Views/ViewsIntegrationTest.php
    @@ -18,41 +18,160 @@
    +        'plugin_id' => 'in_operator',
    +      ]
    +    ];
    ...
    +        'plugin_id' => 'dblog_types',
    +      ]
    +    ];
    

    Strictly there should be a comma after the ]

  3. +++ b/core/modules/dblog/tests/src/Kernel/Views/ViewsIntegrationTest.php
    @@ -18,41 +18,160 @@
    +   * Create a set of log entries.
    +   * @return array
    

    New line in between the first line and the @return.

  4. +++ b/core/modules/dblog/tests/src/Kernel/Views/ViewsIntegrationTest.php
    @@ -76,41 +195,28 @@ public function testIntegration() {
    +      'type' => 'my-module'
    

    Need comma at the end.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Discussed with @catch - we think we need to fix this using hook_ENTITY_TYPE_presave() so that default views supplied by contrib and custom modules can be updated too. This means the hook_update_N() needs to determine which views need to be saved again and save them.

dagmar’s picture

Status: Needs work » Needs review
FileSize
6.01 KB
34.86 KB

Thanks @alexpott for the review and the new patch. The proposed approach discussed by you and @catch seems reasonable. Here is the new patch that use the hook_ENTITY_TYPE_presave.

Status: Needs review » Needs work

The last submitted patch, 47: 2851293-47.patch, failed testing.

dagmar’s picture

Status: Needs work » Needs review
FileSize
34.85 KB
466 bytes
jibran’s picture

Category: Task » Bug report
Related issues: +#1874534: Introduce a dblog / watchdog views integration

I disagree with this direction hook_ENTITY_TYPE_presavewill be called whenever a view is saved which is not ideal imo. The view integration is introduced in #1874534: Introduce a dblog / watchdog views integration. I doubt that contrib is using this but that's beside the point. We never cared about the default views but we do update the existing views which this patch was already fixing. Updating the config object in preSave means we are creating a mismatch between the default and saved config which is not right.
Consider the other side of the picture for a moment not fixing the default views would show the error after importing and views should have to be updated and saved and that's what we actually want so that config can be updated properly.

If we still want to go down preSave road then every time we change the existing views data we have to write the preSave for the respective module which is not ideal. If BC break is the issue then we should consider keeping the original mapping as is and we should consider the new mapping so that contrib can catch up then we can even nuke the update hook altogether. Thoughts?

+++ b/core/modules/dblog/dblog.views.inc
@@ -20,11 +20,19 @@ function dblog_views_data() {
-      'id' => 'numeric',
+      'id' => 'standard',

@@ -39,9 +47,9 @@ function dblog_views_data() {
-      'id' => 'numeric',
+      'id' => 'standard',

@@ -52,7 +60,7 @@ function dblog_views_data() {
-      'base' => 'users',
+      'base' => 'users_field_data',

These changes qualify this patch as a bug.

jibran’s picture


  $data['watchdog']['uid'] = [
    'title' => t('UID'),
    'help' => t('The user ID of the user on which the log entry was written..'),
    'field' => [
      'id' => 'numeric',
    ],
    'filter' => [
      'id' => 'numeric',
    ],
    'argument' => [
      'id' => 'numeric',
    ],
    'relationship' => [
      'title' => t('User'),
      'help' => t('The user on which the log entry as written.'),
      'base' => 'users',
      'base field' => 'uid',
      'id' => 'standard',
    ],
  ];

We have a relationship defined with users table. Logically speaking it is possible to create a reverse relationship and use the watchdog data in any view so I don't think checking watchdog as a base table is the right thing to do here. Do we care about this or not?

jibran’s picture

Title: Upgrade dblog views integration » dblog_views_data is using the wrong views field pluigns
Status: Needs review » Needs work

Let's update the title as well.

+++ b/core/modules/dblog/dblog.views.inc
@@ -20,11 +20,19 @@ function dblog_views_data() {
+  $data['watchdog']['table']['join'] = [
+    // Link ourselves to the {users_field_data} table.
+    'users_field_data' => [
+      'field' => 'uid',
+      'left_field' => 'uid',
+    ],
+  ];

@@ -52,7 +60,7 @@ function dblog_views_data() {
     'relationship' => [
       'title' => t('User'),
       'help' => t('The user on which the log entry as written.'),
-      'base' => 'users',
+      'base' => 'users_field_data',
       'base field' => 'uid',
       'id' => 'standard',

If we are doing former then we don't need latter. Every time users_field_data table would be available in views the former would show the watchdog fields as well. If we remove latter then it is a BC break even it is not changing the views field plugin id but we are changing the field existing relationship and we need to update those fields as well in post-update hook.

alexpott’s picture

@jibran we've used preSave() before to fix default views - and we do care about default views - #2248983: Define the revision metadata base fields in the entity annotation in order for the storage to create them only in the revision table

jibran’s picture

It's sad that no one from VDC has reviewed that patch or +1ed the approach :(

Lendude’s picture

@jibran, @dawehner and I went into that approach here #2784233: Allow multiple vocabularies in the taxonomy filter and I 'ranted' about it some more here #2810097: Allow views to provide the canonical entity URL of all entities, not just nodes..

Summary: I'm not happy with the approach, but I couldn't find anything better (tried some things in #2784233: Allow multiple vocabularies in the taxonomy filter). We need an post_module_install event that fires after every module install or something.

dagmar’s picture

Title: dblog_views_data is using the wrong views field pluigns » dblog_views_data is using the wrong views field and filter plugins
Status: Needs work » Needs review
FileSize
3.72 KB
34.87 KB

We have a relationship defined with users table. Logically speaking it is possible to create a reverse relationship and use the watchdog data in any view so I don't think checking watchdog as a base table is the right thing to do here. Do we care about this or not?

In my opinion, we should not encourage users to join watchdog table from other tables. I cannot image a user case for that. I know this is not 100% related but there is a mention regarding the eventual use of foreign keys to watchdog here: https://www.drupal.org/node/2494221#comment-9957997

Anyway if we need to do that in the future, we can add it without break the BC. The thing right now is that using {user} is incorrect because all the actual data of the user actually lives in {users_field_data}.

Status: Needs review » Needs work

The last submitted patch, 56: 2851293-53.patch, failed testing.

dagmar’s picture

Status: Needs work » Needs review
FileSize
1.25 KB
34.81 KB
catch’s picture

+++ b/core/modules/dblog/dblog.module
@@ -115,3 +116,67 @@ function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $fo
+
+          // Delete all the attributes related to numeric fields.
+          foreach ($numeric_attributes_to_delete as $key) {
+            if (isset($new_value[$key])) {
+              unset($new_value[$key]);
+            }
+          }

Ideally we'd check if these need deleting before unsetting them, then @trigger_error('...', E_USER_DEPRECATED); when they're found. That way at least once we have phpunit integration with deprecations we'll be able to inform contrib modules easier.

https://www.drupal.org/core/deprecation

dagmar’s picture

@catch sorry, I don't understand your point. I'm removing those values not because they are deprecated but because they are not part of the standard plugin options. Those values are still valid for numeric fields and indicate they are deprecated may be confusing, don't you think?

catch’s picture

Oh I missed that code is just moving around rather than related to the upgrade path.

The deprecation notice could happen here.

+++ b/core/modules/dblog/dblog.module
@@ -115,3 +116,72 @@ function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $fo
+
+    // Iterate all filters looking for type filters to update.
+    if (isset($display['display_options']['filters'])) {
+      foreach ($display['display_options']['filters'] as $filter_name => $filter) {
+        if (isset($filter['table']) &&
+            $filter['table'] === 'watchdog' &&
+            $filter['plugin_id'] == 'in_operator' &&
+            $filter['field'] == 'type') {
+
+          $filter['plugin_id'] = 'dblog_types';
+          $view->set("display.$display_name.display_options.filters.$filter_name", $filter);
+        }
+      }

On the question of using hook_ENTITY_TYPE_presave() for this:

It's not a case of caring about default views as such, it's about default configuration in general, and having a standard way to handle this for backwards compatibility.

Using hook_ENTITY_TYPE_presave() looks heavy-handed, but I don't think it's that bad really:

- generally it's a couple of quick if checks
- it allows us to catch any case where a configuration entity might be saved via the API - i.e. created in hook_install() or manually using $entity->save() in test coverage and similar.
- we can remove all this code in 9.0.x

However it'd be good to make it clearer which bit is the bc-layer and which bit isn't, so people don't make the same mistake I just did.

edited to add:
While this has been discussed a few times (especially on the two issues that Lendude linked), the docs on https://www.drupal.org/docs/8/api/update-api/updating-configuration-in-d... don't mention this at all, so we should probably open a new task to update that documentation with the presave approach. While it's not perfect given it's only used for cases that need bc layers I do think it's OK if we document it better (or at least we should open a new generic issue for config entity updates/bc to try to replace the pattern with something better).

Status: Needs review » Needs work

The last submitted patch, 58: 2851293-58.patch, failed testing.

xjm’s picture

So looks like we have input here from the dblog subsystem maintainer (thanks @dagmar!), a Views maintainer (thanks @Lendude), and a Configuration system maintainer (@alexpott). I think the concern here is around the correct approach for the upgrade path? In #55 @Lendude says it seems nonideal but also that they didn't come up with anything better.

catch’s picture

I've opened #2870724: Define and document best practices for configuration entity updates/bc layers to discuss the upgrade path approach more generally.

Lendude’s picture

@catch++
thanks for the great summary and opening the issue.

@xjm++
And yes, hook_ENTITY_TYPE_presave() certainly beats doing nothing.

Took the 'Needs subsystem maintainer' tag off, @dawehners concern was with doing assertions before running updates in the update path test, and we are not doing that currently, so I assume that's ok.

dagmar’s picture

Status: Needs work » Needs review
FileSize
35.32 KB
2.48 KB

Thanks everyone. This should fix the remaining test. Thanks @Berdir for the hints

(12:54:19) berdir: dagmar_drupal: what exactly is $view? config entities do not support the . syntax like config objects do?
(12:54:41) berdir: dagmar_drupal: if you did a corresponding set() before, then you likely just wrote a bogus top level property with exactly that name
dawehner’s picture

(12:54:19) berdir: dagmar_drupal: what exactly is $view? config entities do not support the . syntax like config objects do?

OOOH, interesting, I had no idea.

  1. +++ b/core/modules/dblog/dblog.install
    @@ -90,3 +92,20 @@ function dblog_schema() {
    +  \Drupal::service('module_handler')->resetImplementations();
    

    You can actually use \Drupal::moduleHandler here ...

  2. +++ b/core/modules/dblog/dblog.module
    @@ -115,3 +116,75 @@ function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $fo
    +    $view->set('display', $displays);
    

    Really good to know!

dagmar’s picture

Thanks @dawehner

dagmar’s picture

I think all the concerns (#67 and #59) has been addressed. Who want to move this back to RBTC?

alexpott’s picture

  1. +++ b/core/modules/dblog/dblog.module
    @@ -115,3 +116,75 @@ function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $fo
    +  $numeric_attributes_to_delete = [
    +    'set_precision',
    +    'precision',
    +    'decimal',
    +    'separator',
    +    'format_plural',
    +    'format_plural_string',
    +    'prefix',
    +    'suffix',
    +  ];
    ...
    +          // Delete all the attributes related to numeric fields.
    +          foreach ($numeric_attributes_to_delete as $key) {
    +            if (isset($field[$key])) {
    +              unset($displays[$display_name]['display_options']['fields'][$field_name][$key]);
    +            }
    +          }
    

    This could be less complex... just

    unset(
      $displays[$display_name]['display_options']['fields'][$field_name]['set_precision'],
      $displays[$display_name]['display_options']['fields'][$field_name]['precision'],
      $displays[$display_name]['display_options']['fields'][$field_name]['decimal'],
      $displays[$display_name]['display_options']['fields'][$field_name]['separator'],
      $displays[$display_name]['display_options']['fields'][$field_name]['format_plural'],
      $displays[$display_name]['display_options']['fields'][$field_name]['format_plural_string'],
      $displays[$display_name]['display_options']['fields'][$field_name]['prefix'],
      $displays[$display_name]['display_options']['fields'][$field_name]['suffix']
    );
    

    No loops, no ifs... unset doesn't fail if the thing doesn't exist and can do multiple at once.

  2. +++ b/core/modules/dblog/dblog.module
    @@ -115,3 +116,75 @@ function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $fo
    +            in_array($field['field'], ['wid', 'uid'])) {
    

    Should set strict to TRUE... its a third argument.

dagmar’s picture

Thanks @alexpott

dagmar’s picture

(17:38:58) alexpott: dagmar_drupal: as xjm suggests dawehner is an excellent final reviewer - he often often offers review swaps for a patch review on anyone else's issue. (One of the many reason's why dawehner is awesome)

@dawehner this is all yours :)

dawehner’s picture

  1. +++ b/core/modules/dblog/dblog.module
    @@ -115,3 +116,70 @@ function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $fo
    +/**
    + * Implements hook_ENTITY_TYPE_presave() for views entities.
    + *
    + * This hook ensures there are no views based that are using a wrong plugin for
    + * wid and uid fields on views that use watchdog as base table.
    + */
    +function dblog_view_presave(ViewEntityInterface $view) {
    

    Do you mind adding some line of documention that this should be removed in Drupal 9 again?

  2. +++ b/core/modules/dblog/src/Plugin/views/filter/DblogTypes.php
    @@ -0,0 +1,24 @@
    +   * {@inheritdoc}
    +   */
    +  public function getValueOptions() {
    +    if (!isset($this->valueOptions)) {
    +      $this->valueOptions = _dblog_get_message_types();
    +    }
    +    return $this->valueOptions;
    

    you could just use an options callback instead of introducing a PHP class, see \Drupal\views\Plugin\views\filter\InOperator::getValueOptions

jibran’s picture

May I ask why we moved from post-update hook to update hook?

dagmar’s picture

Thanks @dawehner!

you could just use an options callback instead of introducing a PHP class, see \Drupal\views\Plugin\views\filter\InOperator::getValueOptions

We can, but I'm deliberate doing this because we are trying to deprecate _dblog_get_message_types and the options callback doesn't support the use of services.

May I ask why we moved from post-update hook to update hook?

@jibran sure! So, hook_update_N is used to fix drupal from a broken state, and post update hooks are used to create content after drupal was fixed . In this case seems appropriated to use hook_update_N for this tasks.

jibran’s picture

AFAICS there are changes in views plugins and there is no schema change so Drupal is not broken only certain views are broken. Moreover, use of entity API is not a good idea in update hook that's why we introduced the post-update hooks so IMO we should use a post-update hook.

dagmar’s picture

@jbrian I understand​ your point, however in my opinion we should use this hook_update_N here just because we can.

If in the future we break the functionality, the tests in this patch will let us know. And then we can decide if this definitely needs to live in a post update hook or there's another way to fix the problem.

This is the first time we use this approach, and i think there is room to experiment with this approach.

dawehner’s picture

We can, but I'm deliberate doing this because we are trying to deprecate _dblog_get_message_types and the options callback doesn't support the use of services.

What about making this possible? If you use controller_resolver to make this possible.

Moreover, use of entity API is not a good idea in update hook that's why we introduced the post-update hooks so IMO we should use a post-update hook.

I agree, we should really not use the config entity API, it can causes arbitrary issues you can't see upfront.

dagmar’s picture

What about making this possible? If you use controller_resolver to make this possible.

We still don't have the code to replace this. This is part of #2458191: Provide a storage backend for dblog module. Honestly think we should leave this as is.

I agree, we should really not use the config entity API, it can causes arbitrary issues you can't see upfront.

So, we have a problem here. Core maintainers are suggesting we should use hook_update_N (see #46), but there are other arguments (supported by subsystem maintainer and top core contributor :) ) that indicate this is not a good approach.

What do we want to do here? @alexpott, @xjm?

dagmar’s picture

I'm providing the two approaches here and let core maintainers decide which is the best option. As I said in in #77 the fact that using hook_update_N makes this work and the fact that we have test coverage that ensure this will not break in the future, is enough to go with the hook_update_N approach.

But, what @jibran said regarding the hook schema makes sense too. So both patches with the same functionality and different implementations.

I would like to propose move this to RBTC if there are no other objections and let core maintainers decide which alternative is best.

Status: Needs review » Needs work

The last submitted patch, 80: 2851293-80-hook_update_n.patch, failed testing.

dagmar’s picture

Status: Needs work » Needs review
dawehner’s picture

@dagmar and @dawehner discussed about this specific issue:

We agreed on trying out the following idea:

* use hook_update_N
* use config api
* call to dblog_view_presave() directly

This ensure we don't use the entity api, while still working early.

dagmar’s picture

Status: Needs review » Needs work
dagmar’s picture

Status: Needs work » Needs review
FileSize
5.16 KB
5.16 KB

I spoke with @dawehner and it seems simpler to have a bit of code duplication instead of doing some weird conversion that could eventually stop working in the future.

dagmar’s picture

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

I agree that code duplication here is not a bad thing. Its a once thing, which probably will not be adapted. If we want to adapt it we probably should rebuild it.

dagmar’s picture

Title: dblog_views_data is using the wrong views field and filter plugins » dblog is using the wrong views field, filter and relathionships definitions
Issue summary: View changes

Based in my experience during the live commit in Baltimore, I have a better understanding of the necessity of a detailed issue summary. I hope my changes now make easier to review this patch.

dagmar’s picture

Title: dblog is using the wrong views field, filter and relathionships definitions » dblog is using the wrong views field, filter and relationships definitions
jibran’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/modules/dblog/dblog.views.inc
@@ -20,11 +20,19 @@ function dblog_views_data() {
+  $data['watchdog']['table']['join'] = [
+    // Link ourselves to the {users_field_data} table.
+    'users_field_data' => [
+      'field' => 'uid',
+      'left_field' => 'uid',
+    ],
+  ];

This is actually not a bug and not needed at all. Given that we already have a relationship to user_field_data this adds confusion.
The relationship shows the user details of the log entry. Whereas this doesn't make sense why in a user view we want to show its log entries by default.

dagmar’s picture

@jibran this is actually a bug. If you try to display the username of the user that triggered a log, you can't.

jibran’s picture

Status: Needs review » Needs work
FileSize
564.22 KB

That's not true.
Please see following:

dagmar’s picture

Status: Needs work » Needs review
FileSize
38.69 KB
610 bytes

We have a quick IRC conversation with @jibran and now I understand what he was saying. So the thing is, with this change:

     'relationship' => [
       'title' => t('User'),
       'help' => t('The user on which the log entry as written.'),
-      'base' => 'users',
+      'base' => 'users_field_data',

This the following code is not neccesary:

+  $data['watchdog']['table']['join'] = [
+    // Link ourselves to the {users_field_data} table.
+    'users_field_data' => [
+      'field' => 'uid',
+      'left_field' => 'uid',
+    ],
+  ];
+

My comment in #91 was because I thought this was the default behavior without applying the patch. Thanks @jibran!

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Thank you! @dagmar for fixing that.

+++ b/core/modules/dblog/tests/modules/dblog_test_views/test_views/views.view.dblog_integration_test.yml
@@ -0,0 +1,505 @@
+        name:

+++ b/core/modules/dblog/tests/src/Kernel/Views/ViewsIntegrationTest.php
@@ -18,41 +18,161 @@
+    // The uid relationship should now join to the {users_field_data} table.
+    $tables = array_keys($view->getBaseTables());
+    $this->assertTrue(in_array('users_field_data', $tables));
+    $this->assertFalse(in_array('users', $tables));

We already have a name field in the view and we have these asserts so we already have a test coverage for the bug described in #91. Back to RTBC.

dagmar’s picture

Issue summary: View changes

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 93: 2851293-93.patch, failed testing.

dagmar’s picture

Status: Needs work » Reviewed & tested by the community

Unrelated fail. Back to RBTC.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record
  1. This looking great. we just need a change record and for the deprecation notices to link to it (as per https://www.drupal.org/core/deprecation)
  2. +++ b/core/modules/dblog/dblog.module
    @@ -115,3 +116,72 @@ function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $fo
    + * @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
    

    Should link to the change record.

  3. +++ b/core/modules/dblog/dblog.module
    @@ -115,3 +116,72 @@ function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $fo
    +          @trigger_error("The numeric plugin for watchdog.$field_name field is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Must use standard plugin instead", E_USER_DEPRECATED);
    ...
    +          @trigger_error("The in_operator plugin for watchdog.type filter is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Must use dblog_types plugin instead", E_USER_DEPRECATED);
    

    These should point to the change record.

    It's great having these deprecation notices here because it means we'll know to remove it for 9.x

akashkrishnan01’s picture

patch has been re-rolled, points have been changed as alexpott mentioned

akashkrishnan01’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 99: 2851293-99.patch, failed testing.

dagmar’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record
FileSize
38.66 KB
2.17 KB

@akashkrishnan it seems you forgot to add half part of the patch. Please read the patch documentation it seems you missed step 9. And also remember to provide an interdiff

Change record added: https://www.drupal.org/node/2876378

jibran’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, @dagmar. I have updated some wording and title for the change notice.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed ef0fe5d and pushed to 8.4.x. Thanks!

I've credited @jibran, @Lendude, @dawehner and @catch for reviewing the patch. @xjm and @tstoeckler thank you for your comments.

@akashkrishnan thanks for trying to address #98 in #99. I've not credited you because you didn't provide an interdiff and your patch was half formed. It's good practice to review your patches before uploading. A good indication that something has gone wrong when you are only making minor changes is patch size. @dagmar has provided you useful links. Thanks for contributing!

  • alexpott committed 3260c6c on 8.4.x
    Issue #2851293 by dagmar, alexpott, Munavijayalakshmi, jibran, Lendude,...

Status: Fixed » Closed (fixed)

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