Skip to content

Commit

Permalink
import / export tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed Mar 4, 2014
1 parent ebcdbec commit 98ab5d4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ see more at: https://developers.google.com/bigquery/query-reference#joins
@user.save
```

```ruby
User.create([{name: "miki"}, {name: "jara"}])

```

NOTE: by default the adapter will set Id values as an SecureRandom.hex, and for now all the foreign keys are created as a STRING type

#### Deletion and edition of single rows:
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record/connection_adapters/bigquery_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def bigquery_export(bucket_location = nil)
def bigquery_load(bucket_location = [])
bucket_location = bucket_location.empty? ? ["#{cfg[:database]}/#{table_name}.json"] : bucket_location
cfg = connection_config
fields = columns.map{|o| {name: o.name, type: o.type} }
fields = columns.map{|o| {name: o.name, type: o.sql_type, mode: "nullable" } }
GoogleBigquery::Jobs.load(cfg[:project],
cfg[:database],
table_name,
Expand Down
2 changes: 1 addition & 1 deletion lib/google_bigquery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ module GoogleBigquery
require File.join(File.dirname(__FILE__), *%w[google_bigquery railtie]) if ::Rails.version.to_s >= '3.1'
end

end
end
9 changes: 5 additions & 4 deletions lib/google_bigquery/jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def self.list(project_id, body={})
end

#export data
#TODO: get mappings for formatting options
def self.export(project_id, dataset_id, table_id, bucket_location)
body = {'projectId'=> project_id,
'configuration'=> {
Expand All @@ -77,7 +78,7 @@ def self.export(project_id, dataset_id, table_id, bucket_location)
:body_object=> body,
:parameters=> {"projectId"=> project_id}
)

job_id = JSON.parse(res.body)["jobReference"]["jobId"]
puts 'Waiting for export to complete..'

Expand All @@ -98,15 +99,15 @@ def self.export(project_id, dataset_id, table_id, bucket_location)
end
end

#TODO: get mappings for formatting options
def self.load(project_id, dataset_id, table_id, sources, fields)
body = { 'projectId'=> project_id,
'configuration'=> {
'load'=> {
'sourceFormat' => "NEWLINE_DELIMITED_JSON",
'sourceUri' => sources.first,
'sourceUris' => sources,
'schema' => {
"fields"=> fields
},

'destinationTable'=> {
'projectId'=> project_id,
'datasetId'=> dataset_id,
Expand Down
28 changes: 14 additions & 14 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@
ActiveRecord::Schema.define(version: 20140227015551) do

create_table "log_data", force: true do |t|
t.string "name"
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
t.string "name"
t.string "description"
t.timestamp "created_at"
t.timestamp "updated_at"
end

create_table "posts", force: true do |t|
t.string "title"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "title"
t.string "user_id"
t.timestamp "created_at"
t.timestamp "updated_at"
end

create_table "users", force: true do |t|
t.string "name"
t.boolean "admin"
t.integer "phone"
t.datetime "created_at"
t.datetime "updated_at"
t.string "last_name"
t.string "name"
t.boolean "admin"
t.integer "phone"
t.timestamp "created_at"
t.timestamp "updated_at"
t.string "last_name"
end

end

0 comments on commit 98ab5d4

Please sign in to comment.