Skip to content

Commit

Permalink
Add Down Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
mamhoff committed May 6, 2020
1 parent 4b60c7b commit 2196e80
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions db/migrate/20200504210159_remove_site_id_from_nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,22 @@ def up
end

def down
# This IS, in fact, reversible - but it's cumbersome to do. If someone tells me it benefits them
# I will implement.
raise ActiveRecord::IrreversibleMigration
add_column :alchemy_nodes, :site_id, :integer, null: true
sql = <<~SQL
UPDATE alchemy_nodes
SET site_id = (
SELECT alchemy_languages.site_id FROM alchemy_languages WHERE alchemy_nodes.language_id = alchemy_languages.id
) WHERE
EXISTS (
SELECT *
FROM alchemy_languages
WHERE alchemy_languages.id = alchemy_nodes.language_id
);
SQL

connection.execute(sql)
change_column :alchemy_nodes, :site_id, :integer, null: false
add_index :alchemy_nodes, :site_id
add_foreign_key :alchemy_nodes, :alchemy_sites, column: :site_id
end
end

0 comments on commit 2196e80

Please sign in to comment.