Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v16] Normalize line breaks and spacing on PostgreSQL session playback. #44315

Merged
merged 2 commits into from
Jul 17, 2024
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
13 changes: 12 additions & 1 deletion lib/player/db/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,18 @@ func (p *PostgresTranslator) TranslateEvent(evt events.AuditEvent) *events.Sessi
const lineBreak = "\r\n"

func (p *PostgresTranslator) generateCommandPrint(metadata events.DatabaseMetadata, command string) string {
return lineBreak + fmt.Sprintf("%s=> %s", metadata.DatabaseName, command) + lineBreak
lead := metadata.DatabaseName + "=> "
leadSpacing := strings.Repeat(" ", len(lead))

var sb strings.Builder
commandLines := strings.Split(command, "\n")
sb.WriteString(lead + strings.TrimSpace(commandLines[0]) + lineBreak)

for i := 1; i < len(commandLines); i++ {
sb.WriteString(leadSpacing + strings.TrimSpace(commandLines[i]) + lineBreak)
}

return lineBreak + sb.String()
}

func (p *PostgresTranslator) generatePreparedStatementPrint(metadata events.Metadata, databaseMetadata events.DatabaseMetadata, portalName string) *events.SessionPrint {
Expand Down
61 changes: 61 additions & 0 deletions lib/player/db/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,67 @@ func TestPostgresRecording(t *testing.T) {
nil,
},
},
"queries with line spacing": {
events: []events.AuditEvent{
&events.DatabaseSessionQuery{
DatabaseMetadata: events.DatabaseMetadata{DatabaseName: "test"},
DatabaseQuery: "SELECT \n1;",
},
&events.DatabaseSessionCommandResult{
DatabaseMetadata: events.DatabaseMetadata{DatabaseName: "test"},
Status: events.Status{Success: true},
AffectedRecords: 1,
},
&events.DatabaseSessionQuery{
DatabaseMetadata: events.DatabaseMetadata{DatabaseName: "test"},
DatabaseQuery: "SELECT *\nfrom events\nlimit 1;",
},
&events.DatabaseSessionCommandResult{
DatabaseMetadata: events.DatabaseMetadata{DatabaseName: "test"},
Status: events.Status{Success: true},
AffectedRecords: 1,
},
&events.DatabaseSessionQuery{
DatabaseMetadata: events.DatabaseMetadata{DatabaseName: "test"},
DatabaseQuery: "SELECT *\r\nfrom events\r\nlimit 1;",
},
&events.DatabaseSessionCommandResult{
DatabaseMetadata: events.DatabaseMetadata{DatabaseName: "test"},
Status: events.Status{Success: true},
AffectedRecords: 1,
},
&events.DatabaseSessionQuery{
DatabaseMetadata: events.DatabaseMetadata{DatabaseName: "test"},
DatabaseQuery: "SELECT *\r\nfrom events\r\nlimit 1;",
},
&events.DatabaseSessionCommandResult{
DatabaseMetadata: events.DatabaseMetadata{DatabaseName: "test"},
Status: events.Status{Success: true},
AffectedRecords: 1,
},
&events.DatabaseSessionQuery{
DatabaseMetadata: events.DatabaseMetadata{DatabaseName: "test"},
DatabaseQuery: "SELECT *\t\r\n from events\t\r\n limit 1;",
},
&events.DatabaseSessionCommandResult{
DatabaseMetadata: events.DatabaseMetadata{DatabaseName: "test"},
Status: events.Status{Success: true},
AffectedRecords: 1,
},
},
expectedPrints: [][]byte{
[]byte("\r\ntest=> SELECT\r\n 1;\r\n"),
[]byte("SUCCESS\r\n(1 row affected)\r\n"),
[]byte("\r\ntest=> SELECT *\r\n from events\r\n limit 1;\r\n"),
[]byte("SUCCESS\r\n(1 row affected)\r\n"),
[]byte("\r\ntest=> SELECT *\r\n from events\r\n limit 1;\r\n"),
[]byte("SUCCESS\r\n(1 row affected)\r\n"),
[]byte("\r\ntest=> SELECT *\r\n from events\r\n limit 1;\r\n"),
[]byte("SUCCESS\r\n(1 row affected)\r\n"),
[]byte("\r\ntest=> SELECT *\r\n from events\r\n limit 1;\r\n"),
[]byte("SUCCESS\r\n(1 row affected)\r\n"),
},
},
} {
t.Run(name, func(t *testing.T) {
translator := NewPostgresTranslator()
Expand Down
Loading