Default a Postgres column to the current date in a Rails migration

If you want to have a Postgres column (aside from created_at) that you want to be populated with the current date if no other date is specified, you may be tempted to create a migration like this:

add_column :invoices, :paid_on, :date, default: 'now()'

That will look like it works — you create a record, it gets populated with today’s date, and all is good. However, if you look at your schema, you will notice that new field has a default of today’s date instead of now(). Oops. 🙂

You might try to create the column with the recommendation from the Postgres documentation:

add_column :invoices, :paid_on, :date, default: 'CURRENT_DATE'

But that fails because Rails tries to quote that ‘CURRENT_DATE’ for you before it goes to Postgres, which blows up. Now what?

Here’s how to do what you want:

add_column :invoices, :paid_on, :date, default: { expr: "('now'::text)::date" }

This avoids the quoting problem (by using expr) and avoids the always-insert-migration-date’s-date problem (by using the default function of (‘now’::text)::date, which is effectively the same as CURRENT_DATE.

And now when you insert a record without specifying a value for that field, you get the date of the insertion, rather than the date of the field being created. 🙂

HTTP Request IDs improve visibility across the application stack

Visibility into your application is necessary to properly analyze and troubleshoot issues as they arise. One of the key factors to good visibility is using logs as event streams and treating them as the canonical source of what happened on an app.

The challenge with using logs is to correlate events across the stack consisting of your application's code and Heroku's platform components. Today, we are releasing new functionality that makes this simple. By using the emerging X-Request-ID convention, you can easily correlate multiple log entries to individual HTTP(s) requests.

HTTP Request IDs are now enabled on all Heroku apps.

Understanding X-Request-ID on Heroku

Each HTTP request that reaches your dyno will now contain an X-Request-ID header. Heroku will pass through existing X-Request-ID headers if they are set by the HTTP client. For requests that lack the header, Heroku generates an ID. Your application code can read the request ID from the header and include it in logs, or use it for other purposes. Heroku will write request ID values in the logs from our routing layer as request_id.

The X-Request-ID header is an emerging convention introduced by the Rails framework. It is a standard HTTP header, however, and can be easily accessed from any language or framework running on Heroku.

For full documentation on this feature, please visit the Dev Center.

Using Request IDs

Your application code can read and log the X-Request-ID header value, giving you further insight into end to end behavior and performance. We have provided examples implementing this in Node.js, Python, Ruby (Rack Middleware or Rails), and Java.

To get the most out of request IDs, be sure to store your logs for later search and analysis by using a logging add-on provider.

Conclusion

This capability will allow you to see how requests and application code may be modifying behavior through the application stack, find the source of request errors, and get improved overall visibility into the performance characteristics of your application.

Join us at SxSW Interactive

Every year tens of thousands of people descend on Austin, TX for SxSW Interactive to learn about the latest in digital technologies. This year Heroku is descending as well and we want to see you there.

Orange Oasis

Fri., March 7th and Sat., March 8th
Brass House – 115 San Jacinto Blvd (@ 2nd St)

Heroku will be a part of ExactTarget’s Orange Oasis at Brass House. Close to the convention center, but out of the noise and crowds of SxSW, we’ll have a Purple Pavilion inside the Orange Oasis. Please stop by and say hello, or schedule a private meeting or demo with one of sales or technical reps.

Hours:

Friday, March 7th

  • 1p – 4p Meetings and product demos
  • 4p – 6p Join us for Happy Hour

Saturday, March 8th

  • 10a – 4p Meetings and product demos
  • 4p – 6p Join us for Happy Hour

Heroku SxSW User Meetup

Sunday, March 9th
C Boys – 2008 South Congress Ave (@Leland)

Join us for the Heroku SxSW User Meetup on Sunday March 9th from 5-8pm at C Boys on South Congress.

We will have lots of yummy food from The Original New Orleans Po Boy and Gumbo Shop, a haiku poetry slam with Bob Makela, author of Barstool Poetry, Heroku experts to answer your questions, and an open bar if you are so inclined.

Getting there and back: C Boys is located south of Lady Bird lake. It is a 1.7 mile walk up the well-known South Congress Ave shopping area from the Austin Convention Center. The CapMetro southbound bus will take you there. Get off at the Congress and Oltorf stop. If you are in a car, there is parking, but please do not drink and drive. Cabs are also always an option.

Space is limited, RSVP now. See you there.

Jr. Ruby on Rails developers

Over on the Planet Argon blog, you can read an enlightening interview with two of our Jr. Ruby on Rails developers. Both of them made a career change in their 30s and went through a coding academy here in Portland, Oregon.

<p><a href="http://blog.planetargon.com/entries/2014/2/27/lessons-from-planet-argon-junior-developers">Check it out&#8230;</a></p><div class="feedflare">