<commit_before>package tasks

import (
	"fmt"
	"log"
	"regexp"
	"strings"

	"github.com/headmade/backuper/backuper"
)

const (
	tmpFileSqlSuffix = ".sql"
)

type backupPostgresTask struct {
	*backupTask
}

func newBackupPostgresTask(config *backuper.TaskConfig) BackupTaskInterface {
	postgresTask := backupPostgresTask{newBackupTask(config)}

	params := &config.Params
	tmpFileBase := strings.Join([]string{
		(*params)["db_host"],
		(*params)["db_port"],
		(*params)["db_base"],
	}, "_")

	tmpFileBase = strings.Join(
		regexp.MustCompile(`[^\d\w]+`).Split(tmpFileBase, -1),
		"_",
	)

	postgresTask.tmpFileBase = tmpFileBase + tmpFileSqlSuffix
	return &postgresTask
}

func (postgresTask *backupPostgresTask) GenerateTmpFile(tmpFilePath string) ([]byte, error) {

	tables := postgresTask.config.Params["db_tables"]

	if len(tables) == 0 {
		tables = "\\*"
	}

	params := &postgresTask.config.Params

	cmd := fmt.Sprintf("PGPASSWORD=%s pg_dump %s -h %s -p %s -U %s %s -t %s %s >%s",
		(*params)["db_pass"],
		postgresTask.recreateFlag(),
		(*params)["db_host"],
		(*params)["db_port"],
		(*params)["db_user"],
		(*params)["db_base"],
		(*params)["tables"],
		postgresTask.compressionFilter(),
		tmpFilePath,
	)
	log.Println(cmd)

	return System(cmd)
}

func (postgresTask *backupPostgresTask) compressionFilter() (cf string) {
	if postgresTask.needCompression() {
		cf = " | bzip2 -c "
	}
	return
}

func (postgresTask *backupPostgresTask) recreateFlag() (rf string) {
	if len(postgresTask.config.Params["recreate"]) > 0 {
		rf = "-c"
	}
	return
}
<commit_msg>Fix the case when no table given<commit_after>package tasks

import (
	"fmt"
	"log"
	"regexp"
	"strings"

	"github.com/headmade/backuper/backuper"
)

const (
	tmpFileSqlSuffix = ".sql"
)

type backupPostgresTask struct {
	*backupTask
}

func newBackupPostgresTask(config *backuper.TaskConfig) BackupTaskInterface {
	postgresTask := backupPostgresTask{newBackupTask(config)}

	params := &config.Params
	tmpFileBase := strings.Join([]string{
		(*params)["db_host"],
		(*params)["db_port"],
		(*params)["db_base"],
	}, "_")

	tmpFileBase = strings.Join(
		regexp.MustCompile(`[^\d\w]+`).Split(tmpFileBase, -1),
		"_",
	)

	postgresTask.tmpFileBase = tmpFileBase + tmpFileSqlSuffix
	return &postgresTask
}

func (postgresTask *backupPostgresTask) GenerateTmpFile(tmpFilePath string) ([]byte, error) {

	tables := postgresTask.config.Params["db_tables"]

	if len(tables) == 0 {
		tables = "\\*"
	}

	params := &postgresTask.config.Params

	cmd := fmt.Sprintf("PGPASSWORD=%s pg_dump %s -h %s -p %s -U %s %s %s %s >%s",
		(*params)["db_pass"],
		postgresTask.recreateFlag(),
		(*params)["db_host"],
		(*params)["db_port"],
		(*params)["db_user"],
		(*params)["db_base"],
		postgresTask.tablesFlag(),
		postgresTask.compressionFilter(),
		tmpFilePath,
	)
	log.Println(cmd)

	return System(cmd)
}

func (postgresTask *backupPostgresTask) compressionFilter() (cf string) {
	if postgresTask.needCompression() {
		cf = " | bzip2 -c "
	}
	return
}

func (postgresTask *backupPostgresTask) recreateFlag() (rf string) {
	if len(postgresTask.config.Params["recreate"]) > 0 {
		rf = "-c"
	}
	return
}

func (postgresTask *backupPostgresTask) tablesFlag() (tf string) {
	tables := postgresTask.config.Params["tables"]
	if len(tables) > 0 {
		tf = "-t " + tables
	}
	return
}

{'fraction_non_alphanumeric': 0.17113588430250076, 'fraction_numerical': 0.0027116601385959627, 'mean_word_length': 4.928571428571429, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '6941294', 'n_tokens_mistral': 1296, 'n_tokens_neox': 1233, 'n_words': 226}