SetLatestVersion sets is_latest=true on the given version and false on all other versions of the same server. Passing an empty version clears is_latest for all rows. The clear and set are issued as separate statements because the unique partial index idx_unique_latest_per_server is non-deferrable a
(ctx context.Context, tx pgx.Tx, serverName, version string)
| 908 | // idx_unique_latest_per_server is non-deferrable and Postgres checks it row-by-row within |
| 909 | // a single UPDATE, which would trip when flipping one row's flag off and another's on. |
| 910 | func (db *PostgreSQL) SetLatestVersion(ctx context.Context, tx pgx.Tx, serverName, version string) error { |
| 911 | if ctx.Err() != nil { |
| 912 | return ctx.Err() |
| 913 | } |
| 914 | |
| 915 | executor := db.getExecutor(tx) |
| 916 | |
| 917 | if _, err := executor.Exec(ctx, |
| 918 | `UPDATE servers SET is_latest = false WHERE server_name = $1 AND is_latest = true AND version <> $2`, |
| 919 | serverName, version, |
| 920 | ); err != nil { |
| 921 | return fmt.Errorf("failed to clear previous latest version: %w", err) |
| 922 | } |
| 923 | |
| 924 | if version == "" { |
| 925 | return nil |
| 926 | } |
| 927 | |
| 928 | if _, err := executor.Exec(ctx, |
| 929 | `UPDATE servers SET is_latest = true WHERE server_name = $1 AND version = $2 AND is_latest = false`, |
| 930 | serverName, version, |
| 931 | ); err != nil { |
| 932 | return fmt.Errorf("failed to set latest version: %w", err) |
| 933 | } |
| 934 | |
| 935 | return nil |
| 936 | } |
| 937 | |
| 938 | // Close closes the database connection |
| 939 | func (db *PostgreSQL) Close() error { |
nothing calls this directly
no test coverage detected