(ctx context.Context, databaseURL string, statementTimeout *time.Duration)
| 131 | } |
| 132 | |
| 133 | func openPgxV5DBPool(ctx context.Context, databaseURL string, statementTimeout *time.Duration) (*pgxpool.Pool, error) { |
| 134 | const ( |
| 135 | defaultIdleInTransactionSessionTimeout = 11 * time.Second // should be greater than statement timeout because statements count towards idle-in-transaction |
| 136 | defaultStatementTimeout = 10 * time.Second |
| 137 | ) |
| 138 | |
| 139 | pgxConfig, err := pgxpool.ParseConfig(databaseURL) |
| 140 | if err != nil { |
| 141 | return nil, fmt.Errorf("error parsing database URL: %w", err) |
| 142 | } |
| 143 | |
| 144 | // Sets a parameter in a parameter map (aimed at a Postgres connection |
| 145 | // configuration map), but only if that parameter wasn't already set. |
| 146 | setParamIfUnset := func(runtimeParams map[string]string, name, val string) { |
| 147 | if currentVal := runtimeParams[name]; currentVal != "" { |
| 148 | return |
| 149 | } |
| 150 | |
| 151 | runtimeParams[name] = val |
| 152 | } |
| 153 | |
| 154 | runtimeParams := pgxConfig.ConnConfig.RuntimeParams |
| 155 | if runtimeParams == nil { |
| 156 | runtimeParams = make(map[string]string) |
| 157 | pgxConfig.ConnConfig.RuntimeParams = runtimeParams |
| 158 | } |
| 159 | |
| 160 | var statementTimeoutMilliseconds string |
| 161 | if statementTimeout != nil { |
| 162 | statementTimeoutMilliseconds = strconv.Itoa(int(statementTimeout.Milliseconds())) |
| 163 | } |
| 164 | |
| 165 | setParamIfUnset(runtimeParams, "application_name", "river CLI") |
| 166 | setParamIfUnset(runtimeParams, "idle_in_transaction_session_timeout", strconv.Itoa(int(defaultIdleInTransactionSessionTimeout.Milliseconds()))) |
| 167 | runtimeParams["statement_timeout"] = cmp.Or( |
| 168 | statementTimeoutMilliseconds, |
| 169 | runtimeParams["statement_timeout"], |
| 170 | strconv.Itoa(int(defaultStatementTimeout.Milliseconds())), |
| 171 | ) |
| 172 | |
| 173 | dbPool, err := pgxpool.NewWithConfig(ctx, pgxConfig) |
| 174 | if err != nil { |
| 175 | return nil, fmt.Errorf("error connecting to Postgres database: %w", err) |
| 176 | } |
| 177 | |
| 178 | return dbPool, nil |
| 179 | } |
| 180 | |
| 181 | func openSQLitePool(protocol, urlWithoutProtocol string) (*sql.DB, error) { |
| 182 | dbPool, err := sql.Open(protocol, urlWithoutProtocol) |
no test coverage detected
searching dependent graphs…