Skip to content

Instantly share code, notes, and snippets.

@ivanvanderbyl
Created July 22, 2012 03:05
Show Gist options
  • Select an option

  • Save ivanvanderbyl/3158133 to your computer and use it in GitHub Desktop.

Select an option

Save ivanvanderbyl/3158133 to your computer and use it in GitHub Desktop.
Using 128bit UUIDs with ActiveRecord
class CreateJobs < ActiveRecord::Migration
def up
create_table :jobs do |t|
t.timestamps
end
sql = <<-SQL
ALTER TABLE "jobs" ALTER COLUMN id DROP DEFAULT,
ALTER COLUMN id TYPE uuid USING uuid_generate_v4(),
ALTER COLUMN id SET DEFAULT uuid_generate_v4();
DROP SEQUENCE "jobs_id_seq";
SQL
execute sql
add_index :jobs, :id
end
def down
remove_index :jobs, :id
drop_table :jobs
end
end
class SetupPostgresExts < ActiveRecord::Migration
def self.up
sql = <<-SQL
CREATE EXTENSION IF NOT EXISTS hstore;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS plpgsql;
SQL
execute sql
end
def self.down
execute "DROP EXTENSION hstore"
execute "DROP EXTENSION \"uuid-ossp\""
execute "DROP EXTENSION plpgsql"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment