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

#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
# 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
  ])
end

Instance Method Details

#client ⇒ Object



47
48
49
# File 'lib/bigquery_migration/action.rb', line 47

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

#copy_table ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/bigquery_migration/action.rb', line 87

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



51
52
53
# File 'lib/bigquery_migration/action.rb', line 51

def create_dataset
  client.create_dataset
end

#create_table ⇒ Object



55
56
57
# File 'lib/bigquery_migration/action.rb', line 55

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

#delete_table ⇒ Object



59
60
61
# File 'lib/bigquery_migration/action.rb', line 59

def delete_table
  client.delete_table
end

#insert ⇒ Object



79
80
81
# File 'lib/bigquery_migration/action.rb', line 79

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

#insert_select ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/bigquery_migration/action.rb', line 97

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



70
71
72
73
74
75
76
77
# File 'lib/bigquery_migration/action.rb', line 70

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



63
64
65
66
67
68
# File 'lib/bigquery_migration/action.rb', line 63

def patch_table
  client.patch_table(
    columns: config[:columns],
    add_columns: config[:add_columns]
  )
end

#preview ⇒ Object



83
84
85
# File 'lib/bigquery_migration/action.rb', line 83

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