Esempio n. 1
0
        public static void CreateOrUpdateTable(this SqlConnection connection, object obj, Type type, string tableName, bool autoIncrementalTableMigrations, bool useCompression, bool useSingleTable)
        {
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }

            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (string.IsNullOrWhiteSpace(tableName))
            {
                throw new ArgumentException(nameof(tableName));
            }

            if (!connection.CheckIfTableExists(tableName))
            {
                connection.CreateTable(obj, type, tableName, useCompression, useSingleTable);

                var indexesColumnNames = GetBaseDomainEventProperties();
                if (indexesColumnNames != null && indexesColumnNames.Length > 0)
                {
                    foreach (var columnName in indexesColumnNames)
                    {
                        connection.CreateIndex(tableName, columnName);
                    }
                }
            }
            else if (!useSingleTable && autoIncrementalTableMigrations)
            {
                var tableSchema = connection.GetTableSchema(tableName);

                var properties = type.GetProperties().Where(property => tableSchema.All(t => !string.Equals(t.Item1, property.Name, StringComparison.OrdinalIgnoreCase))).ToArray();
                if (properties.Length > 0)
                {
                    foreach (var property in properties)
                    {
                        connection.AlterTableAddColumn(property, obj, tableName, useCompression);
                    }
                }
            }
        }