Rails: Devise and Email Capitalization
This week, I found a bug for one of our Rails clients that was worth a quick blog post. The client website runs on Rails 3.2.8 with ActiveRecord and PostgreSQL, uses RailsAdmin for an admin interface, Devise for user authentication, and CanCan for user authorization. Before we found the bug, our code looked something like this:
class SomeController < ApplicationController
def some_method
user = User.find_or_create_by_email(params[:email])
# do some stuff with the user provided parameters
if user.save
render :json => {}
else
render :json => {}, :status => 500
end
end
end
It’s important to note that the 500 error wasn’t reported to the website visitor — there were no visible UI notes to indicate the process had failed. But besides that, this code looks sane, right? We are looking up or creating a user from the provided email, updating the user parameters, and then attempting to save. For the most part, this worked fine, until we came across a situation where the user data was not getting updated properly.
Looking through the logs, I found that the user experiencing the bug was entering mixed caps emails, for example, …
rails
Detecting table rewrites with the ctid column
In a recent article, I mentioned that changing the column definition of a Postgres table will sometimes cause a full table rewrite, but sometimes it will not. The rewrite depends on both the nature of the change and the version of Postgres you are using. So how can you tell for sure if changing a large table will do a rewrite or not? I’ll show one method using the internal system column ctid.
Naturally, you do not want to perform this test using your actual table. In this example, we will create a simple dummy table. As long as the column types are the same as your real table, you can determine if the change will do a table rewrite on your version of PostgreSQL.
The aforementioned ctid column represents the physical location of the table’s row on disk. This is one of the rare cases in which this column can be useful. The ctid value consists of two numbers: the first is the “page” that the row resides in, and the second number is the slot in that page where it resides. To make things confusing, the page numbering starts at 0, while the slot starts at 1, which is why the very first row is always at ctid (0,1). However, the only important …
database postgres
Job Opening: DevOps Engineer
This position has been filled. See our active job listings here.
We’re looking for a full-time, salaried DevOps engineer to work with our existing hosting and system administration team and consult with our clients on their needs. If you like to figure out problems, solve them, can take responsibility for getting a job done well without intensive oversight, please read on!
What is in it for you?
- Work from your home office
- Flexible full-time work hours
- Health insurance benefit
- 401(k) retirement savings plan
- Annual bonus opportunity
- Ability to move without being tied to your job location
What you will be doing:
- Remotely set up and maintain Linux servers (mostly RHEL/CentOS, Debian, and Ubuntu), daemons, and custom software written mostly in Ruby, Python, Perl, and PHP
- Audit and improve security, reliability, backups, monitoring (with Nagios etc.)
- Support developer use of major language ecosystems: Perl’s CPAN, Python PyPI (pip/easy_install), Ruby gems, PHP PEAR/PECL, etc.
- Automate provisioning with Chef, Puppet, etc.
- Work with internal and customer systems and staff
- Use open source tools and contribute back as opportunity arises
- Use your desktop platform of choice: Linux, Mac OS X, Windows
What you will need:
- Professional …
hosting jobs-closed
PostgreSQL search_path Behaviour
PostgreSQL has a great feature: schemas. So you have one database with multiple schemas. This is a really great solution for the data separation between different applications. Each of them can use different schema, and they also can share some schemas between them.
I have noticed that some programmers tend to name the working schema as their user name. This is not a bad idea, however once I had a strange behaviour with such a solution.
I’m using user name szymon
in the database szymon
.
First let’s create a simple table and add some values. I will add one row with information about the table name.
# CREATE TABLE a (t TEXT);
# INSERT INTO a(t) VALUES ('This is table a');
Let’s check if the row is where it should be:
# SELECT t FROM a;
t
-----------------
This is table a
(1 row)
Now let’s create another schema and name it like my user’s name.
# CREATE SCHEMA szymon;
Let’s now create table a
in the new schema.
# CREATE TABLE szymon.a (t TEXT);
So now there are two tables a
in different schemas:
# SELECT t FROM pg_tables WHERE tablename = 'a';
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | …
postgres
Piggybak on Heroku
Several weeks ago, we were contacted through our website with a request for Heroku support on Piggybak. Piggybak is an open source Ruby on Rails ecommerce platform developed and maintained by End Point. Piggybak is similar to many other Rails gems in that it can be installed from Rubygems in any Rails application, and Heroku understands this requirement from the application’s Gemfile. This is a brief tutorial for getting a Rails application up and running with Piggybak. For the purpose of this tutorial, I’ll be using the existing Piggybak demo for deployment, instead of creating a Rails application from scratch.
a) First, clone the existing Piggybak demo. This will be your base application. On your development machine (local or other), you must run bundle install to get all the application’s dependencies.
b) Next, add config.assets.initialize_on_precompile = false to config/application.rb to allow your assets to be compiled without requiring creating a local database.
c) Next, compile the assets according to this Heroku article with the command RAILS_ENV=production bundle exec rake assets:precompile. This will generate all the application assets into the public/assets/ directory. …
ecommerce hosting piggybak rails
Postgres alter column problems and solutions
A common situation for database-backed applications is the need to change the attributes of a column. One can change the data type, or more commonly, only the size limitation, e.g. VARCHAR(32) gets changed to VARCHAR(42). There are a few ways to accomplish this in PostgreSQL, from a straightforward ALTER COLUMN, to replacing VARCHAR with TEXT (plus a table constraint), to some advanced system catalog hacking.
The most common example of such a change is expanding a VARCHAR declaration to allow more characters. For example, your “checksum” column was based on MD5 (at 32 characters), and now needs to be based on Keccak (Keccak is pronounced “catch-ack”) (at 64 characters) In other words, you need a column in your table to change from VARCHAR(32) to VARCHAR(64). The canonical approach is to do this:
ALTER TABLE foobar ALTER COLUMN checksum TYPE VARCHAR(64);
This approach works fine, but it has two huge and interrelated problems: locking and time. This approach locks the table for as long as the command takes to run. And by lock, we are talking a heavy “access exclusive” lock which shuts everything else out of the table. If your table is small, this …
database postgres sql
Using cec-client to Control HDMI Devices
Maintaining the horde of computers it takes to run Liquid Galaxy installations in all corners of the globe is a big job. As of November of 2012, we’re monitoring 154 computers at permanent installations in addition to keeping our development and testing systems running like the well-oiled machines we want them to be. All that fluff aside, end users never see the fruits of our labor unless the TVs are working as expected! Without methods for getting and setting the status of displays, we are completely blind to what people are actually experiencing in front of a Liquid Galaxy.
Enter HDMI-CEC. CEC is a protocol that allows HDMI-connected devices to control each other in various ways. It has a set of standard features that make it easy for home users with a stack of DVD players or TiVos or other devices to change the active source, put everything on standby, control the volume, and some other handy tricks.
We typically use Samsung TVs which support CEC under the trade name “Anynet+”. To interface between computers and TVs, we use Pulse Eight’s USB-CEC adapters which, in conjunction with libCEC, give us a command line interface for arbitrary commands to the TV.
libCEC is available on …
visionport sysadmin
Getting Started with the Perl Debugger
The Perl debugger is not an easy system to leap into unprepared, especially if you learned to program in the “modern era”, with fancy, helpful GUIs and other such things.

So, for those of us who are old school, and those who aren’t but wondering what the fuss is about, here’s a very gentle introduction to debugging Perl code interactively.
First, an aside. You may think to yourself, “Hey, command-line scripting is passé; nobody does that any more. All my code runs within a website (maybe a modern MVC framework), so how can I make use of a command-line debugger?”
Well, that’s where test-driven development and its related methodologies come in. If you have developed using a Perl test framework, you can use the approach outlined here.
The debugger is invoked by using the “-d” switch on the command line. You control the execution of your code with the “n” command:
$ perl -d debug.pl Loading DB routines from perl5db.pl version 1.33 Editor support available. Enter h or h h' for help, or
man perldebug’ for more help. main::(debug.pl:1): my $x = 1; DB<1> n1 main::(debug.pl:2): $x = $x …
perl