Skip to content

Commit

Permalink
Fixes #1304
Browse files Browse the repository at this point in the history
  • Loading branch information
jzabroski committed Jul 22, 2020
1 parent 2446f2f commit e6bb09a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ protected override string FormatIdentity(ColumnDefinition column)

protected override string FormatType(ColumnDefinition column)
{
FormatTypeValidator(column);

//rather than base.FormatType, which will use serials for identities, we go instead to ColumnBase.FormatType, exposed via base.ColumnBaseFormatType
return base.ColumnBaseFormatType(column);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,27 @@ public override string AddPrimaryKeyConstraint(string tableName, IEnumerable<Col
return string.Format(", {0}PRIMARY KEY ({1})", pkName, cols);
}

protected void FormatTypeValidator(ColumnDefinition column)
{
if (column.Type == DbType.DateTimeOffset && (column.Precision < 0 || column.Precision > 6))
{
throw new ArgumentOutOfRangeException($"Postgres {nameof(DbType.DateTimeOffset)} data type 'timestamp[(p)]' with time zone' supports allowed range from 0 to 6. " +
$"See: https://www.postgresql.org/docs/12/datatype-datetime.html");
}
}

/// <inheritdoc />
protected override string FormatType(ColumnDefinition column)
{
FormatTypeValidator(column);

if (column.IsIdentity)
{
if (column.Type == DbType.Int64)
return "bigserial";
return "serial";
}


return ColumnBaseFormatType(column);
}
Expand Down
14 changes: 14 additions & 0 deletions test/FluentMigrator.Tests/Unit/Generators/GeneratorTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,20 @@ public static AlterColumnExpression GetAlterColumnExpression()
return expression;
}

public static CreateColumnExpression GetCreateColumnExpressionWithDateTimeOffsetType()
{
var expression = new CreateColumnExpression();
expression.TableName = TestTableName1;

expression.Column = new ColumnDefinition();
expression.Column.Name = TestColumnName1;
expression.Column.IsNullable = true;
expression.Column.Type = DbType.DateTimeOffset;
expression.Column.ModificationType = ColumnModificationType.Create;

return expression;
}

public static CreateColumnExpression GetCreateColumnExpressionWithNullableCustomType()
{
var expression = new CreateColumnExpression();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ public override void CanRenameColumnWithDefaultSchema()
result.ShouldBe("ALTER TABLE \"public\".\"TestTable1\" RENAME COLUMN \"TestColumn1\" TO \"TestColumn2\";");
}

[Test]
public void CannotCreateColumnWithDateTimeOffsetPrecisionOutOfRange()
{
var expression = GeneratorTestHelper.GetCreateColumnExpressionWithDateTimeOffsetType();
expression.Column.Precision = 7;

Should.Throw(() => Generator.Generate(expression), typeof(ArgumentOutOfRangeException));
}

public abstract void CanCreateTableWithIdentityWithCustomSchema();
public abstract void CanCreateTableWithIdentityWithDefaultSchema();
public abstract void CanCreateColumnWithAutoIncrementAndCustomSchema();
Expand Down

0 comments on commit e6bb09a

Please sign in to comment.