경로 통일
This commit is contained in:
35
internal/importer/helpers.go
Normal file
35
internal/importer/helpers.go
Normal file
@@ -0,0 +1,35 @@
|
||||
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
|
||||
}
|
||||
@@ -20,7 +20,7 @@ import (
|
||||
|
||||
const (
|
||||
defaultSchema = "public"
|
||||
replicaTable = "user_program_info_replica"
|
||||
ReplicaTable = "user_program_info_replica"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -63,7 +63,7 @@ func EnsureUserProgramReplica(ctx context.Context, conn *pgx.Conn, csvPath, sche
|
||||
logDir = "log"
|
||||
}
|
||||
|
||||
if err := createReplicaTable(ctx, conn, schema, replicaTable); err != nil {
|
||||
if err := createReplicaTable(ctx, conn, schema, ReplicaTable); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -97,38 +97,10 @@ func ImportUserProgramUpdates(ctx context.Context, conn *pgx.Conn, updateDir, sc
|
||||
return nil
|
||||
}
|
||||
|
||||
latestDate, err := latestCreatedDate(ctx, conn, schema, replicaTable)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
fileDate, err := dateFromFilename(file)
|
||||
if err != nil {
|
||||
_ = writeImportLog(logDir, importLog{
|
||||
StartedAt: time.Now(),
|
||||
CSVPath: file,
|
||||
Status: "skipped",
|
||||
Error: fmt.Sprintf("cannot parse date from filename: %v", err),
|
||||
LatestDate: latestDate,
|
||||
})
|
||||
continue
|
||||
}
|
||||
if !fileDate.After(latestDate) {
|
||||
_ = writeImportLog(logDir, importLog{
|
||||
StartedAt: time.Now(),
|
||||
CSVPath: file,
|
||||
Status: "skipped",
|
||||
Error: fmt.Sprintf("file date %s not after latest date %s", fileDate.Format("2006-01-02"), latestDate.Format("2006-01-02")),
|
||||
LatestDate: latestDate,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
if err := importSingle(ctx, conn, file, schema, logDir); err != nil {
|
||||
return err
|
||||
}
|
||||
latestDate = fileDate
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -362,7 +334,7 @@ func nullOrString(val string) any {
|
||||
func importSingle(ctx context.Context, conn *pgx.Conn, csvPath, schema, logDir string) error {
|
||||
startedAt := time.Now()
|
||||
|
||||
res, err := copyAndUpsertCSV(ctx, conn, csvPath, schema, replicaTable)
|
||||
res, err := copyAndUpsertCSV(ctx, conn, csvPath, schema, ReplicaTable)
|
||||
logStatus := "succeeded"
|
||||
logErrMsg := ""
|
||||
if err != nil {
|
||||
@@ -479,7 +451,7 @@ func quoteColumns(cols []string) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
func latestCreatedDate(ctx context.Context, conn *pgx.Conn, schema, table string) (time.Time, error) {
|
||||
func LatestCreatedDate(ctx context.Context, conn *pgx.Conn, schema, table string) (time.Time, error) {
|
||||
var ts sql.NullTime
|
||||
query := fmt.Sprintf("SELECT MAX(created_at) FROM %s", pgx.Identifier{schema, table}.Sanitize())
|
||||
if err := conn.QueryRow(ctx, query).Scan(&ts); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user