Created
July 22, 2012 03:05
-
-
Save ivanvanderbyl/3158133 to your computer and use it in GitHub Desktop.
Using 128bit UUIDs with ActiveRecord
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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