-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #213 from moov-io/file-tracking-table
pipeline: record accepted files in database table
- Loading branch information
Showing
10 changed files
with
204 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package files | ||
|
||
import "context" | ||
|
||
type MockRepository struct { | ||
Err error | ||
} | ||
|
||
func (r *MockRepository) Record(_ context.Context, file AcceptedFile) error { | ||
return r.Err | ||
} | ||
|
||
func (r *MockRepository) Cancel(_ context.Context, fileID string) error { | ||
return r.Err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package files | ||
|
||
import "time" | ||
|
||
type AcceptedFile struct { | ||
FileID string | ||
ShardKey string | ||
|
||
Hostname string | ||
|
||
AcceptedAt time.Time | ||
CanceledAt time.Time | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package files | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/moov-io/base/telemetry" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
type Repository interface { | ||
Record(ctx context.Context, file AcceptedFile) error | ||
Cancel(ctx context.Context, fileID string) error | ||
} | ||
|
||
func NewRepository(db *sql.DB) Repository { | ||
if db == nil { | ||
return &MockRepository{} | ||
} | ||
return &sqlRepository{db: db} | ||
} | ||
|
||
type sqlRepository struct { | ||
db *sql.DB | ||
} | ||
|
||
func (r *sqlRepository) Record(ctx context.Context, file AcceptedFile) error { | ||
ctx, span := telemetry.StartSpan(ctx, "files-record", trace.WithAttributes( | ||
attribute.String("achgateway.file_id", file.FileID), | ||
)) | ||
defer span.End() | ||
|
||
qry := `INSERT INTO files (file_id, shard_key, hostname, accepted_at) VALUES (?,?,?,?);` | ||
_, err := r.db.ExecContext(ctx, qry, | ||
file.FileID, | ||
file.ShardKey, | ||
file.Hostname, | ||
file.AcceptedAt, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("recording file failed: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (r *sqlRepository) Cancel(ctx context.Context, fileID string) error { | ||
ctx, span := telemetry.StartSpan(ctx, "files-cancel", trace.WithAttributes( | ||
attribute.String("achgateway.file_id", fileID), | ||
)) | ||
defer span.End() | ||
|
||
qry := `UPDATE files SET canceled_at = ? WHERE file_id = ? AND canceled_at IS NULL;` | ||
_, err := r.db.ExecContext(ctx, qry, | ||
// SET | ||
time.Now().In(time.UTC), | ||
// WHERE | ||
fileID, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("saving file cancellation failed: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package files | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/moov-io/achgateway/internal/dbtest" | ||
"github.com/moov-io/base" | ||
"github.com/moov-io/base/database" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRepository(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("-short flag was specified") | ||
} | ||
|
||
conf := dbtest.CreateTestDatabase(t, dbtest.LocalDatabaseConfig()) | ||
db := dbtest.LoadDatabase(t, conf) | ||
require.NoError(t, db.Ping()) | ||
|
||
repo := NewRepository(db) | ||
if _, ok := repo.(*sqlRepository); !ok { | ||
t.Errorf("unexpected repository type: %T", repo) | ||
} | ||
|
||
ctx := context.Background() | ||
fileID1 := base.ID() | ||
accepted := AcceptedFile{ | ||
FileID: fileID1, | ||
ShardKey: base.ID(), | ||
Hostname: "achgateway-0", | ||
AcceptedAt: time.Now(), | ||
} | ||
|
||
// Record | ||
err := repo.Record(ctx, accepted) | ||
require.NoError(t, err) | ||
|
||
err = repo.Record(ctx, accepted) | ||
require.ErrorContains(t, err, "Duplicate entry") | ||
require.True(t, database.UniqueViolation(err)) | ||
|
||
// Second File | ||
fileID2 := base.ID() | ||
accepted.FileID = fileID2 | ||
err = repo.Record(ctx, accepted) | ||
require.NoError(t, err) | ||
|
||
// Cancel | ||
err = repo.Cancel(ctx, fileID1) | ||
require.NoError(t, err) | ||
|
||
err = repo.Cancel(ctx, base.ID()) | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CREATE TABLE files( | ||
file_id VARCHAR(128) PRIMARY KEY, | ||
shard_key VARCHAR(50) NOT NULL, | ||
hostname VARCHAR(100) NOT NULL, | ||
accepted_at TIMESTAMP NOT NULL, | ||
canceled_at TIMESTAMP | ||
); |