Skip to content

Commit

Permalink
Remove
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Nov 3, 2024
1 parent 67447fd commit 4108c22
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 185 deletions.
2 changes: 0 additions & 2 deletions contracts/database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ type Blueprint interface {
DropIfExists()
// GetAddedColumns Get the added columns.
GetAddedColumns() []ColumnDefinition
// GetChangedColumns Get the changed columns.
//GetChangedColumns() []ColumnDefinition
// GetCommands Get the commands.
GetCommands() []*Command
// GetTableName Get the table name with prefix.
Expand Down
4 changes: 0 additions & 4 deletions contracts/database/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ package schema
type ColumnDefinition interface {
// AutoIncrement set the column as auto increment
AutoIncrement() ColumnDefinition
// Change the column
//Change()
// GetAutoIncrement returns the autoIncrement value
GetAutoIncrement() bool
// GetChange returns the change value
//GetChange() bool
// GetDefault returns the default value
GetDefault() any
// GetLength returns the length value
Expand Down
2 changes: 0 additions & 2 deletions contracts/database/schema/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
type Grammar interface {
// CompileAdd Compile an add column command.
CompileAdd(blueprint Blueprint, command *Command) string
// CompileChange Compile a change column command into a series of SQL statements.
CompileChange(blueprint Blueprint, command *Command) string
// CompileCreate Compile a create table command.
CompileCreate(blueprint Blueprint, query orm.Query) string
// CompileDropAllDomains Compile the SQL needed to drop all domains.
Expand Down
25 changes: 1 addition & 24 deletions database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,11 @@ func (r *Blueprint) GetAddedColumns() []schema.ColumnDefinition {
var columns []schema.ColumnDefinition
for _, column := range r.columns {
columns = append(columns, column)
//if column.change == nil || !*column.change {
// columns = append(columns, column)
//}
}

return columns
}

//func (r *Blueprint) GetChangedColumns() []schema.ColumnDefinition {
// var columns []schema.ColumnDefinition
// for _, column := range r.columns {
// if column.change != nil && *column.change {
// columns = append(columns, column)
// }
// }
//
// return columns
//}

func (r *Blueprint) GetCommands() []*schema.Command {
return r.commands
}
Expand Down Expand Up @@ -147,8 +133,6 @@ func (r *Blueprint) ToSql(query ormcontract.Query, grammar schema.Grammar) []str
switch command.Name {
case constants.CommandAdd:
statements = append(statements, grammar.CompileAdd(r, command))
case constants.CommandChange:
statements = append(statements, grammar.CompileChange(r, command))
case constants.CommandCreate:
statements = append(statements, grammar.CompileCreate(r, query))
case constants.CommandDropIfExists:
Expand Down Expand Up @@ -181,15 +165,8 @@ func (r *Blueprint) addColumn(column *ColumnDefinition) {
r.columns = append(r.columns, column)

if !r.isCreate() {
//var name string
//if column.GetChange() {
// name = constants.CommandChange
//} else {
// name = constants.CommandAdd
//}
name := constants.CommandAdd
r.addCommand(&schema.Command{
Name: name,
Name: constants.CommandAdd,
Column: column,
})
}
Expand Down
23 changes: 0 additions & 23 deletions database/schema/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,35 +142,12 @@ func (s *BlueprintTestSuite) TestGetAddedColumns() {
name: &name,
}

//change := true
//changedColumn := &ColumnDefinition{
// change: &change,
// name: &name,
//}

s.blueprint.columns = []*ColumnDefinition{addedColumn}

s.Len(s.blueprint.GetAddedColumns(), 1)
s.Equal(addedColumn, s.blueprint.GetAddedColumns()[0])
}

//func (s *BlueprintTestSuite) TestGetChangedColumns() {
// name := "name"
// change := true
// addedColumn := &ColumnDefinition{
// name: &name,
// }
// changedColumn := &ColumnDefinition{
// change: &change,
// name: &name,
// }
//
// s.blueprint.columns = []*ColumnDefinition{addedColumn, changedColumn}
//
// s.Len(s.blueprint.GetChangedColumns(), 1)
// s.Equal(changedColumn, s.blueprint.GetChangedColumns()[0])
//}

func (s *BlueprintTestSuite) TestGetTableName() {
s.blueprint.SetTable("users")
s.Equal("goravel_users", s.blueprint.GetTableName())
Expand Down
27 changes: 7 additions & 20 deletions database/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (

type ColumnDefinition struct {
autoIncrement *bool
//change *bool
comment *string
def any
length *int
name *string
nullable *bool
ttype *string
unsigned *bool
comment *string
def any
length *int
name *string
nullable *bool
ttype *string
unsigned *bool
}

func (r *ColumnDefinition) AutoIncrement() schema.ColumnDefinition {
Expand All @@ -23,10 +22,6 @@ func (r *ColumnDefinition) AutoIncrement() schema.ColumnDefinition {
return r
}

//func (r *ColumnDefinition) Change() {
// r.change = convert.Pointer(true)
//}

func (r *ColumnDefinition) GetAutoIncrement() (autoIncrement bool) {
if r.autoIncrement != nil {
return *r.autoIncrement
Expand All @@ -35,14 +30,6 @@ func (r *ColumnDefinition) GetAutoIncrement() (autoIncrement bool) {
return
}

//func (r *ColumnDefinition) GetChange() (change bool) {
// if r.change != nil {
// return *r.change
// }
//
// return
//}

func (r *ColumnDefinition) GetDefault() any {
return r.def
}
Expand Down
7 changes: 0 additions & 7 deletions database/schema/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ func (s *ColumnDefinitionTestSuite) GetAutoIncrement() {
s.True(s.columnDefinition.GetAutoIncrement())
}

func (s *ColumnDefinitionTestSuite) GetChange() {
s.False(s.columnDefinition.GetChange())

s.columnDefinition.change = convert.Pointer(true)
s.True(s.columnDefinition.GetChange())
}

func (s *ColumnDefinitionTestSuite) GetDefault() {
s.Nil(s.columnDefinition.GetDefault())

Expand Down
36 changes: 1 addition & 35 deletions database/schema/grammars/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,6 @@ func (r *Postgres) CompileAdd(blueprint schema.Blueprint, command *schema.Comman
return fmt.Sprintf("alter table %s add column %s", blueprint.GetTableName(), getColumn(r, blueprint, command.Column))
}

func (r *Postgres) CompileChange(blueprint schema.Blueprint, command *schema.Command) string {
var changes []string

for _, modifier := range r.modifiers {
if change := modifier(blueprint, command.Column); change != "" {
changes = append(changes, change)
}
}

column := strings.Join(prefixArray("alter column "+command.Column.GetName(), changes), ", ")

return fmt.Sprintf("alter table %s %s", blueprint.GetTableName(), column)
}

func (r *Postgres) CompileCreate(blueprint schema.Blueprint, query orm.Query) string {
return fmt.Sprintf("create table %s (%s)", blueprint.GetTableName(), strings.Join(getColumns(r, blueprint), ","))
}
Expand Down Expand Up @@ -120,18 +106,6 @@ func (r *Postgres) GetModifiers() []func(blueprint schema.Blueprint, column sche
}

func (r *Postgres) ModifyDefault(blueprint schema.Blueprint, column schema.ColumnDefinition) string {
if column.GetChange() {
if !column.GetAutoIncrement() {
if column.GetDefault() == nil {
return "drop default"
} else {
return fmt.Sprintf("set default %s", getDefaultValue(column.GetDefault()))
}
}

return ""
}

if column.GetDefault() != nil {
return fmt.Sprintf(" default %s", getDefaultValue(column.GetDefault()))
}
Expand All @@ -140,14 +114,6 @@ func (r *Postgres) ModifyDefault(blueprint schema.Blueprint, column schema.Colum
}

func (r *Postgres) ModifyNullable(blueprint schema.Blueprint, column schema.ColumnDefinition) string {
if column.GetChange() {
if column.GetNullable() {
return "drop not null"
} else {
return "set not null"
}
}

if column.GetNullable() {
return " null"
} else {
Expand All @@ -156,7 +122,7 @@ func (r *Postgres) ModifyNullable(blueprint schema.Blueprint, column schema.Colu
}

func (r *Postgres) ModifyIncrement(blueprint schema.Blueprint, column schema.ColumnDefinition) string {
if !column.GetChange() && !blueprint.HasCommand("primary") && slices.Contains(r.serials, column.GetType()) && column.GetAutoIncrement() {
if !blueprint.HasCommand("primary") && slices.Contains(r.serials, column.GetType()) && column.GetAutoIncrement() {
return " primary key"
}

Expand Down
68 changes: 0 additions & 68 deletions database/schema/grammars/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func (s *PostgresSuite) TestCompileAdd() {
mockColumn.EXPECT().GetName().Return("name").Once()
mockColumn.EXPECT().GetType().Return("string").Twice()
mockColumn.EXPECT().GetDefault().Return("goravel").Twice()
mockColumn.EXPECT().GetChange().Return(false).Times(3)
mockColumn.EXPECT().GetNullable().Return(false).Once()
mockColumn.EXPECT().GetLength().Return(1).Once()
mockBlueprint.EXPECT().HasCommand("primary").Return(false).Once()
Expand All @@ -42,24 +41,6 @@ func (s *PostgresSuite) TestCompileAdd() {
s.Equal("alter table users add column name varchar(1) default 'goravel' not null", sql)
}

func (s *PostgresSuite) TestCompileChange() {
mockBlueprint := mocksschema.NewBlueprint(s.T())
mockColumn := mocksschema.NewColumnDefinition(s.T())

mockBlueprint.EXPECT().GetTableName().Return("users").Once()
mockColumn.EXPECT().GetAutoIncrement().Return(false).Once()
mockColumn.EXPECT().GetDefault().Return("goravel").Twice()
mockColumn.EXPECT().GetName().Return("name").Once()
mockColumn.EXPECT().GetChange().Return(true).Times(3)
mockColumn.EXPECT().GetNullable().Return(true).Once()

sql := s.grammar.CompileChange(mockBlueprint, &contractsschema.Command{
Column: mockColumn,
})

s.Equal("alter table users alter column name set default 'goravel', alter column name drop not null", sql)
}

func (s *PostgresSuite) TestCompileCreate() {
mockColumn1 := mocksschema.NewColumnDefinition(s.T())
mockColumn2 := mocksschema.NewColumnDefinition(s.T())
Expand All @@ -78,15 +59,12 @@ func (s *PostgresSuite) TestCompileCreate() {
// postgres.go::TypeInteger
mockColumn1.EXPECT().GetAutoIncrement().Return(true).Once()
// postgres.go::ModifyDefault
mockColumn1.EXPECT().GetChange().Return(false).Once()
mockColumn1.EXPECT().GetDefault().Return(nil).Once()
// postgres.go::ModifyIncrement
mockColumn1.EXPECT().GetChange().Return(false).Once()
mockBlueprint.EXPECT().HasCommand("primary").Return(false).Once()
mockColumn1.EXPECT().GetType().Return("integer").Once()
mockColumn1.EXPECT().GetAutoIncrement().Return(true).Once()
// postgres.go::ModifyNullable
mockColumn1.EXPECT().GetChange().Return(false).Once()
mockColumn1.EXPECT().GetNullable().Return(false).Once()

// utils.go::getColumns
Expand All @@ -96,14 +74,11 @@ func (s *PostgresSuite) TestCompileCreate() {
// postgres.go::TypeString
mockColumn2.EXPECT().GetLength().Return(100).Once()
// postgres.go::ModifyDefault
mockColumn2.EXPECT().GetChange().Return(false).Once()
mockColumn2.EXPECT().GetDefault().Return(nil).Once()
// postgres.go::ModifyIncrement
mockColumn2.EXPECT().GetChange().Return(false).Once()
mockBlueprint.EXPECT().HasCommand("primary").Return(false).Once()
mockColumn2.EXPECT().GetType().Return("string").Once()
// postgres.go::ModifyNullable
mockColumn2.EXPECT().GetChange().Return(false).Once()
mockColumn2.EXPECT().GetNullable().Return(true).Once()

s.Equal("create table users (id serial primary key not null,name varchar(100) null)",
Expand Down Expand Up @@ -145,42 +120,15 @@ func (s *PostgresSuite) TestModifyDefault() {
setup func()
expectSql string
}{
{
name: "with change and AutoIncrement",
setup: func() {
mockColumn.EXPECT().GetChange().Return(true).Once()
mockColumn.EXPECT().GetAutoIncrement().Return(true).Once()
},
},
{
name: "with change and not AutoIncrement, default is nil",
setup: func() {
mockColumn.EXPECT().GetChange().Return(true).Once()
mockColumn.EXPECT().GetAutoIncrement().Return(false).Once()
mockColumn.EXPECT().GetDefault().Return(nil).Once()
},
expectSql: "drop default",
},
{
name: "with change and not AutoIncrement, default is not nil",
setup: func() {
mockColumn.EXPECT().GetChange().Return(true).Once()
mockColumn.EXPECT().GetAutoIncrement().Return(false).Once()
mockColumn.EXPECT().GetDefault().Return("goravel").Twice()
},
expectSql: "set default 'goravel'",
},
{
name: "without change and default is nil",
setup: func() {
mockColumn.EXPECT().GetChange().Return(false).Once()
mockColumn.EXPECT().GetDefault().Return(nil).Once()
},
},
{
name: "without change and default is not nil",
setup: func() {
mockColumn.EXPECT().GetChange().Return(false).Once()
mockColumn.EXPECT().GetDefault().Return("goravel").Twice()
},
expectSql: " default 'goravel'",
Expand All @@ -205,22 +153,11 @@ func (s *PostgresSuite) TestModifyNullable() {
mockBlueprint := mocksschema.NewBlueprint(s.T())

mockColumn := mocksschema.NewColumnDefinition(s.T())
mockColumn.EXPECT().GetChange().Return(true).Once()
mockColumn.EXPECT().GetNullable().Return(true).Once()

s.Equal("drop not null", s.grammar.ModifyNullable(mockBlueprint, mockColumn))

mockColumn.EXPECT().GetChange().Return(true).Once()
mockColumn.EXPECT().GetNullable().Return(false).Once()

s.Equal("set not null", s.grammar.ModifyNullable(mockBlueprint, mockColumn))

mockColumn.EXPECT().GetChange().Return(false).Once()
mockColumn.EXPECT().GetNullable().Return(true).Once()

s.Equal(" null", s.grammar.ModifyNullable(mockBlueprint, mockColumn))

mockColumn.EXPECT().GetChange().Return(false).Once()
mockColumn.EXPECT().GetNullable().Return(false).Once()

s.Equal(" not null", s.grammar.ModifyNullable(mockBlueprint, mockColumn))
Expand All @@ -230,11 +167,6 @@ func (s *PostgresSuite) TestModifyIncrement() {
mockBlueprint := mocksschema.NewBlueprint(s.T())

mockColumn := mocksschema.NewColumnDefinition(s.T())
mockColumn.EXPECT().GetChange().Return(true).Once()

s.Empty(s.grammar.ModifyIncrement(mockBlueprint, mockColumn))

mockColumn.EXPECT().GetChange().Return(false).Once()
mockBlueprint.EXPECT().HasCommand("primary").Return(false).Once()
mockColumn.EXPECT().GetType().Return("bigInteger").Once()
mockColumn.EXPECT().GetAutoIncrement().Return(true).Once()
Expand Down

0 comments on commit 4108c22

Please sign in to comment.