Last active
June 10, 2016 02:06
-
-
Save holysugar/6f67d642d08b4ddf9c7c357a9d573814 to your computer and use it in GitHub Desktop.
AR::Base の columns から bq で使えるスキーマの json ファイル生成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Author
holysugar
commented
Jun 10, 2016
- nullable のような制約について mysql のものを bq にそのまま付けるべきかはやや悩ましいところある
- あと time 列とかを別途追加すべきかとか
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment