Skip to content

Commit

Permalink
Use the default schema for migration data operations.
Browse files Browse the repository at this point in the history
Fixes #27100
  • Loading branch information
AndriySvyryd committed Jan 7, 2022
1 parent 7e94966 commit b4b0c72
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 46 deletions.
60 changes: 42 additions & 18 deletions src/EFCore.Analyzers/Properties/AnalyzerStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -861,23 +861,23 @@ protected virtual IEnumerable<IReadOnlyModificationCommand> GenerateModification
{
throw new InvalidOperationException(
RelationalStrings.InsertDataOperationValuesCountMismatch(
operation.Values.GetLength(1), operation.Columns.Length, FormatTable(operation.Table, operation.Schema)));
operation.Values.GetLength(1), operation.Columns.Length, FormatTable(operation.Table, operation.Schema ?? model?.GetDefaultSchema())));
}

if (operation.ColumnTypes != null
&& operation.Columns.Length != operation.ColumnTypes.Length)
{
throw new InvalidOperationException(
RelationalStrings.InsertDataOperationTypesCountMismatch(
operation.ColumnTypes.Length, operation.Columns.Length, FormatTable(operation.Table, operation.Schema)));
operation.ColumnTypes.Length, operation.Columns.Length, FormatTable(operation.Table, operation.Schema ?? model?.GetDefaultSchema())));
}

if (operation.ColumnTypes == null
&& model == null)
{
throw new InvalidOperationException(
RelationalStrings.InsertDataOperationNoModel(
FormatTable(operation.Table, operation.Schema)));
FormatTable(operation.Table, operation.Schema ?? model?.GetDefaultSchema())));
}

var propertyMappings = operation.ColumnTypes == null
Expand All @@ -887,7 +887,7 @@ protected virtual IEnumerable<IReadOnlyModificationCommand> GenerateModification
for (var i = 0; i < operation.Values.GetLength(0); i++)
{
var modificationCommand = Dependencies.ModificationCommandFactory.CreateModificationCommand(
new ModificationCommandParameters(operation.Table, operation.Schema, SensitiveLoggingEnabled));
new ModificationCommandParameters(operation.Table, operation.Schema ?? model?.GetDefaultSchema(), SensitiveLoggingEnabled));
for (var j = 0; j < operation.Columns.Length; j++)
{
var name = operation.Columns[j];
Expand Down Expand Up @@ -1142,7 +1142,7 @@ private static IColumnMapping[] GetPropertyMappings(
string? schema,
IModel? model)
{
var table = model?.GetRelationalModel().FindTable(tableName, schema);
var table = model?.GetRelationalModel().FindTable(tableName, schema ?? model.GetDefaultSchema());
if (table == null)
{
throw new InvalidOperationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ protected override void Generate(
MigrationCommandListBuilder builder,
bool terminate = true)
{
GenerateIdentityInsert(builder, operation, on: true);
GenerateIdentityInsert(builder, operation, on: true, model);

var sqlBuilder = new StringBuilder();
((SqlServerUpdateSqlGenerator)Dependencies.UpdateSqlGenerator).AppendBulkInsertOperation(
Expand All @@ -1395,15 +1395,15 @@ protected override void Generate(
builder.Append(sqlBuilder.ToString());
}

GenerateIdentityInsert(builder, operation, on: false);
GenerateIdentityInsert(builder, operation, on: false, model);

if (terminate)
{
builder.EndCommand();
}
}

private void GenerateIdentityInsert(MigrationCommandListBuilder builder, InsertDataOperation operation, bool on)
private void GenerateIdentityInsert(MigrationCommandListBuilder builder, InsertDataOperation operation, bool on, IModel? model)
{
var stringTypeMapping = Dependencies.TypeMappingSource.GetMapping(typeof(string));

Expand All @@ -1414,14 +1414,14 @@ private void GenerateIdentityInsert(MigrationCommandListBuilder builder, InsertD
.Append(") AND [object_id] = OBJECT_ID(")
.Append(
stringTypeMapping.GenerateSqlLiteral(
Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)))
Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema ?? model?.GetDefaultSchema())))
.AppendLine("))");

using (builder.Indent())
{
builder
.Append("SET IDENTITY_INSERT ")
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema))
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema ?? model?.GetDefaultSchema()))
.Append(on ? " ON" : " OFF")
.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,15 @@ public void InsertDataOperation_throws_for_types_count_mismatch()
[ConditionalFact]
public void InsertDataOperation_throws_for_missing_entity_type()
=> Assert.Equal(
RelationalStrings.DataOperationNoTable("dbo.People"),
RelationalStrings.DataOperationNoTable("dbo1.People"),
Assert.Throws<InvalidOperationException>(
() =>
Generate(
CreateGotModel,
new InsertDataOperation
{
Table = "People",
Schema = "dbo",
Schema = "dbo1",
Columns = new[] { "First Name" },
Values = new object[,] { { "John" } }
})).Message);
Expand Down Expand Up @@ -695,7 +695,7 @@ public virtual void DefaultValue_with_line_breaks(bool isUnicode)
});

