<commit_before>require 'open3'
module Util
  class DbManager

    attr_accessor :con, :pub_con, :alt_pub_con, :event, :migration

    def initialize(params={})
      # Should only manage content of ctgov db schema
      if params[:event]
        @event = params[:event]
      else
        @event = Support::LoadEvent.create({:event_type=>'',:status=>'',:description=>'',:problems=>''})
      end
    end

    def dump_schema(schema_name)
      fm=Util::FileManager.new
      file_name="#{fm.pg_dump_file}_#{schema_name}"
      File.delete(file_name) if File.exist?(file_name)

      cmd="pg_dump aact -v -h localhost -p 5432 -U #{AACT::Application::AACT_DB_SUPER_USERNAME} --clean --exclude-table ar_internal_metadata --exclude-table schema_migrations --schema #{schema_name} -b -c -C -Fc -f #{file_name}"
      run_command_line(cmd)
    end

    def dump_database
      fm=Util::FileManager.new
      File.delete(fm.pg_dump_file) if File.exist?(fm.pg_dump_file)
      cmd="pg_dump #{AACT::Application::AACT_BACK_DATABASE_URL} -v -h localhost -p 5432 -U #{AACT::Application::AACT_DB_SUPER_USERNAME} --clean --exclude-table ar_internal_metadata --exclude-table schema_migrations --schema ctgov -b -c -C -Fc -f #{fm.pg_dump_file}"
      run_command_line(cmd)
      copy_dump_file_to_public_server
    end

    def copy_dump_file_to_public_server
      fm=Util::FileManager.new
      cmd="scp #{fm.pg_dump_file} ctti@#{AACT::Application::AACT_PUBLIC_HOSTNAME}:/aact-files/dump_files"
      system(cmd)
    end

    def refresh_public_db
      dump_file_name=Util::FileManager.new.pg_dump_file
      return nil if dump_file_name.nil?
      begin
        success_code=true
        revoke_db_privs   # Prevent users from logging in while db restore is running.

        # Refresh the aact_alt database first.  If something goes wrong, don't restore aact.
        terminate_alt_db_sessions

        begin
          #  Drop the existing ctgov schema with cascade. If dependencies exist on anything in ctgov, the restore won't be able to
          #  drop before replacing - resulting in a db of duplicate data.  Get rid of it using CASCADE' first.
          log "  dropping ctgov schema in alt public database..."
          cmd="DROP SCHEMA ctgov CASCADE;"
          PublicBase.establish_connection(AACT::Application::AACT_ALT_PUBLIC_DATABASE_URL).connection.execute(cmd)
          cmd="CREATE SCHEMA ctgov;"
          PublicBase.establish_connection(AACT::Application::AACT_ALT_PUBLIC_DATABASE_URL).connection.execute(cmd)
          cmd="GRANT USAGE ON SCHEMA ctgov TO read_only;"
          PublicBase.establish_connection(AACT::Application::AACT_ALT_PUBLIC_DATABASE_URL).connection.execute(cmd)
        rescue
        end
        log "  restoring alt public database..."
        cmd="pg_restore -c -j 5 -v -h #{public_host_name} -p 5432 -U #{AACT::Application::AACT_DB_SUPER_USERNAME}  -d aact_alt #{dump_file_name}"
        run_restore_command_line(cmd)

        log "  verifying alt public database..."
        public_studies_count = PublicBase.establish_connection(AACT::Application::AACT_ALT_PUBLIC_DATABASE_URL).connection.execute('select count(*) from studies;').first['count'].to_i

        if public_studies_count != background_study_count
          success_code = false
          msg = "SOMETHING WENT WRONG! PROBLEM IN PRODUCTION DATABASE: aact_alt.  Study count is #{public_studies_count}. Should be #{background_study_count}"
          event.add_problem(msg)
          log msg
          grant_db_privs
          return false
        end
        log "  all systems go... we can update primary public aact...."

        # If all goes well with AACT_ALT DB, proceed with AACT

        terminate_db_sessions
        begin
          log "  dropping ctgov schema in main public database..."
          cmd="DROP SCHEMA ctgov CASCADE;"
          PublicBase.establish_connection(AACT::Application::AACT_PUBLIC_DATABASE_URL).connection.execute(cmd)
          cmd="CREATE SCHEMA ctgov;"
          PublicBase.establish_connection(AACT::Application::AACT_PUBLIC_DATABASE_URL).connection.execute(cmd)
        rescue
        end
        log "  restoring main public database..."
        cmd="pg_restore -c -j 5 -v -h #{public_host_name} -p 5432 -U #{AACT::Application::AACT_DB_SUPER_USERNAME}  -d #{public_db_name} #{dump_file_name}"
        run_restore_command_line(cmd)<commit_msg>aact-1056:  dump database without object owner info so that others can restore without getting those errors if they don’t have a ctti role<commit_after>require 'open3'
