Adding a Boolean Column to an Existing Table with Alembic and SQLAlchemy

Ever find yourself needing to add a new boolean column to an existing table? I do! Recently I’ve been using SQLAlchemy and Alembic to manage migrations and I frequently find myself looking up how to achieve this task. Below is a recipe for adding a boolean column to an existing table. We want to avoid the three state boolean problem so we’ll be making this column null: false. Since we’re adding this column to an existing table we have existing rows that will have empty values for this new column. Read On →

HATEOAS with Ember Data

This post was originally published on thoughtbot’s blog. Ember Data introduced strong conventions around how to structure API responses. While these conventions allow us to move quickly, there are additional steps we can take to minimize the coupling between the front end and back end. Using concepts from HATEOAS (Hypermedia as the Engine of Application State) we can make our Ember applications more flexible and resilient to changes on the server. Read On →

Datetime Parsing with Go

While working on a side project recently (https://github.com/srt32/sfpd_crime) I wrote a CSV parsing script in Go and I realized I did not understand how to parse a non-standard string into a time.Time type. Here’s what I learned. My date data looked like the following: 12/18/2014 07:00:00 but the Go reference time is in the following format: Mon Jan 2 15:04:05 -0700 MST 2006. So, how do we go from my format to the required format? Read On →

Speed Up JavaScript Capybara Specs by Blacklisting URLs

This post was originally published on thoughtbot’s blog. On a project recently, our full test suite began to crawl (taking ~9 minutes instead of less than 1) on our local machines running OS X but ran normally on CI. This slowdown took our productivity to near zero. We discovered our Capybara specs with js: true set were the culprit but we couldn’t figure out why. read more…

Postgres Window Functions

This post was originally published on thoughtbot’s blog. We recently ran into a case where a join was getting out of hand and we were struggling with how to rein in the size of it. Then we found window functions. Window functions (Oracle calls them analytic functions) are a part of the SQL standard and this post will explore how to use them in Postgres. Let’s see how they work and what kind of problems they can help us solve. Read On →

An Introduction to Go Tools and Clean Code

I’ve begun learning Go and while the language has been fun to learn and use, figuring out all the tooling has been less than simple. Here’s a quick overview of a sample tool chain to get you started with community acceptable code. Step 1: the compiler Coming from a dynamically typed background, types and compile errors have been enjoyable (I hope the feeling lasts). Step 2: go fmt This tool automatically formats Go source code and as the docs explain, it: Read On →

Writing Functional SQL in PostreSQL: Don't do it

During some recent PostgreSQL rabbit hole digging I discovered the concept of ‘functional query notation’ and it mildly blew my mind. The Question Assuming we have two tables: contacts table having columns: id and name conversations table having columns: id and contact_id (FK to contacts.id) The following two queries return the same data: dot notation (pretty typical): SELECT contacts.name, contacts.id, conversations.id FROM contacts INNER JOIN conversations ON contacts. Read On →

Why Postgres Won't Always Use an Index

This post was originally published on thoughtbot’s blog. We were analyzing slow responses on a project recently and found ourselves questioning the performance of some PostgreSQL queries. As is typical when digging into queries, we had some questions around what the query was doing. So, we used our handy EXPLAIN command to shed some light on the database’s behavior. Upon inspecting the query it turned out an index we had created was not being used. Read On →

Workflows for Writing Migrations With Rollbacks in Mind

This post was originally published on thoughtbot’s blog thoughtbot’s dotfiles include an alias mapping migrate to rake db:migrate db:rollback && rake db:migrate It is also a good practice in our workflow to use the migrate alias in place of rake db:migrate. Let’s explore what this command does and why we recommend it. Reversibility rake db:migrate db:rollback will run any pending migrations and then attempt to roll back those migrations. If the migration and rollback are successful (signified by &&), rake db:migrate will then run the migration again. Read On →

Refactoring Ruby Iteration Patterns to the Database

Check out my recent article on thoughtbot’s blog. Frequently on projects we need to run a calculation through an ActiveRecord association. For example, we might want to get a user’s all time purchases, a company’s total amount of products sold, or in our case, the total amount of loans made to a campaign. This sort of calculation is ripe for using a map and an inject but frequently the solution is more elegant and faster if we can have SQL do the work instead. Read On →