Skip to content

Commit

Permalink
Add --limit option in dump command
Browse files Browse the repository at this point in the history
  • Loading branch information
ma91n committed Feb 29, 2024
1 parent f4cfb1d commit 2fe05fd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@
testdata/csv/*.csv

dist/

*.xlsx
12 changes: 5 additions & 7 deletions cli/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (

const DefaultColumnCnt = 32

var DumpWithDataRowLimit = 10

var query = `SELECT tab.relname AS table_name
, tabdesc.description AS table_description
, col.column_name
Expand Down Expand Up @@ -90,7 +88,7 @@ type ColumnDef struct {
DefaultValue string
}

func Dump(dbSource, targetFile string, tableNameArg, systemColumnArg string) error {
func Dump(dbSource, targetFile, tableNameArg, systemColumnArg string, maxDumpSize int) error {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

Expand Down Expand Up @@ -222,7 +220,7 @@ func Dump(dbSource, targetFile string, tableNameArg, systemColumnArg string) err
_ = f.SetCellStyle(sheetName, axisComment, axisName, style)
}

records, err := selectExistsRecords(ctx, conn, tableDef)
records, err := selectExistsRecords(ctx, conn, tableDef, maxDumpSize)
if err != nil {
return fmt.Errorf("select exists records: %w", err)
}
Expand Down Expand Up @@ -326,14 +324,14 @@ func Str(a any) string {
return a.(string)
}

func selectExistsRecords(ctx context.Context, conn *pgxpool.Pool, tableDef TableDef) ([][]any, error) {
rows, err := conn.Query(ctx, fmt.Sprintf(`select * from %s limit %d`, tableDef.Name, DumpWithDataRowLimit))
func selectExistsRecords(ctx context.Context, conn *pgxpool.Pool, tableDef TableDef, maxDumpSize int) ([][]any, error) {
rows, err := conn.Query(ctx, fmt.Sprintf(`select * from %s limit %d`, tableDef.Name, maxDumpSize))
if err != nil {
return nil, fmt.Errorf("db access: %w", err)
}
defer rows.Close()

dataRecords := make([][]any, 0, DumpWithDataRowLimit)
dataRecords := make([][]any, 0, maxDumpSize)

for rows.Next() {
g := make([]any, len(tableDef.Columns))
Expand Down
2 changes: 1 addition & 1 deletion cli/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Test_dump(t *testing.T) {
for _, tt := range tests {

t.Run(tt.name, func(t *testing.T) {
if err := Dump(tt.args.dbSource, tt.args.targetFile, tt.args.tableNameArg, tt.args.systemColumnArg); (err != nil) != tt.wantErr {
if err := Dump(tt.args.dbSource, tt.args.targetFile, tt.args.tableNameArg, tt.args.systemColumnArg, 10); (err != nil) != tt.wantErr {
t.Errorf("dump() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
11 changes: 6 additions & 5 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ var (
app = kingpin.New("exceltesting", "Excel file driven testing helper tool").Version(Version)
source = app.Flag("source", `Database source (e.g. postgres://user:pass@host/dbname?sslmode=disable). EXCELTESTING_CONNECTION envvar is acceptable.`).Short('c').Envar("EXCELTESTING_CONNECTION").String()

dumpCommand = app.Command("dump", "Generate excel template file from database")
dumpFile = dumpCommand.Arg("file", "Target excel file path (e.g. dump.xlsx)").Required().NoEnvar().String()
table = dumpCommand.Flag("table", "Dump target table names (e.g. table1,table2,table3)").NoEnvar().String()
systemcolum = dumpCommand.Flag("systemcolum", "Specific system columns for cell style (e.g. created_at,updated_at,revision)").NoEnvar().String()
dumpCommand = app.Command("dump", "Generate excel template file from database")
dumpFile = dumpCommand.Arg("file", "Target excel file path (e.g. dump.xlsx)").Required().NoEnvar().String()
table = dumpCommand.Flag("table", "Dump target table names (e.g. table1,table2,table3)").NoEnvar().String()
systemcolum = dumpCommand.Flag("systemcolum", "Specific system columns for cell style (e.g. created_at,updated_at,revision)").NoEnvar().String()
maxDumpRecordLimit = dumpCommand.Flag("limit", "Max dump record limit size (e.g. created_at,updated_at,revision)").NoEnvar().Default("500").Int()

loadCommand = app.Command("load", "Load from excel file to database")
loadFile = loadCommand.Arg("file", "Target excel file path (e.g. input.xlsx)").Required().NoEnvar().ExistingFile()
Expand All @@ -40,7 +41,7 @@ func Main() {
var err error
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case dumpCommand.FullCommand():
err = Dump(*source, *dumpFile, *table, *systemcolum)
err = Dump(*source, *dumpFile, *table, *systemcolum, *maxDumpRecordLimit)
case loadCommand.FullCommand():
req := exceltesting.LoadRequest{
TargetBookPath: *loadFile,
Expand Down

0 comments on commit 2fe05fd

Please sign in to comment.