<commit_before>require 'open3'
module Util
  class DbManager

    attr_accessor :con, :stage_con, :pub_con, :load_event

    def initialize(params={})
      @load_event = params[:load_event]
    end

    def save_static_copy
      begin
        fm=Util::FileManager.new
        schema_diagram_file=File.open("#{fm.backend_schema_diagram}")
        admin_schema_diagram_file=File.open("#{fm.backend_admin_schema_diagram}")
        data_dictionary_file=File.open("#{fm.backend_data_dictionary}")
        nlm_protocol_file=fm.make_file_from_website("nlm_protocol_definitions.html",fm.nlm_protocol_data_url)
        nlm_results_file=fm.make_file_from_website("nlm_results_definitions.html",fm.nlm_results_data_url)

        zip_file_name="#{fm.class.static_copies_directory}/#{Time.now.strftime('%Y%m%d')}_clinical_trials.zip"
        File.delete(zip_file_name) if File.exist?(zip_file_name)
        Zip::File.open(zip_file_name, Zip::File::CREATE) {|zipfile|
          zipfile.add('schema_diagram.png',schema_diagram_file)
          zipfile.add('admin_schema_diagram.png',admin_schema_diagram_file)
          zipfile.add('data_dictionary.xlsx',data_dictionary_file)
          zipfile.add('postgres_data.dmp',fm.pg_dump_file)
          zipfile.add('nlm_protocol_definitions.html',nlm_protocol_file)
          zipfile.add('nlm_results_definitions.html',nlm_results_file)
        }
      rescue => error
        load_event.add_problem("#{error.message} (#{error.class} #{error.backtrace}")
      end
      return zip_file_name
    end

    def dump_database
      begin
        # First populate db named 'aact' from background db so the dump file will be configured to restore db named aact
        psql_file="#{Util::FileManager.dump_directory}/aact.psql"
        File.delete(psql_file) if File.exist?(psql_file)
        terminate_active_sessions('aact_back')
        cmd="pg_dump --no-owner --no-acl -h localhost -U #{ENV['DB_SUPER_USERNAME']} aact_back > #{psql_file}"
        run_command_line(cmd)

        # clear out previous content of staging db
        puts "Recreating public schema in aact staging database..."
        terminate_active_sessions('aact')
        stage_con.execute('DROP SCHEMA IF EXISTS public CASCADE')
        stage_con.execute('CREATE SCHEMA public')

        # refresh staging db
        puts "Refreshing aact staging database..."
        cmd="psql -h localhost aact < #{psql_file} > /dev/null"
        terminate_active_sessions('aact')
        run_command_line(cmd)

        fm=Util::FileManager.new
        db_name=ActiveRecord::Base.connection.current_database
        File.delete(fm.pg_dump_file) if File.exist?(fm.pg_dump_file)
        cmd="pg_dump aact -v -h localhost -p 5432 -U #{ENV['DB_SUPER_USERNAME']} --no-password --clean --exclude-table schema_migrations  -c -C -Fc -f  #{fm.pg_dump_file}"
        terminate_active_sessions('aact')
        run_command_line(cmd)
      rescue => error
        load_event.add_problem("#{error.message} (#{error.class} #{error.backtrace}")
      end
    end

    def refresh_public_db
      begin
        success_code=true
        revoke_db_privs
        terminate_active_sessions(public_db_name)<commit_msg>aact-578:  Exclude the schema_migrations table when we dump the aact_back database.<commit_after>require 'open3'
module Util
  class DbManager

    attr_accessor :con, :stage_con, :pub_con, :load_event

    def initialize(params={})
      @load_event = params[:load_event]
    end

    def save_static_copy
      begin
        fm=Util::FileManager.new
        schema_diagram_file=File.open("#{fm.backend_schema_diagram}")
        admin_schema_diagram_file=File.open("#{fm.backend_admin_schema_diagram}")
        data_dictionary_file=File.open("#{fm.backend_data_dictionary}")
        nlm_protocol_file=fm.make_file_from_website("nlm_protocol_definitions.html",fm.nlm_protocol_data_url)
        nlm_results_file=fm.make_file_from_website("nlm_results_definitions.html",fm.nlm_results_data_url)

        zip_file_name="#{fm.class.static_copies_directory}/#{Time.now.strftime('%Y%m%d')}_clinical_trials.zip"
        File.delete(zip_file_name) if File.exist?(zip_file_name)
        Zip::File.open(zip_file_name, Zip::File::CREATE) {|zipfile|
          zipfile.add('schema_diagram.png',schema_diagram_file)
          zipfile.add('admin_schema_diagram.png',admin_schema_diagram_file)
          zipfile.add('data_dictionary.xlsx',data_dictionary_file)
          zipfile.add('postgres_data.dmp',fm.pg_dump_file)
          zipfile.add('nlm_protocol_definitions.html',nlm_protocol_file)
          zipfile.add('nlm_results_definitions.html',nlm_results_file)
        }
      rescue => error
        load_event.add_problem("#{error.message} (#{error.class} #{error.backtrace}")
      end
      return zip_file_name
    end

    def dump_database
      begin
        # First populate db named 'aact' from background db so the dump file will be configured to restore db named aact
        psql_file="#{Util::FileManager.dump_directory}/aact.psql"
        File.delete(psql_file) if File.exist?(psql_file)
        terminate_active_sessions('aact_back')
        cmd="pg_dump --no-owner --no-acl -h localhost -U #{ENV['DB_SUPER_USERNAME']} --exclude-table schema_migrations aact_back > #{psql_file}"
        run_command_line(cmd)

        # clear out previous content of staging db
        puts "Recreating public schema in aact staging database..."
        terminate_active_sessions('aact')
        stage_con.execute('DROP SCHEMA IF EXISTS public CASCADE')
        stage_con.execute('CREATE SCHEMA public')

        # refresh staging db
        puts "Refreshing aact staging database..."
        cmd="psql -h localhost aact < #{psql_file} > /dev/null"
        terminate_active_sessions('aact')
        run_command_line(cmd)

        fm=Util::FileManager.new
        db_name=ActiveRecord::Base.connection.current_database
        File.delete(fm.pg_dump_file) if File.exist?(fm.pg_dump_file)
        cmd="pg_dump aact -v -h localhost -p 5432 -U #{ENV['DB_SUPER_USERNAME']} --no-password --clean --exclude-table schema_migrations  -c -C -Fc -f  #{fm.pg_dump_file}"
        terminate_active_sessions('aact')
        run_command_line(cmd)
      rescue => error
        load_event.add_problem("#{error.message} (#{error.class} #{error.backtrace}")
      end
    end

    def refresh_public_db
      begin
        success_code=true
        revoke_db_privs
        terminate_active_sessions(public_db_name)
{'fraction_non_alphanumeric': 0.10901207804273769, 'fraction_numerical': 0.002013007122948281, 'mean_word_length': 3.433081674673988, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '790190', 'n_tokens_mistral': 2166, 'n_tokens_neox': 2093, 'n_words': 339}