Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant index on :task_name #346

Merged
merged 1 commit into from
Feb 26, 2021

Conversation

adrianna-chang-shopify
Copy link
Contributor

For: #342

Confirmed that the index on just :task_name is redundant given our composite index on [:task_name, :created_at]. We can remove it.

I think we need to remove the index from the table itself in a change_table block (instead of using https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-remove_index) due to LHM constraints (as mentioned here, modifying indexes outside of table blocks seems to pose problems for LHMs).

@sambostock
Copy link
Contributor

I suspect the reason #239 was able to get around LHM constraints was because it consolidated everything into a create_table block. I feel like change_table or add_index would both have the same problems for LHM (don't modify existing tables without using LHM). 🤔

@adrianna-chang-shopify
Copy link
Contributor Author

Oof yeah good shout @sambostock, whether we use change_table or not likely won't solve the LHM issue because it's still modifying the table 🤦‍♀️ Didn't think that one through very hard lol.

Given the apps that use LHMs are a minority, we should just be able to ship this, and any app that requires use of LHMs can delete this migration and rewrite it as an LHM if need be (I think only Partners had an issue with this)?

@sambostock
Copy link
Contributor

While I do agree it's probably fine to ship as is and let consumers rewrite migrations as LHM, Shopify's data stores best practices recommend everyone use LHM for all table changes, so I'm not sure how uncommon it is internally. That said, the only other thing we could do would be to try to detect LHM and generate an appropriate migration, but I don't know if that makes sense.

# frozen_string_literal: true
class RemoveIndexOnTaskName < ActiveRecord::Migration[6.1]
def up
change_table(:maintenance_tasks_runs) do |t|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to use remove_index and add_index directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reason I could see using this over remove_index and add_index directly is that it makes it easier to change it to an LHM:

class RemoveIndexOnTaskName < ActiveRecord::Migration[6.1]
  def up
    change_table(:maintenance_tasks_runs) do |t|
      t.remove_index(:task_name)
    end
  end

  def down
    change_table(:maintenance_tasks_runs) do |t|
      t.index(:task_name)
    end
  end
end
require 'lhm'

class RemoveIndexOnTaskName < ActiveRecord::Migration[6.1]
  def up
    Lhm.change_table :maintenance_tasks_runs do |t|
      t.remove_index(:task_name)
    end
  end

  def down
    Lhm.change_table :maintenance_tasks_runs do |t|
      t.index(:task_name)
    end
  end
end

Not sure if if this is something we care about that much though 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup it's really nice if it lets people adapt it more easily to LHM.

@adrianna-chang-shopify
Copy link
Contributor Author

adrianna-chang-shopify commented Feb 26, 2021

Yeah, it's a really good point @sambostock - it's tricky to find the balance between making this meet our internal best practices vs trying to keep things conventional for use externally. Looking at the dependency list in SDB, there are a number of gems using Shopify/lhm 😬 But I'm not sure it's worth the complexity of having to detect and generate new LHM migrations.. Maybe a good middle point would be to document rewriting one of our migrations as an LHM in the README?

Edit: After reviewing how LHMs can be used with AR migrations, if we keep the change_table syntax it's very simple to change the migration to an LHM, so I think we can rely on users to make the changes if they need an LHM.

@adrianna-chang-shopify adrianna-chang-shopify merged commit a2a2d7c into main Feb 26, 2021
@adrianna-chang-shopify adrianna-chang-shopify deleted the remove-redundant-index branch February 26, 2021 19:52
@adrianna-chang-shopify adrianna-chang-shopify temporarily deployed to rubygems March 1, 2021 21:40 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants