Problem/Motivation

Drupal\Component\Plugin\Exception\ContextException: The 'entity:user' context is required and not present. in Drupal\Core\Plugin\Context\Context->getContextValue() (line 73 of core\lib\Drupal\Core\Plugin\Context\Context.php).

Upgrading to Drupal 9.3.0 or higher versions from 9.2.10 does something to the uid column that locks the users out of the site. I couldn't log in with my administrator account after the upgrade so I had to reset the password using a php reset script. When I finally was able to log in, every row in 'admin/people' tab was empty except for the account that was created during Drupal installation. Please see attached screenshot.

Additionally, the site wasn't accessible to anonymous users after the upgrade. I had to manually add an anonymous user with uid 0 to solve that problem.

All of the users are basically locked out meaning their credentials do not work after the upgrade. I am running 4 Drupal instances on the same platform (Windows Server + MSSQL DB). I've had similar issues with all of them after the upgrade.

I found a thread that is possibly related.
https://www.drupal.org/project/drupal/issues/3258118#:~:text=The%20under....

Drupal 9.3.0 introduced some database changes and user role dependencies - https://www.drupal.org/project/drupal/releases/9.3.0#:~:text=User%20role...

I wonder if that has anything to do with it. Any idea when we might get a patch/version release to tackle this issue?

Steps to reproduce

1) Upgrade to Drupal 9.3.x from Drupal 9.2.10
2) Run update.php script through the URL
3) Try to log in once the database update is complete

Issue fork sqlsrv-3263493

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

devKhairul created an issue. See original summary.

devKhairul’s picture

Title: Upgrading to Drupal 9.3.x changes users table and makes locks them out of the site » Upgrading to Drupal 9.3.x changes users table and locks users out of the site
devKhairul’s picture

@Beakerboy @david_garcia

Anything?

alexpott’s picture

@devKhairul I can confirm that there is a problem with updating from Drupal 9.2 to 9.3 with the SQL server driver.

The problem is because we can't create a new identity column on a table and set the values as we want to. Therefore we have a tricky issue to solve. After updating from 9.2 to 9.3 there is no user with UID 0 anymore.

Here is a work around that fixes my local test site. Use with caution and please please please backup your database first. But this does have a chance of fixing your site if you have run 9.3 database updates on an SQL server backed site.

Here's the SQL to run against your database:

/* Create a table to hold the temporary data. */
CREATE TABLE user_temp_9301(
    [uid] [int] NOT NULL,
    [uuid] [varchar](128) NOT NULL,
    [langcode] [varchar](12) NOT NULL,
);

/* Insert the data into the temporary table. Note this assumes that prior to updating to 9.3 your user UIDs where sequential. */
insert into user_temp_9301
select uid - 1, uuid, langcode from users;

/* Before running this make sure you have a backup and that the data is present in the new user_temp_9301 table */
truncate table users;
SET IDENTITY_INSERT users ON;
insert into users (uid, uuid, langcode) select uid, uuid, langcode from user_temp_9301;

/* At this point the data should be back in the users table. Make sure this true before doing the next query. */
drop table user_temp_9301;
alexpott’s picture

@devKhairul note looking at the issue summary it appears you've manually added a user with the UID of 0. You'll need to change the SQL to

insert into user_temp_9301
select uid - 1, uuid, langcode from users where uid > 0;

to cope with that.

devKhairul’s picture

@alexpott

Thanks for your input. Much appreciated. I'll give that a try. Any idea when we might be getting a patch for this issue or will it be addressed in the newer version of the module?

alexpott’s picture

@devKhairul well we've updated core today to not run the update that breaks stuff on SqlServer - see #3265802: user_update_9301() causes data loss and a broken site on SQL Server and the plan now is to add an update to the sqlsrv module that does the change. The current plan is to do that in this issue so you should see the patch / MR arrive here and then be committed... hopefully that'll be soon.

We have another related MySQL 8 issue to fix on Drupal 9.3.x before we might do an off-schedule Drupal 9 release to ensure no one else breaks their site due to this, if D9 release managers agree to this.

@devKhairul hopefully you didn't do the update on live and are still running on Drupal 9.2.x - that's still under security support so I'd hold on trying to upgrade until sqlsrv is updated to account for the update.

devKhairul’s picture

@alexpott

That's great news!! I haven't updated the production sites to 9.3.x yet. We're still on 9.2.12. I'm going to hold off until we have a permanent solution since 9.2.x is due to receive security coverage until June. It makes sense to me to just wait and do the upgrade when the solution is officially committed to the module.

devKhairul’s picture

@alexpott

Any update on this? Thank you!!!

alexpott’s picture

@devKhairul I spoke to @beakerboy about this - I thought he had some ideas.

chrisck’s picture

Hello just wanted to toss my hat in the ring for this issue. We've attempted a Drupal 9.3 upgrade in mid-December last year and ran into the same error:

Drupal\Component\Plugin\Exception\ContextException: The 'entity:user' context is required and not present. in Drupal\Core\Plugin\Context\Context->getContextValue() (line 73 of core\lib\Drupal\Core\Plugin\Context\Context.php).

