There are several database columns which should be set NOT NULL. As well, nullable columns do not need 'not null' => FALSE because that is the default.

Original report

I have noticed some potential improvements that can be made to the database schema:

  • {election_vote} does not need to store election_id or post_id. It can get those by joining to {election_ballot}.
  • There are a bunch of fields that can set NOT NULL:
    • {election_post}: election_id, title, changed
    • {election_vote}: election_id, post_id, ballot_id
    • {election_ballot}: election_id, post_id, uid
    • {election}: title, uid, changed
    • {election_candidate}: post_id, election_id, changed
  • On columns where NULL is permitted, the column comment should state what it means for the column to be NULL.

I would be happy to write a patch once we have agreement about the changes.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Liam Morland created an issue. See original summary.

pjcdawkins’s picture

{election_vote} does not need to store election_id or post_id. It can get those by joining to {election_ballot}.

Please allow for the possibility of setting election_vote.ballot_id NULL, and still being able to count votes. It's to theoretically allow counting the entire election's votes without having any trace of who voted, which means you could support #2006326: Make election_vote records anonymous if you wanted. So, that's also why election_vote.ballot_id should not be NOT NULL.

I'm OK with the rest.

There are probably excessive indexes on some tables.

Liam Morland’s picture

Ah, good point. Yes, best to leave those columns in and leave ballot_id able to be NULL. I'll work on a patch for the rest.

Liam Morland’s picture

The third bullet, documentation of NULLs, will be handled in #2621482: Document NULLs in schema.

Liam Morland’s picture

Status: Active » Needs review
FileSize
14.4 KB

The patch also removes "'not null' => FALSE," because that is the default.

Liam Morland’s picture

FileSize
15.18 KB

This update patch includes a couple more columns declared NOT NULL.

Liam Morland’s picture

Updated version leaves election_ballot.uid nullable to allow for anonymous voting, allowing #2006326: Make election_vote records anonymous.

Liam Morland’s picture

Liam Morland’s picture

Title: Database schema improvements » Set many database column NOT NULL
Issue summary: View changes
pjcdawkins’s picture

Status: Needs review » Needs work

I certainly would not make these database schema choices again if redesigning the module.

However, this is a complex and relatively risky patch, which could break existing installs if they had set these columns NULL before (for whatever reason).

So we'd at least need a clear explanation of the practical benefits.

  1. +++ b/election.install
    @@ -32,13 +32,13 @@ function election_schema() {
           'uid' => array(
             'description' => 'The {users}.uid of the election\'s owner/creator.',
             'type' => 'int',
             'unsigned' => TRUE,
    -        'not null' => FALSE,
    +        'not null' => TRUE,
    

    To me this would imply that if a user is deleted, all their elections must be deleted. I don't think site owners necessarily want that.

  2. +++ b/election.install
    @@ -107,7 +102,7 @@ function election_schema() {
           'changed' => array(
             'description' => 'The Unix timestamp for when the election was most recently changed.',
             'type' => 'int',
    -        'not null' => FALSE,
    +        'not null' => TRUE,
    

    Philosophically at least, NULL here would mean that the election has not been changed since it was created.

  3. +++ b/election_candidate/election_candidate.install
    @@ -26,13 +26,13 @@ function election_candidate_schema() {
             'description' => 'The post for which the candidate is standing. Relates to {election_post}.post_id.',
             'type' => 'int',
             'unsigned' => TRUE,
    -        'not null' => FALSE,
    +        'not null' => TRUE,
    

    Candidates are not deleted when posts are deleted - this is explicitly set to NULL in election_candidate_entity_delete().

pjcdawkins’s picture

I suggest documentation (#2621482: Document NULLs in schema) is more important than actually setting these NOT NULL.

Liam Morland’s picture

I was thinking that columns which should be NOT NULL should be set that way, then the others can be documented.

I think normally modified dates are the same as created date for items that have never been edited.

I will re-roll this.

Liam Morland’s picture

Status: Needs work » Needs review
FileSize
14.29 KB
Liam Morland’s picture

Hi, Patrick, Do you have any feedback about the patch in #13? If you don't want to make any schema changes, then we should move on to #2621482: Document NULLs in schema. It just makes sense to me that we make any desired columns NOT NULL before we try to document the meaning of NULLs.

Liam Morland’s picture

Title: Set many database column NOT NULL » Set many database columns NOT NULL

  • Liam Morland committed 74e5559 on 7.x-1.x
    Issue #2615130: Remove redundant "'not null' => FALSE" declarations....
Liam Morland’s picture

  • Liam Morland committed ecef728 on 7.x-1.x
    Issue #2615130: Set many database columns NOT NULL.
    
Liam Morland’s picture

Assigned: Unassigned » Liam Morland
Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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