Skip to content

Instantly share code, notes, and snippets.

@yanguango
Created September 27, 2010 06:38
Show Gist options
  • Save yanguango/598698 to your computer and use it in GitHub Desktop.
Save yanguango/598698 to your computer and use it in GitHub Desktop.
class CreateProductPreferences < ActiveRecord::Migration
def self.up
create_table :product_preferences, :force => true do |t|
t.integer :user_id
t.integer :product_id
t.float :preference
t.timestamps
end
end
def self.down
drop_table :product_preferences
end
end
class AddIndexToProductPreferences < ActiveRecord::Migration
def self.up
remove_column :product_preferences, :id
change_column :product_preferences, :user_id, :integer, :limit => 8, :null => false
change_column :product_preferences, :product_id, :integer, :limit => 8, :null => false
change_column :product_preferences, :preference, :float, :null => false
add_index :product_preferences, :user_id
add_index :product_preferences, :product_id
execute("ALTER TABLE product_preferences ADD PRIMARY KEY user_id")
execute("ALTER TABLE product_preferences ADD PRIMARY KEY product_id")
end
def self.down
add_column :product_preferences, :id
add_index :product_preferences, :id
change_column :product_preferences, :user_id, :integer
change_column :product_preferences, :product_id, :integer
change_column :product_preferences, :preference, :float
remove_index :product_preferences, :user_id
remove_index :product_preferences, :product_id
execute("ALTER TABLE product_preferences REMOVE PRIMARY KEY user_id")
execute("ALTER TABLE product_preferences REMOVE PRIMARY KEY product_id")
execute("ALTER TABLE product_preferences ADD PRIMARY KEY id")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment