public void AddDefaultTable() { _provider.AddTable("TestTwo", new Column("Id", DbType.Int32, ColumnProperty.PrimaryKey), new Column("TestId", DbType.Int32, ColumnProperty.ForeignKey) ); }
public static ITransformationProvider AddManyToManyJoiningTable(this ITransformationProvider database, string schema, string lhsTableName, string lhsKey, string rhsTableName, string rhsKey, string joiningTableName) { string joiningTableWithSchema = TransformationProviderUtility.FormatTableName(schema, joiningTableName); string joinLhsKey = Inflector.Singularize(lhsTableName) + "Id"; string joinRhsKey = Inflector.Singularize(rhsTableName) + "Id"; database.AddTable(joiningTableWithSchema, new Column(joinLhsKey, DbType.Guid, ColumnProperty.NotNull), new Column(joinRhsKey, DbType.Guid, ColumnProperty.NotNull)); string pkName = "PK_" + joiningTableName; pkName = ShortenKeyNameToBeSuitableForOracle(pkName); database.AddPrimaryKey(pkName, joiningTableWithSchema, joinLhsKey, joinRhsKey); string lhsTableNameWithSchema = TransformationProviderUtility.FormatTableName(schema, lhsTableName); string rhsTableNameWithSchema = TransformationProviderUtility.FormatTableName(schema, rhsTableName); string lhsFkName = TransformationProviderUtility.CreateForeignKeyName(lhsTableName, joiningTableName); database.AddForeignKey(lhsFkName, joiningTableWithSchema, joinLhsKey, lhsTableNameWithSchema, lhsKey, ForeignKeyConstraintType.NoAction); string rhsFkName = TransformationProviderUtility.CreateForeignKeyName(rhsTableName, joiningTableName); database.AddForeignKey(rhsFkName, joiningTableWithSchema, joinRhsKey, rhsTableNameWithSchema, rhsKey, ForeignKeyConstraintType.NoAction); return(database); }
public override void Execute(ITransformationProvider provider) { var columnsAccessors = Actions.OfType<ColumnAddAction>(); Actions = Actions.Except(columnsAccessors.OfType<IAction>()).ToList(); provider.AddTable(this.Name, columnsAccessors.Select(x => x.ToColumn()).ToArray()); base.Execute(provider); }
/// <summary> /// Расширяет таблицу SchemaInfo полем AssemblyKey. /// Использутеся при переходе от старой версии /// /// Должно быть удалено в будущем /// </summary> /// <param name="provider"></param> public static void Update1To3(ITransformationProvider provider) { provider.AddTable( "SchemaTmp", new Column("Version", DbType.Int64, ColumnProperty.PrimaryKey), new Column("AssemblyKey", DbType.String.WithSize(200), ColumnProperty.PrimaryKey, "''")); string sql = provider.FormatSql( "INSERT INTO {0:NAME} ({1:NAME}) SELECT {1:NAME} FROM {2:NAME}", "SchemaTmp", "Version", "SchemaInfo"); provider.ExecuteNonQuery(sql); provider.RemoveTable("SchemaInfo"); provider.RenameTable("SchemaTmp", "SchemaInfo"); }
/// <summary> /// Расширяет таблицу SchemaInfo полем AssemblyKey. /// Использутеся при переходе от старой версии /// /// Должно быть удалено в будущем /// </summary> /// <param name="provider"></param> public static void Update1To3(ITransformationProvider provider) { provider.AddTable( "SchemaTmp", new Column("Version", DbType.Int64, ColumnProperty.PrimaryKey), new Column("AssemblyKey", DbType.String.WithSize(200), ColumnProperty.PrimaryKey, "''")); string sql = provider.FormatSql( "INSERT INTO {0:NAME} ({1:NAME}) SELECT {1:NAME} FROM {2:NAME}", "SchemaTmp", "Version", "SchemaInfo"); provider.ExecuteNonQuery(sql); provider.RemoveTable("SchemaInfo"); provider.RenameTable("SchemaTmp", "SchemaInfo"); }
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 Create(ITransformationProvider provider) { provider.AddTable(_newTable); }
public void Create(ITransformationProvider provider) { provider.AddTable(_newTable); }
/// <summary> /// Adds new table with columns specified and executes all inner migration parts (column, keys, constraint manipulation). /// </summary> /// <param name="database">The database.</param> public override void Migrate(ITransformationProvider database) { database.AddTable(Name, newColumns.Select(x => x.GetColumn()).ToArray()); base.Migrate(database); }
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); }
/// <summary> /// Adds new table with columns specified and executes all inner migration parts (column, keys, constraint manipulation). /// </summary> /// <param name="database">The database.</param> public override void Migrate(ITransformationProvider database) { database.AddTable(Name, newColumns.Select(x => x.GetColumn()).ToArray()); base.Migrate(database); }