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

Extract the short-hand column methods into ColumnMethods #19030

Merged
merged 3 commits into from Feb 24, 2015

Conversation

kamipo
Copy link
Member

@kamipo kamipo commented Feb 22, 2015

Definition of the short-hand column methods is duplicated in Table and TableDefinition.
However, bigint have not been added to Table. Such problem can be prevented by
extracting the methods into ColumnMethods.

In addition, the short-hand column methods should be able to define multiple columns,
but PostgreSQL::ColumnMethods was not able to.

:uuid,
:xml,
].each do |column_type|
define_method column_type do |*args, **options|
Copy link
Contributor

Choose a reason for hiding this comment

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

Methods defined using define_method are slightly slower to execute than those defined by def so not recommended for hotspots. Similarly, splatting and globbing *args for most methods that only have one (all but xml and tsvector here) could be a performance penalty.

Perhaps make a define_column_type factory method that goes something like define_column_for('something', args: boolean) (false by default) which would generate the appropriate method using the pattern:

eval <<-EOS
  def(...)
    ...
  end
end

Then you can build up on that further with aggregate method like:

  define_column_types_for(:bigserial, :bigint, ...)
  define_column_types_for(:xml, :tsvector, args: true)

@kamipo kamipo force-pushed the extract_short_hand_column_methods branch from 4ef2630 to 621f17a Compare February 22, 2015 21:16
@kamipo
Copy link
Member Author

kamipo commented Feb 22, 2015

@egilburg Thank you for your review. I think the migration is not a hotspot of the performance, but I agree to prefer eval than define_method. so it was changed.

with_change_table do |t|
@connection.expect :add_column, nil, [:delete_me, :foo, :xml, {}]
@connection.expect :add_column, nil, [:delete_me, :bar, :xml, {}]
t.xml :foo, :bar
Copy link
Member Author

Choose a reason for hiding this comment

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

I think it is a bug that the short-hand methods can't define multiple columns.

@kamipo kamipo force-pushed the extract_short_hand_column_methods branch from 621f17a to 24eb440 Compare February 22, 2015 22:49
@kamipo
Copy link
Member Author

kamipo commented Feb 22, 2015

@sgrif Thank you for your review. I have changed that do not meta-programming for the API site.

rafaelfranca added a commit that referenced this pull request Feb 24, 2015
Extract the short-hand column methods into `ColumnMethods`
@rafaelfranca rafaelfranca merged commit 5cde302 into rails:master Feb 24, 2015
@kamipo kamipo deleted the extract_short_hand_column_methods branch February 24, 2015 01:42
kamipo added a commit to kamipo/rails that referenced this pull request Feb 24, 2015
Only `primary_key` should be extracted by d47357e in rails#19030, but
`new_coclumn_definition` was also extracted because rails#17631 is merged
previously, then rails#19030 is auto merged without conflicts.

This commit is for move back `new_column_definition` into
`TableDefinition`.
kamipo added a commit to kamipo/rails that referenced this pull request Nov 7, 2015
The short-hand methods was added by rails#19030.
schema dumping support was added by rails#19185.

This commit is follow up to these PRs.
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.

None yet

4 participants