Skip to content

Instantly share code, notes, and snippets.

@raven-rock
Created July 28, 2019 03:07
Show Gist options
  • Save raven-rock/09d9fa0e4f7291890ba8eab81e35107c to your computer and use it in GitHub Desktop.
Save raven-rock/09d9fa0e4f7291890ba8eab81e35107c to your computer and use it in GitHub Desktop.
MySQL integer type from integer range
def mysql_int_type(int_range)
case
when (0..255) .cover?(int_range) then "tinyint unsigned"
when (0..65535) .cover?(int_range) then "smallint unsigned"
when (0..16777215) .cover?(int_range) then "mediumint unsigned"
when (0..4294967295) .cover?(int_range) then "int unsigned"
when (0..18446744073709551615) .cover?(int_range) then "bigint unsigned"
when ( -128..127) .cover?(int_range) then "tinyint"
when ( -32768..32767) .cover?(int_range) then "smallint"
when ( -8388608..8388607) .cover?(int_range) then "mediumint"
when ( -2147483648..2147483647) .cover?(int_range) then "int"
when ( -9223372036854775808..9223372036854775807) .cover?(int_range) then "bigint"
else "varchar(#{int_range.last.to_s.length})"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment