Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #2051751 from drupal.org: message.mid is part of the primary key but is not specified to be 'not null'. #20

Merged
merged 1 commit into from
Jun 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions message.install
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ function message_schema() {
'type' => 'serial',
'unsigned' => TRUE,
'description' => 'The Unique ID of the message.',
'not null' => TRUE,
),
'type' => array(
'description' => 'Reference to a message a type.',
Expand Down Expand Up @@ -527,3 +528,35 @@ function message_update_7012() {
db_drop_index('message', 'type');
db_add_index('message', 'type_index', array('type'));
}

/**
* message.mid is part of the primary key but is not specified to be 'not
* null'.
*/
function message_update_7013() {
// Here we have some special concern:
// 1. Keep the field as serial so the auto_increment will not affected.
// 2. Before setting (or change) a field as serial PK should be dropped.
// 3. BTW, if change a serial field without any index will fail in MySQL.

// Let's add a temporary unique key for mid so MySQL will let it go.
db_add_unique_key('message', 'temp_key', array('mid'));

// Now remove the PK before changing a field as serial (well, even it is
// already a serial field, originally).
db_drop_primary_key('message');

// Here we can successfully alter the serial field without error message.
// See https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_change_field/7
db_change_field('message', 'mid', 'mid', array(
'type' => 'serial',
'unsigned' => TRUE,
'description' => 'The Unique ID of the message.',
'not null' => TRUE,
), array(
'primary key' => array('mid'),
));

// Finally, remove the temporary unique key because no longer useful.
db_drop_unique_key('message', 'temp_key');
}