<commit_before>module Pave
  class Database
    include Pave::Shell

    attr_accessor :name

    def self.create(name)
      say ""
      return say "Options should be given after the database name. For details run: `pave help`" unless name && name.size > 0
      say "Creating mysql database: #{name}."
      new(name).setup
    end

    def initialize(name)
      @name = name
    end

    def setup
      sh "mysql -uroot -e 'CREATE DATABASE #{name}'"
    end

    def remote_db
      require 'json'
      live_domain = shell("php -r \"error_reporting(0);require('./config/site.php');echo LIVE_DOMAIN;\"").output
      db_json = shell("php -r \"error_reporting(0);"\
                      "\\$_SERVER = array('HTTP_HOST' => '#{live_domain}');"\
                      "require('./config/site.php');"\
                      "echo json_encode("\
                        "array('host' => DB_SERVER,"\
                              "'user' => DB_USERNAME,"\
                              "'pass' => <PASSWORD>,"\
                              "'name' => DB_DATABASE));\"").output
      JSON.parse(db_json)
    end

    def remote_url(remote="live")
      "#{Pave::Remote.server(remote)}:#{Pave::Remote.directory(remote)}"
    end

    def dump_file
      "#{Time.now.strftime("%Y-%m-%d")}-#{name}.sql.gz"
    end

    def dump
      say "Creating dump of #{name} at #{Dir.pwd}/#{dump_file}"
      sh "mysqldump -uroot #{name} | gzip > #{dump_file}"
    end

    def dump_remote(remote="live")
      server = Pave::Remote.server(remote)
      directory = Pave::Remote.directory(remote)
      db = remote_db
      say "Remotely creating dump of #{name} at #{server}:#{directory}/db/#{dump_file}"
      sh "ssh #{server} 'cd #{directory}/db; mysqldump -u#{db[:user]} -p#{db[:pass]} #{db[:name]} | gzip > #{dump_file}'"
    end

    def execute
      # Execute SQL file.
      say "Executing #{dump_file} on #{name}"
      sh "gzip -dc ./db/#{dump_file} | mysql -uroot #{name}"
    end

    def execute_remote(remote="live")
      server = Pave::Remote.server(remote)
      directory = Pave::Remote.directory(remote)
      say "Successfully pushed! You rock! Now to execute..."
      # sh "ssh #{server} 'cd #{directory}/db; gzip -dc #{file} | mysql -u#{db_user} -p#{db_pass} #{name}'"
    end

    def upload(remote="live")
      # Upload the project's local database dump to remotes db directory.
      say "Uploading SQL dump to #{remote_url}/db/#{dump_file}"<commit_msg>Use db[:name] instead of name<commit_after>module Pave
  class Database
    include Pave::Shell

    attr_accessor :name

    def self.create(name)
      say ""
      return say "Options should be given after the database name. For details run: `pave help`" unless name && name.size > 0
      say "Creating mysql database: #{name}."
      new(name).setup
    end

    def initialize(name)
      @name = name
    end

    def setup
      sh "mysql -uroot -e 'CREATE DATABASE #{name}'"
    end

    def remote_db
      require 'json'
      live_domain = shell("php -r \"error_reporting(0);require('./config/site.php');echo LIVE_DOMAIN;\"").output
      db_json = shell("php -r \"error_reporting(0);"\
                      "\\$_SERVER = array('HTTP_HOST' => '#{live_domain}');"\
                      "require('./config/site.php');"\
                      "echo json_encode("\
                        "array('host' => DB_SERVER,"\
                              "'user' => DB_USERNAME,"\
                              "'pass' => <PASSWORD>,"\
                              "'name' => DB_DATABASE));\"").output
      JSON.parse(db_json)
    end

    def remote_url(remote="live")
      "#{Pave::Remote.server(remote)}:#{Pave::Remote.directory(remote)}"
    end

    def dump_file
      "#{Time.now.strftime("%Y-%m-%d")}-#{name}.sql.gz"
    end

    def dump
      say "Creating dump of #{name} at #{Dir.pwd}/#{dump_file}"
      sh "mysqldump -uroot #{name} | gzip > #{dump_file}"
    end

    def dump_remote(remote="live")
      server = Pave::Remote.server(remote)
      directory = Pave::Remote.directory(remote)
      db = remote_db
      say "Remotely creating dump of #{db[:name]} at #{server}:#{directory}/db/#{dump_file}"
      sh "ssh #{server} 'cd #{directory}/db; mysqldump -u#{db[:user]} -p#{db[:pass]} #{db[:name]} | gzip > #{dump_file}'"
    end

    def execute
      # Execute SQL file.
      say "Executing #{dump_file} on #{name}"
      sh "gzip -dc ./db/#{dump_file} | mysql -uroot #{name}"
    end

    def execute_remote(remote="live")
      server = Pave::Remote.server(remote)
      directory = Pave::Remote.directory(remote)
      say "Successfully pushed! You rock! Now to execute..."
      # sh "ssh #{server} 'cd #{directory}/db; gzip -dc #{file} | mysql -u#{db_user} -p#{db_pass} #{name}'"
    end

    def upload(remote="live")
      # Upload the project's local database dump to remotes db directory.
      say "Uploading SQL dump to #{remote_url}/db/#{dump_file}"
{'fraction_non_alphanumeric': 0.16091954022988506, 'fraction_numerical': 0.0012099213551119178, 'mean_word_length': 2.317725752508361, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '2716960', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1549, 'n_words': 395}