Created
November 14, 2017 14:37
-
-
Save elrayle/2076a00088c9f741047967502b3725e2 to your computer and use it in GitHub Desktop.
Process sqlite separately
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 AddSourceTypeToPermissionTemplates < ActiveRecord::Migration[4.2] | |
| def self.up | |
| add_column :permission_templates, :source_type, :string, default: 'admin_set' | |
| unless connection.adapter_name.downcase.starts_with?('sqlite') && Hyrax::PermissionTemplate.any? | |
| # if this is not sqlite, then we can simply rename | |
| # if this is sqlite, we can rename if the table is empty | |
| rename_column :permission_templates, :admin_set_id, :source_id | |
| else | |
| # otherwise, sqlite throws an error when renaming a foreign key that already has values, | |
| # so we'll add a column and copy the data in. | |
| # NOTE: The old column :admin_set_id will still exist, but not be used. | |
| remove_index :permission_template_accesses, :permission_template | |
| rename_column :permission_templates, :admin_set_id, :source_id | |
| # add_column :permission_templates, :source_id, :string | |
| # add_index :permission_templates, :source_id | |
| # ActiveRecord::Base.connection.execute "UPDATE permission_templates SET source_id = admin_set_id" | |
| end | |
| end | |
| def self.down | |
| remove_column :permission_templates, :source_type | |
| unless connection.adapter_name.downcase.starts_with?('sqlite') && Hyrax::PermissionTemplate.any? | |
| # if this is not sqlite, then we can simply rename | |
| # if this is sqlite, we can rename if the table is empty | |
| rename_column :permission_templates, :source_id, :admin_set_id | |
| else | |
| # otherwise, sqlite throws an error when renaming a foreign key that already has values, | |
| # so we'll add a column and copy the data in. | |
| # NOTE: The old column :admin_set_id will still exist, but not be used. | |
| remove_column :permission_templates, :source_id | |
| end | |
| end | |
| end |
Author
elrayle
commented
Nov 14, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment