From 2b403f19920b6dc134a2e6d35c7aafad44fa775b Mon Sep 17 00:00:00 2001 From: Ruben Bartelink Date: Mon, 28 Mar 2022 17:25:35 +0100 Subject: [PATCH] Extract InternalCreateOrValidateTableAsync --- src/FSharp.AWS.DynamoDB/TableContext.fs | 48 +++++++++++++------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/FSharp.AWS.DynamoDB/TableContext.fs b/src/FSharp.AWS.DynamoDB/TableContext.fs index a105030..f559073 100644 --- a/src/FSharp.AWS.DynamoDB/TableContext.fs +++ b/src/FSharp.AWS.DynamoDB/TableContext.fs @@ -904,7 +904,6 @@ type TableContext<'TRecord> internal return () } - member internal _.InternalDescribe() : Async = let rec wait () = async { let! ct = Async.CancellationToken @@ -919,7 +918,7 @@ type TableContext<'TRecord> internal } wait () - member internal __.CreateOrValidateTableAsync(createThroughput) : Async = + member internal __.InternalProvision(?makeCreateTableRequest) : Async = let (|Conflict|_|) (e : exn) = match e with | :? AmazonDynamoDBException as e when e.StatusCode = HttpStatusCode.Conflict -> Some() @@ -936,11 +935,10 @@ type TableContext<'TRecord> internal |> invalidOp return desc - | Choice2Of2 (:? ResourceNotFoundException) when Option.isSome createThroughput -> + | Choice2Of2 (:? ResourceNotFoundException) when Option.isSome makeCreateTableRequest -> let! ct = Async.CancellationToken - let ctr = template.Info.Schemata.CreateCreateTableRequest(tableName, Option.get createThroughput) let! response = - client.CreateTableAsync(ctr, ct) + client.CreateTableAsync(makeCreateTableRequest.Value (), ct) |> Async.AwaitTaskCorrect |> Async.Catch @@ -961,26 +959,17 @@ type TableContext<'TRecord> internal checkOrCreate 9 // up to 9 retries, i.e. 10 attempts before we let exception propagate - /// - /// Asynchronously verify that the table exists and is compatible with record key schema. - /// - /// Create the table instance now instance if it does not exist. Defaults to false. - /// Provisioned throughput for the table if newly created. Defaults to (10,10). - [] - member __.VerifyTableAsync(?createIfNotExists : bool, ?provisionedThroughput : ProvisionedThroughput) : Async = - let throughputIfCreate = - if createIfNotExists = Some true then - let throughput = match provisionedThroughput with Some p -> p | None -> ProvisionedThroughput(10L, 10L) - Some throughput - else None - __.CreateOrValidateTableAsync(throughputIfCreate) |> Async.Ignore + member internal _.InternalCreateTableRequest(throughput) = + template.Info.Schemata.CreateCreateTableRequest(tableName, throughput) + member internal t.InternalCreateOrValidateTableAsync(createThroughput) = + t.InternalProvision(fun () -> t.InternalCreateTableRequest(createThroughput)) /// /// Asynchronously verify that the table exists and is compatible with record key schema, or throw.
/// See also InitializeTableAsync, which performs the same check, but can create or re-provision the Table if required. ///
member __.VerifyTableAsync() : Async = - __.CreateOrValidateTableAsync(None) |> Async.Ignore + __.InternalProvision() |> Async.Ignore /// /// Asynchronously verifies that the table exists and is compatible with record key schema, throwing if it is incompatible.
@@ -988,8 +977,8 @@ type TableContext<'TRecord> internal /// See also VerifyTableAsync, which only verifies the Table is present and correct. ///
/// Provisioned throughput to use for the table. - member __.InitializeTableAsync(throughput : ProvisionedThroughput) : Async = - __.CreateOrValidateTableAsync(Some throughput) |> Async.Ignore + member t.InitializeTableAsync(throughput : ProvisionedThroughput) : Async = + t.InternalCreateOrValidateTableAsync(throughput) |> Async.Ignore /// /// Asynchronously verifies that the table exists and is compatible with record key schema, throwing if it is incompatible.
@@ -998,12 +987,25 @@ type TableContext<'TRecord> internal ///
/// Provisioned throughput to use for the table. member __.ProvisionTableAsync(throughput : ProvisionedThroughput) : Async = async { - let! desc = __.CreateOrValidateTableAsync(Some throughput) - let provisioned = desc.ProvisionedThroughput + let! tableDescription = __.InternalCreateOrValidateTableAsync(throughput) + let provisioned = tableDescription.ProvisionedThroughput if throughput.ReadCapacityUnits <> provisioned.ReadCapacityUnits || throughput.WriteCapacityUnits <> provisioned.WriteCapacityUnits then do! __.UpdateProvisionedThroughputAsync(throughput) } + /// + /// Asynchronously verify that the table exists and is compatible with record key schema. + /// + /// Create the table instance now instance if it does not exist. Defaults to false. + /// Provisioned throughput for the table if newly created. Defaults to (10,10). + [] + member t.VerifyTableAsync(?createIfNotExists : bool, ?provisionedThroughput : ProvisionedThroughput) : Async = + if createIfNotExists = Some true then + let throughput = match provisionedThroughput with Some p -> p | None -> ProvisionedThroughput(10L, 10L) + t.InitializeTableAsync(throughput) + else + t.VerifyTableAsync() + // Deprecated factory method, to be removed. Replaced with // 1. TableContext<'T> ctor (synchronous) // 2. InitializeTableAsync OR VerifyTableAsync (explicitly async to signify that verification/creation is a costly and/or privileged operation)