private static void CreateGotModel(ModelBuilder b)
=> b.Entity(
=> b.HasDefaultSchema("dbo").Entity(
"Person", pb =>
{
pb.ToTable("People");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -750,12 +750,12 @@ public override void InsertDataOperation_required_args()
base.InsertDataOperation_required_args();

AssertSql(
@"IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'First Name') AND [object_id] = OBJECT_ID(N'[People]'))
SET IDENTITY_INSERT [People] ON;
INSERT INTO [People] ([First Name])
@"IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'First Name') AND [object_id] = OBJECT_ID(N'[dbo].[People]'))
SET IDENTITY_INSERT [dbo].[People] ON;
INSERT INTO [dbo].[People] ([First Name])
VALUES (N'John');
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'First Name') AND [object_id] = OBJECT_ID(N'[People]'))
SET IDENTITY_INSERT [People] OFF;
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'First Name') AND [object_id] = OBJECT_ID(N'[dbo].[People]'))
SET IDENTITY_INSERT [dbo].[People] OFF;
");
}

Expand All @@ -764,12 +764,12 @@ public override void InsertDataOperation_required_args_composite()
base.InsertDataOperation_required_args_composite();

AssertSql(
@"IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'First Name', N'Last Name') AND [object_id] = OBJECT_ID(N'[People]'))
SET IDENTITY_INSERT [People] ON;
INSERT INTO [People] ([First Name], [Last Name])
@"IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'First Name', N'Last Name') AND [object_id] = OBJECT_ID(N'[dbo].[People]'))
SET IDENTITY_INSERT [dbo].[People] ON;
INSERT INTO [dbo].[People] ([First Name], [Last Name])
VALUES (N'John', N'Snow');
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'First Name', N'Last Name') AND [object_id] = OBJECT_ID(N'[People]'))
SET IDENTITY_INSERT [People] OFF;
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'First Name', N'Last Name') AND [object_id] = OBJECT_ID(N'[dbo].[People]'))
SET IDENTITY_INSERT [dbo].[People] OFF;
");
}

Expand All @@ -778,13 +778,13 @@ public override void InsertDataOperation_required_args_multiple_rows()
base.InsertDataOperation_required_args_multiple_rows();

AssertSql(
@"IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'First Name') AND [object_id] = OBJECT_ID(N'[People]'))
SET IDENTITY_INSERT [People] ON;
INSERT INTO [People] ([First Name])
@"IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'First Name') AND [object_id] = OBJECT_ID(N'[dbo].[People]'))
SET IDENTITY_INSERT [dbo].[People] ON;
INSERT INTO [dbo].[People] ([First Name])
VALUES (N'John'),
(N'Daenerys');
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'First Name') AND [object_id] = OBJECT_ID(N'[People]'))
SET IDENTITY_INSERT [People] OFF;
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'First Name') AND [object_id] = OBJECT_ID(N'[dbo].[People]'))
SET IDENTITY_INSERT [dbo].[People] OFF;
");
}

Expand Down

0 comments on commit b4b0c72

Please sign in to comment.