(ctx context.Context)
| 656 | } |
| 657 | |
| 658 | func (d *Driver) getVersion(ctx context.Context) (string, error) { |
| 659 | // Redshift doesn't support SHOW server_version to retrieve the clean version number. |
| 660 | // We can parse the output of `SELECT version()` to get the PostgreSQL version and the |
| 661 | // Redshift version because Redshift is based on PostgreSQL. |
| 662 | // For example, the output of `SELECT version()` is: |
| 663 | // PostgreSQL 8.0.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3), Redshift 1.0.48042 |
| 664 | // We will return the 'Redshift 1.0.48042 based on PostgreSQL 8.0.2'. |
| 665 | rows, err := d.db.QueryContext(ctx, "SELECT version()") |
| 666 | if err != nil { |
| 667 | return "", err |
| 668 | } |
| 669 | defer rows.Close() |
| 670 | var version string |
| 671 | for rows.Next() { |
| 672 | if err := rows.Scan(&version); err != nil { |
| 673 | return "", err |
| 674 | } |
| 675 | } |
| 676 | if err := rows.Err(); err != nil { |
| 677 | return "", err |
| 678 | } |
| 679 | // We try to parse the version string to get the PostgreSQL version and the Redshift version, but it's not a big deal if we fail. |
| 680 | // We will just return the version string as is. |
| 681 | pgVersion, redshiftVersion, err := getPgVersionAndRedshiftVersion(version) |
| 682 | if err != nil { |
| 683 | slog.Debug("Failed to parse version string", slog.String("version", version)) |
| 684 | // nolint |
| 685 | return version, nil |
| 686 | } |
| 687 | return buildRedshiftVersionString(redshiftVersion, pgVersion), nil |
| 688 | } |
| 689 | |
| 690 | // parseVersionRegex is a regex to parse the output from Redshift's `SELECT version()`, captures the PostgreSQL version and the Redshift version. |
| 691 | var parseVersionRegex = regexp.MustCompile(`(?i)^PostgreSQL (?P<pgVersion>\d+\.\d+\.\d+) on .*, Redshift (?P<redshiftVersion>\d+\.\d+\.\d+)`) |
no test coverage detected