Class: BigqueryMigration::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/bigquery_migration/action.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/bigquery_migration/action.rb', line 8

def config
  @config
end

#optsObject (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_actionsObject



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

#clientObject



49
50
51
# File 'lib/bigquery_migration/action.rb', line 49

def client
  @client ||= BigqueryMigration.new(@config, @opts)
end

#copy_tableObject



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_datasetObject



53
54
55
# File 'lib/bigquery_migration/action.rb', line 53

def create_dataset
  client.create_dataset
end

#create_tableObject



57
58
59
# File 'lib/bigquery_migration/action.rb', line 57

def create_table
  client.create_table(columns: config[:columns])
end

#delete_tableObject



61
62
63
# File 'lib/bigquery_migration/action.rb', line 61

def delete_table
  client.delete_table
end

#insertObject



88
89
90
# File 'lib/bigquery_migration/action.rb', line 88

def insert
  client.insert_all_table_data(rows: config[:rows])
end

#insert_selectObject



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_tableObject



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_tableObject



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_tableObject



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

#previewObject



92
93
94
# File 'lib/bigquery_migration/action.rb', line 92

def preview
  client.list_table_data(max_results: config[:max_results])
end

#runObject



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.message, error_class: e.class.to_s, error_backtrace: e.backtrace }
    success = false
  ensure
    success = false if result[:success] == false
  end
  [success, result]
end

#table_infoObject



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