Bug Fixes for Christmas


Merry Christmas everyone! After all the lovely presents, food, and company I've had over the last few days, I decided to give myself one last gift - bug fixes!

When I put up my last post, I noticed some weird stuff happening with the post dates on the site. They seemed to be spontaneously changing, usually to the current date, and looking directly at the database, some posts were missing dates altogether! I wasn't able to nail down exactly what was happening, but I'm pretty sure it was something to do with my use of a default value on my mongoose schema, like this. My theory is that I was somehow ending up with existing posts getting into memory without their creation date, which was then causing the schema's default value to be applied upon a save. Or maybe it was because I was trying to do partial updates when I should have been doing a 'full-resource-put', again causing the default date to overwrite the existing data.

Either way, I eventually decided to remove the default value from the mongoose schema, and handle creation dates manually. I think taking out the extra moving part was a good decision, especially seeing as this wasn't the first time it had tripped me up - last time it was the fact that mongoose's Model.findOneAndUpdate maps directly to mongodb's findAndModify, which means that mongoose schema features like defaults and validators are not applied. This makes sense when you read about it, but I think it's surprising to newbies.

While fixing that bug, I also went through the process of making the integration tests use promises instead of done callbacks. Previously, unhandled exceptions would disappear somewhere, and tests would just time out, which was forcing a lot of console.log debugging. Now that everything is wrapped in promises, mocha is able to catch failures properly and show the relevant stack trace or failing assertion, which is much more useful!

Anyway, with the weird date bug out of the way, I had to go and do a bit of manual database repair-work. The timestamps on my first 3 posts won't be exactly correct, but they're close enough. After that, I realised that I was displaying posts oldest-first on the front page (not what I wanted), and that dates weren't being formatted on individual post pages, so I fixed those too! Again I'm struck by how awesome the whole continuous delivery thing is - getting those fixes tested in a clean, remote environment, and then live into prod, was just a matter of git push. Pretty cool stuff!