public void Execute(ITransformationProvider database)
        {
            var columnDefinitions = new List <Column>();

            foreach (var builder in _columnBuilders)
            {
                columnDefinitions.AddRange(builder());
            }

            if (_createTable)
            {
                database.AddTable("[dbo]." + _tableName, columnDefinitions.ToArray());
            }
            else
            {
                string queryTableName = "[dbo]." + _tableName;

                BeforeColumnsAreAddedOrChanged(queryTableName, database);

                foreach (var column in columnDefinitions)
                {
                    if (database.ColumnExists(queryTableName, column.Name))
                    {
                        database.ChangeColumn(queryTableName, column);
                    }
                    else
                    {
                        database.AddColumn(queryTableName, column);
                    }
                }
            }

            foreach (var fk in _foreignKeys)
            {
                fk(database);
            }
        }
 public void ChangeColumn()
 {
     _provider.ChangeColumn("TestTwo", new Column("TestId", DbType.String, 50));
     Assert.IsTrue(_provider.ColumnExists("TestTwo", "TestId"));
     _provider.Insert("TestTwo", new[] { "Id", "TestId" }, new object[] { 1, "Not an Int val." });
 }
예제 #3
0
 /// <summary>
 /// Changes existing column.
 /// </summary>
 /// <param name="database">The database.</param>
 public override void Migrate(ITransformationProvider database)
 {
     var column = GetColumn();
     
     database.ChangeColumn(TableName, column.Name, column.ColumnType, false);
 }
예제 #4
0
 public override void Execute(ITransformationProvider provider)
 {
     provider.ChangeColumn(Table.Name, this.ToColumn());
 }
예제 #5
0
        /// <summary>
        /// Changes existing column.
        /// </summary>
        /// <param name="database">The database.</param>
        public override void Migrate(ITransformationProvider database)
        {
            var column = GetColumn();

            database.ChangeColumn(TableName, column.Name, column.ColumnType, false);
        }
