<commit_before>namespace :pgsql do
    namespace :backup do
        desc "Backup the database"
        task :data do
            on roles(:db) do |host|
                backup_path = "#{fetch(:deploy_to)}/backups"
                execute :mkdir, "-p #{backup_path}"

                basename = 'database'
                username, password, database, host = get_remote_database_config()
                filename = "#{basename}_#{fetch(:stage)}_#{database}_data_#{Time.now.strftime '%Y-%m-%d_%H:%M:%S'}.dump"
                hostcmd = host.nil? ? '' : "-h #{host}"

                if fetch(:dump_ignored_table_patterns)
                    where = []
                    fetch(:dump_ignored_table_patterns).each { |pattern|
                        where.push("tablename like '#{pattern}'")
                    }
                    tables = []
                    output = capture(
                        "PGPASSWORD='<PASSWORD>}'",
                        :psql,
                        "-A -U #{username} #{hostcmd} -d #{database}",
                        "-c \"SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname ='public' AND #{where.join(' OR ')} ORDER BY tablename \"",
                        " | sed '1d;$d'"
                    )

                    output.each_line { |line|
                        tables.push(line.strip)
                    }
                else
                    tables = fetch(:dump_ignored_tables)
                end

                ignored_table = ""
                tables.each { |table|
                    ignored_table = "#{ignored_table} -T #{database}.#{table}"
                }

                execute(
                    "PGPASSWORD='<PASSWORD>}'",
                    :pg_dump,
                    "-Fc",
                    "-a -n public",
                    "-U #{username} #{hostcmd} -d #{database}",
                    "#{fetch(:pgdump_args)} #{ignored_table}",
                    " > #{backup_path}/#{filename}"
                )

                purge_old_backups "#{basename}", "#{backup_path}"

                latest = "#{backup_path}/#{basename}_#{fetch(:stage)}_#{database}_data_latest.dump"
                if test("[ -f #{latest} ]")
                    execute "rm #{latest}"
                end
                execute "ln -s #{backup_path}/#{filename} #{latest}"
            end
        end

        desc "Backup the database"
        task :schema do
            on roles(:db) do |host|
                backup_path = "#{fetch(:deploy_to)}/backups"
                execute :mkdir, "-p #{backup_path}"

                basename = 'database'
                username, password, database, host = get_remote_database_config()
                filename = "#{basename}_#{fetch(:stage)}_#{database}_schema_#{Time.now.strftime '%Y-%m-%d_%H:%M:%S'}.dump"
                hostcmd = host.nil? ? '' : "-h #{host}"


                execute(
                    "PGPASSWORD='#{password}'",
                    :pg_dump,
                    "-Fc",
                    "-s",<commit_msg>Update pgsql.rake<commit_after>namespace :pgsql do
    namespace :backup do
        desc "Backup the database"
        task :data do
            on roles(:db) do |host|
                backup_path = "#{fetch(:deploy_to)}/backups"
                execute :mkdir, "-p #{backup_path}"

                basename = 'database'
                username, password, database, host = get_remote_database_config()
                filename = "#{basename}_#{fetch(:stage)}_#{database}_data_#{Time.now.strftime '%Y-%m-%d_%H:%M:%S'}.dump"
                hostcmd = host.nil? ? '' : "-h #{host}"

                if fetch(:dump_ignored_table_patterns)
                    where = []
                    fetch(:dump_ignored_table_patterns).each { |pattern|
                        where.push("tablename like '#{pattern}'")
                    }
                    tables = []
                    output = capture(
                        "PGPASSWORD='<PASSWORD>}'",
                        :psql,
                        "-A -U #{username} #{hostcmd} -d #{database} --no-owner",
                        "-c \"SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname ='public' AND #{where.join(' OR ')} ORDER BY tablename \"",
                        " | sed '1d;$d'"
                    )

                    output.each_line { |line|
                        tables.push(line.strip)
                    }
                else
                    tables = fetch(:dump_ignored_tables)
                end

                ignored_table = ""
                tables.each { |table|
                    ignored_table = "#{ignored_table} -T #{database}.#{table}"
                }

                execute(
                    "PGPASSWORD='<PASSWORD>}'",
                    :pg_dump,
                    "-Fc",
                    "-a -n public",
                    "-U #{username} #{hostcmd} -d #{database} --no-owner",
                    "#{fetch(:pgdump_args)} #{ignored_table}",
                    " > #{backup_path}/#{filename}"
                )

                purge_old_backups "#{basename}", "#{backup_path}"

                latest = "#{backup_path}/#{basename}_#{fetch(:stage)}_#{database}_data_latest.dump"
                if test("[ -f #{latest} ]")
                    execute "rm #{latest}"
                end
                execute "ln -s #{backup_path}/#{filename} #{latest}"
            end
        end

        desc "Backup the database"
        task :schema do
            on roles(:db) do |host|
                backup_path = "#{fetch(:deploy_to)}/backups"
                execute :mkdir, "-p #{backup_path}"

                basename = 'database'
                username, password, database, host = get_remote_database_config()
                filename = "#{basename}_#{fetch(:stage)}_#{database}_schema_#{Time.now.strftime '%Y-%m-%d_%H:%M:%S'}.dump"
                hostcmd = host.nil? ? '' : "-h #{host}"


                execute(
                    "PGPASSWORD='#{password}'",
                    :pg_dump,
                    "-Fc",
                    "-s",
{'fraction_non_alphanumeric': 0.13896146309601567, 'fraction_numerical': 0.00032658393207054214, 'mean_word_length': 1.2444118724807622, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '948184', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1573, 'n_words': 316}