Skip to content

Commit

Permalink
Summary of the changes
Browse files Browse the repository at this point in the history
        - Generated SQL >> CREATE SEQUENCE wrong when using decimal
        was generating (18, 2) which SQL returned an error
        - Added tests for int, long, short, byte, decimal

Fixes #26562
  • Loading branch information
bobbyangers committed Nov 8, 2021
1 parent 252ece7 commit 56d1a60
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ protected virtual void Generate(
{
builder
.Append(" AS ")
.Append(typeMapping.StoreType);
.Append(typeMapping.StoreTypeNameBase);

// set the typeMapping for use with operation.StartValue (i.e. a long) below
typeMapping = Dependencies.TypeMappingSource.GetMapping(typeof(long));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,8 @@ protected override void Generate(
{
var dropHistoryTableOperation = new DropTableOperation
{
Name = historyTableName, Schema = historyTableSchema,
Name = historyTableName,
Schema = historyTableSchema,
};

Generate(dropHistoryTableOperation, model, builder, terminate);
Expand Down Expand Up @@ -874,7 +875,7 @@ protected override void Generate(

builder
.Append(" AS ")
.Append(typeMapping.StoreType);
.Append(typeMapping.StoreTypeNameBase);
}

builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,144 @@ public void CreateIndexOperation_unique_online()
");
}

[ConditionalFact]
public void CreateSequenceOperation_long_no_cycle()
{
Generate(
new CreateSequenceOperation
{
Name = "CountByBigInt",
Schema = "dbo",
ClrType = typeof(long),
IncrementBy = 234,
StartValue = 22,
MinValue = 1,
MaxValue = 9876543,
IsCyclic = false
});

AssertSql(
@"CREATE SEQUENCE [dbo].[CountByBigInt] START WITH 22 INCREMENT BY 234 MINVALUE 1 MAXVALUE 9876543 NO CYCLE;
");
}

[ConditionalFact]
public void CreateSequenceOperation_long_is_cyclic()
{
Generate(
new CreateSequenceOperation
{
Name = "CountByBigInt",
Schema = "dbo",
ClrType = typeof(long),
IncrementBy = 234,
StartValue = 22,
IsCyclic = true
});

AssertSql(
@"CREATE SEQUENCE [dbo].[CountByBigInt] START WITH 22 INCREMENT BY 234 NO MINVALUE NO MAXVALUE CYCLE;
");
}

[ConditionalFact]
public void CreateSequenceOperation_int_is_not_cyclic()
{
Generate(
new CreateSequenceOperation
{
Name = "CountByInt",
Schema = "dbo",
ClrType = typeof(int),
IncrementBy = 1,
IsCyclic = false
});

AssertSql(
@"CREATE SEQUENCE [dbo].[CountByInt] AS int START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE NO CYCLE;
");
}

[ConditionalFact]
public void CreateSequenceOperation_short_is_not_cyclic()
{
Generate(
new CreateSequenceOperation
{
Name = "CountByShort",
Schema = "dbo",
ClrType = typeof(short),
IncrementBy = 456,
StartValue = 44,
IsCyclic = false
});

AssertSql(
@"CREATE SEQUENCE [dbo].[CountByShort] AS smallint START WITH 44 INCREMENT BY 456 NO MINVALUE NO MAXVALUE NO CYCLE;
");
}

[ConditionalFact]
public void CreateSequenceOperation_byte_is_not_cyclic()
{
Generate(
new CreateSequenceOperation
{
Name = "CountByTinyInt",
Schema = "dbo",
ClrType = typeof(byte),
IncrementBy = 3,
StartValue = 33,
IsCyclic = false
});

AssertSql(
@"CREATE SEQUENCE [dbo].[CountByTinyInt] AS tinyint START WITH 33 INCREMENT BY 3 NO MINVALUE NO MAXVALUE NO CYCLE;
");
}

[ConditionalFact]
public void CreateSequenceOperation_decimal_is_cyclic()
{
Generate(
new CreateSequenceOperation
{
Name = "CountByDecimal",
Schema = "dbo",
ClrType = typeof(decimal),
IncrementBy = 82,
StartValue = 593,
MinValue = 5,
MaxValue = 777777,
IsCyclic = false
});

AssertSql(
@"CREATE SEQUENCE [dbo].[CountByDecimal] AS decimal START WITH 593 INCREMENT BY 82 MINVALUE 5 MAXVALUE 777777 NO CYCLE;
");
}

[ConditionalFact]
public void CreateSequenceOperation_decimal_is_not_cyclic()
{
Generate(
new CreateSequenceOperation
{
Name = "CountByDecimal",
Schema = "dbo",
ClrType = typeof(decimal),
IncrementBy = 82,
StartValue = 593,
MinValue = 5,
MaxValue = 777777,
IsCyclic = false
});

AssertSql(
@"CREATE SEQUENCE [dbo].[CountByDecimal] AS decimal START WITH 593 INCREMENT BY 82 MINVALUE 5 MAXVALUE 777777 NO CYCLE;
");
}

[ConditionalFact]
public virtual void AddColumnOperation_identity_legacy()
{
Expand Down

0 comments on commit 56d1a60

Please sign in to comment.