<commit_before># encoding: utf-8

module CartoDB
  module Synchronization
    class Adapter
      DESTINATION_SCHEMA = 'public'

      attr_accessor :table

      def initialize(table_name, runner, database, user)
        @table_name   = table_name
        @runner       = runner
        @database     = database
        @user         = user
      end

      def run(&tracker)
        runner.run(&tracker)
        result = results.select(&:success?).first
        if runner.remote_data_updated?
          overwrite(table_name, result)
          cartodbfy(table_name)
        end
        self
      rescue => exception
        puts exception.to_s
        puts exception.backtrace
      end

      def overwrite(table_name, result)
        return false unless runner.remote_data_updated?

        temporary_name = temporary_name_for(result.table_name)
        move_to_schema(result)

        database.transaction do
          rename(table_name, temporary_name) if exists?(table_name)
          rename(result.table_name, table_name)
          drop(temporary_name) if exists?(temporary_name)
        end<commit_msg>CDB-1240 #resolve Clean up temporary tables on failed table sync<commit_after># encoding: utf-8

module CartoDB
  module Synchronization
    class Adapter
      DESTINATION_SCHEMA = 'public'

      attr_accessor :table

      def initialize(table_name, runner, database, user)
        @table_name   = table_name
        @runner       = runner
        @database     = database
        @user         = user
      end

      def run(&tracker)
        runner.run(&tracker)
        result = results.select(&:success?).first
        if runner.remote_data_updated?
          overwrite(table_name, result)
          cartodbfy(table_name)
        end
        self
      rescue => exception
        puts exception.to_s
        puts exception.backtrace
      end

      def overwrite(table_name, result)
        return false unless runner.remote_data_updated?

        temporary_name = temporary_name_for(result.table_name)
        move_to_schema(result)

        database.transaction do
          rename(table_name, temporary_name) if exists?(table_name)
          rename(result.table_name, table_name)
          drop(temporary_name) if exists?(temporary_name)
        end
      rescue => exception
        drop(result.table_name) if exists?(result.table_name)
{'fraction_non_alphanumeric': 0.06903854299025837, 'fraction_numerical': 0.0025412960609911056, 'mean_word_length': 2.2669432918395573, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '5200274', 'n_tokens_mistral': 671, 'n_tokens_neox': 666, 'n_words': 149}