Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Nov 30, 2024
1 parent e262254 commit 04de683
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
18 changes: 10 additions & 8 deletions database/schema/grammars/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,9 @@ func (r *Mysql) TypeDate(column schema.ColumnDefinition) string {

func (r *Mysql) TypeDateTime(column schema.ColumnDefinition) string {
current := "CURRENT_TIMESTAMP"
if column.GetPrecision() > 0 {
current = fmt.Sprintf("CURRENT_TIMESTAMP(%d)", column.GetPrecision())
precision := column.GetPrecision()
if precision > 0 {
current = fmt.Sprintf("CURRENT_TIMESTAMP(%d)", precision)
}
if column.GetUseCurrent() {
column.Default(Expression(current))
Expand All @@ -247,8 +248,8 @@ func (r *Mysql) TypeDateTime(column schema.ColumnDefinition) string {
column.OnUpdate(Expression(current))
}

if column.GetPrecision() > 0 {
return fmt.Sprintf("datetime(%d)", column.GetPrecision())
if precision > 0 {
return fmt.Sprintf("datetime(%d)", precision)
} else {
return "datetime"
}
Expand Down Expand Up @@ -334,8 +335,9 @@ func (r *Mysql) TypeTimeTz(column schema.ColumnDefinition) string {

func (r *Mysql) TypeTimestamp(column schema.ColumnDefinition) string {
current := "CURRENT_TIMESTAMP"
if column.GetPrecision() > 0 {
current = fmt.Sprintf("CURRENT_TIMESTAMP(%d)", column.GetPrecision())
precision := column.GetPrecision()
if precision > 0 {
current = fmt.Sprintf("CURRENT_TIMESTAMP(%d)", precision)
}
if column.GetUseCurrent() {
column.Default(Expression(current))
Expand All @@ -344,8 +346,8 @@ func (r *Mysql) TypeTimestamp(column schema.ColumnDefinition) string {
column.OnUpdate(Expression(current))
}

if column.GetPrecision() > 0 {
return fmt.Sprintf("timestamp(%d)", column.GetPrecision())
if precision > 0 {
return fmt.Sprintf("timestamp(%d)", precision)
} else {
return "timestamp"
}
Expand Down
32 changes: 32 additions & 0 deletions database/schema/grammars/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,22 @@ func (s *MysqlSuite) TestModifyOnUpdate() {
s.Empty(s.grammar.ModifyOnUpdate(mockBlueprint, mockColumn))
}

func (s *MysqlSuite) TestTypeDateTime() {
mockColumn := mocksschema.NewColumnDefinition(s.T())
mockColumn.EXPECT().GetPrecision().Return(3).Once()
mockColumn.EXPECT().GetUseCurrent().Return(true).Once()
mockColumn.EXPECT().Default(Expression("CURRENT_TIMESTAMP(3)")).Return(mockColumn).Once()
mockColumn.EXPECT().GetUseCurrentOnUpdate().Return(true).Once()
mockColumn.EXPECT().OnUpdate(Expression("CURRENT_TIMESTAMP(3)")).Return(mockColumn).Once()
s.Equal("datetime(3)", s.grammar.TypeDateTime(mockColumn))

mockColumn = mocksschema.NewColumnDefinition(s.T())
mockColumn.EXPECT().GetPrecision().Return(0).Once()
mockColumn.EXPECT().GetUseCurrent().Return(false).Once()
mockColumn.EXPECT().GetUseCurrentOnUpdate().Return(false).Once()
s.Equal("datetime", s.grammar.TypeDateTime(mockColumn))
}

func (s *MysqlSuite) TestTypeDecimal() {
mockColumn := mocksschema.NewColumnDefinition(s.T())
mockColumn.EXPECT().GetTotal().Return(4).Once()
Expand Down Expand Up @@ -356,3 +372,19 @@ func (s *MysqlSuite) TestTypeString() {

s.Equal("varchar(255)", s.grammar.TypeString(mockColumn2))
}

func (s *MysqlSuite) TestTypeTimestamp() {
mockColumn := mocksschema.NewColumnDefinition(s.T())
mockColumn.EXPECT().GetPrecision().Return(3).Once()
mockColumn.EXPECT().GetUseCurrent().Return(true).Once()
mockColumn.EXPECT().Default(Expression("CURRENT_TIMESTAMP(3)")).Return(mockColumn).Once()
mockColumn.EXPECT().GetUseCurrentOnUpdate().Return(true).Once()
mockColumn.EXPECT().OnUpdate(Expression("CURRENT_TIMESTAMP(3)")).Return(mockColumn).Once()
s.Equal("timestamp(3)", s.grammar.TypeTimestamp(mockColumn))

mockColumn = mocksschema.NewColumnDefinition(s.T())
mockColumn.EXPECT().GetPrecision().Return(0).Once()
mockColumn.EXPECT().GetUseCurrent().Return(false).Once()
mockColumn.EXPECT().GetUseCurrentOnUpdate().Return(false).Once()
s.Equal("timestamp", s.grammar.TypeTimestamp(mockColumn))
}
8 changes: 8 additions & 0 deletions database/schema/grammars/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,11 @@ func (s *PostgresSuite) TestTypeString() {

s.Equal("varchar", s.grammar.TypeString(mockColumn2))
}

func (s *PostgresSuite) TestTypeTimestamp() {
mockColumn := mocksschema.NewColumnDefinition(s.T())
mockColumn.EXPECT().GetUseCurrent().Return(true).Once()
mockColumn.EXPECT().Default(Expression("CURRENT_TIMESTAMP")).Return(mockColumn).Once()
mockColumn.EXPECT().GetPrecision().Return(3).Once()
s.Equal("timestamp(3) without time zone", s.grammar.TypeTimestamp(mockColumn))
}
4 changes: 2 additions & 2 deletions database/schema/grammars/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func getDefaultValue(def any) string {
return "'" + cast.ToString(cast.ToInt(value)) + "'"
case Expression:
return string(value)
default:
return "'" + cast.ToString(def) + "'"
}

return "'" + cast.ToString(def) + "'"
}

func getType(grammar schema.Grammar, column schema.ColumnDefinition) string {
Expand Down

0 comments on commit 04de683

Please sign in to comment.