Coalesce favorites tables

This commit is contained in:
Liam P. White 2019-02-19 22:37:45 -05:00
parent f85f70f89d
commit 47d1e4fc20
2 changed files with 68 additions and 3429 deletions

View File

@ -0,0 +1,43 @@
class CoalesceFavoritesTables < ActiveRecord::Migration[5.2]
def up
create_table :temp_faves do |t|
t.integer :user_id, type: "integer"
t.integer :post_id, type: "integer"
end
# Remove the trigger and insert function
execute <<~SQL
DROP FUNCTION favorites_insert_trigger CASCADE;
SQL
# Copy data from all the descendant tables
execute <<~SQL
INSERT INTO temp_faves (user_id, post_id)
SELECT user_id, post_id FROM favorites;
SQL
# Add indexes and constraints after all the importing
# is done
add_index :temp_faves, :user_id
add_index :temp_faves, :post_id
change_column_null :temp_faves, :user_id, false
change_column_null :temp_faves, :post_id, false
add_foreign_key :temp_faves, :users
add_foreign_key :temp_faves, :posts
# Remove all the descendant tables
(0..99).each do |i|
drop_table "favorites_#{i}"
end
# Then remove the original and rename in place
drop_table :favorites
rename_table :temp_faves, :favorites
end
def down
fail ActiveRecord::IrreversibleMigration
end
end

File diff suppressed because it is too large Load Diff