Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/policy/snapshot_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (p *Policy) Test(ctx context.Context, e *Executor, source, snapshotDirector
if err != nil {
return err
}
e.log.Debug("Found tests", "tests", tests)
for _, test := range tests {

selector := strings.TrimPrefix(test, snapshotDirectory+"/")
Expand Down
16 changes: 9 additions & 7 deletions pkg/policy/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

func persistSnapshot(ctx context.Context, e *Executor, path string, table string) error {
ef, err := os.OpenFile(filepath.Join("%s/", path, fmt.Sprintf("table_%s.csv", table)), os.O_CREATE|os.O_WRONLY, 0777)
ef, err := os.OpenFile(filepath.Join(path, fmt.Sprintf("table_%s.csv", table)), os.O_CREATE|os.O_WRONLY, 0777)
if err != nil {
return fmt.Errorf("error opening file %q: %w", table, err)
}
Expand Down Expand Up @@ -109,6 +109,10 @@ func (ce *Executor) extractTableNames(ctx context.Context, query string) ([]stri
return nil, err
}
}
if err := rows.Err(); err != nil {
ce.log.Error("Error fetching rows", "query", query, "error", err)
return tableNames, err
}

var arrayJsonMap []map[string](interface{})
err = json.Unmarshal([]byte(s), &arrayJsonMap)
Expand Down Expand Up @@ -149,10 +153,6 @@ func (ce *Executor) extractTableNames(ctx context.Context, query string) ([]stri
}
}

if err := rows.Err(); err != nil {
ce.log.Error("Error fetching rows", "query", query, "error", err)
return tableNames, err
}
// It is possible that tables are used multiple times so need to dedupe prior to returning
return removeDuplicateValues(tableNames), err
}
Expand All @@ -178,7 +178,7 @@ func StoreOutput(ctx context.Context, e *Executor, pol *Policy, destination stri
return fmt.Errorf("failed to create views: %w", err)
}

ef, err := os.OpenFile(fmt.Sprintf("%s/data.csv", destination), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
ef, err := os.OpenFile(filepath.Join(destination, "snapshot_data.csv"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
if err != nil {
e.log.Error("error opening file:", err)
return err
Expand All @@ -204,11 +204,13 @@ func storeOutput(ctx context.Context, e *Executor, w io.Writer, sql string) erro

func (ce *Executor) checkTableExistence(ctx context.Context, tableName string) (query string, err error) {

explainQuery := fmt.Sprintf("select pg_get_viewdef('%s'::regclass::oid) ", tableName)
explainQuery := fmt.Sprintf("select coalesce(pg_get_viewdef('%s'::regclass::oid),'') ", tableName)
rows, err := ce.conn.Query(ctx, explainQuery)
if err != nil {
ce.log.Error("error running explain", "tableName", tableName, "err", err)
return "", err
}

var s string
for rows.Next() {

Expand Down