36 lines
943 B
Go
36 lines
943 B
Go
package importer
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
// LatestID returns the maximum id in the replica table.
|
|
func LatestID(ctx context.Context, conn *pgx.Conn, schema, table string) (int64, error) {
|
|
var id sql.NullInt64
|
|
query := fmt.Sprintf("SELECT MAX(id) FROM %s", pgx.Identifier{schema, table}.Sanitize())
|
|
if err := conn.QueryRow(ctx, query).Scan(&id); err != nil {
|
|
return 0, err
|
|
}
|
|
if !id.Valid {
|
|
return 0, nil
|
|
}
|
|
return id.Int64, nil
|
|
}
|
|
|
|
// CountUpToID returns the number of rows with id <= maxID.
|
|
func CountUpToID(ctx context.Context, conn *pgx.Conn, schema, table string, maxID int64) (int64, error) {
|
|
var count sql.NullInt64
|
|
query := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE id <= $1", pgx.Identifier{schema, table}.Sanitize())
|
|
if err := conn.QueryRow(ctx, query, maxID).Scan(&count); err != nil {
|
|
return 0, err
|
|
}
|
|
if !count.Valid {
|
|
return 0, nil
|
|
}
|
|
return count.Int64, nil
|
|
}
|