module Util
  class DbManager

    attr_accessor :con, :pub_con, :alt_pub_con, :event, :migration

    def initialize(params={})
      # Should only manage content of ctgov db schema
      if params[:event]
        @event = params[:event]
      else
        @event = Support::LoadEvent.create({:event_type=>'',:status=>'',:description=>'',:problems=>''})
      end
    end

    def dump_schema(schema_name)
      fm=Util::FileManager.new
      file_name="#{fm.pg_dump_file}_#{schema_name}"
      File.delete(file_name) if File.exist?(file_name)

      cmd="pg_dump aact -v -h localhost -p 5432 -U #{AACT::Application::AACT_DB_SUPER_USERNAME} --clean --no-owners --exclude-table ar_internal_metadata --exclude-table schema_migrations --schema #{schema_name} -b -c -C -Fc -f #{file_name}"
      run_command_line(cmd)
    end

    def dump_database
      fm=Util::FileManager.new
      File.delete(fm.pg_dump_file) if File.exist?(fm.pg_dump_file)
      cmd="pg_dump #{AACT::Application::AACT_BACK_DATABASE_URL} -v -h localhost -p 5432 -U #{AACT::Application::AACT_DB_SUPER_USERNAME} --clean  --no-owners --exclude-table ar_internal_metadata --exclude-table schema_migrations --schema ctgov -b -c -C -Fc -f #{fm.pg_dump_file}"
      run_command_line(cmd)
      copy_dump_file_to_public_server
    end

    def copy_dump_file_to_public_server
      fm=Util::FileManager.new
      cmd="scp #{fm.pg_dump_file} ctti@#{AACT::Application::AACT_PUBLIC_HOSTNAME}:/aact-files/dump_files"
      system(cmd)
    end

    def refresh_public_db
      dump_file_name=Util::FileManager.new.pg_dump_file
      return nil if dump_file_name.nil?
      begin
        success_code=true
        revoke_db_privs   # Prevent users from logging in while db restore is running.

        # Refresh the aact_alt database first.  If something goes wrong, don't restore aact.
        terminate_alt_db_sessions

        begin
          #  Drop the existing ctgov schema with cascade. If dependencies exist on anything in ctgov, the restore won't be able to
          #  drop before replacing - resulting in a db of duplicate data.  Get rid of it using CASCADE' first.
          log "  dropping ctgov schema in alt public database..."
          cmd="DROP SCHEMA ctgov CASCADE;"
          PublicBase.establish_connection(AACT::Application::AACT_ALT_PUBLIC_DATABASE_URL).connection.execute(cmd)
          cmd="CREATE SCHEMA ctgov;"
          PublicBase.establish_connection(AACT::Application::AACT_ALT_PUBLIC_DATABASE_URL).connection.execute(cmd)
          cmd="GRANT USAGE ON SCHEMA ctgov TO read_only;"
          PublicBase.establish_connection(AACT::Application::AACT_ALT_PUBLIC_DATABASE_URL).connection.execute(cmd)
        rescue
        end
        log "  restoring alt public database..."
        cmd="pg_restore -c -j 5 -v -h #{public_host_name} -p 5432 -U #{AACT::Application::AACT_DB_SUPER_USERNAME}  -d aact_alt #{dump_file_name}"
        puts cmd
        exit
        run_restore_command_line(cmd)

        log "  verifying alt public database..."
        public_studies_count = PublicBase.establish_connection(AACT::Application::AACT_ALT_PUBLIC_DATABASE_URL).connection.execute('select count(*) from studies;').first['count'].to_i

        if public_studies_count != background_study_count
          success_code = false
          msg = "SOMETHING WENT WRONG! PROBLEM IN PRODUCTION DATABASE: aact_alt.  Study count is #{public_studies_count}. Should be #{background_study_count}"
          event.add_problem(msg)
          log msg
          grant_db_privs
          return false
        end
        log "  all systems go... we can update primary public aact...."

        # If all goes well with AACT_ALT DB, proceed with AACT

        terminate_db_sessions
        begin
          log "  dropping ctgov schema in main public database..."
          cmd="DROP SCHEMA ctgov CASCADE;"
          PublicBase.establish_connection(AACT::Application::AACT_PUBLIC_DATABASE_URL).connection.execute(cmd)
          cmd="CREATE SCHEMA ctgov;"
          PublicBase.establish_connection(AACT::Application::AACT_PUBLIC_DATABASE_URL).connection.execute(cmd)
        rescue
        end
        log "  restoring main public database..."
        cmd="pg_restore -c -j 5 -v -h #{public_host_name} -p 5432 -U #{AACT::Application::AACT_DB_SUPER_USERNAME}  -d #{public_db_name} #{dump_file_name}"
        run_restore_command_line(cmd)
{'fraction_non_alphanumeric': 0.09755824372759857, 'fraction_numerical': 0.004704301075268817, 'mean_word_length': 3.1510925151092515, 'pattern_counts': {'":': 0, '<': 3, '<?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': '790191', 'n_tokens_mistral': 2912, 'n_tokens_neox': 2761, 'n_words': 662}