Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee0e49c

Browse files
committed
Add consistency check for audit logs SQL queries
1 parent 0587a91 commit ee0e49c

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

coderd/database/modelqueries_internal_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package database
22

33
import (
4+
"regexp"
5+
"strings"
46
"testing"
57
"time"
68

9+
"github.com/google/go-cmp/cmp"
710
"github.com/stretchr/testify/require"
811

912
"github.com/coder/coder/v2/testutil"
@@ -54,3 +57,41 @@ func TestWorkspaceTableConvert(t *testing.T) {
5457
"'workspace.WorkspaceTable()' is not missing at least 1 field when converting to 'WorkspaceTable'. "+
5558
"To resolve this, go to the 'func (w Workspace) WorkspaceTable()' and ensure all fields are converted.")
5659
}
60+
61+
// TestAuditLogsQueryConsistency ensures that GetAuditLogsOffset and CountAuditLogs
62+
// have identical WHERE clauses to prevent filtering inconsistencies.
63+
// This test is a guard rail to prevent developer oversight mistakes.
64+
func TestAuditLogsQueryConsistency(t *testing.T) {
65+
t.Parallel()
66+
67+
getWhereClause := extractWhereClause(getAuditLogsOffset)
68+
require.NotEmpty(t, getWhereClause, "failed to extract WHERE clause from GetAuditLogsOffset")
69+
70+
countWhereClause := extractWhereClause(countAuditLogs)
71+
require.NotEmpty(t, countWhereClause, "failed to extract WHERE clause from CountAuditLogs")
72+
73+
// Compare the WHERE clauses
74+
if diff := cmp.Diff(getWhereClause, countWhereClause); diff != "" {
75+
t.Errorf("GetAuditLogsOffset and CountAuditLogs WHERE clauses must be identical to ensure consistent filtering.\nDiff:\n%s", diff)
76+
}
77+
}
78+
79+
// extractWhereClause extracts the WHERE clause from a SQL query string
80+
func extractWhereClause(query string) string {
81+
// Find WHERE and get everything after it
82+
wherePattern := regexp.MustCompile(`(?is)WHERE\s+(.*)`)
83+
whereMatches := wherePattern.FindStringSubmatch(query)
84+
if len(whereMatches) < 2 {
85+
return ""
86+
}
87+
88+
whereClause := whereMatches[1]
89+
90+
// Remove ORDER BY, LIMIT, OFFSET clauses from the end
91+
whereClause = regexp.MustCompile(`(?is)\s+(ORDER BY|LIMIT|OFFSET).*$`).ReplaceAllString(whereClause, "")
92+
93+
// Remove SQL comments
94+
whereClause = regexp.MustCompile(`(?m)--.*$`).ReplaceAllString(whereClause, "")
95+
96+
return strings.TrimSpace(whereClause)
97+
}

coderd/database/queries.sql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/auditlogs.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ END
223223
-- Filter by build_reason
224224
AND CASE
225225
WHEN @build_reason::text != '' THEN
226-
COALESCE(wb_build.reason::text, wb_workspace.reason::text) = @build_reason
226+
COALESCE(wb_build.reason::text, wb_workspace.reason::text) = @build_reason
227227
ELSE true
228228
END
229229
-- Filter request_id

0 commit comments

Comments
 (0)