bigquery_migration 0.1.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +19 -0
- data/README.md +107 -0
- data/Rakefile +10 -0
- data/bigquery_migration.gemspec +31 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/example/example.yml +22 -0
- data/example/schema.json +22 -0
- data/exe/bq_migrate +4 -0
- data/lib/bigquery_migration.rb +29 -0
- data/lib/bigquery_migration/action.rb +85 -0
- data/lib/bigquery_migration/action_runner.rb +60 -0
- data/lib/bigquery_migration/bigquery_wrapper.rb +675 -0
- data/lib/bigquery_migration/cli.rb +105 -0
- data/lib/bigquery_migration/config_loader.rb +51 -0
- data/lib/bigquery_migration/error.rb +6 -0
- data/lib/bigquery_migration/hash_util.rb +35 -0
- data/lib/bigquery_migration/logger.rb +45 -0
- data/lib/bigquery_migration/schema.rb +388 -0
- data/lib/bigquery_migration/time_with_zone.rb +38 -0
- data/lib/bigquery_migration/version.rb +3 -0
- metadata +183 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 305437e02b080f7394ac109f9ccde37f7cf9a02f
|
4
|
+
data.tar.gz: c3f890726c06b6cfa7830240bf8f701a7957d14f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4ace45d55a36b4c62e1561c754ac83c18ae7ed1b8148bde2c8e8c40136a852346ee7a30ebda0731e5ee8d390bf3e34d660393c5d040adae1c9889de0f03cd5f9
|
7
|
+
data.tar.gz: 7626f505d9580eee34731800bee1e1a655e5d64be5e192895898e3ac5d1fbea21ffc47d134cfbfa6ceaa85e52e63e18506bf5d9eea87b4dacfed4654513638b1
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at sonots@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# BigqueryMigration
|
2
|
+
|
3
|
+
BigqueryMigraiton is a tool or a ruby library to migrate (or alter) BigQuery table schema.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'bigquery_migration'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install bigquery_migration
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### CLI
|
24
|
+
|
25
|
+
config.yml
|
26
|
+
|
27
|
+
```yaml
|
28
|
+
bigquery: &bigquery
|
29
|
+
json_keyfile: your-project-000.json
|
30
|
+
dataset: your_dataset_name
|
31
|
+
table: your_table_name
|
32
|
+
|
33
|
+
actions:
|
34
|
+
- action: create_dataset
|
35
|
+
<<: *bigquery
|
36
|
+
- action: migrate_table
|
37
|
+
<<: *bigquery
|
38
|
+
columns:
|
39
|
+
- { name: 'timestamp', type: 'TIMESTAMP' }
|
40
|
+
- name: 'record'
|
41
|
+
type: 'RECORD'
|
42
|
+
fields:
|
43
|
+
- { name: 'string', type: 'STRING' }
|
44
|
+
- { name: 'integer', type: 'INTEGER' }
|
45
|
+
```
|
46
|
+
|
47
|
+
Run
|
48
|
+
|
49
|
+
```
|
50
|
+
$ bundle exec bq_migrate run config.yml # dry-run
|
51
|
+
$ bundle exec bq_migrate run config.yml --exec
|
52
|
+
```
|
53
|
+
|
54
|
+
### Library
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
require 'bigquery_migration'
|
58
|
+
|
59
|
+
config = {
|
60
|
+
json_keyfile: '/path/to/your-project-000.json'
|
61
|
+
dataset: 'your_dataset_name'
|
62
|
+
table: 'your_table_name'
|
63
|
+
}
|
64
|
+
columns = [
|
65
|
+
{ name: 'string', type: 'STRING' },
|
66
|
+
{ name: 'record', type: 'RECORD', fields: [
|
67
|
+
{ name: 'integer', type: 'INTEGER' },
|
68
|
+
{ name: 'timestamp', type: 'TIMESTAMP' },
|
69
|
+
] }
|
70
|
+
]
|
71
|
+
|
72
|
+
migrator = BigqueryMigration.new(config)
|
73
|
+
migrator.migrate_table(columns: columns)
|
74
|
+
# migrator.migrate_table(schema_file: '/path/to/schema.json')
|
75
|
+
```
|
76
|
+
|
77
|
+
## Development
|
78
|
+
|
79
|
+
### Run example:
|
80
|
+
|
81
|
+
Prepare `example/your-project-000.json`, then
|
82
|
+
|
83
|
+
```
|
84
|
+
$ bundle exec bq_migrate run example/example.yml # dry-run
|
85
|
+
$ bundle exec bq_migrate run example/example.yml --exec
|
86
|
+
```
|
87
|
+
|
88
|
+
### Run test:
|
89
|
+
|
90
|
+
```
|
91
|
+
$ bundle exec rake test
|
92
|
+
```
|
93
|
+
|
94
|
+
To run tests which directly connects to BigQuery, prepare `example/your-project-000.json`, then
|
95
|
+
|
96
|
+
````
|
97
|
+
$ bundle exec rake test
|
98
|
+
```
|
99
|
+
|
100
|
+
## Contributing
|
101
|
+
|
102
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sonots/bigquery_migration. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
103
|
+
|
104
|
+
|
105
|
+
## License
|
106
|
+
|
107
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'bigquery_migration/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "bigquery_migration"
|
8
|
+
spec.version = BigqueryMigration::VERSION
|
9
|
+
spec.authors = ["Naotoshi Seo"]
|
10
|
+
spec.email = ["sonots@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Migrate BigQuery table schema}
|
13
|
+
spec.description = %q{Migrate BigQuery table schema.}
|
14
|
+
spec.homepage = "https://github.com/sonots/bigquery_migration"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "google-api-client"
|
23
|
+
spec.add_dependency "tzinfo"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "pry-byebug"
|
28
|
+
spec.add_development_dependency "test-unit"
|
29
|
+
spec.add_development_dependency "test-unit-rr"
|
30
|
+
spec.add_development_dependency "test-unit-power_assert"
|
31
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "bigquery_migration"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/example/example.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
bigquery: &bigquery
|
2
|
+
json_keyfile: example/your-project-000.json
|
3
|
+
dataset: your_dataset_name
|
4
|
+
table: your_table_name
|
5
|
+
|
6
|
+
actions:
|
7
|
+
- action: create_dataset
|
8
|
+
<<: *bigquery
|
9
|
+
- action: migrate_table
|
10
|
+
<<: *bigquery
|
11
|
+
columns:
|
12
|
+
- { name: 'timestamp', type: 'TIMESTAMP' }
|
13
|
+
- name: 'record'
|
14
|
+
type: 'RECORD'
|
15
|
+
fields:
|
16
|
+
- { name: 'string', type: 'STRING' }
|
17
|
+
- { name: 'integer', type: 'INTEGER' }
|
18
|
+
- action: migrate_table
|
19
|
+
<<: *bigquery
|
20
|
+
schema_file: example/schema.json
|
21
|
+
- action: delete_table
|
22
|
+
<<: *bigquery
|
data/example/schema.json
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
[
|
2
|
+
{
|
3
|
+
"name":"timestamp",
|
4
|
+
"type":"TIMESTAMP"
|
5
|
+
},
|
6
|
+
{
|
7
|
+
"name":"long",
|
8
|
+
"type":"INTEGER"
|
9
|
+
},
|
10
|
+
{
|
11
|
+
"name":"string",
|
12
|
+
"type":"STRING"
|
13
|
+
},
|
14
|
+
{
|
15
|
+
"name":"double",
|
16
|
+
"type":"FLOAT"
|
17
|
+
},
|
18
|
+
{
|
19
|
+
"name":"boolean",
|
20
|
+
"type":"BOOLEAN"
|
21
|
+
}
|
22
|
+
]
|
data/exe/bq_migrate
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "bigquery_migration/version"
|
2
|
+
require "bigquery_migration/error"
|
3
|
+
require "bigquery_migration/schema"
|
4
|
+
require "bigquery_migration/logger"
|
5
|
+
require "bigquery_migration/bigquery_wrapper"
|
6
|
+
|
7
|
+
class BigqueryMigration
|
8
|
+
def self.logger
|
9
|
+
@logger ||= Logger.new(STDOUT)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.logger=(logger)
|
13
|
+
@logger = logger
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(*args)
|
17
|
+
@wrapper = BigqueryWrapper.new(*args)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Delegate to BigqueryWrapper instance
|
21
|
+
BigqueryWrapper.instance_methods(false).each do |name|
|
22
|
+
next if method_defined?(name)
|
23
|
+
class_eval <<-"EOS", __FILE__, __LINE__ + 1
|
24
|
+
def #{name}(*args, &block)
|
25
|
+
@wrapper.#{name}(*args, &block)
|
26
|
+
end
|
27
|
+
EOS
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require_relative 'schema'
|
2
|
+
require_relative 'error'
|
3
|
+
require_relative 'hash_util'
|
4
|
+
require_relative 'bigquery_wrapper'
|
5
|
+
|
6
|
+
class BigqueryMigration
|
7
|
+
class Action
|
8
|
+
attr_reader :config, :opts
|
9
|
+
|
10
|
+
def initialize(config, opts = {})
|
11
|
+
@config = HashUtil.deep_symbolize_keys(config)
|
12
|
+
@opts = HashUtil.deep_symbolize_keys(opts)
|
13
|
+
|
14
|
+
@action = @config[:action]
|
15
|
+
unless self.class.supported_actions.include?(@action)
|
16
|
+
raise ConfigError, "Action #{@action} is not supported"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def run
|
21
|
+
begin
|
22
|
+
success = true
|
23
|
+
result = send(@action)
|
24
|
+
rescue => e
|
25
|
+
result = { error: e.message, error_class: e.class.to_s, error_backtrace: e.backtrace }
|
26
|
+
success = false
|
27
|
+
ensure
|
28
|
+
success = false if result[:success] == false
|
29
|
+
end
|
30
|
+
[success, result]
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.supported_actions
|
34
|
+
Set.new(%w[
|
35
|
+
create_dataset
|
36
|
+
create_table
|
37
|
+
delete_table
|
38
|
+
patch_table
|
39
|
+
migrate_table
|
40
|
+
insert
|
41
|
+
preview
|
42
|
+
])
|
43
|
+
end
|
44
|
+
|
45
|
+
def client
|
46
|
+
@client ||= BigqueryMigration.new(@config, @opts)
|
47
|
+
end
|
48
|
+
|
49
|
+
def create_dataset
|
50
|
+
client.create_dataset
|
51
|
+
end
|
52
|
+
|
53
|
+
def create_table
|
54
|
+
client.create_table(columns: config[:columns])
|
55
|
+
end
|
56
|
+
|
57
|
+
def delete_table
|
58
|
+
client.delete_table
|
59
|
+
end
|
60
|
+
|
61
|
+
def patch_table
|
62
|
+
client.patch_table(
|
63
|
+
columns: config[:columns],
|
64
|
+
add_columnss: config[:add_columnss]
|
65
|
+
)
|
66
|
+
end
|
67
|
+
|
68
|
+
def migrate_table
|
69
|
+
client.migrate_table(
|
70
|
+
schema_file: config[:schema_file],
|
71
|
+
columns: config[:columns],
|
72
|
+
backup_dataset: config[:backup_dataset],
|
73
|
+
backup_table: config[:backup_table]
|
74
|
+
)
|
75
|
+
end
|
76
|
+
|
77
|
+
def insert
|
78
|
+
client.insert_all_table_data(rows: config[:rows])
|
79
|
+
end
|
80
|
+
|
81
|
+
def preview
|
82
|
+
client.list_table_data(max_results: config[:max_results])
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|