Related with #3525924: DB error in updating 3.1.7 => 3.1.8 in that way that the same update function fails, but the reason is different.

I upgraded from 3.1.4 to 3.1.16.

The error is this:

>  [error]  Exception thrown while performing a schema update. SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_credit_notes_date__timezone' in 'WHERE': SELECT 1 AS `expression`
> FROM
> `node_revision__field_credit_notes` `t`
> WHERE (`field_credit_notes_date` IS NOT NULL) OR (`field_credit_notes_date__timezone` IS NOT NULL) OR (`field_credit_notes_price` IS NOT NULL) OR (`field_credit_notes_file` IS NOT NULL)
> LIMIT 1 OFFSET 0; Array

The timezone column indeed doesn't exist at all in that table, not sure what the best approach is here to fix.

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

swentel created an issue. See original summary.

apmsooner’s picture

Status: Active » Postponed (maintainer needs more info)

I wonder if commenting this line out @ line 121 would help?

// Clear cached definitions to ensure we work with the latest data.
  $entity_type_manager->clearCachedDefinitions();

Attach your field config otherwise prior to pulling in the update and I'll see if I can reproduce the issue and provide a solution.

apmsooner’s picture

The timezone column was added in version 3.1.14 so maybe try updating to 3.1.13 first actually.

apmsooner’s picture

I think the issue is these update hooks are perhaps competing with each other with the schema changes. If you can get past that issue this way, I'll update the project page with a note for others. I'm planning on putting out a 4.0.1 version over the weekend also so would recommend jumping to that as its basically the same but will support 11.x, has lots of new features and 3.x will no longer be supported.

swentel’s picture

Ah yes, I see that custom_field_update_8007() is adding the timezone column, so it's a racing condition indeed.

I'll test the approach of upgrading to 3.1.13 first. I'll ping back when I've tested this approach.. Commenting out line 121 might work isolated, but if another update runs first which also clears these definitions, you'll probably end up with the same error.

