Skip to content

Commit

Permalink
feat: [#280] Add Time methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Nov 21, 2024
1 parent b35420c commit 383f26f
Show file tree
Hide file tree
Showing 13 changed files with 1,915 additions and 450 deletions.
22 changes: 22 additions & 0 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 Down
8 changes: 8 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 @@ -27,8 +29,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
OnUpdate(value string) ColumnDefinition
// Places set the decimal places
Places(places int) ColumnDefinition
// Total set the decimal total
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
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]
}

Check warning on line 221 in database/schema/blueprint.go

View check run for this annotation

Codecov / codecov/patch

database/schema/blueprint.go#L220-L221

Added lines #L220 - L221 were not covered by tests

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()

Check warning on line 285 in database/schema/blueprint.go

View check run for this annotation

Codecov / codecov/patch

database/schema/blueprint.go#L283-L285

Added lines #L283 - L285 were not covered by tests
}

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
55 changes: 43 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 *string
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

Check warning on line 41 in database/schema/column.go

View check run for this annotation

Codecov / codecov/patch

database/schema/column.go#L38-L41

Added lines #L38 - L41 were not covered by tests
}

func (r *ColumnDefinition) GetAllowed() []string {
return r.allowed
}
Expand Down Expand Up @@ -112,6 +121,22 @@ func (r *ColumnDefinition) GetType() (ttype string) {
return
}

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

Check warning on line 127 in database/schema/column.go

View check run for this annotation

Codecov / codecov/patch

database/schema/column.go#L126-L127

Added lines #L126 - L127 were not covered by tests

return
}

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

Check warning on line 135 in database/schema/column.go

View check run for this annotation

Codecov / codecov/patch

database/schema/column.go#L134-L135

Added lines #L134 - L135 were not covered by tests

return
}

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

func (r *ColumnDefinition) OnUpdate(value string) schema.ColumnDefinition {
r.onUpdate = convert.Pointer(value)

return r

Check warning on line 153 in database/schema/column.go

View check run for this annotation

Codecov / codecov/patch

database/schema/column.go#L150-L153

Added lines #L150 - L153 were not covered by tests
}

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

Expand Down
70 changes: 66 additions & 4 deletions database/schema/grammars/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,33 @@ func (r *Mysql) TypeChar(column schema.ColumnDefinition) string {
return fmt.Sprintf("char(%d)", column.GetLength())
}

func (r *Mysql) TypeDate(column schema.ColumnDefinition) string {
return "date"

Check warning on line 218 in database/schema/grammars/mysql.go

View check run for this annotation

Codecov / codecov/patch

database/schema/grammars/mysql.go#L217-L218

Added lines #L217 - L218 were not covered by tests
}

func (r *Mysql) TypeDateTime(column schema.ColumnDefinition) string {
current := "CURRENT_TIMESTAMP"
if column.GetPrecision() > 0 {
current = fmt.Sprintf("CURRENT_TIMESTAMP(%d)", column.GetPrecision())
}
if column.GetUseCurrent() {
column.Default(current)
}
if column.GetUseCurrentOnUpdate() {
column.OnUpdate(current)
}

Check warning on line 231 in database/schema/grammars/mysql.go

View check run for this annotation

Codecov / codecov/patch

database/schema/grammars/mysql.go#L221-L231

Added lines #L221 - L231 were not covered by tests

if column.GetPrecision() > 0 {
return fmt.Sprintf("datetime(%d)", column.GetPrecision())
} else {
return "datetime"
}

Check warning on line 237 in database/schema/grammars/mysql.go

View check run for this annotation

Codecov / codecov/patch

database/schema/grammars/mysql.go#L233-L237

Added lines #L233 - L237 were not covered by tests
}

func (r *Mysql) TypeDateTimeTz(column schema.ColumnDefinition) string {
return r.TypeDateTime(column)

Check warning on line 241 in database/schema/grammars/mysql.go

View check run for this annotation

Codecov / codecov/patch

database/schema/grammars/mysql.go#L240-L241

Added lines #L240 - L241 were not covered by tests
}

func (r *Mysql) TypeDecimal(column schema.ColumnDefinition) string {
return fmt.Sprintf("decimal(%d, %d)", column.GetTotal(), column.GetPlaces())
}
Expand Down Expand Up @@ -259,10 +286,6 @@ func (r *Mysql) TypeMediumText(column schema.ColumnDefinition) string {
return "mediumtext"
}

func (r *Mysql) TypeText(column schema.ColumnDefinition) string {
return "text"
}

func (r *Mysql) TypeSmallInteger(column schema.ColumnDefinition) string {
return "smallint"
}
Expand All @@ -276,6 +299,45 @@ func (r *Mysql) TypeString(column schema.ColumnDefinition) string {
return "varchar(255)"
}

func (r *Mysql) TypeText(column schema.ColumnDefinition) string {
return "text"

Check warning on line 303 in database/schema/grammars/mysql.go

View check run for this annotation

Codecov / codecov/patch

database/schema/grammars/mysql.go#L302-L303

Added lines #L302 - L303 were not covered by tests
}

func (r *Mysql) TypeTime(column schema.ColumnDefinition) string {
if column.GetPrecision() > 0 {
return fmt.Sprintf("time(%d)", column.GetPrecision())
} else {
return "time"
}

Check warning on line 311 in database/schema/grammars/mysql.go

View check run for this annotation

Codecov / codecov/patch

database/schema/grammars/mysql.go#L306-L311

Added lines #L306 - L311 were not covered by tests
}

func (r *Mysql) TypeTimeTz(column schema.ColumnDefinition) string {
return r.TypeTime(column)

Check warning on line 315 in database/schema/grammars/mysql.go

View check run for this annotation

Codecov / codecov/patch

database/schema/grammars/mysql.go#L314-L315

Added lines #L314 - L315 were not covered by tests
}

func (r *Mysql) TypeTimestamp(column schema.ColumnDefinition) string {
current := "CURRENT_TIMESTAMP"
if column.GetPrecision() > 0 {
current = fmt.Sprintf("CURRENT_TIMESTAMP(%d)", column.GetPrecision())
}
if column.GetUseCurrent() {
column.Default(current)
}
if column.GetUseCurrentOnUpdate() {
column.OnUpdate(current)
}

Check warning on line 328 in database/schema/grammars/mysql.go

View check run for this annotation

Codecov / codecov/patch

database/schema/grammars/mysql.go#L318-L328

Added lines #L318 - L328 were not covered by tests

if column.GetPrecision() > 0 {
return fmt.Sprintf("timestamp(%d)", column.GetPrecision())
} else {
return "timestamp"
}

Check warning on line 334 in database/schema/grammars/mysql.go

View check run for this annotation

Codecov / codecov/patch

database/schema/grammars/mysql.go#L330-L334

Added lines #L330 - L334 were not covered by tests
}

func (r *Mysql) TypeTimestampTz(column schema.ColumnDefinition) string {
return r.TypeTimestamp(column)

Check warning on line 338 in database/schema/grammars/mysql.go

View check run for this annotation

Codecov / codecov/patch

database/schema/grammars/mysql.go#L337-L338

Added lines #L337 - L338 were not covered by tests
}

func (r *Mysql) TypeTinyInteger(column schema.ColumnDefinition) string {
return "tinyint"
}
Expand Down
Loading

0 comments on commit 383f26f

Please sign in to comment.