Skip to content

Instantly share code, notes, and snippets.

@tiagopog
Last active August 29, 2015 14:18
Show Gist options
  • Save tiagopog/ba6fca8cdd633ea1e7fd to your computer and use it in GitHub Desktop.
Save tiagopog/ba6fca8cdd633ea1e7fd to your computer and use it in GitHub Desktop.
JSONB example with Rails
# db/migrate/*_create_authentications.rb
class CreateAuthentications < ActiveRecord::Migration
def change
create_table :authentications do |t|
t.references :user, index: true
t.integer :provider, default: 0, null: false
t.jsonb :access_data, null: false, default: '{}'
t.timestamps
end
add_index :authentications, :access_data, using: :gin
end
end
# db/migrate/*__add_index_to_access_data_path_on_authentications.rb
class AddIndexToAccessDataPathOnAuthentications < ActiveRecord::Migration
def up
execute <<-SQL
CREATE INDEX access_data_token_on_authentications ON authentications ((access_data->>'token'))
SQL
end
def down
remove_index :authentications, name: :access_data_token_on_authentications
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment