Skip to content

Commit

Permalink
feat: [#579] Go Migrator Support Custom Column Types (#830)
Browse files Browse the repository at this point in the history
* feat: [#579] Go Migrator Support for Custom Column Types

* fix auth test
  • Loading branch information
almas1992 authored Jan 23, 2025
1 parent dfc4083 commit 149fb37
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 8 deletions.
10 changes: 6 additions & 4 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,12 @@ func (s *AuthTestSuite) TestParse_Success() {
s.mockCache.EXPECT().GetBool("jwt:disabled:"+token, false).Return(false).Once()

payload, err := s.auth.Parse(token)
now := carbon.Now()
s.Equal(&contractsauth.Payload{
Guard: testUserGuard,
Key: "1",
ExpireAt: jwt.NewNumericDate(carbon.Now().AddMinutes(2).StdTime()).Local(),
IssuedAt: jwt.NewNumericDate(carbon.Now().StdTime()).Local(),
ExpireAt: jwt.NewNumericDate(now.AddMinutes(2).StdTime()).Local(),
IssuedAt: jwt.NewNumericDate(now.StdTime()).Local(),
}, payload)
s.Nil(err)
}
Expand All @@ -296,11 +297,12 @@ func (s *AuthTestSuite) TestParse_SuccessWithPrefix() {
s.mockCache.EXPECT().GetBool("jwt:disabled:"+token, false).Return(false).Once()

payload, err := s.auth.Parse("Bearer " + token)
now := carbon.Now()
s.Equal(&contractsauth.Payload{
Guard: testUserGuard,
Key: "1",
ExpireAt: jwt.NewNumericDate(carbon.Now().AddMinutes(2).StdTime()).Local(),
IssuedAt: jwt.NewNumericDate(carbon.Now().StdTime()).Local(),
ExpireAt: jwt.NewNumericDate(now.AddMinutes(2).StdTime()).Local(),
IssuedAt: jwt.NewNumericDate(now.StdTime()).Local(),
}, payload)
s.Nil(err)

Expand Down
2 changes: 2 additions & 0 deletions contracts/database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Blueprint interface {
Build(query orm.Query, grammar Grammar) error
// Char Create a new char column on the table.
Char(column string, length ...int) ColumnDefinition
// Column Create a new custom type column on the table.
Column(column string, ttype string) ColumnDefinition
// Create Indicate that the table needs to be created.
Create()
// Date Create a new date column on the table.
Expand Down
4 changes: 4 additions & 0 deletions database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func (r *Blueprint) Char(column string, length ...int) schema.ColumnDefinition {
return columnImpl
}

func (r *Blueprint) Column(column, ttype string) schema.ColumnDefinition {
return r.createAndAddColumn(ttype, column)
}

func (r *Blueprint) Create() {
r.addCommand(&schema.Command{
Name: constants.CommandCreate,
Expand Down
2 changes: 1 addition & 1 deletion database/schema/grammars/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ func getType(grammar schema.Grammar, column schema.ColumnDefinition) string {
return callResult[0].String()
}

return ""
return column.GetType()
}
4 changes: 2 additions & 2 deletions database/schema/grammars/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func TestGetType(t *testing.T) {

// invalid type
mockColumn1 := mocksschema.NewColumnDefinition(t)
mockColumn1.EXPECT().GetType().Return("invalid").Once()
mockColumn1.EXPECT().GetType().Return("invalid").Twice()

mockGrammar1 := mocksschema.NewGrammar(t)

assert.Empty(t, getType(mockGrammar1, mockColumn1))
assert.Equal(t, "invalid", getType(mockGrammar1, mockColumn1))
}
41 changes: 40 additions & 1 deletion database/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ func (s *SchemaSuite) TestColumnTypes_Postgres() {
s.Equal("timestamp(2) without time zone", column.Type)
s.Equal("timestamp", column.TypeName)
}
if column.Name == "custom_type" {
s.False(column.Autoincrement)
s.Equal("This is a custom type column", column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("macaddr", column.Type)
s.Equal("macaddr", column.TypeName)
}
if column.Name == "date" {
s.False(column.Autoincrement)
s.Empty(column.Collation)
Expand Down Expand Up @@ -547,6 +555,14 @@ func (s *SchemaSuite) TestColumnTypes_Sqlite() {
s.Equal("datetime", column.Type)
s.Equal("datetime", column.TypeName)
}
if column.Name == "custom_type" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("geometry", column.Type)
s.Equal("geometry", column.TypeName)
}
if column.Name == "date" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
Expand Down Expand Up @@ -841,6 +857,14 @@ func (s *SchemaSuite) TestColumnTypes_Mysql() {
s.Equal("timestamp(2)", column.Type)
s.Equal("timestamp", column.TypeName)
}
if column.Name == "custom_type" {
s.False(column.Autoincrement)
s.Equal("This is a custom type column", column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("geometry", column.Type)
s.Equal("geometry", column.TypeName)
}
if column.Name == "date" {
s.False(column.Autoincrement)
s.Empty(column.Collation)
Expand Down Expand Up @@ -1164,6 +1188,14 @@ func (s *SchemaSuite) TestColumnTypes_Sqlserver() {
s.Equal("datetime2(22)", column.Type)
s.Equal("datetime2", column.TypeName)
}
if column.Name == "custom_type" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Empty(column.Default)
s.False(column.Nullable)
s.Equal("geometry", column.Type)
s.Equal("geometry", column.TypeName)
}
if column.Name == "date" {
s.False(column.Autoincrement)
s.Empty(column.Collation)
Expand Down Expand Up @@ -2362,6 +2394,11 @@ func (s *SchemaSuite) createTableAndAssertColumnsForColumnMethods(schema contrac
table.BigInteger("big_integer").Comment("This is a big_integer column")
table.Boolean("boolean_default").Default(true).Comment("This is a boolean column with default value")
table.Char("char").Comment("This is a char column")
if schema.GetConnection() != database.DriverPostgres.String() {
table.Column("custom_type", "geometry").Comment("This is a custom type column")
} else {
table.Column("custom_type", "macaddr").Comment("This is a custom type column")
}
table.Date("date").Comment("This is a date column")
table.DateTime("date_time", 3).Comment("This is a date time column")
table.DateTimeTz("date_time_tz", 3).Comment("This is a date time with time zone column")
Expand Down Expand Up @@ -2401,11 +2438,13 @@ func (s *SchemaSuite) createTableAndAssertColumnsForColumnMethods(schema contrac

columnListing := schema.GetColumnListing(table)

s.Equal(34, len(columnListing))
s.Equal(35, len(columnListing))
s.Contains(columnListing, "another_deleted_at")
s.Contains(columnListing, "big_integer")
s.Contains(columnListing, "boolean_default")
s.Contains(columnListing, "char")
s.Contains(columnListing, "created_at")
s.Contains(columnListing, "custom_type")
s.Contains(columnListing, "date")
s.Contains(columnListing, "date_time")
s.Contains(columnListing, "date_time_tz")
Expand Down
49 changes: 49 additions & 0 deletions mocks/database/schema/Blueprint.go

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

0 comments on commit 149fb37

Please sign in to comment.