Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
When performing a migration upgrade from drupal 6 to drupal 8, I needed a way to migrate existing auto increment fields, and then have them work as the serial fields is designed to after migration.
The default with serial field is to always assign new numbers, what I needed was to assigns the value passed in if there is one, otherwise, do the auto assigning.
Comment | File | Size | Author |
---|---|---|---|
#23 | interdiff.txt | 2.99 KB | jacktonkin |
#23 | serial-existing_values-2946075-23.patch | 5.45 KB | jacktonkin |
Comments
Comment #2
singularoChanges in this patch:
- Add support for default/specified values for migrations.
Comment #3
singularoComment #4
joelpittetWas there a reason why it was set to needs work?
Comment #5
joelpittetThis looks to have fixed migrate issues for me so marking it as RTBC, thanks @singularo
Comment #6
edysmpadding a new field into a existing type with data throw this:
we need to update the
initOldEntries
function too...Comment #7
heddnNeeded a re-roll to apply via composer patches.
Comment #8
Jed_BH CreditAttribution: Jed_BH commentedWill this work with migrating from 7 to 8?
Comment #9
joelpittet@Jed_BH I can't see why not it doesn't look D6 specific.
Comment #10
Jed_BH CreditAttribution: Jed_BH commentedFor some reason my values got imported, but for some reason the next new value starts from 38k something. Any thoughts on how to fix that?
Comment #11
singularoRerolled patch after recent merges.
Comment #12
singularoAfter the import, the values should just start right where the imported ones finished.
Re-rolled the patch again due to slight changes as others get merged into upstream.
Comment #13
colorfieldThank you @singularo, looks good! I've changed the variable naming a bit to indicate that it is an external value, then set this value as null explicitly in
generateValueFromName()
and removed theint
type hinting in the methods signature. Even if php5 support is dropped, as we are not using type hinting for primitive values in other methods, it should be a module wide change.Also, it would be good to have test coverage for migrations #3061273: Create migration unit tests, I will work on this during the week.
Comment #14
colorfieldComment #15
rosinegrean CreditAttribution: rosinegrean at PitechPlus commentedComment #16
plousia CreditAttribution: plousia commentedApplying the patch from #13 results in this error on saving an existing node with the Serial field:
Comment #17
jacktonkin CreditAttribution: jacktonkin at ISSUP commentedPreviously re-saving an entity with no value in the serial field set the serial to 1 (see #3058891: Do not return a default value if the serial id has not not been initialized on existing entities). With this patch it attempts to set it to -1 and fails as the field schema is for unsigned integers.
Possible solutions:
SerialItem::preSave()
ifSerialItem::getSerial()
returns NULL and the current value is -1.All but (2) above change the behaviour when re-saving uninitialised entities, but given the module is in alpha and #3058891: Do not return a default value if the serial id has not not been initialized on existing entities looks like a bug (multiple entities end up with the same serial in the default case) (1) or (3) look like better options?
I have been testing the current patch in a new project and can confirm it works well in the case where there are no uninitialised entities. Happy to update the patch with either approach.
Comment #18
liquidcms CreditAttribution: liquidcms commentedI am not 100% sure what this patch is intended to do; but certainly doesn't do what i would have thought.
Starting point:
users 1 (uid = 1), 1000 to 1015 (test accounts)
I want to migrate users with id from 1004 to 1010, so i delete existing users from 1003-1015.
I import (using Feeds, not sure if this should matter, with simple plugin i created which basically just imports the value) test users (1004, to 1010).
- the first time it worked but serial ids were 2001 to 2007 - which makes no sense
- i deleted them and tried again.. this time 2 failed, and ids of the rest were the same (2xxx)
- i deleted again and re-tried.. and this time they all failed:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1011' for key 'PRIMARY': INSERT INTO {serial_eaafd7fb525fde97bdace1f0a513d383} (sid, uniqid) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1); Array ( [:db_insert_placeholder_0] => 1011 [:db_insert_placeholder_1] => 5e5598c9180601.96844770 )
I manually tried to insert a new user; he gets ID 1000, which is a duplicate of an existing user.
very little of this was what i was hoping for :(
Comment #19
liquidcms CreditAttribution: liquidcms commentedi see the serial table now and it has an entry for 1011, which i guess is why the import with that value fails.. but i do not have a user in my system with that value.
perhaps what i am trying to do is not what the intent of this patch is.. i think what i need is to add a serial field but set it to disabled (at which time it acts like a simple text field) and then after migration is done, enable the serial effect to start up at either the value set in the field config or after the last value migrated into the db.
Comment #20
liquidcms CreditAttribution: liquidcms commentedlooking at the values stored in the serial table compared to those in the field table and it certainly looks corrupted.. so i deleted all the values from the serial table except the serial id =1 for uid = 1. i also deleted all the entries from the field table except that 1 as well.
this time when i import user with (intended) serial id values 1005 to 1011, i get very odd results.
serial table: entries for 1010 and 1011 (id 1 is gone), and missing most of them
field table: 1, 2004 to 2010
so clearly wrong.. but possibly something to do with my Feeds import target (which really is only making the Serial field visible in the UI)?
Comment #21
gbirch CreditAttribution: gbirch at Tech-Tamer, LLC commentedReroll patch against current dev (adf0e9c).
Comment #22
gbirch CreditAttribution: gbirch at Tech-Tamer, LLC commentedAnd here's the re-roll diff, which I find more confusing than helpful, but maybe it will help someone else.
Comment #23
jacktonkin CreditAttribution: jacktonkin at ISSUP commentedRe-rolled against 2.0.x
Comment #24
benjifisherI wonder how this issue will be affected by #3414661: Serial not generating on entity save in certain contexts. I am adding that as a related issue.
Looking at the patch in #23, I have a few comments.
Normally, when adding a new optional parameter, we add it after the existing parameters, for backwards compatibility (BC).
This can be simplified. Something like this: