/// <summary> /// Asynchronously Creates a table with specified capacities, hash and range keys and indexes. /// If it doesn't exist yet. /// </summary> public static async Task CreateTableIfNotExistsAsync <TEntity>(this DataContext context, CreateTableArgs <TEntity> args) { var entityType = typeof(TEntity); string tableName = context.GetTableNameForType(entityType); // checking if the table exists if ( await Task.Run(() => { Table t; return(Table.TryLoadTable(context.Client, tableName, out t)); }) ) { return; } context.Log("Table {0} doesn't exist - about to create it...", tableName); // creating the table asyncronously await context.Client.CreateTableAsync(args.GetCreateTableRequest(tableName)); context.Log("Waiting for the table {0} to be created...", tableName); // waiting till the table is created await TillTableIsCreatedAsync(context.Client, tableName); context.Log("Table {0} created successfully!", tableName); // now adding initial entities if (args.GetInitialEntitiesFunc != null) { context.Log("About to fill table {0} with initial entities...", tableName); Exception initialFillException = null; try { var table = context.GetTable <TEntity>(); foreach (var entity in args.GetInitialEntitiesFunc()) { table.InsertOnSubmit(entity); } await context.SubmitChangesAsync(); context.Log("Table {0} successfully filled with initial entities.", tableName); } catch (Exception ex) { initialFillException = ex; } // if we failed to add initial data, then removing the table if (initialFillException != null) { context.Log("An error occured while filling table {0} with initial entities. So, removing the table...", tableName); await DeleteTableAsync <TEntity>(context); context.Log("Table {0} removed.", tableName); throw initialFillException; } } }
/// <summary> /// Creates a table with specified capacities, hash and range keys and indexes. /// If it doesn't exist yet. /// </summary> public static void CreateTableIfNotExists <TEntity>(this DataContext context, CreateTableArgs <TEntity> args) { GeneralUtils.SafelyRunSynchronously(context.CreateTableIfNotExistsAsync, args); }