<commit_before>require 'dropbox_sdk'

namespace :db do

  desc "Dump development database, upload to Dropbox and restore staging from it"
  task :push => :environment do
    db_name = db_name = "#{ENV["DEVELOPMENT_DATABASE_ROOTNAME"] || File.basename(Rails.root)}_development"
    db_file = "#{db_name}.dump"
    dump_dir = "tmp/db"
    dump_path = File.expand_path(File.join(dump_dir, db_file))
    dump_public_path = "public/tmp/db/#{db_file}"
    staging_app_name = ENV["HEROKU_STAGING_APP_NAME"] || "#{File.basename(Rails.root)}-staging"
    # Dump database
    puts "Dumping database to #{dump_public_path}"
    `mkdir -p #{File.expand_path(dump_dir)}`
    `pg_dump -Fc --no-acl --no-owner -h localhost -U pedr #{db_name}_development > #{dump_path}`
    # Upload to Dropbox
    puts "Uploading to Dropbox ..."
    client = DropboxClient.new(ENV["DROPBOX_ACCESS_TOKEN"])
    file = open(dump_path)
    response = client.put_file(dump_public_path, file, true)
    dump_public_url = client.media(dump_public_path)["url"]
    puts "Uploaded dump to #{dump_public_url}"
    puts "Restoring database to staging ..."
    `heroku pgbackups:restore DATABASE #{dump_public_url} --confirm #{staging_app_name}`
    puts "Done"
  end

  desc "Kill Postgres connections, Drop, create, migrate then seed the database"
  task :rebuild => [:environment, "db:kill", "db:drop", "db:create", "db:migrate", "db:seed"]

  desc "Kill Postgres connections, Drop, create then migrate the database with RAILS_ENV=test"
  task :rebuild_test => [:environment, "db:set_test_environment", "db:kill", "db:drop", "db:create", "db:migrate"]

  desc "Kill all Postgres connections"
  task :kill => :environment do
    db_name = "#{File.basename(Rails.root)}_#{Rails.env}"
    sh = <<EOF
ps xa \
| grep postgres: \
| grep #{db_name} \
| grep -v grep \
| awk '{print $1}' \
| xargs kill
EOF
    puts `#{sh}`
  end

end<commit_msg>Make user name dynamic<commit_after>require 'dropbox_sdk'

namespace :db do

  desc "Dump development database, upload to Dropbox and restore staging from it"
  task :push => :environment do
    db_name = db_name = "#{ENV["DEVELOPMENT_DATABASE_ROOTNAME"] || File.basename(Rails.root)}_development"
    db_file = "#{db_name}.dump"
    dump_dir = "tmp/db"
    dump_path = File.expand_path(File.join(dump_dir, db_file))
    dump_public_path = "public/tmp/db/#{db_file}"
    staging_app_name = ENV["HEROKU_STAGING_APP_NAME"] || "#{File.basename(Rails.root)}-staging"
    # Dump database
    puts "Dumping database to #{dump_public_path}"
    `mkdir -p #{File.expand_path(dump_dir)}`
    `pg_dump -Fc --no-acl --no-owner -h localhost -U #{`whoami`} #{db_name}_development > #{dump_path}`
    # Upload to Dropbox
    puts "Uploading to Dropbox ..."
    client = DropboxClient.new(ENV["DROPBOX_ACCESS_TOKEN"])
    file = open(dump_path)
    response = client.put_file(dump_public_path, file, true)
    dump_public_url = client.media(dump_public_path)["url"]
    puts "Uploaded dump to #{dump_public_url}"
    puts "Restoring database to staging ..."
    `heroku pgbackups:restore DATABASE #{dump_public_url} --confirm #{staging_app_name}`
    puts "Done"
  end

  desc "Kill Postgres connections, Drop, create, migrate then seed the database"
  task :rebuild => [:environment, "db:kill", "db:drop", "db:create", "db:migrate", "db:seed"]

  desc "Kill Postgres connections, Drop, create then migrate the database with RAILS_ENV=test"
  task :rebuild_test => [:environment, "db:set_test_environment", "db:kill", "db:drop", "db:create", "db:migrate"]

  desc "Kill all Postgres connections"
  task :kill => :environment do
    db_name = "#{File.basename(Rails.root)}_#{Rails.env}"
    sh = <<EOF
ps xa \
| grep postgres: \
| grep #{db_name} \
| grep -v grep \
| awk '{print $1}' \
| xargs kill
EOF
    puts `#{sh}`
  end

end
{'fraction_non_alphanumeric': 0.14394336654431045, 'fraction_numerical': 0.0005243838489774515, 'mean_word_length': 4.298611111111111, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '64040', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1345, 'n_words': 330}