Skip to content

Commit

Permalink
Provision -> UpdateTableIfRequired refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Apr 7, 2022
1 parent 5ae2300 commit 494b05a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 59 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ type Counter private (table : TableContext<CounterEntry>, key : TableKey) =
static member Create(client : IAmazonDynamoDB, tableName : string) = async {
let table = TableContext<CounterEntry>(client, tableName)
let throughput = ProvisionedThroughput(readCapacityUnits = 10L, writeCapacityUnits = 10L)
do! table.CreateTableIfNotExistsAsync(Throughput.Provisioned throughput)
do! table.VerifyOrCreateTableAsync(Throughput.Provisioned throughput)
let initialEntry = { Id = Guid.NewGuid() ; Value = 0L }
let! key = table.PutItemAsync(initialEntry)
return Counter(table, key)
Expand Down
12 changes: 6 additions & 6 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
* Added `TryGetItemAsync` (same as `GetItemAsync`, but returns `None`, instead of throwing, if an item is not present)
* Switched test framework to Xunit, assertions to Unquote, runner to `dotnet test`
* Clarified Creation/Verification APIs:
* Obsoleted `TableContext.Create` (replace with `TableContext.Scripting.Initialize`, `TableContext.CreateTableIfNotExistsAsync`, `TableContext.VerifyTableAsync`)
* Obsoleted `TableContext.Create` (replace with `TableContext.Scripting.Initialize`, `TableContext.VerifyOrCreateTableAsync`, `TableContext.VerifyTableAsync`)
* Added `TableContext` constructor (replaces `TableContext.Create(verifyTable = false)`)
* Added `TableContext.Scripting.Initialize` (replaces `TableContext.Create()`)
* Added `TableContext.VerifyTableAsync` overload that only performs verification but never creates a Table
* Added `TableContext.CreateTableIfNotExistsAsync` (replaces `TableContext.VerifyTableAsync(createIfNotExists = true)`)
* Added `TableContext.ProvisionTableAsync` (as per `CreateTableIfNotExistsAsync` but does an `UpdateTableAsync` if `throughput` or `streaming` has changed)
* Added Support for `Throughput.OnDemand` mode (sets `BillingMode` to `PAY_PER_REQUEST` rather than attempting to configure a `ProvisionedThroughput`)
* Added ability to configure DynamoDB streaming (via `Streaming` DU) to `CreateTableIfNotExistsAsync` and `ProvisionTableAsync`
* Removed `TableContext.CreateAsync` (replace with `TableContext.VerifyTableAsync` or `CreateTableIfNotExistsAsync`)
* Added `TableContext.VerifyOrCreateTableAsync` (replaces `TableContext.VerifyTableAsync(createIfNotExists = true)`)
* Added `TableContext.UpdateTableIfRequiredAsync` (conditional `UpdateTableAsync` to establish specified `throughput` or `streaming` only if required)
* Added `Throughput.OnDemand` mode (sets `BillingMode` to `PAY_PER_REQUEST` rather than attempting to configure a `ProvisionedThroughput`)
* Added ability to configure DynamoDB streaming (via `Streaming` DU) to `VerifyOrCreateTableAsync` and `UpdateTableIfRequiredAsync`
* Removed `TableContext.CreateAsync` (replace with `TableContext.VerifyTableAsync` or `VerifyOrCreateTableAsync`)
* Replaced `TableKeySchemata.CreateCreateTableRequest` with `ApplyToCreateTableRequest`

### 0.9.3-beta
Expand Down
19 changes: 12 additions & 7 deletions src/FSharp.AWS.DynamoDB/Script.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ type EasyCounters private (table : TableContext<CounterEntry>) =
// Create the table if necessary. Verifies schema is correct if it has already been created
// NOTE the hard coded initial throughput provisioning - arguably this belongs outside of your application logic
let throughput = ProvisionedThroughput(readCapacityUnits = 10L, writeCapacityUnits = 10L)
do! table.CreateTableIfNotExistsAsync(Throughput.Provisioned throughput)
do! table.VerifyOrCreateTableAsync(Throughput.Provisioned throughput)
return EasyCounters(table)
}

Expand All @@ -138,16 +138,21 @@ type EasyCounters private (table : TableContext<CounterEntry>) =
/// Variant of EasyCounters that splits the provisioning step from the (optional) validation that the table is present
type SimpleCounters private (table : TableContext<CounterEntry>) =

static member Provision(client : IAmazonDynamoDB, tableName : string, readCapacityUnits, writeCapacityUnits) : Async<unit> =
static member Provision(client : IAmazonDynamoDB, tableName : string, readCapacityUnits, writeCapacityUnits) : Async<unit> = async {
let table = TableContext<CounterEntry>(client, tableName)
// normally, RCU/WCU provisioning only happens first time the Table is created and is then considered an external concern
// here we use `ProvisionTableAsync` instead of `CreateTableIfNotExistsAsync` to reset it each time we deploy the app
let provisionedThroughput = ProvisionedThroughput(readCapacityUnits, writeCapacityUnits)
table.ProvisionTableAsync(Throughput.Provisioned provisionedThroughput)
let throughput = Throughput.Provisioned provisionedThroughput
// normally, RCU/WCU provisioning only happens first time the Table is created and is then considered an external concern
// here we use `UpdateTableIfRequiredAsync` to reset it each time we deploy the app
do! table.VerifyOrCreateTableAsync(throughput)
do! table.UpdateTableIfRequiredAsync(throughput) }

static member ProvisionOnDemand(client : IAmazonDynamoDB, tableName : string) : Async<unit> =
static member ProvisionOnDemand(client : IAmazonDynamoDB, tableName : string) : Async<unit> = async {
let table = TableContext<CounterEntry>(client, tableName)
table.ProvisionTableAsync(Throughput.OnDemand)
let throughput = Throughput.OnDemand
do! table.VerifyOrCreateTableAsync(throughput)
// as per the Provision, above, we reset to OnDemand, if it got reconfigured since it was originally created
do! table.UpdateTableIfRequiredAsync(throughput) }

/// We only want to do the initialization bit once per instance of our application
/// Similar to EasyCounters.Create in that it ensures the table is provisioned correctly
Expand Down
Loading

0 comments on commit 494b05a

Please sign in to comment.