We could not figure out what the problem was and rolled back to Drupal 9.2.x as a temporary measure. After tracing multiple Drupal threads from Drupal core issue queue, I've landed here, which is promising. I've pasted in the error code in h2 tags to help with searchability for others having the same issue.

Drupal 9.2.15
Microsoft-IIS/10.0
SQLServer 13.00.4001
PHP 7.4.8

chrisck’s picture

Issue summary: View changes

Updating issue with error message to help with searchability.

devKhairul’s picture

@alexpott Thank you!! I hope @beakerboy has some good news for us in store.

devKhairul’s picture

@alexpott - Is there a way to submit a request to extend security coverage for Drupal 9.2.4 in case we don't have a solution in the next months?

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

KenKodz’s picture

I've been working with @beakerboy in my attempt to fix the changeField method to support changing a field to serial when there is existing data in the table. This requires the following to occur:

  1. Create a new table
  2. Make the requested modifications to the field
  3. Copy the data from the old table to the new table
  4. Add the indexes back to the table with any changes as needed by the request
  5. Drop the old table
  6. Rename the new table to the old table's name

If anyone has any recommendations to improve this, please let me know. I am still learning MS SQL and Drupal.

KenKodz’s picture

One thing to note, once this is code is merged, we still need to get the core team to update user_update_9301 in the user.install file to allow it to run if the new version of the module or greater is installed.

alexpott’s picture

@KenLewis - I don't think you will need the core team to do anything - the sqlsrv module can provide its own update - we already stopped running the update for sqlsrv - see #3265802: user_update_9301() causes data loss and a broken site on SQL Server . I don't think we can revert that change because we need to be sure that the latest sqlsrv module is being used.

alexpott’s picture

@KenLewis I'd also be tempted to be pragmatic here and implement something like #4 in a update function in sqlsrv for now - and implement a generic fix later. It's more important to get people to 9.3.x than to have a generic fix.

devKhairul’s picture

Hello, any update on this?

devKhairul’s picture

@KenLewis @alexpott - Hi guys, do you guys have any information regarding this? We haven't heard anything in a while. We are still running 9.2 but 9.4 is out. We'd appreciate some insight so we can make an informed decision. :)

devKhairul’s picture

Update:

So after months of waiting, I went ahead and decided to upgrade to Drupal 9.4.5 from 9.2.21.

* The upgrades were successful. However, it didn't upgrade the user module so you get a red-hot error when you go to the 'status report' page.
* I can no longer add new users to the site. I assume this is because the user module upgrade was skipped.

The site is working well so far. These are however tiny sites. I have two huge sites that I'm planning to upgrade soon. I'll see how that goes.

I don't know what consequences await for me but I guess I'll wait and see.

pstewart’s picture

Having read through this issue, it looks like the following actions are needed to resolve:

  1. Test and review MR 79 to ensure sqlsrv can cope with schema changes that convert columns to serial.
  2. Implement an update function to ensure the user uid column is converted to serial as per user_update_9301. This update function should ensure it only attempts to do work if the column is not already an appropriate identity column in the database (i.e. if the site admin has already fixed the schema manually to work round the problem while awaiting the fix from this issue).

Is this correct?

devKhairul’s picture

@pstewart

I think number 2 will fix the problem with the user table. I could not find any way to manually fix the schema to get around the problem.

rramirezsn’s picture

StatusFileSize
new9.09 KB

Hi:
I ran to this same issue when upgrading a production site from 9.2 to 9.5.
After the database update there was not posible to create users so i fixed the schema manually to work around the problem but now i had a error on the site state report:

error

No coinciden las definiciones de entidad y/o campo
Se detectaron los siguientes cambios en las definiciones de tipo de entidad y de campo.
Usuario
El campo ID de Usuario necesita ser actualizado.

Any idea how to solve it?

solideogloria’s picture

The translation of the above comment:

Entity and/or field definitions do not match
The following changes were detected in the entity type and field definitions.
User
The User ID field needs to be updated.

solideogloria’s picture

Version: 4.3.1 » 4.4.x-dev

Updating target branch

solideogloria’s picture

Status: Active » Needs work

pstewart credited glassb.

pstewart’s picture

Status: Needs work » Reviewed & tested by the community

Noting that most of the discussion has been happening on #3371473: SQL Server Identity error when updating to Drupal 10.1.0, which includes successful testing of MR87. The merge request will be completed on this issue and once this issue has been closed as fixed the other issue will be closed as duplicate.

pstewart changed the visibility of the branch 3263493-upgrading-to-drupal to hidden.

  • pstewart committed 59dbf0f5 on 4.4.x
    Issue #3263493 by pstewart, KenKodz, devKhairul, alexpott, solideogloria...
solideogloria’s picture

Should this now be closed as Fixed?

pstewart’s picture

I'm planning to mark all the issues resolved in 4.4.x as fixed when we do the stable release, which will hopefully be the same as rc1 if no problems arise.

pstewart’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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