Skip to content

Commit

Permalink
refactor: move codegenned sql queries into internal packages of each …
Browse files Browse the repository at this point in the history
…dal (#2567)

The codegenned types are an implementation detail and shouldn't be
leaked out of their respective dal package. For convenience though, some
of the sql types are just type mapped to the new package.
  • Loading branch information
alecthomas authored Sep 1, 2024
1 parent 37aed73 commit b849d95
Show file tree
Hide file tree
Showing 31 changed files with 130 additions and 91 deletions.
5 changes: 2 additions & 3 deletions backend/controller/cronjobs/cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/benbjohnson/clock"

"github.com/TBD54566975/ftl/backend/controller/cronjobs/dal"
cronsql "github.com/TBD54566975/ftl/backend/controller/cronjobs/sql"
parentdal "github.com/TBD54566975/ftl/backend/controller/dal"
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
"github.com/TBD54566975/ftl/backend/schema"
Expand Down Expand Up @@ -175,7 +174,7 @@ func (s *Service) scheduleCronJob(ctx context.Context, tx *dal.DAL, job model.Cr

logger.Tracef("Scheduling cron job %q async_call execution at %s", job.Key, nextAttemptForJob)
origin := &parentdal.AsyncOriginCron{CronJobKey: job.Key}
id, err := tx.CreateAsyncCall(ctx, cronsql.CreateAsyncCallParams{
id, err := tx.CreateAsyncCall(ctx, dal.CreateAsyncCallParams{
ScheduledAt: nextAttemptForJob,
Verb: schema.RefKey{Module: job.Verb.Module, Name: job.Verb.Name},
Origin: origin.String(),
Expand All @@ -189,7 +188,7 @@ func (s *Service) scheduleCronJob(ctx context.Context, tx *dal.DAL, job model.Cr
return fmt.Errorf("failed to calculate future execution for cron job %q with schedule %q: %w", job.Key, job.Schedule, err)
}
logger.Tracef("Updating cron job %q with last attempt at %s and next attempt at %s", job.Key, nextAttemptForJob, futureAttemptForJob)
err = tx.UpdateCronJobExecution(ctx, cronsql.UpdateCronJobExecutionParams{
err = tx.UpdateCronJobExecution(ctx, dal.UpdateCronJobExecutionParams{
LastAsyncCallID: id,
LastExecution: nextAttemptForJob,
NextExecution: futureAttemptForJob,
Expand Down
14 changes: 9 additions & 5 deletions backend/controller/cronjobs/dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/alecthomas/types/optional"

"github.com/TBD54566975/ftl/backend/controller/cronjobs/sql"
"github.com/TBD54566975/ftl/backend/controller/cronjobs/dal/internal/sql"
"github.com/TBD54566975/ftl/backend/controller/observability"
"github.com/TBD54566975/ftl/backend/libdal"
"github.com/TBD54566975/ftl/backend/schema"
Expand Down Expand Up @@ -41,9 +41,11 @@ func cronJobFromRow(c sql.CronJob, d sql.Deployment) model.CronJob {
}
}

type CreateAsyncCallParams sql.CreateAsyncCallParams

// CreateAsyncCall creates an async_call row and returns its id
func (d *DAL) CreateAsyncCall(ctx context.Context, params sql.CreateAsyncCallParams) (int64, error) {
id, err := d.db.CreateAsyncCall(ctx, params)
func (d *DAL) CreateAsyncCall(ctx context.Context, params CreateAsyncCallParams) (int64, error) {
id, err := d.db.CreateAsyncCall(ctx, sql.CreateAsyncCallParams(params))
if err != nil {
return 0, fmt.Errorf("failed to create async call: %w", libdal.TranslatePGError(err))
}
Expand Down Expand Up @@ -87,10 +89,12 @@ func (d *DAL) IsCronJobPending(ctx context.Context, key model.CronJobKey, startT
return pending, nil
}

type UpdateCronJobExecutionParams sql.UpdateCronJobExecutionParams

// UpdateCronJobExecution updates the last_async_call_id, last_execution, and next_execution of
// the cron job given by the provided key
func (d *DAL) UpdateCronJobExecution(ctx context.Context, params sql.UpdateCronJobExecutionParams) error {
err := d.db.UpdateCronJobExecution(ctx, params)
func (d *DAL) UpdateCronJobExecution(ctx context.Context, params UpdateCronJobExecutionParams) error {
err := d.db.UpdateCronJobExecution(ctx, sql.UpdateCronJobExecutionParams(params))
if err != nil {
return fmt.Errorf("failed to update cron job %q: %w", params.Key, libdal.TranslatePGError(err))
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion backend/controller/dal/async_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/alecthomas/types/either"
"github.com/alecthomas/types/optional"

"github.com/TBD54566975/ftl/backend/controller/dal/internal/sql"
leasedal "github.com/TBD54566975/ftl/backend/controller/leases/dal"
"github.com/TBD54566975/ftl/backend/controller/sql"
"github.com/TBD54566975/ftl/backend/controller/sql/sqltypes"
"github.com/TBD54566975/ftl/backend/libdal"
"github.com/TBD54566975/ftl/backend/schema"
Expand Down
Loading

0 comments on commit b849d95

Please sign in to comment.