Problem/Motivation

Over at #2721313: Upgrade path between revisionable / non-revisionable entities, we're trying to add support for an upgrade path for making entity types revisionable when they already contain data. This means we have to add a NOT NULL field (reivision_id) to a table with existing rows, with initial values that match an existing field, the entity ID.

Proposed resolution

Support a 'initial_from_field' field schema specification key which will automatically populate the added field with values from the specified existing field.

Remaining tasks

Review.

User interface changes

Nope.

API changes

API addition, the new 'initial_from_field' field schema spec key.

Data model changes

Nope.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

amateescu created an issue. See original summary.

amateescu’s picture

Here's what I had in mind.

timmillwood’s picture

Some history of the issue in #2746279: Adding not null fields to table which have data, which has been close in favour of this issue.

amateescu’s picture

I expected the Postgres failure, let's not try to compare integer columns with string columns :) Also fixed the SQLite code.

dawehner’s picture

Looks okay for me.

timmillwood’s picture

Status: Needs review » Reviewed & tested by the community

Awesome job @amateescu!

Even without the postgres and sqlite fix #2721313: Upgrade path between revisionable / non-revisionable entities shows that this solves the issue it sets out to.

daffie’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

In #2746279: Adding not null fields to table which have data there is a test with DESCRIBE {table} for MySQL. There are other methods for PostgreSQL (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'table') and SQLite (PRAGMA table_info(table)) to do the same. I think that they are needed here.

daffie’s picture

We are now testing a "serial_column". Can we add testing for regular not null columns?

amateescu’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs tests

@daffie, the key that we're adding here is not standard SQL behavior, so I don't see how testing any kind of SQL introspection is useful.

Also, the fact that we're testing a "serial" column also doesn't matter, we just need to test against a column that has data in it.

daffie’s picture

@amateescu: You are right. I did not look good enough at the patch.

dawehner’s picture

Category: Task » Feature request

I still think some of the database maintainer should have a look at it, because well, technically for itself this is a new feature of the DB subsystem.

Crell’s picture

I can't tell from the patch itself, is this something that works on table creation, or just on table update? That possible inconsistency is my only concern.

timmillwood’s picture

@Crell - From what I understand this is on table update only, which is by design. The problem we're trying to solve is if you're adding a NOT NULL column to a table which already has data we need to set an initial value before making it not null. When creating a new table there is no data so you can add NOT NULL with no issues.

amateescu’s picture

This works just like the existing 'initial' schema key, which means that it works on both table creation and table updates, it's just that on table creation there are no initial values set because there is no other column with data to copy from.

\Drupal\KernelTests\Core\Database\SchemaTest::assertFieldAdditionRemoval() tests a field spec for both new and existing tables :)

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

This looks good but we should have a change record to announce the support for this and also search docs on d.o in case we need to update stuff - there might be nothing see https://www.drupal.org/developing/api/schema

alexpott’s picture

Issue tags: +8.2.0 release notes
timmillwood’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record
amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Updated the CR a bit to provide a more clear usage example :)

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed bae3e31 and pushed to 8.2.x. Thanks!

  • alexpott committed bae3e31 on 8.2.x
    Issue #2753475 by amateescu, timmillwood: Support using the values of...

Status: Fixed » Closed (fixed)

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