public void GetColumnsReturnsProperCount() { AddTable(); Column[] cols = _provider.GetColumns("Test"); Assert.IsNotNull(cols); Assert.AreEqual(6, cols.Length); }
public string Dump() { StringWriter writer = new StringWriter(); writer.WriteLine("using Migrator;\n"); writer.WriteLine("[Migration(1)]"); writer.WriteLine("public class SchemaDump : Migration"); writer.WriteLine("{"); writer.WriteLine("\tpublic override void Up()"); writer.WriteLine("\t{"); foreach (string table in _provider.GetTables()) { writer.WriteLine("\t\tDatabase.AddTable(\"{0}\",", table); foreach (Column column in _provider.GetColumns(table)) { writer.WriteLine("\t\t\tnew Column(\"{0}\", typeof({1})),", column.Name, column.Type); } writer.WriteLine("\t\t);"); } writer.WriteLine("\t}\n"); writer.WriteLine("\tpublic override void Down()"); writer.WriteLine("\t{"); foreach (string table in _provider.GetTables()) { writer.WriteLine("\t\tDatabase.RemoveTable(\"{0}\");", table); } writer.WriteLine("\t}"); writer.WriteLine("}"); return(writer.ToString()); }
public string Dump() { StringWriter writer = new StringWriter(); writer.WriteLine("using Migrator;\n"); writer.WriteLine("[Migration(1)]"); writer.WriteLine("public class SchemaDump : Migration"); writer.WriteLine("{"); writer.WriteLine("\tpublic override void Up()"); writer.WriteLine("\t{"); foreach (string table in _provider.GetTables()) { writer.WriteLine("\t\tDatabase.AddTable(\"{0}\",", table); var columnLines = new List <string>(); foreach (Column column in _provider.GetColumns(table)) { columnLines.Add(string.Format("\t\t\tnew Column(\"{0}\", DbType.{1}, {2}, (ColumnProperty){3})", column.Name, column.Type, column.Size, (int)column.ColumnProperty)); } writer.WriteLine(string.Join(string.Format(",{0}", Environment.NewLine), columnLines.ToArray())); writer.WriteLine("\t\t);"); } writer.WriteLine("\t}\n"); writer.WriteLine("\tpublic override void Down()"); writer.WriteLine("\t{"); foreach (string table in _provider.GetTables()) { writer.WriteLine("\t\tDatabase.RemoveTable(\"{0}\");", table); } writer.WriteLine("\t}"); writer.WriteLine("}"); return(writer.ToString()); }
public static IEnumerable <Column> GetColumns(ITransformationProvider assertionDatabase, string tableName) { return((IEnumerable <Column>)Enumerable.ToList <Column>((IEnumerable <Column>)assertionDatabase.GetColumns(tableName))); }
public string Dump() { var writer = new StringWriter(); writer.WriteLine("using Migrator;\n"); writer.WriteLine("[Migration(1)]"); writer.WriteLine("public class SchemaDump : Migration"); writer.WriteLine("{"); writer.WriteLine("\tpublic override void Up()"); writer.WriteLine("\t{"); foreach (string table in _provider.GetTables()) { writer.WriteLine("\t\tDatabase.AddTable(\"{0}\",", table); var columnLines = new List <string>(); foreach (Column column in _provider.GetColumns(table)) { if (column.Size > 0 && column.DefaultValue != null) { columnLines.Add(string.Format("\t\t\tnew Column(\"{0}\", DbType.{1}, {2}, {3}, \"{4}\")", column.Name, column.Type, column.Size, getColumnPropertyString(column.ColumnProperty), column.DefaultValue)); } else if (column.Size > 0) { columnLines.Add(string.Format("\t\t\tnew Column(\"{0}\", DbType.{1}, {2}, {3})", column.Name, column.Type, column.Size, getColumnPropertyString(column.ColumnProperty))); } else if (column.DefaultValue != null) { columnLines.Add(string.Format("\t\t\tnew Column(\"{0}\", DbType.{1}, {2}, \"{3}\")", column.Name, column.Type, getColumnPropertyString(column.ColumnProperty), column.DefaultValue)); } else { columnLines.Add(string.Format("\t\t\tnew Column(\"{0}\", DbType.{1}, {2})", column.Name, column.Type, getColumnPropertyString(column.ColumnProperty))); } } foreach (var constraint in _provider.GetForeignKeyConstraints(table)) { columnLines.Add(string.Format("\t\t\tnew ForeignKeyConstraint(\"{0}\", \"{1}\", new[] {{\"{2}\"}}, \"{3}\", new[] {{\"{4}\"}})", constraint.Name, constraint.Table, string.Join("\",\"", constraint.Columns), constraint.PkTable, string.Join("\",\"", constraint.PkColumns))); } writer.WriteLine(string.Join(string.Format(",{0}", Environment.NewLine), columnLines.ToArray())); writer.WriteLine("\t\t);"); foreach (Index index in _provider.GetIndexes(table).Where(x => !x.PrimaryKey)) { if (index.IncludeColumns == null) { writer.WriteLine(string.Format("\t\tDatabase.AddIndex(\"{0}\", new Index() {{ Name = \"{1}\", Unique = {2}, Clustered = {3}, KeyColumns = new[] {{\"{4}\"}}, IncludeColumns = null }});", table, index.Name, index.Unique.ToString().ToLower(), index.Clustered.ToString().ToLower(), string.Join("\",\"", index.KeyColumns))); } else { writer.WriteLine(string.Format("\t\tDatabase.AddIndex(\"{0}\", new Index() {{ Name = \"{1}\", Unique = {2}, Clustered = {3}, KeyColumns = new[] {{\"{4}\"}}, IncludeColumns = new[] {{\"{5}\"}} }});", table, index.Name, index.Unique.ToString().ToLower(), index.Clustered.ToString().ToLower(), string.Join("\",\"", index.KeyColumns), string.Join("\",\"", index.IncludeColumns))); } } writer.WriteLine(""); } writer.WriteLine(""); writer.WriteLine(""); /*foreach (string table in _provider.GetTables()) * { * foreach (var constraint in _provider.GetForeignKeyConstraints(table)) * { * writer.WriteLine("\t\tDatabase.AddForeignKey(\"{0}\", \"{1}\", new[] {{\"{2}\"}}, \"{3}\", new[] {{\"{4}\"}});", constraint.Name, constraint.Table, string.Join("\",\"", constraint.Columns), constraint.PkTable, string.Join("\",\"", constraint.PkColumns)); * writer.WriteLine(""); * } * }*/ writer.WriteLine(""); writer.WriteLine(""); writer.WriteLine("\t}\n"); writer.WriteLine("\tpublic override void Down()"); writer.WriteLine("\t{"); foreach (string table in _provider.GetTables()) { writer.WriteLine("\t\tDatabase.RemoveTable(\"{0}\");", table); writer.WriteLine(""); } writer.WriteLine("\t}"); writer.WriteLine("}"); return(writer.ToString()); }
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); }