Exemplo n.º 1
0
        public async Task InitializeCollectionAsync(string collection)
        {
            var documentTable = Configuration.TableNameConvention.GetDocumentTable(collection);

            using (var connection = Configuration.ConnectionFactory.CreateConnection())
            {
                await connection.OpenAsync();

                try
                {
                    var selectCommand = connection.CreateCommand();

                    var selectBuilder = Dialect.CreateBuilder(Configuration.TablePrefix);
                    selectBuilder.Select();
                    selectBuilder.AddSelector("*");
                    selectBuilder.Table(documentTable);
                    selectBuilder.Take("1");

                    selectCommand.CommandText = selectBuilder.ToSqlString();
                    Configuration.Logger.LogTrace(selectCommand.CommandText);

                    using (var result = await selectCommand.ExecuteReaderAsync())
                    {
                        if (result != null)
                        {
                            try
                            {
                                // Check if the Version column exists
                                result.GetOrdinal(nameof(Document.Version));
                            }
                            catch
                            {
                                result.Close();
                                using (var migrationTransaction = connection.BeginTransaction())
                                {
                                    var migrationBuilder = new SchemaBuilder(Configuration, migrationTransaction);

                                    try
                                    {
                                        migrationBuilder
                                        .AlterTable(documentTable, table => table
                                                    .AddColumn <long>(nameof(Document.Version), column => column.WithDefault(0))
                                                    );

                                        migrationTransaction.Commit();
                                    }
                                    catch
                                    {
                                        // Another thread must have altered it
                                    }
                                }
                            }
                            return;
                        }
                    }
                }
                catch
                {
                    using (var transaction = connection.BeginTransaction())
                    {
                        var builder = new SchemaBuilder(Configuration, transaction);

                        try
                        {
                            // The table doesn't exist, create it
                            builder
                            .CreateTable(documentTable, table => table
                                         .Column <int>(nameof(Document.Id), column => column.PrimaryKey().NotNull())
                                         .Column <string>(nameof(Document.Type), column => column.NotNull())
                                         .Column <string>(nameof(Document.Content), column => column.Unlimited())
                                         .Column <long>(nameof(Document.Version), column => column.WithDefault(0))
                                         )
                            .AlterTable(documentTable, table => table
                                        .CreateIndex("IX_" + documentTable + "_Type", "Type")
                                        );

                            transaction.Commit();
                        }
                        catch
                        {
                            // Another thread must have created it
                        }
                    }
                }
                finally
                {
                    await Configuration.IdGenerator.InitializeCollectionAsync(Configuration, collection);
                }
            }
        }