Skip to content

Commit

Permalink
feat: [#578] Add bool type for migration
Browse files Browse the repository at this point in the history
  • Loading branch information
almas1992 committed Jan 22, 2025
1 parent 245f598 commit d0e46b9
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 2 deletions.
2 changes: 2 additions & 0 deletions contracts/database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type Blueprint interface {
BigIncrements(column string) ColumnDefinition
// BigInteger Create a new big integer (8-byte) column on the table.
BigInteger(column string) ColumnDefinition
// Boolean Create a new boolean column on the table.
Boolean(column string) ColumnDefinition
// Build Execute the blueprint to build / modify the table.
Build(query orm.Query, grammar Grammar) error
// Char Create a new char 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 @@ -34,6 +34,10 @@ func (r *Blueprint) BigInteger(column string) schema.ColumnDefinition {
return r.createAndAddColumn("bigInteger", column)
}

func (r *Blueprint) Boolean(column string) schema.ColumnDefinition {
return r.createAndAddColumn("boolean", column)
}

func (r *Blueprint) Build(query ormcontract.Query, grammar schema.Grammar) error {
for _, sql := range r.ToSql(grammar) {
if _, err := query.Exec(sql); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions database/schema/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ func (s *BlueprintTestSuite) TestBigInteger() {
})
}

func (s *BlueprintTestSuite) TestBoolean() {
name := "name"
s.blueprint.Boolean(name)
s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{
name: &name,
ttype: convert.Pointer("boolean"),
})
}

func (s *BlueprintTestSuite) TestBuild() {
for _, grammar := range s.grammars {
mockQuery := mocksorm.NewQuery(s.T())
Expand Down
4 changes: 4 additions & 0 deletions database/schema/grammars/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ func (r *Mysql) TypeBigInteger(_ schema.ColumnDefinition) string {
return "bigint"
}

func (r *Mysql) TypeBoolean(_ schema.ColumnDefinition) string {
return "tinyint(1)"
}

func (r *Mysql) TypeChar(column schema.ColumnDefinition) string {
return fmt.Sprintf("char(%d)", column.GetLength())
}
Expand Down
6 changes: 6 additions & 0 deletions database/schema/grammars/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,12 @@ func (s *MysqlSuite) TestModifyOnUpdate() {
s.Empty(s.grammar.ModifyOnUpdate(mockBlueprint, mockColumn))
}

func (s *MysqlSuite) TestTypeBoolean() {
mockColumn := mocksschema.NewColumnDefinition(s.T())

s.Equal("tinyint(1)", s.grammar.TypeBoolean(mockColumn))
}

func (s *MysqlSuite) TestTypeDateTime() {
mockColumn := mocksschema.NewColumnDefinition(s.T())
mockColumn.EXPECT().GetPrecision().Return(3).Once()
Expand Down
4 changes: 4 additions & 0 deletions database/schema/grammars/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ func (r *Postgres) TypeBigInteger(column schema.ColumnDefinition) string {
return "bigint"
}

func (r *Postgres) TypeBoolean(_ schema.ColumnDefinition) string {
return "boolean"
}

func (r *Postgres) TypeChar(column schema.ColumnDefinition) string {
length := column.GetLength()
if length > 0 {
Expand Down
6 changes: 6 additions & 0 deletions database/schema/grammars/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,12 @@ func (s *PostgresSuite) TestTypeBigInteger() {
s.Equal("bigint", s.grammar.TypeBigInteger(mockColumn2))
}

func (s *PostgresSuite) TestTypeBoolean() {
mockColumn := mocksschema.NewColumnDefinition(s.T())

s.Equal("boolean", s.grammar.TypeBoolean(mockColumn))
}

func (s *PostgresSuite) TestTypeDecimal() {
mockColumn := mocksschema.NewColumnDefinition(s.T())
mockColumn.EXPECT().GetTotal().Return(4).Once()
Expand Down
4 changes: 4 additions & 0 deletions database/schema/grammars/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ func (r *Sqlite) TypeBigInteger(column schema.ColumnDefinition) string {
return "integer"
}

func (r *Sqlite) TypeBoolean(_ schema.ColumnDefinition) string {
return "tinyint(1)"
}

func (r *Sqlite) TypeChar(column schema.ColumnDefinition) string {
return "varchar"
}
Expand Down
6 changes: 6 additions & 0 deletions database/schema/grammars/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ func (s *SqliteSuite) TestModifyIncrement() {
s.Equal(" primary key autoincrement", s.grammar.ModifyIncrement(mockBlueprint, mockColumn))
}

func (s *SqliteSuite) TestTypeBoolean() {
mockColumn := mocksschema.NewColumnDefinition(s.T())

s.Equal("tinyint(1)", s.grammar.TypeBoolean(mockColumn))
}

func (s *SqliteSuite) TestTypeEnum() {
mockColumn := mocksschema.NewColumnDefinition(s.T())
mockColumn.EXPECT().GetName().Return("a").Once()
Expand Down
4 changes: 4 additions & 0 deletions database/schema/grammars/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ func (r *Sqlserver) TypeBigInteger(_ schema.ColumnDefinition) string {
return "bigint"
}

func (r *Sqlserver) TypeBoolean(_ schema.ColumnDefinition) string {
return "bit"
}

func (r *Sqlserver) TypeChar(column schema.ColumnDefinition) string {
return fmt.Sprintf("nchar(%d)", column.GetLength())
}
Expand Down
6 changes: 6 additions & 0 deletions database/schema/grammars/sqlserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ func (s *SqlserverSuite) TestModifyIncrement() {
s.Equal(" identity primary key", s.grammar.ModifyIncrement(mockBlueprint, mockColumn))
}

func (s *SqlserverSuite) TestTypeBoolean() {
mockColumn := mocksschema.NewColumnDefinition(s.T())

s.Equal("bit", s.grammar.TypeBoolean(mockColumn))
}

func (s *SqlserverSuite) TestTypeDecimal() {
mockColumn := mocksschema.NewColumnDefinition(s.T())
mockColumn.EXPECT().GetTotal().Return(4).Once()
Expand Down
41 changes: 39 additions & 2 deletions database/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (s *SchemaSuite) TestColumnExtraAttributes() {
table.String("nullable").Nullable()
table.String("string_default").Default("goravel")
table.Integer("integer_default").Default(1)
table.Integer("bool_default").Default(true)
table.Boolean("bool_default").Default(true)
table.TimestampTz("use_current").UseCurrent()
table.TimestampTz("use_current_on_update").UseCurrent().UseCurrentOnUpdate()
}))
Expand Down Expand Up @@ -192,6 +192,15 @@ func (s *SchemaSuite) TestColumnTypes_Postgres() {
s.Equal("bigint", column.Type)
s.Equal("int8", column.TypeName)
}
if column.Name == "boolean_default" {
s.False(column.Autoincrement)
s.Empty(column.Collation)
s.Equal("This is a boolean column with default value", column.Comment)
s.Equal("true", column.Default)
s.False(column.Nullable)
s.Equal("boolean", column.Type)
s.Equal("bool", column.TypeName)
}
if column.Name == "char" {
s.False(column.Autoincrement)
s.Empty(column.Collation)
Expand Down Expand Up @@ -513,6 +522,15 @@ func (s *SchemaSuite) TestColumnTypes_Sqlite() {
s.Equal("integer", column.Type)
s.Equal("integer", column.TypeName)
}
if column.Name == "boolean_default" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
s.Equal("'1'", column.Default)
s.False(column.Nullable)
s.Equal("tinyint(1)", column.Type)
s.Equal("tinyint", column.TypeName)
}

if column.Name == "char" {
s.False(column.Autoincrement)
s.Empty(column.Comment)
Expand Down Expand Up @@ -796,6 +814,15 @@ func (s *SchemaSuite) TestColumnTypes_Mysql() {
s.Equal("bigint", column.Type)
s.Equal("bigint", column.TypeName)
}
if column.Name == "boolean_default" {
s.False(column.Autoincrement)
s.Empty(column.Collation)
s.Equal("This is a boolean column with default value", column.Comment)
s.Equal("1", column.Default)
s.False(column.Nullable)
s.Equal("tinyint(1)", column.Type)
s.Equal("tinyint", column.TypeName)
}
if column.Name == "char" {
s.False(column.Autoincrement)
s.Equal("utf8mb4_0900_ai_ci", column.Collation)
Expand Down Expand Up @@ -1110,6 +1137,15 @@ func (s *SchemaSuite) TestColumnTypes_Sqlserver() {
s.Equal("bigint", column.Type)
s.Equal("bigint", column.TypeName)
}
if column.Name == "boolean_default" {
s.False(column.Autoincrement)
s.Empty(column.Collation)
s.Empty(column.Comment)
s.Equal("('1')", column.Default)
s.False(column.Nullable)
s.Equal("bit", column.Type)
s.Equal("bit", column.TypeName)
}
if column.Name == "char" {
s.False(column.Autoincrement)
s.Equal("SQL_Latin1_General_CP1_CI_AS", column.Collation)
Expand Down Expand Up @@ -2324,6 +2360,7 @@ func (s *SchemaSuite) TestViewMethods() {
func (s *SchemaSuite) createTableAndAssertColumnsForColumnMethods(schema contractsschema.Schema, table string) {
err := schema.Create(table, func(table contractsschema.Blueprint) {
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")
table.Date("date").Comment("This is a date column")
table.DateTime("date_time", 3).Comment("This is a date time column")
Expand Down Expand Up @@ -2364,7 +2401,7 @@ func (s *SchemaSuite) createTableAndAssertColumnsForColumnMethods(schema contrac

columnListing := schema.GetColumnListing(table)

s.Equal(33, len(columnListing))
s.Equal(34, len(columnListing))
s.Contains(columnListing, "another_deleted_at")
s.Contains(columnListing, "big_integer")
s.Contains(columnListing, "char")
Expand Down
48 changes: 48 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 d0e46b9

Please sign in to comment.