Skip to content
Closed
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
15 changes: 15 additions & 0 deletions riverdriver/riverdatabasesql/river_database_sql_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,7 @@ type templateReplaceWrapper struct {

func (w templateReplaceWrapper) ExecContext(ctx context.Context, sql string, args ...interface{}) (sql.Result, error) {
sql, args = w.replacer.Run(ctx, sql, args)
convertDurationToInterval(sql, args)
return w.dbtx.ExecContext(ctx, sql, args...)
}

Expand Down Expand Up @@ -1107,3 +1108,17 @@ func schemaTemplateParam(ctx context.Context, schema string) context.Context {
"schema": {Value: schema},
}, nil)
}

// convertDurationToInterval converts Go's time.Duration to PostgreSQL's
func convertDurationToInterval(sql string, args []any) {
if !strings.Contains(sql, "interval") {
return
}

for i, arg := range args {
if d, ok := arg.(time.Duration); ok {
pgInterval := fmt.Sprintf("%d seconds", int(d.Seconds()))
args[i] = pgInterval
}
}
}
32 changes: 32 additions & 0 deletions riverdriver/riverdatabasesql/river_database_sql_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"errors"
"testing"
"time"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -131,3 +132,34 @@ func TestSchemaTemplateParam(t *testing.T) {
require.Equal(t, "SELECT 1 FROM custom_schema.river_job", updatedSQL)
})
}

func TestConvertDurationToInterval(t *testing.T) {
t.Parallel()

testCases := []struct {
Desc string
InputSQL string
InputArgs []any
ExpectedArgs []any
}{
{
Desc: "Convert duration to interval",
InputSQL: `
INSERT INTO river_leader(leader_id, elected_At, expires_at)
VALUES($1, now(), now() + $2::interval)
ON CONFICT (name)
DO NOTHING
`,
InputArgs: []any{"river", 15 * time.Second},
ExpectedArgs: []any{"river", "15 seconds"},
},
}
for _, tt := range testCases {
t.Run(tt.Desc, func(t *testing.T) {
t.Parallel()

convertDurationToInterval(tt.InputSQL, tt.InputArgs)
require.Equal(t, tt.InputArgs, tt.ExpectedArgs)
})
}
}