Skip to content

Instantly share code, notes, and snippets.

@elrayle
Created November 14, 2017 14:37
Show Gist options
  • Save elrayle/2076a00088c9f741047967502b3725e2 to your computer and use it in GitHub Desktop.
Save elrayle/2076a00088c9f741047967502b3725e2 to your computer and use it in GitHub Desktop.
Process sqlite separately
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
@elrayle
Copy link
Author

elrayle commented Nov 14, 2017

class AddSourceTypeToPermissionTemplates < ActiveRecord::Migration[4.2]
  def self.up
    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 AND the table is empty, we can rename
      rename_column :permission_templates, :admin_set_id, :source_id
      add_column :permission_templates, :source_type, :string, default: 'admin_set'
    else
      # otherwise, sqlite throws an error when renaming a foreign key that already has values,
      # so we'll rename the table and copy the data into a new version of the table
      rename_table :permission_templates, :permission_templates_copy
      create_table :permission_templates do |t|
        t.string :source_id
        t.string :source_type
        t.string :visibility
        t.timestamps
        t.date :release_date
        t.string :release_period
      end
      add_index :permission_templates, :source_id
      copy_data_up
      # drop_table :permission_templates_copy
    end
  end

  def self.down
    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 AND the table is empty, we can rename
      rename_column :permission_templates, :source_id, :admin_set_id
      drop_column :permission_templates, :source_type
    else
      # otherwise, sqlite throws an error when renaming a foreign key that already has values,
      # so we'll rename the table and copy the data into a new version of the table
      rename_table :permission_templates, :permission_templates_copy
      create_table :permission_templates do |t|
        t.string :admin_set_id
        t.string :visibility
        t.timestamps
        t.date :release_date
        t.string :release_period
      end
      add_index :permission_templates, :admin_set_id
      copy_data_down
      # drop_table :permission_templates_copy
    end
  end

  def copy_data_up
    sql_query = "INSERT INTO permission_templates (source_id, source_type, visibility, created_at, updated_at, release_date, release_period)" \
                "SELECT admin_set_id, 'admin_set', visibility, created_at, updated_at, release_date, release_period FROM permission_templates_copy"
    ActiveRecord::Base.connection.execute sql_query
  end

  def copy_data_down
    sql_query = "INSERT INTO permission_templates (admin_set_id, visibility, created_at, updated_at, release_date, release_period)" \
                "SELECT source_id, visibility, created_at, updated_at, release_date, release_period FROM permission_templates_copy"
    ActiveRecord::Base.connection.execute sql_query
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment