From 3975f309f85e1aa1090c7be1a2fe6e7c865dde1e Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Thu, 6 Jun 2024 10:37:05 +1000 Subject: [PATCH] refactor: use optional.Option directly in SQLC --- backend/controller/sql/models.go | 6 ++-- backend/controller/sql/querier.go | 6 ++-- backend/controller/sql/queries.sql.go | 22 ++++++------- backend/controller/sql/types.go | 45 --------------------------- go.mod | 4 +-- sqlc.yaml | 32 +++++++++---------- 6 files changed, 35 insertions(+), 80 deletions(-) diff --git a/backend/controller/sql/models.go b/backend/controller/sql/models.go index b2b5a5e8c1..0b7a9ce00b 100644 --- a/backend/controller/sql/models.go +++ b/backend/controller/sql/models.go @@ -442,8 +442,8 @@ type FsmInstance struct { Fsm schema.RefKey Key string Status FsmStatus - CurrentState NullRef - DestinationState NullRef + CurrentState optional.Option[schema.RefKey] + DestinationState optional.Option[schema.RefKey] AsyncCallID optional.Option[int64] } @@ -489,7 +489,7 @@ type Runner struct { Key model.RunnerKey Created time.Time LastSeen time.Time - ReservationTimeout NullTime + ReservationTimeout optional.Option[time.Time] State RunnerState Endpoint string ModuleName optional.Option[string] diff --git a/backend/controller/sql/querier.go b/backend/controller/sql/querier.go index 3671276f8e..166c0940cc 100644 --- a/backend/controller/sql/querier.go +++ b/backend/controller/sql/querier.go @@ -20,7 +20,7 @@ type Querier interface { // reservation key. AcquireAsyncCall(ctx context.Context, ttl time.Duration) (AcquireAsyncCallRow, error) AssociateArtefactWithDeployment(ctx context.Context, arg AssociateArtefactWithDeploymentParams) error - BeginConsumingTopicEvent(ctx context.Context, subscriptionID optional.Option[int64], event NullTopicEventKey) error + BeginConsumingTopicEvent(ctx context.Context, subscriptionID optional.Option[int64], event optional.Option[model.TopicEventKey]) error CompleteEventForSubscription(ctx context.Context, name string, module string) error // Create a new artefact and return the artefact ID. CreateArtefact(ctx context.Context, digest []byte, content []byte) (int64, error) @@ -63,7 +63,7 @@ type Querier interface { GetIngressRoutes(ctx context.Context, method string) ([]GetIngressRoutesRow, error) GetModuleConfiguration(ctx context.Context, module optional.Option[string], name string) ([]byte, error) GetModulesByID(ctx context.Context, ids []int64) ([]Module, error) - GetNextEventForSubscription(ctx context.Context, topic model.TopicKey, cursor NullTopicEventKey) (GetNextEventForSubscriptionRow, error) + GetNextEventForSubscription(ctx context.Context, topic model.TopicKey, cursor optional.Option[model.TopicEventKey]) (GetNextEventForSubscriptionRow, error) GetProcessList(ctx context.Context) ([]GetProcessListRow, error) // Retrieve routing information for a runner. GetRouteForRunner(ctx context.Context, key model.RunnerKey) (GetRouteForRunnerRow, error) @@ -85,7 +85,7 @@ type Querier interface { ListModuleConfiguration(ctx context.Context) ([]ModuleConfiguration, error) LoadAsyncCall(ctx context.Context, id int64) (AsyncCall, error) // get a lock on the subscription row, guaranteeing that it is idle and has not consumed more events - LockSubscriptionAndGetSink(ctx context.Context, key model.SubscriptionKey, cursor NullTopicEventKey) (LockSubscriptionAndGetSinkRow, error) + LockSubscriptionAndGetSink(ctx context.Context, key model.SubscriptionKey, cursor optional.Option[model.TopicEventKey]) (LockSubscriptionAndGetSinkRow, error) NewLease(ctx context.Context, key leases.Key, ttl time.Duration) (uuid.UUID, error) PublishEventForTopic(ctx context.Context, arg PublishEventForTopicParams) error ReleaseLease(ctx context.Context, idempotencyKey uuid.UUID, key leases.Key) (bool, error) diff --git a/backend/controller/sql/queries.sql.go b/backend/controller/sql/queries.sql.go index f8f9b3eae3..e67324423e 100644 --- a/backend/controller/sql/queries.sql.go +++ b/backend/controller/sql/queries.sql.go @@ -112,7 +112,7 @@ SET state = 'executing', WHERE id = $1 ` -func (q *Queries) BeginConsumingTopicEvent(ctx context.Context, subscriptionID optional.Option[int64], event NullTopicEventKey) error { +func (q *Queries) BeginConsumingTopicEvent(ctx context.Context, subscriptionID optional.Option[int64], event optional.Option[model.TopicEventKey]) error { _, err := q.db.Exec(ctx, beginConsumingTopicEvent, subscriptionID, event) return err } @@ -1202,11 +1202,11 @@ LIMIT 1 ` type GetNextEventForSubscriptionRow struct { - Event NullTopicEventKey + Event optional.Option[model.TopicEventKey] Payload []byte } -func (q *Queries) GetNextEventForSubscription(ctx context.Context, topic model.TopicKey, cursor NullTopicEventKey) (GetNextEventForSubscriptionRow, error) { +func (q *Queries) GetNextEventForSubscription(ctx context.Context, topic model.TopicKey, cursor optional.Option[model.TopicEventKey]) (GetNextEventForSubscriptionRow, error) { row := q.db.QueryRow(ctx, getNextEventForSubscription, topic, cursor) var i GetNextEventForSubscriptionRow err := row.Scan(&i.Event, &i.Payload) @@ -1230,7 +1230,7 @@ type GetProcessListRow struct { MinReplicas int32 DeploymentKey model.DeploymentKey DeploymentLabels []byte - RunnerKey NullRunnerKey + RunnerKey optional.Option[model.RunnerKey] Endpoint optional.Option[string] RunnerLabels []byte } @@ -1273,7 +1273,7 @@ type GetRouteForRunnerRow struct { Endpoint string RunnerKey model.RunnerKey ModuleName optional.Option[string] - DeploymentKey NullDeploymentKey + DeploymentKey optional.Option[model.DeploymentKey] State RunnerState } @@ -1304,7 +1304,7 @@ type GetRoutingTableRow struct { Endpoint string RunnerKey model.RunnerKey ModuleName optional.Option[string] - DeploymentKey NullDeploymentKey + DeploymentKey optional.Option[model.DeploymentKey] } func (q *Queries) GetRoutingTable(ctx context.Context, modules []string) ([]GetRoutingTableRow, error) { @@ -1398,7 +1398,7 @@ type GetRunnersForDeploymentRow struct { Key model.RunnerKey Created time.Time LastSeen time.Time - ReservationTimeout NullTime + ReservationTimeout optional.Option[time.Time] State RunnerState Endpoint string ModuleName optional.Option[string] @@ -1514,7 +1514,7 @@ WHERE subs.cursor IS DISTINCT FROM topics.head type GetSubscriptionsNeedingUpdateRow struct { Key model.SubscriptionKey - Cursor NullTopicEventKey + Cursor optional.Option[model.TopicEventKey] Topic model.TopicKey Name string } @@ -1619,7 +1619,7 @@ type InsertDeploymentCreatedEventParams struct { Language string ModuleName string MinReplicas int32 - Replaced NullDeploymentKey + Replaced optional.Option[model.DeploymentKey] } func (q *Queries) InsertDeploymentCreatedEvent(ctx context.Context, arg InsertDeploymentCreatedEventParams) error { @@ -1900,7 +1900,7 @@ type LockSubscriptionAndGetSinkRow struct { } // get a lock on the subscription row, guaranteeing that it is idle and has not consumed more events -func (q *Queries) LockSubscriptionAndGetSink(ctx context.Context, key model.SubscriptionKey, cursor NullTopicEventKey) (LockSubscriptionAndGetSinkRow, error) { +func (q *Queries) LockSubscriptionAndGetSink(ctx context.Context, key model.SubscriptionKey, cursor optional.Option[model.TopicEventKey]) (LockSubscriptionAndGetSinkRow, error) { row := q.db.QueryRow(ctx, lockSubscriptionAndGetSink, key, cursor) var i LockSubscriptionAndGetSinkRow err := row.Scan(&i.SubscriptionID, &i.Sink) @@ -2286,7 +2286,7 @@ type UpsertRunnerParams struct { Endpoint string State RunnerState Labels []byte - DeploymentKey NullDeploymentKey + DeploymentKey optional.Option[model.DeploymentKey] } // Upsert a runner and return the deployment ID that it is assigned to, if any. diff --git a/backend/controller/sql/types.go b/backend/controller/sql/types.go index 1c53616c66..90bc4f4225 100644 --- a/backend/controller/sql/types.go +++ b/backend/controller/sql/types.go @@ -1,60 +1,15 @@ package sql import ( - "database/sql" "database/sql/driver" "fmt" - "time" - "github.com/alecthomas/types/optional" - "github.com/google/uuid" "google.golang.org/protobuf/proto" - "github.com/TBD54566975/ftl/backend/controller/leases" schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema" "github.com/TBD54566975/ftl/backend/schema" - "github.com/TBD54566975/ftl/internal/model" ) -type NullType = optional.Option[Type] -type NullRef = optional.Option[schema.RefKey] -type NullUUID = optional.Option[uuid.UUID] -type NullLeaseKey = optional.Option[leases.Key] -type NullTime = optional.Option[time.Time] -type NullDuration = optional.Option[time.Duration] -type NullRunnerKey = optional.Option[model.RunnerKey] -type NullCronJobKey = optional.Option[model.CronJobKey] -type NullDeploymentKey = optional.Option[model.DeploymentKey] -type NullRequestKey = optional.Option[model.RequestKey] -type NullTopicKey = optional.Option[model.TopicKey] -type NullSubscriptionKey = optional.Option[model.SubscriptionKey] -type NullSubscriberKey = optional.Option[model.SubscriberKey] -type NullTopicEventKey = optional.Option[model.TopicEventKey] - -var _ sql.Scanner = (*NullRunnerKey)(nil) -var _ driver.Valuer = (*NullRunnerKey)(nil) - -var _ sql.Scanner = (*NullCronJobKey)(nil) -var _ driver.Valuer = (*NullCronJobKey)(nil) - -var _ sql.Scanner = (*NullDeploymentKey)(nil) -var _ driver.Valuer = (*NullDeploymentKey)(nil) - -var _ sql.Scanner = (*NullRequestKey)(nil) -var _ driver.Valuer = (*NullRequestKey)(nil) - -var _ sql.Scanner = (*NullTopicKey)(nil) -var _ driver.Valuer = (*NullTopicKey)(nil) - -var _ sql.Scanner = (*NullSubscriptionKey)(nil) -var _ driver.Valuer = (*NullSubscriptionKey)(nil) - -var _ sql.Scanner = (*NullSubscriberKey)(nil) -var _ driver.Valuer = (*NullSubscriberKey)(nil) - -var _ sql.Scanner = (*NullTopicEventKey)(nil) -var _ driver.Valuer = (*NullTopicEventKey)(nil) - // Type is a database adapter type for schema.Type. // // It encodes to/from the protobuf representation of a Type. diff --git a/go.mod b/go.mod index c07508bf3a..fac143e5a0 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module github.com/TBD54566975/ftl go 1.22.2 -retract v1.1.0 - require ( connectrpc.com/connect v1.16.1 connectrpc.com/grpcreflect v1.2.0 @@ -146,3 +144,5 @@ require ( modernc.org/strutil v1.2.0 // indirect modernc.org/token v1.1.0 // indirect ) + +retract v1.1.0 diff --git a/sqlc.yaml b/sqlc.yaml index 73c0214201..399b0a0dc1 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -18,7 +18,7 @@ sql: - db_type: "uuid" nullable: true go_type: - type: "NullUUID" + type: "optional.Option[uuid.UUID]" - db_type: "timestamptz" go_type: "time.Time" - db_type: "pg_catalog.interval" @@ -26,13 +26,13 @@ sql: - db_type: "pg_catalog.interval" nullable: true go_type: - type: "NullDuration" + type: "optional.Option[time.Duration]" - db_type: "module_schema_pb" go_type: "*github.com/TBD54566975/ftl/backend/schema.Module" - db_type: "timestamptz" nullable: true go_type: - type: "NullTime" + type: "optional.Option[time.Time]" - db_type: "pg_catalog.varchar" nullable: true go_type: "github.com/alecthomas/types/optional.Option[string]" @@ -41,40 +41,40 @@ sql: - db_type: "runner_key" nullable: true go_type: - type: "NullRunnerKey" + type: "optional.Option[model.RunnerKey]" - db_type: "schema_ref" go_type: "github.com/TBD54566975/ftl/backend/schema.RefKey" - db_type: "schema_ref" nullable: true go_type: - type: "NullRef" + type: "optional.Option[schema.RefKey]" - db_type: "schema_type" go_type: type: "Type" - db_type: "schema_type" nullable: true go_type: - type: "NullType" + type: "optional.Option[Type]" - db_type: "cron_job_key" go_type: "github.com/TBD54566975/ftl/internal/model.CronJobKey" - db_type: "cron_job_key" nullable: true go_type: - type: "NullCronJobKey" + type: "optional.Option[model.CronJobKey]" - db_type: "lease_key" go_type: "github.com/TBD54566975/ftl/backend/controller/leases.Key" - db_type: "lease_key" nullable: true go_type: - type: "NullLeaseKey" + type: "optional.Option[leases.Key]" - db_type: "deployment_key" go_type: "github.com/TBD54566975/ftl/internal/model.DeploymentKey" - - db_type: "cron_job_state" - go_type: "github.com/TBD54566975/ftl/internal/model.CronJobState" - db_type: "deployment_key" nullable: true go_type: - type: "NullDeploymentKey" + type: "optional.Option[model.DeploymentKey]" + - db_type: "cron_job_state" + go_type: "github.com/TBD54566975/ftl/internal/model.CronJobState" - db_type: "controller_key" go_type: "github.com/TBD54566975/ftl/internal/model.ControllerKey" - db_type: "request_key" @@ -82,31 +82,31 @@ sql: - db_type: "request_key" nullable: true go_type: - type: "NullRequestKey" + type: "optional.Option[model.RequestKey]" - db_type: "topic_key" go_type: "github.com/TBD54566975/ftl/internal/model.TopicKey" - db_type: "topic_key" nullable: true go_type: - type: "NullTopicKey" + type: "optional.Option[model.TopicKey]" - db_type: "subscription_key" go_type: "github.com/TBD54566975/ftl/internal/model.SubscriptionKey" - db_type: "subscription_key" nullable: true go_type: - type: "NullSubscriptionKey" + type: "optional.Option[model.SubscriptionKey]" - db_type: "subscriber_key" go_type: "github.com/TBD54566975/ftl/internal/model.SubscriberKey" - db_type: "subscriber_key" nullable: true go_type: - type: "NullSubscriberKey" + type: "optional.Option[model.SubscriberKey]" - db_type: "topic_event_key" go_type: "github.com/TBD54566975/ftl/internal/model.TopicEventKey" - db_type: "topic_event_key" nullable: true go_type: - type: "NullTopicEventKey" + type: "optional.Option[model.TopicEventKey]" - db_type: "text" go_type: "string" - db_type: "text"