Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the default schema for migration data operations. #27143

Merged
merged 1 commit into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -303,7 +303,7 @@ public virtual void InsertDataOperation_throws_for_unsupported_column_types()
[ConditionalFact]
public void InsertDataOperation_throws_for_values_count_mismatch()
=> Assert.Equal(
RelationalStrings.InsertDataOperationValuesCountMismatch(1, 2, "People"),
RelationalStrings.InsertDataOperationValuesCountMismatch(1, 2, "dbo.People"),
Assert.Throws<InvalidOperationException>(
() =>
Generate(
Expand Down 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