<commit_before>require 'uuidtools'
require_dependency 'carto/uuidhelper'
require_dependency 'carto/query_rewriter'

module Carto
  # Export/import is versioned, but importing should generate a model tree that can be persisted by this class
  class VisualizationsExportPersistenceService
    include Carto::UUIDHelper
    include Carto::QueryRewriter

    def save_import(user, visualization, renamed_tables: {})
      old_username = visualization.user.username if visualization.user
      apply_user_limits(user, visualization)

      ActiveRecord::Base.transaction do
        visualization.id = random_uuid
        visualization.user = user

        ensure_unique_name(user, visualization)

        visualization.layers.each { |layer| layer.fix_layer_user_information(old_username, user, renamed_tables) }
        visualization.analyses.each do |analysis|
          analysis.analysis_node.fix_analysis_node_queries(old_username, user, renamed_tables)
        end

        permission = Carto::Permission.new(owner: user, owner_username: user.username)
        visualization.permission = permission

        map = visualization.map
        map.user = user
        unless map.save
          raise "Errors saving imported map: #{map.errors.full_messages}"
        end

        visualization.analyses.each do |analysis|
          analysis.user_id = user.id
        end

        # INFO: workaround for array saves not working
        tags = visualization.tags
        visualization.tags = nil
        unless visualization.save
          raise "Errors saving imported visualization: #{visualization.errors.full_messages}"
        end
<commit_msg>no real changes<commit_after>require 'uuidtools'
require_dependency 'carto/uuidhelper'
require_dependency 'carto/query_rewriter'

module Carto
  # Export/import is versioned, but importing should generate a model tree that can be persisted by this class
  class VisualizationsExportPersistenceService
    include Carto::UUIDHelper
    include Carto::QueryRewriter

    def save_import(user, visualization, renamed_tables: {})
      old_username = visualization.user.username if visualization.user
      apply_user_limits(user, visualization)
      ActiveRecord::Base.transaction do
        visualization.id = random_uuid
        visualization.user = user

        ensure_unique_name(user, visualization)

        visualization.layers.each { |layer| layer.fix_layer_user_information(old_username, user, renamed_tables) }
        visualization.analyses.each do |analysis|
          analysis.analysis_node.fix_analysis_node_queries(old_username, user, renamed_tables)
        end

        permission = Carto::Permission.new(owner: user, owner_username: user.username)
        visualization.permission = permission

        map = visualization.map
        map.user = user
        unless map.save
          raise "Errors saving imported map: #{map.errors.full_messages}"
        end

        visualization.analyses.each do |analysis|
          analysis.user_id = user.id
        end

        # INFO: workaround for array saves not working
        tags = visualization.tags
        visualization.tags = nil
        unless visualization.save
          raise "Errors saving imported visualization: #{visualization.errors.full_messages}"
        end

{'fraction_non_alphanumeric': 0.06705272782688204, 'fraction_numerical': 0.0, 'mean_word_length': 3.1969309462915603, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1371632', 'n_tokens_mistral': 864, 'n_tokens_neox': 810, 'n_words': 216}