class GoodData::LCM2::SynchronizeLdm

Constants

DESCRIPTION
PARAMS
RESULT_HEADER

Public Class Methods

call(params) click to toggle source
# File lib/gooddata/lcm/actions/synchronize_ldm.rb, line 65
def call(params)
  results = []
  synchronize = []
  params.synchronize.map do |segment_info|
    next if sync_failed_segment(segment_info[:segment_id], params)

    new_segment_info, segment_results = sync_segment_ldm(params, segment_info)
    results.concat(segment_results)
    synchronize << new_segment_info
  end

  {
    results: results,
    params: {
      synchronize: synchronize
    }
  }
end

Private Class Methods

sync_segment_ldm(params, segment_info) click to toggle source
# File lib/gooddata/lcm/actions/synchronize_ldm.rb, line 86
        def sync_segment_ldm(params, segment_info)
          collect_synced_status = collect_synced_status(params)
          failed_projects = ThreadSafe::Array.new
          results = ThreadSafe::Array.new
          client = params.gdc_gd_client
          exclude_fact_rule = params.exclude_fact_rule.to_b
          include_deprecated = params.include_deprecated.to_b
          update_preference = params[:update_preference]
          exist_fallback_to_hard_sync_config = !update_preference.nil? && !update_preference[:fallback_to_hard_sync].nil?
          include_maql_fallback_hard_sync = exist_fallback_to_hard_sync_config && Helpers.to_bool('fallback_to_hard_sync', update_preference[:fallback_to_hard_sync])

          from_pid = segment_info[:from]
          from = params.development_client.projects(from_pid)
          unless from
            segment_id = segment_info[:segment_id]
            error_message = "Failed to sync LDM for segment #{segment_id}. Error: Invalid 'from' project specified - '#{from_pid}'"
            fail(error_message) unless collect_synced_status

            add_failed_segment(segment_id, error_message, short_name, params)
            return [segment_info, results]
          end

          GoodData.logger.info "Creating Blueprint, project: '#{from.title}', PID: #{from_pid}"
          blueprint = from.blueprint(include_ca: params.include_computed_attributes.to_b)
          segment_info[:from_blueprint] = blueprint
          maql_diff = nil
          previous_master = segment_info[:previous_master]
          synchronize_ldm_mode = params[:synchronize_ldm].downcase
          diff_against_master = %w(diff_against_master_with_fallback diff_against_master)
            .include?(synchronize_ldm_mode)
          GoodData.logger.info "Synchronize LDM mode: '#{synchronize_ldm_mode}'"
          if segment_info.key?(:previous_master) && diff_against_master
            if previous_master
              maql_diff_params = [:includeGrain]
              maql_diff_params << :excludeFactRule if exclude_fact_rule
              maql_diff_params << :includeDeprecated if include_deprecated
              maql_diff_params << :includeMaqlFallbackHardSync if include_maql_fallback_hard_sync

              maql_diff = previous_master.maql_diff(blueprint: blueprint, params: maql_diff_params)
            else
              maql_diff = {
                "projectModelDiff" =>
                   {
                     "updateOperations" => [],
                     "updateScripts" => []
                   }
              }
            end

            chunks = maql_diff['projectModelDiff']['updateScripts']
            if chunks.empty?
              GoodData.logger.info "Synchronize LDM to clients will not proceed in mode \
'#{synchronize_ldm_mode}' due to no LDM changes in the segment master project. \
If you had changed LDM of clients manually, please use mode 'diff_against_clients' \
to force synchronize LDM to clients"
            end
          end

          segment_info[:to] = segment_info[:to].pmap do |entry|
            update_status = true
            pid = entry[:pid]
            next if sync_failed_project(pid, params)

            to_project = client.projects(pid)
            unless to_project
              process_failed_project(pid, "Invalid 'to' project specified - '#{pid}'", failed_projects, collect_synced_status)
              next
            end

            GoodData.logger.info "Updating from Blueprint, project: '#{to_project.title}', PID: #{pid}"
            begin
              entry[:ca_scripts] = to_project.update_from_blueprint(
                blueprint,
                update_preference: update_preference,
                exclude_fact_rule: exclude_fact_rule,
                execute_ca_scripts: false,
                maql_diff: maql_diff,
                include_deprecated: include_deprecated
              )
            rescue MaqlExecutionError => e
              if collect_synced_status
                update_status = false
                failed_message = "Applying MAQL to project #{to_project.title} - #{pid} failed. Reason: #{e}"
                process_failed_project(pid, failed_message, failed_projects, collect_synced_status)
              else
                fail e unless previous_master && params[:synchronize_ldm] == 'diff_against_master_with_fallback'

                GoodData.logger.info("Restoring the client project #{to_project.title} from master.")
                entry[:ca_scripts] = to_project.update_from_blueprint(
                  blueprint,
                  update_preference: update_preference,
                  exclude_fact_rule: exclude_fact_rule,
                  execute_ca_scripts: false,
                  include_deprecated: include_deprecated
                )
              end
            end

            results << {
              from: from_pid,
              to: pid,
              status: 'ok'
            } if update_status

            entry
          end

          process_failed_projects(failed_projects, short_name, params) if collect_synced_status
          [segment_info, results]
        end