예제 #1
0
        private async Task CreateIfNotExist(CreateTableRequest request)
        {
            if (await TableExist())
            {
                return;
            }

            await DynamoDb.CreateTableAsync(request);
        }
예제 #2
0
        protected Task <CreateTableResponse> CreateTestTable(string tableName = null)
        {
            var postTableCreateRequest = new CreateTableRequest
            {
                AttributeDefinitions =
                    new List <AttributeDefinition>
                {
                    new AttributeDefinition {
                        AttributeName = nameof(MovieEntity.DirectorId), AttributeType = ScalarAttributeType.S
                    },
                    new AttributeDefinition {
                        AttributeName = nameof(MovieEntity.CreateDate), AttributeType = ScalarAttributeType.S
                    },
                    new AttributeDefinition()
                    {
                        AttributeName = nameof(MovieEntity.MovieId), AttributeType = ScalarAttributeType.S
                    }
                },
                TableName = tableName ?? TestTableName,
                KeySchema =
                    new List <KeySchemaElement>()
                {
                    new KeySchemaElement()
                    {
                        AttributeName = nameof(MovieEntity.DirectorId), KeyType = KeyType.HASH
                    },
                    new KeySchemaElement()
                    {
                        AttributeName = nameof(MovieEntity.CreateDate), KeyType = KeyType.RANGE
                    }
                },
                GlobalSecondaryIndexes = new List <GlobalSecondaryIndex>
                {
                    new GlobalSecondaryIndex
                    {
                        Projection = new Projection {
                            ProjectionType = ProjectionType.ALL
                        },
                        IndexName = TestConstants.MoiveTableMovieIdGsi,
                        KeySchema = new List <KeySchemaElement> {
                            new KeySchemaElement {
                                AttributeName = nameof(MovieEntity.MovieId), KeyType = KeyType.HASH
                            }
                        },
                        ProvisionedThroughput = new ProvisionedThroughput {
                            ReadCapacityUnits = 5, WriteCapacityUnits = 5
                        }
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput {
                    ReadCapacityUnits = 5, WriteCapacityUnits = 6
                },
            };

            return(DynamoDb.CreateTableAsync(postTableCreateRequest));
        }
        private Task CreateTableAsync(DynamoMetadataType table)
        {
            var request = ToCreateTableRequest(table);

            try
            {
                return(ExecAsync(() => DynamoDb.CreateTableAsync(request)));
            }
            catch (AmazonDynamoDBException ex)
            {
                if (ex.ErrorCode == DynamoErrors.AlreadyExists)
                {
                    return(PclExportClient.EmptyTask);
                }

                throw;
            }
        }
예제 #4
0
        private async Task CreateTableAsync(DynamoMetadataType table, CancellationToken token = default)
        {
            var request = ToCreateTableRequest(table);

            CreateTableFilter?.Invoke(request);
            try
            {
                await ExecAsync(async() =>
                                await DynamoDb.CreateTableAsync(request, token).ConfigAwait()).ConfigAwait();
            }
            catch (AmazonDynamoDBException ex)
            {
                if (ex.ErrorCode == DynamoErrors.AlreadyExists)
                {
                    return;
                }

                throw;
            }
        }
        /// <summary>
        /// Gets the DynamoDB table
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        private async Task <DynamoDbTable> TableWithName(string tableName)
        {
            var hashKeyName  = TableConfigProvider.GetTableHashKeyName(tableName);
            var rangeKeyName = TableConfigProvider.GetTableRangeKeyName(tableName);

            Logger.Debug("Checking if table '{0}' exists in DynamoDB...", tableName);

            try
            {
                if (!DynamoDbTable.TryLoadTable(DynamoDb, tableName, out var table))
                {
                    // if we failed to load the table, this is basically a retry that will throw an exception.
                    // The expectation is that this will expose whatever error caused the TryLoadTable method
                    // to return false, but if for some reason it happens to succed on retry, that also works.
                    if (!TableConfigProvider.CreateIfNotExists)
                    {
                        return(DynamoDbTable.LoadTable(DynamoDb, tableName));
                    }
                    //throw new Exception($"Table {tableName} does not exist in DynamoDB.");

                    Logger.Info("Table '{0}' does not exist in DynamoDB. Creating it now...", tableName);

                    var createResp =
                        await DynamoDb.CreateTableAsync(tableName,
                                                        new List <KeySchemaElement>
                    {
                        new KeySchemaElement
                        {
                            AttributeName = hashKeyName,
                            KeyType       = KeyType.HASH
                        },
                        new KeySchemaElement
                        {
                            AttributeName = rangeKeyName,
                            KeyType       = KeyType.RANGE
                        }
                    },
                                                        new List <AttributeDefinition>
                    {
                        new AttributeDefinition
                        {
                            AttributeName = hashKeyName,
                            AttributeType = ScalarAttributeType.S
                        },
                        new AttributeDefinition
                        {
                            AttributeName = rangeKeyName,
                            AttributeType = ScalarAttributeType.S
                        }
                    },
                                                        new ProvisionedThroughput(1, 1));

                    if (createResp.HttpStatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception($"Failed to create table '{tableName}' in DynamoDB. Response code is {createResp.HttpStatusCode}.");
                    }

                    Logger.Info("Successfully created DynamoDB table '{0}'.", tableName);

                    table = DynamoDbTable.LoadTable(DynamoDb, tableName);
                }

                return(table);
            }
            catch (Exception exception)
            {
                Logger.Error($"An error occurred loading the DynamoDB table for type {tableName}.", exception);
                throw;
            }
        }