<commit_before>require 'yaml'
require 'pathname'

Capistrano::Configuration.instance.load do
  #
  # Capistrano sync.rb task for syncing databases and directories between the
  # local development environment and different multi_stage environments. You
  # cannot sync directly between two multi_stage environments, always use your
  # local machine as loop way.
  #
  # Author: <NAME> aka netzpirat
  # Gist:   111597
  #
  # Released under the MIT license.
  # Kindly sponsored by Screen Concept, www.screenconcept.ch
  #
  namespace :sync do
    namespace :down do

      desc <<-DESC
        Syncs database from the selected mutli_stage environement to the local develoment environment.
        The database credentials will be read from your local config/database.yml file and a copy of the
        dump will be kept within the shared sync directory. The amount of backups that will be kept is
        declared in the sync_backups variable and defaults to 5.
      DESC
      task :db, :roles => :db, :only => { :primary => true } do
        # Use production on non-multistage
        set :stage, 'production' unless exists?(:stage)

        filename = "database.#{stage}.#{Time.now.strftime '%Y-%m-%d_%H:%M:%S'}.sql.bz2"
        on_rollback { delete "#{shared_path}/sync/#{filename}" }

        # Remote DB dump
        username, password, database, host = remote_database_config(stage)
        host_option = host ? "--host='#{host}'" : ""
        run "mysqldump -u #{username} --password='#{password}' #{host_option} #{database} | bzip2 -9 > #{shared_path}/sync/#{filename}" do |channel, stream, data|
          puts data
        end
        purge_old_backups "database"

        # Download dump
        download "#{shared_path}/sync/#{filename}", filename

        # Local DB import
        username, password, database, host = database_config('development')
        run_locally "rake db:drop && rake db:create"
        run_locally "bzip2 -d -c #{filename} | mysql -u #{username} --password='#{password}' #{database}"
        run_locally "rm -f #{filename}"

        logger.important "sync database from the stage '#{stage}' to local finished"
        
        # Start db:migrate
        run_locally "rake db:migrate"
      end

    end

    namespace :up do

      desc <<-DESC
        Syncs database from the local develoment environment to the selected mutli_stage environement.
        The database credentials will be read from your local config/database.yml file and a copy of the
        dump will be kept within the shared sync directory. The amount of backups that will be kept is
        declared in the sync_backups variable and defaults to 5.
      DESC
      task :db, :roles => :db, :only => { :primary => true } do
        # Use production on non-multistage
        set :stage, 'production' unless exists?(:stage)
<commit_msg>No failing in db:sync if db:drop fails.<commit_after>require 'yaml'
require 'pathname'

Capistrano::Configuration.instance.load do
  #
  # Capistrano sync.rb task for syncing databases and directories between the
  # local development environment and different multi_stage environments. You
  # cannot sync directly between two multi_stage environments, always use your
  # local machine as loop way.
  #
  # Author: <NAME> aka netzpirat
  # Gist:   111597
  #
  # Released under the MIT license.
  # Kindly sponsored by Screen Concept, www.screenconcept.ch
  #
  namespace :sync do
    namespace :down do

      desc <<-DESC
        Syncs database from the selected mutli_stage environement to the local develoment environment.
        The database credentials will be read from your local config/database.yml file and a copy of the
        dump will be kept within the shared sync directory. The amount of backups that will be kept is
        declared in the sync_backups variable and defaults to 5.
      DESC
      task :db, :roles => :db, :only => { :primary => true } do
        # Use production on non-multistage
        set :stage, 'production' unless exists?(:stage)

        filename = "database.#{stage}.#{Time.now.strftime '%Y-%m-%d_%H:%M:%S'}.sql.bz2"
        on_rollback { delete "#{shared_path}/sync/#{filename}" }

        # Remote DB dump
        username, password, database, host = remote_database_config(stage)
        host_option = host ? "--host='#{host}'" : ""
        run "mysqldump -u #{username} --password='#{password}' #{host_option} #{database} | bzip2 -9 > #{shared_path}/sync/#{filename}" do |channel, stream, data|
          puts data
        end
        purge_old_backups "database"

        # Download dump
        download "#{shared_path}/sync/#{filename}", filename

        # Local DB import
        username, password, database, host = database_config('development')
        run_locally "rake db:drop; rake db:create"
        run_locally "bzip2 -d -c #{filename} | mysql -u #{username} --password='#{password}' #{database}"
        run_locally "rm -f #{filename}"

        logger.important "sync database from the stage '#{stage}' to local finished"
        
        # Start db:migrate
        run_locally "rake db:migrate"
      end

    end

    namespace :up do

      desc <<-DESC
        Syncs database from the local develoment environment to the selected mutli_stage environement.
        The database credentials will be read from your local config/database.yml file and a copy of the
        dump will be kept within the shared sync directory. The amount of backups that will be kept is
        declared in the sync_backups variable and defaults to 5.
      DESC
      task :db, :roles => :db, :only => { :primary => true } do
        # Use production on non-multistage
        set :stage, 'production' unless exists?(:stage)

{'fraction_non_alphanumeric': 0.09507165326808809, 'fraction_numerical': 0.004194337644180357, 'mean_word_length': 2.8435191403626594, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '240732', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1574, 'n_words': 608}