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_partitioned_table ⇒ 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 47 |
# 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 migrate_partitioned_table ]) end |
Instance Method Details
#client ⇒ Object
49 50 51 |
# File 'lib/bigquery_migration/action.rb', line 49 def client @client ||= BigqueryMigration.new(@config, @opts) end |
#copy_table ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/bigquery_migration/action.rb', line 96 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
53 54 55 |
# File 'lib/bigquery_migration/action.rb', line 53 def create_dataset client.create_dataset end |
#create_table ⇒ Object
57 58 59 |
# File 'lib/bigquery_migration/action.rb', line 57 def create_table client.create_table(columns: config[:columns]) end |
#delete_table ⇒ Object
61 62 63 |
# File 'lib/bigquery_migration/action.rb', line 61 def delete_table client.delete_table end |
#insert ⇒ Object
88 89 90 |
# File 'lib/bigquery_migration/action.rb', line 88 def insert client.insert_all_table_data(rows: config[:rows]) end |
#insert_select ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/bigquery_migration/action.rb', line 106 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_partitioned_table ⇒ Object
81 82 83 84 85 86 |
# File 'lib/bigquery_migration/action.rb', line 81 def migrate_partitioned_table client.migrate_partitioned_table( schema_file: config[:schema_file], columns: config[:columns], ) end |
#migrate_table ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/bigquery_migration/action.rb', line 72 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
65 66 67 68 69 70 |
# File 'lib/bigquery_migration/action.rb', line 65 def patch_table client.patch_table( columns: config[:columns], add_columns: config[:add_columns] ) end |
#preview ⇒ Object
92 93 94 |
# File 'lib/bigquery_migration/action.rb', line 92 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
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/bigquery_migration/action.rb', line 115 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 |