Skip to content

Commit

Permalink
feat: [#571] migrator supports After and First column modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
almas1992 committed Feb 10, 2025
1 parent c378d7d commit 3e719ab
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 4 deletions.
2 changes: 1 addition & 1 deletion contracts/database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Blueprint interface {
Char(column string, length ...int) ColumnDefinition
// Column Create a new custom type column on the table.
Column(column string, ttype string) ColumnDefinition
// Comment Add a comment to the table.
// Comment Add a comment to the table. (MySQL / PostgreSQL)
Comment(value string)
// Create Indicate that the table needs to be created.
Create()
Expand Down
14 changes: 11 additions & 3 deletions contracts/database/schema/column.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package schema

type ColumnDefinition interface {
// After Place the column "after" another column (MySQL only)
After(column string) ColumnDefinition
// AutoIncrement set the column as auto increment
AutoIncrement() ColumnDefinition
// Change the column
// Change the column (MySQL / PostgreSQL / SQL Server)
Change() ColumnDefinition
// Comment sets the comment value
// Comment sets the comment value (MySQL / PostgreSQL)
Comment(comment string) ColumnDefinition
// Default set the default value
Default(def any) ColumnDefinition
// First Place the column "first" in the table (MySQL only)
First() ColumnDefinition
// GetAfter returns the after value
GetAfter() string
// GetAllowed returns the allowed value
GetAllowed() []any
// GetAutoIncrement returns the autoIncrement value
Expand Down Expand Up @@ -41,6 +47,8 @@ type ColumnDefinition interface {
GetUseCurrentOnUpdate() bool
// IsChange returns true if the column has changed
IsChange() bool
// IsFirst returns true if the column is first
IsFirst() bool
// IsSetComment returns true if the comment value is set
IsSetComment() bool
// OnUpdate sets the column to use the value on update (Mysql only)
Expand All @@ -51,7 +59,7 @@ type ColumnDefinition interface {
Total(total int) ColumnDefinition
// Nullable allow NULL values to be inserted into the column
Nullable() ColumnDefinition
// Unsigned set the column as unsigned
// Unsigned set the column as unsigned (Mysql only)
Unsigned() ColumnDefinition
// UseCurrent set the column to use the current timestamp
UseCurrent() ColumnDefinition
Expand Down
22 changes: 22 additions & 0 deletions database/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
)

type ColumnDefinition struct {
after string
allowed []any
autoIncrement *bool
change bool
comment *string
def any
first bool
length *int
name *string
nullable *bool
Expand All @@ -31,6 +33,12 @@ func NewColumnDefinition(name string, ttype string) schema.ColumnDefinition {
}
}

func (r *ColumnDefinition) After(column string) schema.ColumnDefinition {
r.after = column

return r
}

func (r *ColumnDefinition) AutoIncrement() schema.ColumnDefinition {
r.autoIncrement = convert.Pointer(true)

Expand All @@ -55,6 +63,16 @@ func (r *ColumnDefinition) Default(def any) schema.ColumnDefinition {
return r
}

func (r *ColumnDefinition) First() schema.ColumnDefinition {
r.first = true

return r
}

func (r *ColumnDefinition) GetAfter() string {
return r.after
}

func (r *ColumnDefinition) GetAllowed() []any {
return r.allowed
}
Expand Down Expand Up @@ -167,6 +185,10 @@ func (r *ColumnDefinition) IsChange() bool {
return r.change
}

func (r *ColumnDefinition) IsFirst() bool {
return r.first
}

func (r *ColumnDefinition) IsSetComment() bool {
return r != nil && r.comment != nil
}
Expand Down
185 changes: 185 additions & 0 deletions mocks/database/schema/ColumnDefinition.go

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

0 comments on commit 3e719ab

Please sign in to comment.