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
Avoid work in a initializer for IntegerMigrator #2102
Conversation
Can you explain why you would want this? Are you creating IntegerMigrator objects without using them? Is this a style issue, are you trying to optimize something currently slow, or is there another reason you believe we should make this change? I disagree that "Initializing object should be a cheap operation." is true in general. There are cases where the statement is true (e.g. Sequel::Model instances), but this doesn't appear to be one of those cases, so I'm hoping you explain the reasoning here. |
I am trying to build Rollback solution allowing to work both for IntegerMigrator and TimestampMigrator. It has interface: rollback = Sequel::Rollback.new(db, migrations_dir)
rollback.run It is expected to rollback the last applied migration, just like Actually, I am going to contribute this approach. But this PR is my test one, to check how difficult it is to contribute to sequel actually. I can't say, that I have real performance issues, but I believe, code in contructor is common anti-pattern:
That's why I picked up this case to fix. PS Thanks for quick response! |
This would work for IntegerMigrator, since when using IntegerMigrator, you can be sure in which order migrations were applied. You cannot be sure in which order the TimestampMigrator applied migrations. When using TimestampMigrator, the migration with the highest version is not necessarily the one most recently applied, and Sequel does not keep track of when migrations were applied. You could assume that the migration with the highest version is the one most recently applied, and that would be correct most of the time, but not all of the time. Note that for IntegerMigrator, what you want is already supported by the
I do not know Java well, maybe the problem the author is describing is a significant issue in Java. However, Java is not Ruby, and I don't think having code in constructors is a problem in Ruby. There are situations where you want to avoid it, to keep object initialization fast, as Sequel::Model does. That's because it's common to create hundreds or thousands of Sequel::Model instances per request and not sure most of the API per instance. For the migrators, usually there is one created per process, and doing the work in the constructor is simpler and results in more readable code. I'm going to reject this as I don't think it improves the code. Hopefully this does not discourage you from contributing to Sequel in the future. |
OK. Yes, you are write about order for TimestampMigrations, but it is often used in development, when you want to rollback last created. It works great for rails. Also even you don’t know the exact order, rollback should work anyways because these migrations were coded in independent features. I think it would be nice to have |
Sequel 5.75.0 will be released in a couple days, and will support |
We can't guarantee the order when we use |
With With The entire reason for TimestampMigrator's existence is to be able to apply migrations out of order. If you only deal with in-order migrations, then the IntegerMigrator is a much better choice. |
Before run, migrator does a lot of work in an initializer. Initializing object should be a cheap operation. Let's avoid any work in there.