예제 #6
0
        public void AutoMigrateEntity(IDatabase db, IDbMigratorService svr)
        {
            _provider.Logger.Log("开始AutoMigrate...");
            try
            {
                //开启事务
                _provider.BeginTransaction();


                var classmaps = db.GetAllMap().Where(p => p.Value != null && p.Value.IgnoredMigrate == false);
                var maps      = classmaps.Select(p => p.Value);

                //确认 表是否存在,不存在就add table
                var dbtables = _provider.GetTables();

                string         colName = "";
                DbType         colDbType = DbType.String;
                int            colSize = 0;
                ColumnProperty colPropertyType = ColumnProperty.None;
                object         colDefaultValue = null;
                string         tableComment = "", colComment = "";
                string         containTableStr  = db.Config.AutoMigrateOnContainTable;
                string[]       containTables    = containTableStr.ToLower().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                bool           hasContainTables = false;
                if (containTables.Length > 0)
                {
                    hasContainTables = true;
                }

                string   withoutTableStr  = db.Config.AutoMigrateWithoutTable;
                string[] withoutTables    = withoutTableStr.ToLower().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                bool     hasWithoutTables = false;
                if (withoutTables.Length > 0)
                {
                    hasWithoutTables = true;
                }

                foreach (var map in maps)
                {
                    var tb = map.TableName;
                    if (hasContainTables)
                    {
                        if (!containTables.Contains(tb.ToLower()))
                        {
                            continue;
                        }
                    }

                    if (hasWithoutTables)
                    {
                        if (withoutTables.Contains(tb.ToLower()))
                        {
                            continue;
                        }
                    }

                    var oneMap     = classmaps.FirstOrDefault(p => p.Value.TableName == tb);
                    var properties = oneMap.Value != null ? oneMap.Value.Properties : null;
                    if (properties != null)
                    {
                        properties = properties.Where(p => p.Ignored == false).ToList();
                    }
                    IPropertyMap identiProperty = null;
                    if (properties != null)
                    {
                        //只支持一个自增列
                        identiProperty = properties.FirstOrDefault(p => p.KeyType == KeyType.Identity || p.KeyType == KeyType.TriggerIdentity);
                    }

                    //存在表
                    if (dbtables.Any(p => p.Equals(tb, StringComparison.InvariantCultureIgnoreCase)))
                    {
                        if (db.Config.CanUpdatedWhenTableExisted == true)
                        {
                            var dbColumns = _provider.GetColumns(tb);

                            if (db.Config.AutoRemoveUnuseColumnInTable == true)//自动清空不在属性列表中的列
                            {
                                var propertyNames = properties.Select(p => p.ColumnName.ToUpper()).ToList();

                                var noUseColumns = dbColumns.Where(p => !propertyNames.Contains(p.Name.ToUpper())).ToList();
                                foreach (var noUseColumn in noUseColumns)
                                {
                                    _provider.RemoveColumn(tb, noUseColumn.Name);
                                }
                            }

                            foreach (var property in properties)
                            {
                                colName = property.ColumnName;
                                if (property.Ignored == true) //标记忽略的跳过
                                {
                                    continue;
                                }
                                //存在指定的列
                                if (dbColumns.Any(p => p.Name.Equals(colName, StringComparison.InvariantCultureIgnoreCase)))
                                {
                                    //oracle 中的blob和clob无法修改
                                    if (_provider.MigratorDbType == MigratorDbType.Oracle || _provider.MigratorDbType == MigratorDbType.OracleManaged)
                                    {
                                        if (property.LobType != LobType.None)
                                        {
                                            continue;
                                        }
                                    }

                                    colPropertyType = GetColumnProperty(property);
                                    colDbType       = svr.GetDBType(property.PropertyInfo.PropertyType);

                                    // 修改列
                                    Column dbCol = dbColumns.FirstOrDefault(p => p.Name.Equals(colName, StringComparison.InvariantCultureIgnoreCase)); //  _provider.GetColumnByName(tb, colName);
                                    if (dbCol != null && property.KeyType == KeyType.NotAKey && dbCol.IsPrimaryKey == false &&
                                        dbCol.IsIdentity == false && dbCol.IsPrimaryKeyWithIdentity == false &&
                                        (dbCol.Size != property.ColumnSize
                                         //|| dbCol.Type !=  colDbType
                                         //|| dbCol.ColumnProperty != colPropertyType
                                        )
                                        ) //只能修改非主键
                                    {
                                        dbCol.Size = property.ColumnSize;
                                        //dbCol.DefaultValue = property.ColumnDefaultValue;

                                        dbCol.ColumnProperty = colPropertyType;
                                        dbCol.Type           = colDbType;
                                        _provider.ChangeColumn(tb, dbCol);
                                    }
                                }
                                else//不存在列
                                {
                                    colDbType       = svr.GetDBType(property.PropertyInfo.PropertyType);
                                    colSize         = property.ColumnSize;
                                    colDefaultValue = property.ColumnDefaultValue;
                                    colPropertyType = GetColumnProperty(property);

                                    //增加列到数据库
                                    _provider.AddColumn(tb, colName, colDbType, colSize, colPropertyType, colDefaultValue);
                                }
                            }



                            ///////////////////////////////////////////
                            //如果是oracle则增加自增序列
                            if (_provider.MigratorDbType == MigratorDbType.Oracle || _provider.MigratorDbType == MigratorDbType.OracleManaged)
                            {
                                AddSequence(tb, map.SequenceName, identiProperty);
                            }

                            //增加表注释
                            tableComment = map.TableDescription;
                            if (!string.IsNullOrEmpty(tableComment))
                            {
                                _provider.AddTableDescription(tb, tableComment);
                            }

                            //增加列注释
                            foreach (var property in properties)
                            {
                                colComment = property.ColumnDescription;
                                if (!string.IsNullOrEmpty(colComment))
                                {
                                    colName = property.ColumnName;
                                    _provider.AddColumnDescription(tb, colName, colComment);
                                }
                            }
                        }
                    }
                    else//不存在表
                    {
                        List <Column> dbColumns = new List <Column>();
                        //组合列
                        foreach (var property in properties)
                        {
                            colName         = property.ColumnName;
                            colDbType       = svr.GetDBType(property.PropertyInfo.PropertyType);
                            colSize         = property.ColumnSize;
                            colDefaultValue = property.ColumnDefaultValue;

                            colPropertyType = GetColumnProperty(property);
                            Column dbcol = new Column(colName, colDbType, colSize, colPropertyType, colDefaultValue);
                            dbColumns.Add(dbcol);
                        }
                        //增加表到数据库
                        _provider.AddTable(tb, dbColumns.ToArray());



                        ///////////////////////////////////////////
                        //如果是oracle则增加自增序列
                        if (_provider.MigratorDbType == MigratorDbType.Oracle || _provider.MigratorDbType == MigratorDbType.OracleManaged)
                        {
                            AddSequence(tb, map.SequenceName, identiProperty);
                        }

                        //增加表注释
                        tableComment = map.TableDescription;
                        if (!string.IsNullOrEmpty(tableComment))
                        {
                            _provider.AddTableDescription(tb, tableComment);
                        }

                        //增加列注释
                        foreach (var property in properties)
                        {
                            colComment = property.ColumnDescription;
                            if (!string.IsNullOrEmpty(colComment))
                            {
                                colName = property.ColumnName;
                                _provider.AddColumnDescription(tb, colName, colComment);
                            }
                        }
                    }
                }

                //提交
                _provider.Commit();
            }
            catch (Exception ex)
            {
                _provider.Logger.Log("AutoMigrate出错:" + ex);
                //回滚
                _provider.Rollback();
                throw new PureDataException("AutoMigrateHelper", ex);
            }
            finally
            {
                db.Close();
            }
            _provider.Logger.Log("AutoMigrate完成 - " + DateTime.Now);
        }