Skip to content

Instantly share code, notes, and snippets.

@holysugar
Last active June 10, 2016 02:06
Show Gist options
  • Save holysugar/6f67d642d08b4ddf9c7c357a9d573814 to your computer and use it in GitHub Desktop.
Save holysugar/6f67d642d08b4ddf9c7c357a9d573814 to your computer and use it in GitHub Desktop.
AR::Base の columns から bq で使えるスキーマの json ファイル生成
module BQSchema
# conversion AR to BQ
# http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#method-i-column
# https://cloud.google.com/bigquery/data-types
def to_bq_schema
self.columns.map{|c|
# FIXME decimal and time type conversion
type = \
case c.type
when :integer, :primary_key
"integer"
when :decimal
"integer" # Really?
when :float
"float"
when :string, :text, :time
"string"
when :binary
"bytes"
when :datetime, :date
"timestamp"
when :boolean
"boolean"
else
c.type.to_s
end
type = "timestamp" if c.name =~ /_time$/i # FIXME application matter. yield ?
{ name: c.name, type: type }
}
end
end
# SomeModel.extend BQSchema
# puts SomeModel.to_bq_schema.to_json
@holysugar
Copy link
Author

  • nullable のような制約について mysql のものを bq にそのまま付けるべきかはやや悩ましいところある
  • あと time 列とかを別途追加すべきかとか

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