Class: BigqueryMigration::Action
- Inherits:
-
Object
- Object
- BigqueryMigration::Action
- Defined in:
- lib/bigquery_migration/action.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Class Method Summary collapse
Instance Method Summary collapse
- #client ⇒ Object
- #copy_table ⇒ Object
- #create_dataset ⇒ Object
- #create_table ⇒ Object
- #delete_table ⇒ Object
-
#initialize(config, opts = {}) ⇒ Action
constructor
A new instance of Action.
- #insert ⇒ Object
- #insert_select ⇒ Object
- #migrate_table ⇒ Object
- #patch_table ⇒ Object
- #preview ⇒ Object
- #run ⇒ Object
- #table_info ⇒ Object
Constructor Details
#initialize(config, opts = {}) ⇒ Action
Returns a new instance of Action.
10 11 12 13 14 15 16 17 18 |
# File 'lib/bigquery_migration/action.rb', line 10 def initialize(config, opts = {}) @config = HashUtil.deep_symbolize_keys(config) @opts = HashUtil.deep_symbolize_keys(opts) @action = @config[:action] unless self.class.supported_actions.include?(@action) raise ConfigError, "Action #{@action} is not supported" end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/bigquery_migration/action.rb', line 8 def config @config end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
8 9 10 |
# File 'lib/bigquery_migration/action.rb', line 8 def opts @opts end |
Class Method Details
.supported_actions ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/bigquery_migration/action.rb', line 33 def self.supported_actions Set.new(%w[ create_dataset create_table delete_table patch_table migrate_table insert preview insert_select copy_table table_info ]) end |
Instance Method Details
#client ⇒ Object
48 49 50 |
# File 'lib/bigquery_migration/action.rb', line 48 def client @client ||= BigqueryMigration.new(@config, @opts) end |
#copy_table ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/bigquery_migration/action.rb', line 88 def copy_table client.copy_table( destination_table: config[:destination_table], destination_dataset: config[:destination_dataset], source_table: config[:source_table], source_dataset: config[:source_dataset], write_disposition: config[:write_disposition], ) end |
#create_dataset ⇒ Object
52 53 54 |
# File 'lib/bigquery_migration/action.rb', line 52 def create_dataset client.create_dataset end |
#create_table ⇒ Object
56 57 58 |
# File 'lib/bigquery_migration/action.rb', line 56 def create_table client.create_table(columns: config[:columns]) end |
#delete_table ⇒ Object
60 61 62 |
# File 'lib/bigquery_migration/action.rb', line 60 def delete_table client.delete_table end |
#insert ⇒ Object
80 81 82 |
# File 'lib/bigquery_migration/action.rb', line 80 def insert client.insert_all_table_data(rows: config[:rows]) end |
#insert_select ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/bigquery_migration/action.rb', line 98 def insert_select client.insert_select( query: config[:query], destination_table: config[:destination_table], destination_dataset: config[:destination_dataset], write_disposition: config[:write_disposition], ) end |
#migrate_table ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/bigquery_migration/action.rb', line 71 def migrate_table client.migrate_table( schema_file: config[:schema_file], columns: config[:columns], backup_dataset: config[:backup_dataset], backup_table: config[:backup_table] ) end |
#patch_table ⇒ Object
64 65 66 67 68 69 |
# File 'lib/bigquery_migration/action.rb', line 64 def patch_table client.patch_table( columns: config[:columns], add_columns: config[:add_columns] ) end |
#preview ⇒ Object
84 85 86 |
# File 'lib/bigquery_migration/action.rb', line 84 def preview client.list_table_data(max_results: config[:max_results]) end |
#run ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/bigquery_migration/action.rb', line 20 def run begin success = true result = send(@action) rescue => e result = { error: e., error_class: e.class.to_s, error_backtrace: e.backtrace } success = false ensure success = false if result[:success] == false end [success, result] end |
#table_info ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/bigquery_migration/action.rb', line 107 def table_info if config[:prefix] tables = client.list_tables[:tables].select {|table| table.start_with?(config[:prefix]) } table_infos = tables.map do |table| result = client.get_table(table: table) result.delete(:responses) result end result = { sum_num_bytes: table_infos.map {|info| info[:num_bytes].to_i }.inject(:+), sum_num_rows: table_infos.map {|info| info[:num_rows].to_i }.inject(:+), table_infos: table_infos, } else client.get_table end end |