Class: Superset::Dataset::UpdateSchema

Inherits:
Request
  • Object
show all
Defined in:
lib/superset/dataset/update_schema.rb

Constant Summary

Constants inherited from Request

Request::PAGE_SIZE

Instance Attribute Summary collapse

Attributes inherited from Request

#page_num

Instance Method Summary collapse

Methods inherited from Request

call, #query_params, #result, #superset_host

Methods included from Superset::Display

#display_headers, #headings, #list, #list_attributes, #result, #rows, #table, #title

Constructor Details

#initialize(source_dataset_id:, target_database_id:, target_schema:, remove_copy_suffix: false) ⇒ UpdateSchema

Returns a new instance of UpdateSchema.



7
8
9
10
11
12
# File 'lib/superset/dataset/update_schema.rb', line 7

def initialize(source_dataset_id: , target_database_id: , target_schema: , remove_copy_suffix: false)
  @source_dataset_id = source_dataset_id
  @target_database_id = target_database_id
  @target_schema = target_schema
  @remove_copy_suffix = remove_copy_suffix
end

Instance Attribute Details

#remove_copy_suffixObject (readonly)

Returns the value of attribute remove_copy_suffix.



5
6
7
# File 'lib/superset/dataset/update_schema.rb', line 5

def remove_copy_suffix
  @remove_copy_suffix
end

#source_dataset_idObject (readonly)

Returns the value of attribute source_dataset_id.



5
6
7
# File 'lib/superset/dataset/update_schema.rb', line 5

def source_dataset_id
  @source_dataset_id
end

#target_database_idObject (readonly)

Returns the value of attribute target_database_id.



5
6
7
# File 'lib/superset/dataset/update_schema.rb', line 5

def target_database_id
  @target_database_id
end

#target_schemaObject (readonly)

Returns the value of attribute target_schema.



5
6
7
# File 'lib/superset/dataset/update_schema.rb', line 5

def target_schema
  @target_schema
end

Instance Method Details

#params_updatedObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/superset/dataset/update_schema.rb', line 34

def params_updated
  @params_updated ||= begin
    new_params = source_dataset.slice(*acceptable_attributes).with_indifferent_access
    
    # primary database and schema changes
    new_params.merge!("database_id": target_database_id)  # add the target database id
    new_params['schema'] = target_schema
    new_params['owners'] = new_params['owners'].map {|o| o['id'] } # expects an array of user ids
    new_params['table_name'] = new_params['table_name'].gsub(/ \(COPY\)/, '') if remove_copy_suffix

    # remove unwanted fields from metrics and columns arrays
    new_params['metrics'].each {|m| m.delete('changed_on') }
    new_params['metrics'].each {|m| m.delete('created_on') }
    new_params['columns'].each {|m| m.delete('changed_on') }
    new_params['columns'].each {|m| m.delete('created_on') }
    new_params['columns'].each {|m| m.delete('type_generic') }
    new_params
  end
end

#performObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/superset/dataset/update_schema.rb', line 14

def perform
  validate_proposed_changes

  response

  msg = if result['schema'] == target_schema
    "Successfully updated dataset schema to #{target_schema} on Database: #{target_database_id}"
  else
    "Error: Failed to update dataset schema to #{target_schema} on Database: #{target_database_id}"
  end

  logger.info "    #{msg}"
  msg

end

#responseObject



30
31
32
# File 'lib/superset/dataset/update_schema.rb', line 30

def response
  @response ||= client.put(route, params_updated)
end

#sql_query_includes_hard_coded_schema?Boolean

check if the sql query embedds the schema name, if so it can not be duplicated cleanly

Returns:

  • (Boolean)


55
56
57
# File 'lib/superset/dataset/update_schema.rb', line 55

def sql_query_includes_hard_coded_schema?
  source_dataset['sql'].include?("#{source_dataset['schema']}.")
end