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

Make the primary key to use for the migration configurable #49

Closed
wants to merge 4 commits into from

Conversation

airhorns
Copy link

This adds a new option to Lhm.change_table called order_column. It allows the specification of a primary key column other than id to use during the migration, or a to specify what column should be used in the absence of a primary key. At Shopify we're moving a whole bunch of tables over to BIGINT(20) sized IDs and some of our freakier tables don't have primary keys or have weird ones, so at least we need this.

Implementation wise the kind of uncomfortable state about what column to use now needs to be given to everyone. I think an appropriate place is the migration, since most objects in the system have access to it and they all need to agree on the value. This required passing the options down a couple more objects for the Migrator creating the Migration to have access, but I think thats acceptable for this increase in functionality.

/cc @dylanahsmith @arthurnn

Instead of relying on a table to have a primary key, we now accept the order_column option and put that everywhere. This kind of uncomfortable state about the migration now needs to be given to everyone and I think an appropriate place is the migration, since most objects in the system have access to it and they all need to agree on the value. This required passing the options down a couple more objects for the Migrator creating the Migration to have access, but I think thats acceptable for this increase in functionality.
end

def can_use_order_column?(column)
numeric_type?(@columns[column][:type])
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about adding a check whether the picked column has a unique key constraint? People can easily shoot themselves in the foot otherwise.

Copy link
Author

Choose a reason for hiding this comment

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

I think thats a great idea, however I actually do (somewhat embarassingly) have a situation where I don't have a uniqueness constraint on my order_column. I think this suggests the constraint of uniqueness is good to recommend but I don't think it is critical for LHM to run so I almost don't want to enforce it.

We don't have one for speed reasons, and because the gem managing the table does a kind of upsert style thing where it will always use an existing row for a given order_column value or just create it if it doesn't exist. That said my case is definitely special so I would be willing to add the constraint and then somehow remove it in my own code if you think it should definitely make it in.

@grobie
Copy link
Contributor

grobie commented Jul 21, 2013

Looks good!

@airhorns
Copy link
Author

Thanks for the feedback @grobie !

@camilo
Copy link
Contributor

camilo commented Apr 3, 2014

ping /cc @arthurnn

@arthurnn
Copy link
Contributor

arthurnn commented Apr 3, 2014

@airhorns do you mind squashing this in one commit, and rebase against latest master. I will give a final review and merge it in!

@ags
Copy link

ags commented Sep 29, 2014

@airhorns Any chance of this getting squashed and rebased? It'd be much appreciated :)

@iamliamnorton
Copy link

@airhorns is there anything we can do to get this into master?

@airhorns
Copy link
Author

airhorns commented Jul 9, 2015

@camilo do you care about this at all?

@airhorns airhorns closed this Jul 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

6 participants