protected override void Given() { var keys = new BsonDocument("x", 1); var createIndexOperation = new CreateIndexOperation(CollectionNamespace, keys, MessageEncoderSettings) { Unique = true }; ExecuteOperationAsync(createIndexOperation).GetAwaiter().GetResult(); _requests = new WriteRequest[] { new InsertRequest(new BsonDocument { { "_id", 1 }, { "x", 1 } }), new InsertRequest(new BsonDocument { { "_id", 2 }, { "x", 1 } }), // will fail new InsertRequest(new BsonDocument { { "_id", 3 }, { "x", 3 } }), new InsertRequest(new BsonDocument { { "_id", 4 }, { "x", 1 } }), // will fail new InsertRequest(new BsonDocument { { "_id", 5 }, { "x", 5 } }), }; _expectedDocuments = new BsonDocument[] { new BsonDocument { { "_id", 1 }, { "x", 1 } }, new BsonDocument { { "_id", 3 }, { "x", 3 } }, new BsonDocument { { "_id", 5 }, { "x", 5 } } }; }
public void TestCreateIndexOperationUnique() { var operations = new List <MigrationOperation>(); var operation = new CreateIndexOperation(); operation.Table = "someTable"; operation.Name = "someIndex"; operation.Columns.Add("column1"); operation.Columns.Add("column2"); operation.Columns.Add("column3"); operation.IsUnique = true; operations.Add(operation); var statments = new NpgsqlMigrationSqlGenerator().Generate(operations, BackendVersion.ToString()); Assert.AreEqual(1, statments.Count()); Assert.AreEqual("CREATE UNIQUE INDEX \"someTable_someIndex\" ON \"someTable\" (\"column1\",\"column2\",\"column3\")", statments.ElementAt(0).Sql); }
protected override void Generate( [NotNull] CreateIndexOperation operation, [CanBeNull] IModel model, [NotNull] MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); var method = (string)operation[NpgsqlAnnotationNames.IndexMethod]; builder.Append("CREATE "); if (operation.IsUnique) { builder.Append("UNIQUE "); } builder .Append("INDEX ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name)) .Append(" ON ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)); if (method != null) { builder .Append(" USING ") .Append(method); } builder .Append(" (") .Append(ColumnList(operation.Columns)) .Append(")"); if (!string.IsNullOrEmpty(operation.Filter)) { builder .Append(" WHERE ") .Append(operation.Filter); } builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); EndStatement(builder); }
public void CreateIndexOperation_method() { var op = new CreateIndexOperation { Name = "IX_People_Name", Table = "People", Schema = "dbo", Columns = new[] { "FirstName" } }; op.AddAnnotation(NpgsqlAnnotationNames.Prefix + NpgsqlAnnotationNames.IndexMethod, "gin"); Generate(op); Assert.Equal( "CREATE INDEX \"IX_People_Name\" ON \"dbo\".\"People\" USING gin (\"FirstName\");" + EOL, Sql); }
protected virtual IEnumerable <MigrationOperation> Add([NotNull] IIndex target) { var targetEntityTypeAnnotations = Annotations.For(target.DeclaringEntityType.RootType()); var operation = new CreateIndexOperation { Name = Annotations.For(target).Name, Schema = targetEntityTypeAnnotations.Schema, Table = targetEntityTypeAnnotations.TableName, Columns = GetColumns(target.Properties), IsUnique = target.IsUnique }; CopyAnnotations(MigrationsAnnotations.For(target), operation); yield return(operation); }
protected override void Generate([NotNull] CreateIndexOperation operation, [CanBeNull] IModel model, [NotNull] MigrationCommandListBuilder builder, bool terminate) { var method = (string)operation[MySqlAnnotationNames.Prefix]; var isFullText = !string.IsNullOrEmpty((string)operation[MySqlAnnotationNames.FullTextIndex]); var isSpatial = !string.IsNullOrEmpty((string)operation[MySqlAnnotationNames.SpatialIndex]); builder.Append("CREATE "); if (operation.IsUnique) { builder.Append("UNIQUE "); } else if (isFullText) { builder.Append("FULLTEXT "); } else if (isSpatial) { builder.Append("SPATIAL "); } builder .Append("INDEX ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name.LimitLength(64))) .Append(" ON ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)); if (method != null) { builder .Append(" USING ") .Append(method); } builder .Append(" (") .Append(ColumnList(operation.Columns)) .Append(")"); if (terminate) { builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); EndStatement(builder); } }
protected virtual IEnumerable <MigrationOperation> Add(IIndex target) { var targetExtensions = MetadataExtensions.Extensions(target); var targetEntityTypeExtensions = MetadataExtensions.Extensions(target.DeclaringEntityType.RootType()); var operation = new CreateIndexOperation { Name = targetExtensions.Name, Schema = targetEntityTypeExtensions.Schema, Table = targetEntityTypeExtensions.Table, Columns = GetColumnNames(target.Properties), IsUnique = target.IsUnique }; CopyAnnotations(Annotations.For(target), operation); yield return(operation); }
public TableBuilder <TColumns> Index(Expression <Func <TColumns, object> > indexExpression, bool unique = false, object anonymousArguments = null) { RuntimeFailureMethods.Requires(indexExpression != null, null, "indexExpression != null"); var createIndexOperation = new CreateIndexOperation(anonymousArguments) { Table = this._createTableOperation.Name, IsUnique = unique }; ( indexExpression.GetPropertyAccessList().Select(p => p.First().Name)).Each(delegate(string c) { createIndexOperation.Columns.Add(c); } ); this._migration.AddOperation(createIndexOperation); return(this); }
public override void Generate([NotNull] CreateIndexOperation createIndexOperation, [NotNull] IndentedStringBuilder stringBuilder) { Check.NotNull(createIndexOperation, "createIndexOperation"); Check.NotNull(stringBuilder, "stringBuilder"); stringBuilder .Append("CreateIndex(") .Append(GenerateLiteral(createIndexOperation.TableName)) .Append(", ") .Append(GenerateLiteral(createIndexOperation.IndexName)) .Append(", new[] { ") .Append(createIndexOperation.ColumnNames.Select(GenerateLiteral).Join()) .Append(" }, isUnique: ") .Append(GenerateLiteral(createIndexOperation.IsUnique)) .Append(", isClustered: ") .Append(GenerateLiteral(createIndexOperation.IsClustered)) .Append(")"); }
protected override void Given() { var keys = new BsonDocument("x", 1); var createIndexOperation = new CreateIndexOperation(DatabaseName, CollectionName, keys).WithUnique(true); ExecuteOperationAsync(createIndexOperation).GetAwaiter().GetResult(); _requests = new WriteRequest[] { new InsertRequest(new BsonDocument { { "_id", 1 }, { "x", 1 } }), new InsertRequest(new BsonDocument { { "_id", 2 }, { "x", 1 } }), // will fail new InsertRequest(new BsonDocument { { "_id", 3 }, { "x", 1 } }), }; _expectedDocuments = new BsonDocument[] { new BsonDocument { { "_id", 1 }, { "x", 1 } } }; }
public static OperationBuilderSurface <CreateIndexOperation> CreateIndex(this IMigrationBuilder builder, string name, string table, IEnumerable <IndexColumnDefinition> columns, IEnumerable <IndexColumnDefinition> includes = null, string schema = null, string catalog = null, string filter = null) { var op = new CreateIndexOperation { Name = new SubObjectName(catalog, schema, table, name), Columns = columns.ToList(), Includes = (includes ?? new List <IndexColumnDefinition>()).ToList(), Filter = filter, }; builder.AddOperation(op); return(new OperationBuilderSurface <CreateIndexOperation>(op)); }
protected override void Generate(CreateIndexOperation createIndexOperation) { if (createIndexOperation == null) { throw new ArgumentNullException("createIndexOperation"); } //if (createIndexOperation.Table.Contains("AspNetRoles") && createIndexOperation.Name.Contains("RoleNameIndex")) //{ // createIndexOperation.Name = "IX_Host_Name"; //} if (createIndexOperation.Table.Contains("AspNetUsers") && createIndexOperation.Name.Contains("UserNameIndex")) { createIndexOperation.Name = "IX_Host_Name"; } base.Generate(createIndexOperation); }
/// <summary> /// Creates a new <see cref="CreateIndexOperation" /> from the specified index. /// </summary> /// <param name="index">The index.</param> /// <returns>The operation.</returns> public static CreateIndexOperation CreateFrom(ITableIndex index) { Check.NotNull(index, nameof(index)); var operation = new CreateIndexOperation { IsUnique = index.IsUnique, Name = index.Name, Schema = index.Table.Schema, Table = index.Table.Name, Columns = index.Columns.Select(p => p.Name).ToArray(), Filter = index.Filter }; operation.AddAnnotations(index.GetAnnotations()); return(operation); }
protected virtual void Generate([NotNull] CreateIndexOperation operation, [NotNull] IndentedStringBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); builder.AppendLine(".CreateIndex("); using (builder.Indent()) { builder .Append("name: ") .Append(_code.Literal(operation.Name)); if (operation.Schema != null) { builder .AppendLine(",") .Append("schema: ") .Append(_code.Literal(operation.Schema)); } builder .AppendLine(",") .Append("table: ") .Append(_code.Literal(operation.Table)) .AppendLine(",") .Append( operation.Columns.Length == 1 ? "column: " : "columns: ") .Append(_code.Literal(operation.Columns)); if (operation.IsUnique) { builder .AppendLine(",") .Append("unique: true"); } builder.Append(")"); Annotations(operation.Annotations, builder); } }
public virtual OperationBuilder <CreateIndexOperation> CreateIndex(string name, string table, string[] columns, string schema = null, bool unique = false) { Check.NotEmpty(name, nameof(name)); Check.NotEmpty(table, nameof(table)); Check.NotEmpty(columns, nameof(columns)); var operation = new CreateIndexOperation { Schema = schema, Table = table, Name = name, Columns = columns, IsUnique = unique }; Operations.Add(operation); return(new OperationBuilder <CreateIndexOperation>(operation)); }
protected virtual IEnumerable <MigrationStatement> Generate(CreateIndexOperation operation) { using (var writer = SqlWriter()) { writer.Write("CREATE "); if (operation.IsUnique) { writer.Write("UNIQUE "); } writer.Write("INDEX "); writer.Write(Quote(string.Format("{0}_{1}", FixName(ObjectName(operation.Table)), FixName(operation.Name)))); writer.Write(" ON "); writer.Write(Quote(operation.Table)); writer.Write("("); WriteColumns(writer, operation.Columns.Select(Quote)); writer.Write(")"); yield return(Statement(writer)); } }
protected virtual IEnumerable <MigrationStatement> Generate(CreateIndexOperation operation) { using (var writer = SqlWriter()) { writer.Write("CREATE "); if (operation.IsUnique) { writer.Write("UNIQUE "); } writer.Write("INDEX "); writer.Write(Quote(CheckName(CreateItemName(BuildIndexName(operation))))); writer.Write(" ON "); writer.Write(Quote(CheckName(ExtractName(operation.Table)))); writer.Write("("); WriteColumns(writer, operation.Columns.Select(Quote)); writer.Write(")"); yield return(Statement(writer)); } }
protected override void Generate( CreateIndexOperation operation, IModel model, MigrationCommandListBuilder builder, bool terminate) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); builder.Append("CREATE "); if (operation.IsUnique) { builder.Append("UNIQUE "); } IndexTraits(operation, model, builder); builder .Append("INDEX ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(TruncateName(operation.Name))) .Append(" ON ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)) .Append(" (") .Append(ColumnList(operation.Columns)) .Append(")"); if (!string.IsNullOrEmpty(operation.Filter)) { builder .Append(" WHERE ") .Append(operation.Filter); } if (terminate) { builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); EndStatement(builder); } }
private string GenerateSqlStatementConcrete(CreateIndexOperation migrationOperation) { DB2DdlBuilder ddlBuilder = new DB2DdlBuilder(); ddlBuilder.AppendSql("CREATE "); if (migrationOperation.IsUnique) { ddlBuilder.AppendSql("UNIQUE "); } ddlBuilder.AppendSql("INDEX "); ddlBuilder.AppendIdentifier(migrationOperation.Name); ddlBuilder.AppendSql(" ON "); ddlBuilder.AppendIdentifier(migrationOperation.Table); ddlBuilder.AppendSql(" ("); ddlBuilder.AppendIdentifierList(migrationOperation.Columns); ddlBuilder.AppendSql(")"); return(ddlBuilder.GetCommandText()); }
/// <summary> /// Generates a migration operation to create a database index. /// </summary> /// <param name="op">The operation that represents creating a database index.</param> /// <returns>A migration operation to create a database index.</returns> protected virtual MigrationStatement Generate(CreateIndexOperation op) { StringBuilder sb = new StringBuilder(); sb = sb.Append("CREATE "); if (op.IsUnique) { sb.Append("UNIQUE "); } //index_col_name specification can end with ASC or DESC. // sort order are permitted for future extensions for specifying ascending or descending index value storage //Currently, they are parsed but ignored; index values are always stored in ascending order. object sort; op.AnonymousArguments.TryGetValue("Sort", out sort); var sortOrder = sort != null && sort.ToString() == "Ascending" ? "ASC" : "DESC"; sb.AppendFormat("index `{0}` on `{1}` (", op.Name, TrimSchemaPrefix(op.Table)); sb.Append(string.Join(",", op.Columns.Select(c => "`" + c + "` " + sortOrder)) + ") "); object indexTypeDefinition; op.AnonymousArguments.TryGetValue("Type", out indexTypeDefinition); var indexType = indexTypeDefinition != null && string.Compare(indexTypeDefinition.ToString(), "BTree", StringComparison.InvariantCultureIgnoreCase) > 0 ? "BTREE" : "HASH"; sb.Append("using " + indexType); // As of MySQL 8.0, ASC and DESC are not permitted for HASH indexes. if (Convert.ToDouble(_providerManifestToken) >= 8.0 && indexType == "HASH") { sb.Replace(sortOrder, string.Empty); } return(new MigrationStatement() { Sql = sb.ToString() }); }
public void Generate_can_output_create_index_statement_clustered() { var createTableOperation = new CreateTableOperation("Customers"); var idColumn = new ColumnModel(PrimitiveTypeKind.Int32) { Name = "Id", IsNullable = true, IsIdentity = true }; createTableOperation.Columns.Add(idColumn); createTableOperation.Columns.Add( new ColumnModel(PrimitiveTypeKind.String) { Name = "Name", IsNullable = false }); createTableOperation.PrimaryKey = new AddPrimaryKeyOperation(); createTableOperation.PrimaryKey.Columns.Add(idColumn.Name); var migrationSqlGenerator = new SqlServerMigrationSqlGenerator(); var createIndexOperation = new CreateIndexOperation { Table = createTableOperation.Name, IsUnique = true, IsClustered = true }; createIndexOperation.Columns.Add(idColumn.Name); var sql = migrationSqlGenerator.Generate( new[] { createIndexOperation }, "2008").Join(s => s.Sql, Environment.NewLine); Assert.Contains( @"CREATE UNIQUE CLUSTERED INDEX [IX_Id] ON [Customers]([Id])", sql); }
public void Generate_ignores_clustered_configuration_since_it_is_not_supported_on_CE() { var createTableOperation = new CreateTableOperation("Customers"); var idColumn = new ColumnModel(PrimitiveTypeKind.Int32) { Name = "Id", IsNullable = true, IsIdentity = true }; createTableOperation.Columns.Add(idColumn); createTableOperation.Columns.Add( new ColumnModel(PrimitiveTypeKind.String) { Name = "Name", IsNullable = false }); createTableOperation.PrimaryKey = new AddPrimaryKeyOperation(); createTableOperation.PrimaryKey.Columns.Add(idColumn.Name); var migrationSqlGenerator = new SqlCeMigrationSqlGenerator(); var createIndexOperation = new CreateIndexOperation { Table = createTableOperation.Name, IsUnique = true, IsClustered = true }; createIndexOperation.Columns.Add(idColumn.Name); var sql = migrationSqlGenerator.Generate( new[] { createIndexOperation }, "4.0").Join(s => s.Sql, Environment.NewLine); Assert.Contains( @"CREATE UNIQUE INDEX [IX_Id] ON [Customers]([Id])", sql); }
void main() { using (var db = new MySqliteEmptyDBContext()) { var generator = db.GetService <IMigrationsSqlGenerator>(); List <MigrationOperation> operations = new List <MigrationOperation>(); var item = new CreateIndexOperation(); item.Columns = new string[] { "index", "age" }; item.Table = "table"; item.Name = "IX_name_age"; item.IsUnique = true; //item.IsDestructiveChange = true; //item[Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal.SqlServerAnnotationNames.Clustered] = false; operations.Add(item); var commands = generator.Generate(operations); } Console.WriteLine("Hello World!"); }
/// <inheritdoc /> protected override void Generate(CreateIndexOperation operation, IModel model, MigrationCommandListBuilder builder) { if (!operation.IfNotExistsCheckRequired()) { base.Generate(operation, model, builder); return; } builder.AppendLine($"IF(IndexProperty(OBJECT_ID('{DelimitIdentifier(operation.Table, operation.Schema)}'), {GenerateSqlLiteral(operation.Name)}, 'IndexId') IS NULL)") .AppendLine("BEGIN"); using (builder.Indent()) { base.Generate(operation, model, builder, false); builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); } builder.AppendLine("END") .EndCommand(); }
public virtual TableBuilder <TColumns> Index( [NotNull] string name, [NotNull] Expression <Func <TColumns, object> > indexExpression, bool unique = false, bool clustered = false) { Check.NotEmpty(name, "name"); Check.NotNull(indexExpression, "indexExpression"); var table = _createTableOperation.Table; var columnNames = indexExpression.GetPropertyAccessList() .Select(p => _propertyInfoToColumnMap[p].Name) .ToArray(); var createIndexOperation = new CreateIndexOperation(table.Name, name, columnNames, unique, clustered); _migrationBuilder.AddOperation(createIndexOperation); return(this); }
public TableBuilder <TColumns> Index( Expression <Func <TColumns, object> > indexExpression, string name = null, bool unique = false, bool clustered = false, object anonymousArguments = null) { Check.NotNull <Expression <Func <TColumns, object> > >(indexExpression, nameof(indexExpression)); CreateIndexOperation createIndexOperation1 = new CreateIndexOperation(anonymousArguments); createIndexOperation1.Name = name; createIndexOperation1.Table = this._createTableOperation.Name; createIndexOperation1.IsUnique = unique; createIndexOperation1.IsClustered = clustered; CreateIndexOperation createIndexOperation = createIndexOperation1; indexExpression.GetSimplePropertyAccessList().Select <PropertyPath, ColumnModel>((Func <PropertyPath, ColumnModel>)(p => this._createTableOperation.Columns.Single <ColumnModel>((Func <ColumnModel, bool>)(c => c.ApiPropertyInfo == p.Single <PropertyInfo>())))).Each <ColumnModel>((Action <ColumnModel>)(c => createIndexOperation.Columns.Add(c.Name))); this._migration.AddOperation((MigrationOperation)createIndexOperation); return(this); }
protected override void Generate( [NotNull] CreateIndexOperation operation, [CanBeNull] IModel model, [NotNull] MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); builder.Append("CREATE "); if (operation.IsUnique) { builder.Append("UNIQUE "); } builder .Append("INDEX ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name)) .Append(" ON ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)); if (operation[NpgsqlAnnotationNames.IndexMethod] is string method && method.Length > 0) { builder .Append(" USING ") .Append(method); } var operators = operation[NpgsqlAnnotationNames.IndexOperators] as string[]; builder .Append(" (") .Append(IndexColumnList(operation.Columns, operators)) .Append(")"); IndexOptions(operation, model, builder); builder.AppendLine(';'); EndStatement(builder); }
public TableBuilder <TColumns> Index( Expression <Func <TColumns, object> > indexExpression, bool unique = false, object anonymousArguments = null) { Check.NotNull(indexExpression, "indexExpression"); var createIndexOperation = new CreateIndexOperation(anonymousArguments) { Table = _createTableOperation.Name, IsUnique = unique }; indexExpression .GetPropertyAccessList() .Select(p => p.Last().Name) .Each(c => createIndexOperation.Columns.Add(c)); _migration.AddOperation(createIndexOperation); return(this); }
/// <summary> /// 新建索引。 /// </summary> /// <param name="table">表格名称。</param> /// <param name="columns">字段列表。</param> /// <param name="unique">是否唯一。</param> /// <param name="clustered">是否聚合。</param> /// <returns>返回迁移实例。</returns> public virtual OperationBuilder <CreateIndexOperation> CreateIndex( string table, string[] columns, bool unique = false, bool clustered = false) { Check.NotNull(columns, nameof(columns)); var operation = new CreateIndexOperation { Table = table, Columns = columns, IsUnique = unique, IsClustered = clustered }; operation.Name = OperationHelper.GetName(NameType.Index, operation.Table, operation.Columns); Operations.Add(operation); return(new OperationBuilder <CreateIndexOperation>(operation)); }
private string GenerateSqlStatementConcrete(CreateIndexOperation migrationOperation) { var ddlBuilder = new SQLiteDdlBuilder(); ddlBuilder.AppendSql("CREATE "); if (migrationOperation.IsUnique) { ddlBuilder.AppendSql("UNIQUE "); } ddlBuilder.AppendSql("INDEX "); ddlBuilder.AppendIdentifier(SQLiteProviderManifestHelper.GetFullIdentifierName(migrationOperation.Table, migrationOperation.Name)); ddlBuilder.AppendSql(" ON "); ddlBuilder.AppendIdentifier(migrationOperation.Table); ddlBuilder.AppendSql(" ("); ddlBuilder.AppendIdentifierList(migrationOperation.Columns); ddlBuilder.AppendSql(")"); return(ddlBuilder.GetCommandText()); }
protected virtual void Generate( [NotNull] CreateIndexOperation operation, [CanBeNull] IModel model, [NotNull] MigrationCommandListBuilder builder, bool terminate) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); builder.Append("CREATE "); if (operation.IsUnique) { builder.Append("UNIQUE "); } IndexTraits(operation, model, builder); builder .Append("INDEX ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name)) .Append(" ON ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)) .Append(" (") .Append(ColumnList(operation.Columns)) .Append(")"); if (operation.Filter != null) { builder .Append(" WHERE ") .Append(operation.Filter); } if (terminate) { builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); EndStatement(builder); } }