Skip to content

Commit 47a3d31

Browse files
authored
Merge pull request #20 from chuva-inc/Issue_2051751_message.mid_is_part_of_the_primary_key_but
Issue #2051751 from drupal.org: message.mid is part of the primary key but is not specified to be 'not null'.
2 parents 363eff2 + 3396a1e commit 47a3d31

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

message.install

+33
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ function message_schema() {
195195
'not null' => TRUE,
196196
'unsigned' => TRUE,
197197
'description' => 'The Unique ID of the message.',
198+
'not null' => TRUE,
198199
),
199200
'type' => array(
200201
'description' => 'Reference to a message a type.',
@@ -528,3 +529,35 @@ function message_update_7012() {
528529
db_drop_index('message', 'type');
529530
db_add_index('message', 'type_index', array('type'));
530531
}
532+
533+
/**
534+
* message.mid is part of the primary key but is not specified to be 'not
535+
* null'.
536+
*/
537+
function message_update_7013() {
538+
// Here we have some special concern:
539+
// 1. Keep the field as serial so the auto_increment will not affected.
540+
// 2. Before setting (or change) a field as serial PK should be dropped.
541+
// 3. BTW, if change a serial field without any index will fail in MySQL.
542+
543+
// Let's add a temporary unique key for mid so MySQL will let it go.
544+
db_add_unique_key('message', 'temp_key', array('mid'));
545+
546+
// Now remove the PK before changing a field as serial (well, even it is
547+
// already a serial field, originally).
548+
db_drop_primary_key('message');
549+
550+
// Here we can successfully alter the serial field without error message.
551+
// See https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_change_field/7
552+
db_change_field('message', 'mid', 'mid', array(
553+
'type' => 'serial',
554+
'unsigned' => TRUE,
555+
'description' => 'The Unique ID of the message.',
556+
'not null' => TRUE,
557+
), array(
558+
'primary key' => array('mid'),
559+
));
560+
561+
// Finally, remove the temporary unique key because no longer useful.
562+
db_drop_unique_key('message', 'temp_key');
563+
}

0 commit comments

Comments
 (0)