I have a Rails app with a couple resources that I need to run queries on that involve bitwise operations. Right now, I'm using PostgreSQL, and I created a migration for my 'user' model that uses the postgres-specific 'BIT VARYING' datatype, since this is what was recommended for the bitwise '&' operation on the postgres website:
add_column :users, :timeslots, :'BIT VARYING'
In one of my queries, I'm using '&' this way:
self.where("available_lbs > 0 AND status = 0 AND ? & timeslot > 0::bit AND available_end >= ?", user.timeslots, Time.now)
This seems to work on my machine, but there are two issues:
- Both the datatype and the query are database-specific, so if I migrate to another database, I'd probably have to change things
- This migration didn't seem to update the schema.rb file properly. It's still using the Rails 'string' datatype when describing how to create the table:
create_table "users", :force => true do |t|
t.string "timeslots", :limit => nil
end
So when I setup the app on a new machine, it uses the wrong datatype and things break. Any ideas on a good solution for this? (I tried using the 'binary' datatype, but that didn't seem to work well with postgres)