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

Add short-hand methods for text and blob types in MySQL #21688

Merged
merged 1 commit into from Jan 6, 2016

Conversation

Projects
None yet
7 participants
@kamipo
Member

kamipo commented Sep 20, 2015

In Pg and Sqlite3, :text and :binary have variable unlimited length.
But in MySQL, these have limited length for each types (see also #21591, #21619).
This change adds short-hand methods for each text and blob types.

Before:

create_table :foos do |t|
  t.blob   :tiny_blob,   limit: 255
  t.binary :medium_blob, limit: 16777215
  t.binary :long_blob,   limit: 2147483647
  t.text   :tiny_text,   limit: 255
  t.text   :medium_text, limit: 16777215
  t.text   :long_text,   limit: 2147483647
end

Short-hand methods:

create_table :foos do |t|
  t.tinyblob   :tiny_blob
  t.mediumblob :medium_blob
  t.longblob   :long_blob
  t.tinytext   :tiny_text
  t.mediumtext :medium_text
  t.longtext   :long_text
end
@rails-bot

This comment has been minimized.

Show comment
Hide comment
@rails-bot

rails-bot Sep 20, 2015

r? @arthurnn

(@rails-bot has picked a reviewer for you, use r? to override)

r? @arthurnn

(@rails-bot has picked a reviewer for you, use r? to override)

Show outdated Hide outdated ...verecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
when 0x1000..0xffffffff; "blob(#{limit})"
when nil, 0x1000..0xffff; 'blob'
when 0x10000..0xffffff; 'mediumblob'
when 0x1000000..0xffffffff; 'longblob'

This comment has been minimized.

@matthewd

matthewd Sep 20, 2015

Member

Why this change? It seems better to leave this up to MySQL to me.

@matthewd

matthewd Sep 20, 2015

Member

Why this change? It seems better to leave this up to MySQL to me.

This comment has been minimized.

@kamipo

kamipo Sep 20, 2015

Member

This is the same way as text_to_sql:

      def text_to_sql(limit) # :nodoc:
        case limit
        when 0..0xff;               'tinytext'
        when nil, 0x100..0xffff;    'text'
        when 0x10000..0xffffff;     'mediumtext'
        when 0x1000000..0xffffffff; 'longtext'
        else raise(ActiveRecordError, "No text type has byte length #{limit}")
        end
      end

blob(2147483647) is not usual. It is longblob usually.

@kamipo

kamipo Sep 20, 2015

Member

This is the same way as text_to_sql:

      def text_to_sql(limit) # :nodoc:
        case limit
        when 0..0xff;               'tinytext'
        when nil, 0x100..0xffff;    'text'
        when 0x10000..0xffffff;     'mediumtext'
        when 0x1000000..0xffffffff; 'longtext'
        else raise(ActiveRecordError, "No text type has byte length #{limit}")
        end
      end

blob(2147483647) is not usual. It is longblob usually.

@jeremy

This comment has been minimized.

Show comment
Hide comment
@jeremy

jeremy Sep 22, 2015

Member

I like this. We've seen a lot of stubbed toes from people trying to figure out the correct :size to trigger the desired MySQL type.

Member

jeremy commented Sep 22, 2015

I like this. We've seen a lot of stubbed toes from people trying to figure out the correct :size to trigger the desired MySQL type.

@pixeltrix

This comment has been minimized.

Show comment
Hide comment
@pixeltrix

pixeltrix Sep 22, 2015

Member

Rather than using t.mediumblob :column why not an option, e.g: t.binary :column, size: :medium - that way the PostgreSQL adapter can just ignore it and the migration is still database independent. I know that trying to write database independent schema.rb is a losing battle so it probably doesn't make a difference in the long run. 😄

Member

pixeltrix commented Sep 22, 2015

Rather than using t.mediumblob :column why not an option, e.g: t.binary :column, size: :medium - that way the PostgreSQL adapter can just ignore it and the migration is still database independent. I know that trying to write database independent schema.rb is a losing battle so it probably doesn't make a difference in the long run. 😄

@jeremy

This comment has been minimized.

Show comment
Hide comment
@jeremy

jeremy Sep 22, 2015

Member

The size label is an improvement too. People look for known terminology, though: "where is bigint" and "I need a mediumtext."

Member

jeremy commented Sep 22, 2015

The size label is an improvement too. People look for known terminology, though: "where is bigint" and "I need a mediumtext."

Add short-hand methods for text and blob types in MySQL
In Pg and Sqlite3, `:text` and `:binary` have variable unlimited length.
But in MySQL, these have limited length for each types (ref #21591, #21619).
This change adds short-hand methods for each text and blob types.

Example:

    create_table :foos do |t|
      t.tinyblob   :tiny_blob
      t.mediumblob :medium_blob
      t.longblob   :long_blob
      t.tinytext   :tiny_text
      t.mediumtext :medium_text
      t.longtext   :long_text
    end
@kamipo

This comment has been minimized.

Show comment
Hide comment
Member

kamipo commented Jan 5, 2016

r? @jeremy

@rails-bot rails-bot assigned jeremy and unassigned arthurnn Jan 5, 2016

@rafaelfranca rafaelfranca merged commit 4d4239f into rails:master Jan 6, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

rafaelfranca added a commit that referenced this pull request Jan 6, 2016

Merge pull request #21688 from kamipo/add_text_and_blob_shorthand_met…
…hods

Add short-hand methods for text and blob types in MySQL

@kamipo kamipo deleted the kamipo:add_text_and_blob_shorthand_methods branch Jan 6, 2016

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