cardiacsociety / web-services

MappCPD API and other services
0 stars 1 forks source link

Remove sql.NullInt64 from ActivityType #33

Closed mikedonnici closed 5 years ago

mikedonnici commented 6 years ago

When databases was updated to include activity types ce_activity_type_id field was added to the ce_m_activity table. All new records should have a value for this field however older records will have this field set to NULL.

To accommodate this the ActivityType.ID was set to use a special type sql.NullInt64 which is a struct.

type ActivityType struct {
    ID       sql.NullInt64 `json:"id" bson:"id"` // can be NULL for old data
    Name     string        `json:"name" bson:"name"`
    Activity Activity      `json:"activity" bson:"activity"`
}

// ... sql pkg
type NullInt64 struct {
    Int64 int64
    Valid bool // Valid is true if Int64 is not NULL
}

In hindsight, this was a bad idea as thesql.NullInt64 is much harder to handle in the code than a straightforward int. This came to light whilst trying to set up integration tests for the internal/activity package (#32).

Setting old values to 0 might be a much easier way to deal with this problem.

At this stage, the GraphQL server is the only part of the application that accesses this type.

mikedonnici commented 5 years ago

Found a better solution... can use IFNULL() in MySQL.

For example:

SELECT IFNULL(type_id, 0) FROM foo;

If type_id is NULL then the expression will return 0 instead.

This is much nicer than sqlNullInt64

Changed in 75d0f14.