Skip to content

Instantly share code, notes, and snippets.

@tian-im
Last active April 24, 2016 16:58
Show Gist options
  • Save tian-im/44696a548ded89197659cf42ebae0752 to your computer and use it in GitHub Desktop.
Save tian-im/44696a548ded89197659cf42ebae0752 to your computer and use it in GitHub Desktop.
Find out all data types that ActiveRecord supports.
# All data types that ActiveRecord supports
PostgresqlModel.connection.type_map.try do |type_map|
type_map.instance_variable_get('@mapping').keys.map do |key|
key.is_a?(String) ? key : nil # others are oid type
end.compact.uniq
end
# PostgreSQL
# [ "bit", "bool", "box", "bpchar", "bytea", "char", "cidr", "circle", "citext", "date", "float4", "float8", "hstore",
# "inet", "int2", "int4", "int8", "interval", "json", "jsonb", "line", "lseg", "ltree", "macaddr", "money", "name", "numeric",
# "oid", "path", "point", "polygon", "text", "time", "timestamp", "timestamptz", "tsvector", "uuid", "varbit", "varchar", "xml" ]
MysqlModel.connection.connection.type_map.try do |type_map|
type_map.instance_variable_get('@mapping').keys.map do |key|
key.source.gsub %r(\^|(\\.*\Z)), ''
end.compact.uniq
end
# Mysql
# [ "bigint", "binary", "bit", "blob", "boolean", "char", "clob", "date", "datetime", "decimal", "double", "enum", "float",
# "int", "longblob", "longtext", "mediumblob", "mediumint", "mediumtext", "number", "numeric", "set", "smallint", "text",
# "time", "timestamp", "tinyblob", "tinyint", "tinytext", "year" ]
SqliteModel.connection.type_map.try do |type_map|
type_map.instance_variable_get('@mapping').keys.map do |key|
key.source.gsub %r(\^|(\\.*\Z)), ''
end.compact.uniq
end
# Sqlite
# [ "binary", "blob", "boolean", "char", "clob", "date", "datetime", "decimal", "double", "float", "int", "number", "numeric",
# "text", "time", "timestamp" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment