Ejemplo n.º 1
0
        public IEnumerable <Action> Compare(IEnumerable <TableInfo> schemaTables, IEnumerable <ColumnInfo> schemaColumns)
        {
            List <Action> results = new List <Action>();

            if (CommandProvider.SupportsSchemas)
            {
                var createSchemas = GetNewSchemas(ModelTables, schemaTables);
                results.AddRange(createSchemas.Select(s => new CreateSchema(s)));
            }

            var createTables = GetNewTables(ModelTables, schemaTables);

            results.AddRange(createTables.Select(tbl => new CreateTable(tbl.ModelType)));

            // when looking for modified columns, don't include tables that were just created
            var existingModelColumns = ModelColumns.Where(col => !createTables.Contains(col.TableInfo));

            var schemaColumnsByTable = schemaColumns.ToLookup(row => row.TableInfo);
            var modelColumnsByTable  = existingModelColumns.ToLookup(row => row.TableInfo);
            var tablesToTypes        = ModelTypes.ToDictionary(t => CommandProvider.GetTableInfo(t));

            foreach (TableInfo tbl in schemaColumnsByTable)
            {
                if (AnyModifiedColumns(schemaColumnsByTable[tbl], modelColumnsByTable[tbl],
                                       out IEnumerable <ColumnInfo> added, out IEnumerable <ColumnInfo> modified, out IEnumerable <ColumnInfo> deleted))
                {
                    if (tbl.RowCount == 0)
                    {
                        // empty tables can be dropped and re-created
                        results.Add(new RebuildTable(tablesToTypes[tbl], added, modified, deleted));
                    }
                    else
                    {
                        // must modify columns directly
                        results.AddRange(added.Select(a => new AddColumn(a)));
                        results.AddRange(deleted.Select(d => new DropColumn(d)));
                        results.AddRange(modified.Select(m => new AlterColumn(m)));
                    }
                }
            }

            //var addColumns = await GetNewColumnsAsync(ModelTypes, createTables.Concat(rebuiltTables), connection);
            //results.AddRange(addColumns.Select(pi => new AddColumn(pi)));

            var dropTables = GetDeletedTables(ModelTables, schemaTables);

            results.AddRange(dropTables.Select(tbl => new DropTable(tbl)));

            var newForeignKeys = ModelColumns.Where(col => createTables.Contains(col.TableInfo) && col.IsForeignKey);

            results.AddRange(newForeignKeys.Select(col => new AddForeignKey(col.ForeignKeyInfo)));

            return(results);
        }
Ejemplo n.º 2
0
        protected virtual MySqlParameter[] GetStoreValues(IBaseModel model)
        {
            int length = ModelColumns.Count();

            MySqlParameter[] parameters = new MySqlParameter[length];
            int counter = 0;

            foreach (DbColumn column in ModelColumns)
            {
                parameters[counter++] = new MySqlParameter(column.Name, column.DbType)
                {
                    Value = column.Property.GetValue(model)
                };
            }
            return(parameters);
        }