Skip to content

Commit

Permalink
feat: [#280] Add Time methods (#733)
Browse files Browse the repository at this point in the history
* feat: [#280] Add Time methods

* Add column methods, TODO: test UseCurrent, Default, etc

* chore: update mocks

* Add test cases, TODO: fix TestColumnMethods_Sqlite

* chore: update mocks

* test

* Add test cases, TODO: optimize TestColumnExtraAttributes

* chore: update mocks

* Add test cases, TODO: optimize TestColumnExtraAttributes

* fix test

* fix test

* fix test

* Add test cases

* Remove test

---------

Co-authored-by: hwbrzzl <hwbrzzl@users.noreply.github.com>
  • Loading branch information
hwbrzzl and hwbrzzl authored Nov 30, 2024
1 parent 65c6a08 commit 5776698
Show file tree
Hide file tree
Showing 22 changed files with 2,477 additions and 507 deletions.
27 changes: 22 additions & 5 deletions contracts/database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ type Blueprint interface {
Char(column string, length ...int) ColumnDefinition
// Create Indicate that the table needs to be created.
Create()
// Date Create a new date column on the table.
Date(column string) ColumnDefinition
// DateTime Create a new date-time column on the table.
DateTime(column string, precision ...int) ColumnDefinition
// DateTimeTz Create a new date-time column (with time zone) on the table.
DateTimeTz(column string, precision ...int) ColumnDefinition
// Decimal Create a new decimal column on the table.
Decimal(column string) ColumnDefinition
// Double Create a new double column on the table.
Expand Down Expand Up @@ -65,10 +71,26 @@ type Blueprint interface {
SmallIncrements(column string) ColumnDefinition
// SmallInteger Create a new small integer (2-byte) column on the table.
SmallInteger(column string) ColumnDefinition
// SoftDeletes Add a "deleted at" timestamp for the table.
SoftDeletes(column ...string) ColumnDefinition
// SoftDeletesTz Add a "deleted at" timestampTz for the table.
SoftDeletesTz(column ...string) ColumnDefinition
// String Create a new string column on the table.
String(column string, length ...int) ColumnDefinition
// Text Create a new text column on the table.
Text(column string) ColumnDefinition
// Time Create a new time column on the table.
Time(column string, precision ...int) ColumnDefinition
// TimeTz Create a new time column (with time zone) on the table.
TimeTz(column string, precision ...int) ColumnDefinition
// Timestamp Create a new time column on the table.
Timestamp(column string, precision ...int) ColumnDefinition
// Timestamps Add nullable creation and update timestamps to the table.
Timestamps(precision ...int)
// TimestampsTz Add creation and update timestampTz columns to the table.
TimestampsTz(precision ...int)
// TimestampTz Create a new time column (with time zone) on the table.
TimestampTz(column string, precision ...int) ColumnDefinition
// TinyIncrements Create a new auto-incrementing tiny integer (1-byte) column on the table.
TinyIncrements(column string) ColumnDefinition
// TinyInteger Create a new tiny integer (1-byte) column on the table.
Expand All @@ -88,8 +110,3 @@ type Blueprint interface {
// UnsignedTinyInteger Create a new unsigned tiny integer (1-byte) column on the table.
UnsignedTinyInteger(column string) ColumnDefinition
}

type IndexConfig struct {
Algorithm string
Name string
}
14 changes: 14 additions & 0 deletions contracts/database/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ type ColumnDefinition interface {
AutoIncrement() ColumnDefinition
// Comment sets the comment value
Comment(comment string) ColumnDefinition
// Default set the default value
Default(def any) ColumnDefinition
// GetAllowed returns the allowed value
GetAllowed() []string
// GetAutoIncrement returns the autoIncrement value
Expand All @@ -19,6 +21,8 @@ type ColumnDefinition interface {
GetName() string
// GetNullable returns the nullable value
GetNullable() bool
// GetOnUpdate returns the onUpdate value
GetOnUpdate() any
// GetPlaces returns the places value
GetPlaces() int
// GetPrecision returns the precision value
Expand All @@ -27,8 +31,14 @@ type ColumnDefinition interface {
GetTotal() int
// GetType returns the type value
GetType() string
// GetUseCurrent returns the useCurrent value
GetUseCurrent() bool
// GetUseCurrentOnUpdate returns the useCurrentOnUpdate value
GetUseCurrentOnUpdate() bool
// IsSetComment returns true if the comment value is set
IsSetComment() bool
// OnUpdate sets the column to use the value on update (Mysql only)
OnUpdate(value any) ColumnDefinition
// Places set the decimal places
Places(places int) ColumnDefinition
// Total set the decimal total
Expand All @@ -37,6 +47,10 @@ type ColumnDefinition interface {
Nullable() ColumnDefinition
// Unsigned set the column as unsigned
Unsigned() ColumnDefinition
// UseCurrent set the column to use the current timestamp
UseCurrent() ColumnDefinition
// UseCurrentOnUpdate set the column to use the current timestamp on update (Mysql only)
UseCurrentOnUpdate() ColumnDefinition
}

type Column struct {
Expand Down
14 changes: 14 additions & 0 deletions contracts/database/schema/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ type Grammar interface {
TypeBigInteger(column ColumnDefinition) string
// TypeChar Create the column definition for a char type.
TypeChar(column ColumnDefinition) string
// TypeDate Create the column definition for a date type.
TypeDate(column ColumnDefinition) string
// TypeDateTime Create the column definition for a date-time type.
TypeDateTime(column ColumnDefinition) string
// TypeDateTimeTz Create the column definition for a date-time (with time zone) type.
TypeDateTimeTz(column ColumnDefinition) string
// TypeDecimal Create the column definition for a decimal type.
TypeDecimal(column ColumnDefinition) string
// TypeDouble Create the column definition for a double type.
Expand All @@ -61,6 +67,14 @@ type Grammar interface {
TypeMediumText(column ColumnDefinition) string
// TypeText Create the column definition for a text type.
TypeText(column ColumnDefinition) string
// TypeTime Create the column definition for a time type.
TypeTime(column ColumnDefinition) string
// TypeTimeTz Create the column definition for a time (with time zone) type.
TypeTimeTz(column ColumnDefinition) string
// TypeTimestamp Create the column definition for a timestamp type.
TypeTimestamp(column ColumnDefinition) string
// TypeTimestampTz Create the column definition for a timestamp (with time zone) type.
TypeTimestampTz(column ColumnDefinition) string
// TypeTinyInteger Create the column definition for a tiny integer type.
TypeTinyInteger(column ColumnDefinition) string
// TypeTinyText Create the column definition for a tiny text type.
Expand Down
5 changes: 5 additions & 0 deletions contracts/database/schema/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ type IndexDefinition interface {
Algorithm(algorithm string) IndexDefinition
Name(name string) IndexDefinition
}

type IndexConfig struct {
Algorithm string
Name string
}
2 changes: 1 addition & 1 deletion database/gorm/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (r *MockMysql) basic() {
r.mockConfig.On("GetBool", "app.debug").Return(true)
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.driver", r.connection)).Return(r.driver.String())
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.charset", r.connection)).Return("utf8mb4")
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.loc", r.connection)).Return("Local")
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.loc", r.connection)).Return("UTC")
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.database", r.connection)).Return(r.database)

mockPool(r.mockConfig)
Expand Down
86 changes: 86 additions & 0 deletions database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ func (r *Blueprint) Decimal(column string) schema.ColumnDefinition {
return r.createAndAddColumn("decimal", column)
}

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

func (r *Blueprint) DateTime(column string, precision ...int) schema.ColumnDefinition {
columnImpl := r.createAndAddColumn("dateTime", column)
if len(precision) > 0 {
columnImpl.precision = &precision[0]
}

return columnImpl
}

func (r *Blueprint) DateTimeTz(column string, precision ...int) schema.ColumnDefinition {
columnImpl := r.createAndAddColumn("dateTimeTz", column)
if len(precision) > 0 {
columnImpl.precision = &precision[0]
}

return columnImpl
}

func (r *Blueprint) Double(column string) schema.ColumnDefinition {
return r.createAndAddColumn("double", column)
}
Expand Down Expand Up @@ -192,6 +214,24 @@ func (r *Blueprint) SmallInteger(column string) schema.ColumnDefinition {
return r.createAndAddColumn("smallInteger", column)
}

func (r *Blueprint) SoftDeletes(column ...string) schema.ColumnDefinition {
newColumn := "deleted_at"
if len(column) > 0 {
newColumn = column[0]
}

return r.Timestamp(newColumn).Nullable()
}

func (r *Blueprint) SoftDeletesTz(column ...string) schema.ColumnDefinition {
newColumn := "deleted_at"
if len(column) > 0 {
newColumn = column[0]
}

return r.TimestampTz(newColumn).Nullable()
}

func (r *Blueprint) String(column string, length ...int) schema.ColumnDefinition {
defaultLength := constants.DefaultStringLength
if len(length) > 0 {
Expand All @@ -208,6 +248,52 @@ func (r *Blueprint) Text(column string) schema.ColumnDefinition {
return r.createAndAddColumn("text", column)
}

func (r *Blueprint) Time(column string, precision ...int) schema.ColumnDefinition {
columnImpl := r.createAndAddColumn("time", column)
if len(precision) > 0 {
columnImpl.precision = &precision[0]
}

return columnImpl
}

func (r *Blueprint) TimeTz(column string, precision ...int) schema.ColumnDefinition {
columnImpl := r.createAndAddColumn("timeTz", column)
if len(precision) > 0 {
columnImpl.precision = &precision[0]
}

return columnImpl
}

func (r *Blueprint) Timestamp(column string, precision ...int) schema.ColumnDefinition {
columnImpl := r.createAndAddColumn("timestamp", column)
if len(precision) > 0 {
columnImpl.precision = &precision[0]
}

return columnImpl
}

func (r *Blueprint) Timestamps(precision ...int) {
r.Timestamp("created_at", precision...).Nullable()
r.Timestamp("updated_at", precision...).Nullable()
}

func (r *Blueprint) TimestampsTz(precision ...int) {
r.TimestampTz("created_at", precision...).Nullable()
r.TimestampTz("updated_at", precision...).Nullable()
}

func (r *Blueprint) TimestampTz(column string, precision ...int) schema.ColumnDefinition {
columnImpl := r.createAndAddColumn("timestampTz", column)
if len(precision) > 0 {
columnImpl.precision = &precision[0]
}

return columnImpl
}

func (r *Blueprint) TinyIncrements(column string) schema.ColumnDefinition {
return r.UnsignedTinyInteger(column).AutoIncrement()
}
Expand Down
71 changes: 59 additions & 12 deletions database/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ import (
)

type ColumnDefinition struct {
allowed []string
autoIncrement *bool
comment *string
def any
length *int
name *string
nullable *bool
places *int
precision *int
total *int
ttype *string
unsigned *bool
allowed []string
autoIncrement *bool
comment *string
def any
length *int
name *string
nullable *bool
onUpdate any
places *int
precision *int
total *int
ttype *string
unsigned *bool
useCurrent *bool
useCurrentOnUpdate *bool
}

func (r *ColumnDefinition) AutoIncrement() schema.ColumnDefinition {
Expand All @@ -32,6 +35,12 @@ func (r *ColumnDefinition) Comment(comment string) schema.ColumnDefinition {
return r
}

func (r *ColumnDefinition) Default(def any) schema.ColumnDefinition {
r.def = def

return r
}

func (r *ColumnDefinition) GetAllowed() []string {
return r.allowed
}
Expand Down Expand Up @@ -80,6 +89,10 @@ func (r *ColumnDefinition) GetNullable() bool {
return false
}

func (r *ColumnDefinition) GetOnUpdate() any {
return r.onUpdate
}

func (r *ColumnDefinition) GetPlaces() (places int) {
if r.places != nil {
return *r.places
Expand Down Expand Up @@ -112,6 +125,22 @@ func (r *ColumnDefinition) GetType() (ttype string) {
return
}

func (r *ColumnDefinition) GetUseCurrent() (useCurrent bool) {
if r.useCurrent != nil {
return *r.useCurrent
}

return
}

func (r *ColumnDefinition) GetUseCurrentOnUpdate() (useCurrentOnUpdate bool) {
if r.useCurrentOnUpdate != nil {
return *r.useCurrentOnUpdate
}

return
}

func (r *ColumnDefinition) IsSetComment() bool {
return r != nil && r.comment != nil
}
Expand All @@ -122,6 +151,12 @@ func (r *ColumnDefinition) Nullable() schema.ColumnDefinition {
return r
}

func (r *ColumnDefinition) OnUpdate(value any) schema.ColumnDefinition {
r.onUpdate = value

return r
}

func (r *ColumnDefinition) Places(places int) schema.ColumnDefinition {
r.places = convert.Pointer(places)

Expand All @@ -139,3 +174,15 @@ func (r *ColumnDefinition) Unsigned() schema.ColumnDefinition {

return r
}

func (r *ColumnDefinition) UseCurrent() schema.ColumnDefinition {
r.useCurrent = convert.Pointer(true)

return r
}

func (r *ColumnDefinition) UseCurrentOnUpdate() schema.ColumnDefinition {
r.useCurrentOnUpdate = convert.Pointer(true)

return r
}
Loading

0 comments on commit 5776698

Please sign in to comment.