(The project isn't on D11 yet, so can't go for 4.x - and we'll keep on the D10 LTS for a while before upgrading anyway)

apmsooner’s picture

4.x still supports D10 also FWIW.

swentel’s picture

Interesting, did the 3.1.13 approach, then to 4.0.0 but the same timezone problem (on another field/table though), so that trick doesn't seem to work either. Will paste my config later.

swentel’s picture

Actually, I'm confused, the update did run, but with a SQL error:

>  [notice] Update started: custom_field_update_8007
>  [error]  Failed to update field_invoices on node: Exception thrown while performing a schema update. SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_invoices_date_exigibility__timezone' in 'WHERE': SELECT 1 AS `expression`
> FROM
> `node_revision__field_invoices` `t`
> WHERE (`field_invoices_number` IS NOT NULL) OR (`field_invoices_date` IS NOT NULL) OR (`field_invoices_date__timezone` IS NOT NULL) OR (`field_invoices_date_exigibility` IS NOT NULL) OR (`field_invoices_date_exigibility__timezone` IS NOT NULL) OR (`field_invoices_price` IS NOT NULL) OR (`field_invoices_file` IS NOT NULL)
> LIMIT 1 OFFSET 0; Array
> (
> )
>  
>  [notice] Update completed: custom_field_update_8007
>  [notice] Update started: custom_field_update_10001
>  [notice] Update completed: custom_field_update_10001
>  [notice] Processed 19 out of 19. (Table: node__field_credit_notes, Chunk: 1/1)
>  [notice] Processed 50 out of 58. (Table: node_revision__field_credit_notes, Chunk: 1/2)
>  [notice] Processed 8 out of 58. (Table: node_revision__field_credit_notes, Chunk: 2/2)
>  [notice] Processed 31 out of 31. (Table: node__field_partial_payments, Chunk: 1/1)
>  [notice] Processed 48 out of 48. (Table: node_revision__field_partial_payments, Chunk: 1/1)
>  [notice] Message: Restored 19 rows in node__field_credit_notes
> 
>  [notice] Message: Restored 58 rows in node_revision__field_credit_notes
> 
>  [notice] Message: Restored 31 rows in node__field_partial_payments
> 
>  [notice] Message: Restored 48 rows in node_revision__field_partial_payments
swentel’s picture

Oh, this is not good: all the data in node_revision__field_invoices and node__field_invoices is lost now (luckily on a local install heh)

apmsooner’s picture

Odd, others I know updated that had datetime fields and didn't have any of these issues. I'll have to roll back my own local and try doing some tests to see if I can figure something out for you. Appears that 8007 is indeed an issue.

apmsooner’s picture

@swentel,

Theres a weird thing going on between the updates that's causing 8005 to fail when the other updates try to run. I'm not really sure how to fix them so they run in sequence so the only immediate solution I can provide you is to first just install the update for 8005 which is in version 3.1.8 and then you should be able to advance to the latest 4.x version and run the additional updates without issue.

Steps assuming you have ddev installed locally but you will need to deploy each step separately

  1. ddev composer require drupal/custom_field:3.1.8
  2. ddev drush updb
  3. ^ installs update 8005 only
  4. deploy
  5. ddev composer require 'drupal/custom_field:^4.0'
  6. ddev drush updb
  7. ^ installs all other updates
  8. deploy

Let me know please if this gets you past the issue and I'll note this for others. I'm not sure otherwise how to prevent the failure and want to make sure you can at least move forward. Thanks

apmsooner’s picture

And actually step 1 can be: ddev composer require drupal/custom_field:3.1.13 cause the next field update is in 3.1.14 where the conflict is happening.

swentel’s picture

That did not do the trick. When going to 4.0, I'm getting the same SQL error again as above and the resulting table after the update is empty.

However, I think I know what's going on: my field has two datetime fields. And that's the problem if I read the code right: custom_field_update_8007() iterates every custom field and calls addExtraColumns for that single custom field. It goes in for my first date field, creates the column and then tries to select the existing data. However, at that point, the second date timezone column has not been created yet, and the query does seem to know already there should be a second timezone column, but it doesn't exist yet.

Note: the timezone columns for the two date fields have been created, but it's when the first timezone field is being added, all data is gone because the select query fails. For the second field, the select query does not fail, however, at this point, there's no data anymore as it has been truncated when adding the first column.

I guess ::addExtraColumns should be refactored to allow receiving an array of fields with their extra columns so that they are added all before trying to select the data from the table to preserve the data.

apmsooner’s picture

Ahh okay, thanks for the additional info. I will spend some more time on this today and see if I can come up with an actual fix then. Adding/removing columns to fields has always been a black hole of trial and error for alot of devs but I'll figure this thing out soon.

apmsooner’s picture

Version: 3.1.x-dev » 4.0.x-dev
Status: Postponed (maintainer needs more info) » Active

apmsooner’s picture

Status: Active » Needs review

Okay @swentel,

  1. I had to move the logic out of 8005 to a new update 10003 as it needed to run after 8007 which added the extra __timezone column.
  2. I refactored the addExtraColumns() function to support multiple instances of the same subfield type on a field so now the db truncation/restoration only happens once.

I've tested this with multiple datetime subfields on various custom fields on multiple entity types with varying cardinality and the updates now do what they are supposed to and data is restored properly. You should be able to update now to latest 4.x with this patch without issue. Please let me know how it goes and thank you for all the feedback!

swentel’s picture

Ok, I've planned to test the patch coming friday and I'll let you know!

apmsooner’s picture

@swentel, are you still able to test this out today? I would like to put out a new release over the weekend and while i'm pretty sure I've fixed this bug, you're testing would instill some additional confidence. Thanks

swentel’s picture

Status: Needs review » Reviewed & tested by the community

@ampsooner all is good, you can release!

apmsooner’s picture

Status: Reviewed & tested by the community » Fixed

Thank you for the confirmation and all the great feedback! A new release will be going out over the weekend so look out for that and now that you're on 4.x, you might dig deeper into all the new features related to date fields as well as the new daterange field. There's some pretty advanced functionality in the widget/formatter settings you might find useful :)

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

apmsooner’s picture

Status: Fixed » Closed (fixed)