@@ -195,6 +195,7 @@ function message_schema() {
195
195
'not null' => TRUE,
196
196
'unsigned' => TRUE,
197
197
'description' => 'The Unique ID of the message.',
198
+ 'not null' => TRUE,
198
199
),
199
200
'type' => array(
200
201
'description' => 'Reference to a message a type.',
@@ -528,3 +529,35 @@ function message_update_7012() {
528
529
db_drop_index('message', 'type');
529
530
db_add_index('message', 'type_index', array('type'));
530
531
}
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