diff --git a/contracts/database/schema/blueprint.go b/contracts/database/schema/blueprint.go index 691b24539..fcf921ccf 100644 --- a/contracts/database/schema/blueprint.go +++ b/contracts/database/schema/blueprint.go @@ -13,8 +13,8 @@ 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. GetTableName() string // HasCommand Determine if the blueprint has a specific command. diff --git a/contracts/database/schema/column.go b/contracts/database/schema/column.go index b5f810cde..50960f82b 100644 --- a/contracts/database/schema/column.go +++ b/contracts/database/schema/column.go @@ -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 diff --git a/contracts/database/schema/grammar.go b/contracts/database/schema/grammar.go index 3ae99fe3d..5ea560eda 100644 --- a/contracts/database/schema/grammar.go +++ b/contracts/database/schema/grammar.go @@ -6,9 +6,7 @@ import ( type Grammar interface { // CompileAdd Compile an add column command. - CompileAdd(blueprint Blueprint) string - // CompileChange Compile a change column command into a series of SQL statements. - CompileChange(blueprint Blueprint) string + CompileAdd(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. @@ -22,7 +20,7 @@ type Grammar interface { // CompileDropIfExists Compile a drop table (if exists) command. CompileDropIfExists(blueprint Blueprint) string // CompileTables Compile the query to determine the tables. - CompileTables(database string) string + CompileTables() string // CompileTypes Compile the query to determine the types. CompileTypes() string // CompileViews Compile the query to determine the views. diff --git a/contracts/database/schema/schema.go b/contracts/database/schema/schema.go index f903cec7c..6a240ec37 100644 --- a/contracts/database/schema/schema.go +++ b/contracts/database/schema/schema.go @@ -66,7 +66,6 @@ type Connection interface { type Command struct { Algorithm string Column ColumnDefinition - Columns []string From string Index string On string diff --git a/database/schema/blueprint.go b/database/schema/blueprint.go index 853b72700..b57f905a9 100644 --- a/database/schema/blueprint.go +++ b/database/schema/blueprint.go @@ -3,18 +3,10 @@ package schema import ( ormcontract "github.com/goravel/framework/contracts/database/orm" "github.com/goravel/framework/contracts/database/schema" + "github.com/goravel/framework/database/schema/constants" "github.com/goravel/framework/support/convert" ) -const ( - commandAdd = "add" - commandChange = "change" - commandComment = "comment" - commandCreate = "create" - commandDropIfExists = "dropIfExists" - defaultStringLength = 255 -) - type Blueprint struct { columns []*ColumnDefinition commands []*schema.Command @@ -56,36 +48,27 @@ func (r *Blueprint) Build(query ormcontract.Query, grammar schema.Grammar) error func (r *Blueprint) Create() { r.addCommand(&schema.Command{ - Name: commandCreate, + Name: constants.CommandCreate, }) } func (r *Blueprint) DropIfExists() { r.addCommand(&schema.Command{ - Name: commandDropIfExists, + Name: constants.CommandDropIfExists, }) } func (r *Blueprint) GetAddedColumns() []schema.ColumnDefinition { var columns []schema.ColumnDefinition for _, column := range r.columns { - if column.change == nil || !*column.change { - columns = append(columns, column) - } + 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 } func (r *Blueprint) GetTableName() string { @@ -127,7 +110,7 @@ func (r *Blueprint) SetTable(name string) { } func (r *Blueprint) String(column string, length ...int) schema.ColumnDefinition { - defaultLength := defaultStringLength + defaultLength := constants.DefaultStringLength if len(length) > 0 { defaultLength = length[0] } @@ -148,13 +131,11 @@ func (r *Blueprint) ToSql(query ormcontract.Query, grammar schema.Grammar) []str var statements []string for _, command := range r.commands { switch command.Name { - case commandAdd: - statements = append(statements, grammar.CompileAdd(r)) - case commandChange: - statements = append(statements, grammar.CompileChange(r)) - case commandCreate: + case constants.CommandAdd: + statements = append(statements, grammar.CompileAdd(r, command)) + case constants.CommandCreate: statements = append(statements, grammar.CompileCreate(r, query)) - case commandDropIfExists: + case constants.CommandDropIfExists: statements = append(statements, grammar.CompileDropIfExists(r)) } } @@ -170,10 +151,10 @@ func (r *Blueprint) addAttributeCommands(grammar schema.Grammar) { attributeCommands := grammar.GetAttributeCommands() for _, column := range r.columns { for _, command := range attributeCommands { - if command == "comment" && column.comment != nil { + if command == constants.CommandComment && column.comment != nil { r.addCommand(&schema.Command{ Column: column, - Name: commandComment, + Name: constants.CommandComment, }) } } @@ -182,6 +163,13 @@ func (r *Blueprint) addAttributeCommands(grammar schema.Grammar) { func (r *Blueprint) addColumn(column *ColumnDefinition) { r.columns = append(r.columns, column) + + if !r.isCreate() { + r.addCommand(&schema.Command{ + Name: constants.CommandAdd, + Column: column, + }) + } } func (r *Blueprint) addCommand(command *schema.Command) { @@ -189,26 +177,12 @@ func (r *Blueprint) addCommand(command *schema.Command) { } func (r *Blueprint) addImpliedCommands(grammar schema.Grammar) { - var commands []*schema.Command - if len(r.GetAddedColumns()) > 0 && !r.isCreate() { - commands = append(commands, &schema.Command{ - Name: commandAdd, - }) - } - if len(r.GetChangedColumns()) > 0 && !r.isCreate() { - commands = append(commands, &schema.Command{ - Name: commandChange, - }) - } - if len(commands) > 0 { - r.commands = append(commands, r.commands...) - } r.addAttributeCommands(grammar) } func (r *Blueprint) isCreate() bool { for _, command := range r.commands { - if command.Name == commandCreate { + if command.Name == constants.CommandCreate { return true } } diff --git a/database/schema/blueprint_test.go b/database/schema/blueprint_test.go index 62512be83..37b619863 100644 --- a/database/schema/blueprint_test.go +++ b/database/schema/blueprint_test.go @@ -8,6 +8,7 @@ import ( "github.com/goravel/framework/contracts/database" "github.com/goravel/framework/contracts/database/schema" + "github.com/goravel/framework/database/schema/constants" "github.com/goravel/framework/database/schema/grammars" mocksorm "github.com/goravel/framework/mocks/database/orm" mocksschema "github.com/goravel/framework/mocks/database/schema" @@ -86,107 +87,6 @@ func (s *BlueprintTestSuite) TestAddAttributeCommands() { } } -func (s *BlueprintTestSuite) TestAddImpliedCommands() { - var ( - mockGrammar *mocksschema.Grammar - ) - - tests := []struct { - name string - columns []*ColumnDefinition - commands []*schema.Command - setup func() - expectCommands []*schema.Command - }{ - { - name: "Should not add the add command when there are added columns but it is a create operation", - columns: []*ColumnDefinition{ - { - name: convert.Pointer("name"), - }, - }, - commands: []*schema.Command{ - { - Name: "create", - }, - }, - setup: func() { - mockGrammar.EXPECT().GetAttributeCommands().Return([]string{}).Once() - }, - expectCommands: []*schema.Command{ - { - Name: "create", - }, - }, - }, - { - name: "Should not add the change command when there are changed columns but it is a create operation", - columns: []*ColumnDefinition{ - { - name: convert.Pointer("name"), - change: convert.Pointer(true), - }, - }, - commands: []*schema.Command{ - { - Name: "create", - }, - }, - setup: func() { - mockGrammar.EXPECT().GetAttributeCommands().Return([]string{}).Once() - }, - expectCommands: []*schema.Command{ - { - Name: "create", - }, - }, - }, - { - name: "Should add the add, change, attribute commands when there are added and changed columns, and it is not a create operation", - columns: []*ColumnDefinition{ - { - name: convert.Pointer("name"), - comment: convert.Pointer("comment"), - }, - { - name: convert.Pointer("age"), - change: convert.Pointer(true), - }, - }, - setup: func() { - mockGrammar.EXPECT().GetAttributeCommands().Return([]string{"comment"}).Once() - }, - expectCommands: []*schema.Command{ - { - Name: "add", - }, - { - Name: "change", - }, - { - Column: &ColumnDefinition{ - name: convert.Pointer("name"), - comment: convert.Pointer("comment"), - }, - Name: "comment", - }, - }, - }, - } - - for _, test := range tests { - s.Run(test.name, func() { - mockGrammar = mocksschema.NewGrammar(s.T()) - s.blueprint.columns = test.columns - s.blueprint.commands = test.commands - test.setup() - - s.blueprint.addImpliedCommands(mockGrammar) - s.Equal(test.expectCommands, s.blueprint.commands) - }) - } -} - func (s *BlueprintTestSuite) TestBigIncrements() { name := "name" s.blueprint.BigIncrements(name) @@ -238,54 +138,32 @@ func (s *BlueprintTestSuite) TestBuild() { func (s *BlueprintTestSuite) TestGetAddedColumns() { name := "name" - change := true addedColumn := &ColumnDefinition{ name: &name, } - changedColumn := &ColumnDefinition{ - change: &change, - name: &name, - } - s.blueprint.columns = []*ColumnDefinition{addedColumn, changedColumn} + 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()) } func (s *BlueprintTestSuite) TestHasCommand() { - s.False(s.blueprint.HasCommand(commandCreate)) + s.False(s.blueprint.HasCommand(constants.CommandCreate)) s.blueprint.Create() - s.True(s.blueprint.HasCommand(commandCreate)) + s.True(s.blueprint.HasCommand(constants.CommandCreate)) } func (s *BlueprintTestSuite) TestIsCreate() { s.False(s.blueprint.isCreate()) s.blueprint.commands = []*schema.Command{ { - Name: commandCreate, + Name: constants.CommandCreate, }, } s.True(s.blueprint.isCreate()) @@ -329,7 +207,7 @@ func (s *BlueprintTestSuite) TestInteger() { func (s *BlueprintTestSuite) TestString() { column := "name" customLength := 100 - length := defaultStringLength + length := constants.DefaultStringLength ttype := "string" s.blueprint.String(column) s.Contains(s.blueprint.GetAddedColumns(), &ColumnDefinition{ @@ -348,6 +226,7 @@ func (s *BlueprintTestSuite) TestString() { func (s *BlueprintTestSuite) TestToSql() { for driver, grammar := range s.grammars { + // Create a table mockQuery := mocksorm.NewQuery(s.T()) s.blueprint.Create() s.blueprint.String("name") @@ -360,6 +239,15 @@ func (s *BlueprintTestSuite) TestToSql() { } else { s.Empty(s.blueprint.ToSql(mockQuery, grammar)) } + + // Update a table + s.SetupTest() + s.blueprint.String("avatar") + if driver == database.DriverPostgres { + s.Len(s.blueprint.ToSql(mockQuery, grammar), 1) + } else { + s.Empty(s.blueprint.ToSql(mockQuery, grammar)) + } } } diff --git a/database/schema/column.go b/database/schema/column.go index 6dd3a25c2..87fdd9f61 100644 --- a/database/schema/column.go +++ b/database/schema/column.go @@ -7,7 +7,6 @@ import ( type ColumnDefinition struct { autoIncrement *bool - change *bool comment *string def any length *int @@ -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 @@ -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 } diff --git a/database/schema/column_test.go b/database/schema/column_test.go index ffccab196..c95bf770a 100644 --- a/database/schema/column_test.go +++ b/database/schema/column_test.go @@ -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()) diff --git a/database/schema/common_schema.go b/database/schema/common_schema.go index 3cd63189d..c145f6b35 100644 --- a/database/schema/common_schema.go +++ b/database/schema/common_schema.go @@ -19,7 +19,7 @@ func NewCommonSchema(grammar schema.Grammar, orm orm.Orm) *CommonSchema { func (r *CommonSchema) GetTables() ([]schema.Table, error) { var tables []schema.Table - if err := r.orm.Query().Raw(r.grammar.CompileTables("")).Scan(&tables); err != nil { + if err := r.orm.Query().Raw(r.grammar.CompileTables()).Scan(&tables); err != nil { return nil, err } diff --git a/database/schema/constants/constants.go b/database/schema/constants/constants.go new file mode 100644 index 000000000..e7dc816db --- /dev/null +++ b/database/schema/constants/constants.go @@ -0,0 +1,10 @@ +package constants + +const ( + CommandAdd = "add" + CommandChange = "change" + CommandComment = "comment" + CommandCreate = "create" + CommandDropIfExists = "dropIfExists" + DefaultStringLength = 255 +) diff --git a/database/schema/grammars/postgres.go b/database/schema/grammars/postgres.go index b3792a8b0..f9c9b96e8 100644 --- a/database/schema/grammars/postgres.go +++ b/database/schema/grammars/postgres.go @@ -7,6 +7,7 @@ import ( "github.com/goravel/framework/contracts/database/orm" "github.com/goravel/framework/contracts/database/schema" + "github.com/goravel/framework/database/schema/constants" ) type Postgres struct { @@ -17,7 +18,7 @@ type Postgres struct { func NewPostgres() *Postgres { postgres := &Postgres{ - attributeCommands: []string{"comment"}, + attributeCommands: []string{constants.CommandComment}, serials: []string{"bigInteger", "integer", "mediumInteger", "smallInteger", "tinyInteger"}, } postgres.modifiers = []func(schema.Blueprint, schema.ColumnDefinition) string{ @@ -29,29 +30,8 @@ func NewPostgres() *Postgres { return postgres } -func (r *Postgres) CompileAdd(blueprint schema.Blueprint) string { - return fmt.Sprintf("alter table %s %s", blueprint.GetTableName(), strings.Join(prefixArray("add column", getColumns(r, blueprint)), ",")) -} - -func (r *Postgres) CompileChange(blueprint schema.Blueprint) string { - var columns []string - for _, column := range blueprint.GetChangedColumns() { - var changes []string - - for _, modifier := range r.modifiers { - if change := modifier(blueprint, column); change != "" { - changes = append(changes, change) - } - } - - columns = append(columns, strings.Join(prefixArray("alter column "+column.GetName(), changes), ", ")) - } - - if len(columns) == 0 { - return "" - } - - return fmt.Sprintf("alter table %s %s", blueprint.GetTableName(), strings.Join(columns, ", ")) +func (r *Postgres) CompileAdd(blueprint schema.Blueprint, command *schema.Command) string { + return fmt.Sprintf("alter table %s add column %s", blueprint.GetTableName(), getColumn(r, blueprint, command.Column)) } func (r *Postgres) CompileCreate(blueprint schema.Blueprint, query orm.Query) string { @@ -78,7 +58,7 @@ func (r *Postgres) CompileDropIfExists(blueprint schema.Blueprint) string { return fmt.Sprintf("drop table if exists %s", blueprint.GetTableName()) } -func (r *Postgres) CompileTables(database string) string { +func (r *Postgres) CompileTables() string { return "select c.relname as name, n.nspname as schema, pg_total_relation_size(c.oid) as size, " + "obj_description(c.oid, 'pg_class') as comment from pg_class c, pg_namespace n " + "where c.relkind in ('r', 'p') and n.oid = c.relnamespace and n.nspname not in ('pg_catalog', 'information_schema') " + @@ -126,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())) } @@ -146,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 { @@ -162,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" } diff --git a/database/schema/grammars/postgres_test.go b/database/schema/grammars/postgres_test.go index f3e949c66..64e052407 100644 --- a/database/schema/grammars/postgres_test.go +++ b/database/schema/grammars/postgres_test.go @@ -5,7 +5,7 @@ import ( "github.com/stretchr/testify/suite" - contractsmigration "github.com/goravel/framework/contracts/database/schema" + contractsschema "github.com/goravel/framework/contracts/database/schema" mocksschema "github.com/goravel/framework/mocks/database/schema" ) @@ -22,70 +22,23 @@ func (s *PostgresSuite) SetupTest() { s.grammar = NewPostgres() } -func (s *PostgresSuite) TestCompileChange() { - var ( - mockBlueprint *mocksschema.Blueprint - mockColumn1 *mocksschema.ColumnDefinition - mockColumn2 *mocksschema.ColumnDefinition - ) - - tests := []struct { - name string - setup func() - expectSql string - }{ - { - name: "no changes", - setup: func() { - mockBlueprint.EXPECT().GetChangedColumns().Return([]contractsmigration.ColumnDefinition{}).Once() - }, - }, - { - name: "single change", - setup: func() { - mockColumn1.EXPECT().GetAutoIncrement().Return(false).Once() - mockColumn1.EXPECT().GetDefault().Return("goravel").Twice() - mockColumn1.EXPECT().GetName().Return("name").Once() - mockColumn1.EXPECT().GetChange().Return(true).Times(3) - mockColumn1.EXPECT().GetNullable().Return(true).Once() - mockBlueprint.EXPECT().GetTableName().Return("users").Once() - mockBlueprint.EXPECT().GetChangedColumns().Return([]contractsmigration.ColumnDefinition{mockColumn1}).Once() - }, - expectSql: "alter table users alter column name set default 'goravel', alter column name drop not null", - }, - { - name: "multiple changes", - setup: func() { - mockColumn1.EXPECT().GetAutoIncrement().Return(false).Once() - mockColumn1.EXPECT().GetDefault().Return("goravel").Twice() - mockColumn1.EXPECT().GetName().Return("name").Once() - mockColumn1.EXPECT().GetChange().Return(true).Times(3) - mockColumn1.EXPECT().GetNullable().Return(true).Once() - mockColumn2.EXPECT().GetAutoIncrement().Return(false).Once() - mockColumn2.EXPECT().GetDefault().Return(1).Twice() - mockColumn2.EXPECT().GetName().Return("age").Once() - mockColumn2.EXPECT().GetChange().Return(true).Times(3) - mockColumn2.EXPECT().GetNullable().Return(false).Once() - mockBlueprint.EXPECT().GetTableName().Return("users").Once() - mockBlueprint.EXPECT().GetChangedColumns().Return([]contractsmigration.ColumnDefinition{mockColumn1, mockColumn2}).Once() - }, - expectSql: "alter table users alter column name set default 'goravel', alter column name drop not null, alter column age set default '1', alter column age set not null", - }, - } - - for _, test := range tests { - s.Run(test.name, func() { - mockBlueprint = mocksschema.NewBlueprint(s.T()) - mockColumn1 = mocksschema.NewColumnDefinition(s.T()) - mockColumn2 = mocksschema.NewColumnDefinition(s.T()) +func (s *PostgresSuite) TestCompileAdd() { + mockBlueprint := mocksschema.NewBlueprint(s.T()) + mockColumn := mocksschema.NewColumnDefinition(s.T()) - test.setup() + mockBlueprint.EXPECT().GetTableName().Return("users").Once() + mockColumn.EXPECT().GetName().Return("name").Once() + mockColumn.EXPECT().GetType().Return("string").Twice() + mockColumn.EXPECT().GetDefault().Return("goravel").Twice() + mockColumn.EXPECT().GetNullable().Return(false).Once() + mockColumn.EXPECT().GetLength().Return(1).Once() + mockBlueprint.EXPECT().HasCommand("primary").Return(false).Once() - sql := s.grammar.CompileChange(mockBlueprint) + sql := s.grammar.CompileAdd(mockBlueprint, &contractsschema.Command{ + Column: mockColumn, + }) - s.Equal(test.expectSql, sql) - }) - } + s.Equal("alter table users add column name varchar(1) default 'goravel' not null", sql) } func (s *PostgresSuite) TestCompileCreate() { @@ -96,7 +49,7 @@ func (s *PostgresSuite) TestCompileCreate() { // postgres.go::CompileCreate mockBlueprint.EXPECT().GetTableName().Return("users").Once() // utils.go::getColumns - mockBlueprint.EXPECT().GetAddedColumns().Return([]contractsmigration.ColumnDefinition{ + mockBlueprint.EXPECT().GetAddedColumns().Return([]contractsschema.ColumnDefinition{ mockColumn1, mockColumn2, }).Once() // utils.go::getColumns @@ -106,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 @@ -124,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)", @@ -173,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'", @@ -233,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)) @@ -258,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() diff --git a/database/schema/grammars/utils.go b/database/schema/grammars/utils.go index 8733e9021..420916f21 100644 --- a/database/schema/grammars/utils.go +++ b/database/schema/grammars/utils.go @@ -18,12 +18,16 @@ func addModify(modifiers []func(schema.Blueprint, schema.ColumnDefinition) strin return sql } +func getColumn(grammar schema.Grammar, blueprint schema.Blueprint, column schema.ColumnDefinition) string { + sql := fmt.Sprintf("%s %s", column.GetName(), getType(grammar, column)) + + return addModify(grammar.GetModifiers(), sql, blueprint, column) +} + func getColumns(grammar schema.Grammar, blueprint schema.Blueprint) []string { var columns []string for _, column := range blueprint.GetAddedColumns() { - sql := fmt.Sprintf("%s %s", column.GetName(), getType(grammar, column)) - - columns = append(columns, addModify(grammar.GetModifiers(), sql, blueprint, column)) + columns = append(columns, getColumn(grammar, blueprint, column)) } return columns diff --git a/database/schema/postgres_schema.go b/database/schema/postgres_schema.go index f8c7c94fb..703b53a5b 100644 --- a/database/schema/postgres_schema.go +++ b/database/schema/postgres_schema.go @@ -119,15 +119,6 @@ func (r *PostgresSchema) DropAllViews() error { return err } -func (r *PostgresSchema) getSchema() string { - schema := r.config.GetString(fmt.Sprintf("database.connections.%s.search_path", r.orm.Name())) - if schema == "" { - return "public" - } - - return schema -} - func (r *PostgresSchema) GetTypes() ([]contractsschema.Type, error) { var types []contractsschema.Type if err := r.orm.Query().Raw(r.grammar.CompileTypes()).Scan(&types); err != nil { @@ -136,3 +127,12 @@ func (r *PostgresSchema) GetTypes() ([]contractsschema.Type, error) { return r.processor.ProcessTypes(types), nil } + +func (r *PostgresSchema) getSchema() string { + schema := r.config.GetString(fmt.Sprintf("database.connections.%s.search_path", r.orm.Name())) + if schema == "" { + return "public" + } + + return schema +} diff --git a/mocks/database/schema/Blueprint.go b/mocks/database/schema/Blueprint.go index e04fb18a0..7ac792b3b 100644 --- a/mocks/database/schema/Blueprint.go +++ b/mocks/database/schema/Blueprint.go @@ -179,49 +179,49 @@ func (_c *Blueprint_GetAddedColumns_Call) RunAndReturn(run func() []schema.Colum return _c } -// GetChangedColumns provides a mock function with given fields: -func (_m *Blueprint) GetChangedColumns() []schema.ColumnDefinition { +// GetCommands provides a mock function with given fields: +func (_m *Blueprint) GetCommands() []*schema.Command { ret := _m.Called() if len(ret) == 0 { - panic("no return value specified for GetChangedColumns") + panic("no return value specified for GetCommands") } - var r0 []schema.ColumnDefinition - if rf, ok := ret.Get(0).(func() []schema.ColumnDefinition); ok { + var r0 []*schema.Command + if rf, ok := ret.Get(0).(func() []*schema.Command); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).([]schema.ColumnDefinition) + r0 = ret.Get(0).([]*schema.Command) } } return r0 } -// Blueprint_GetChangedColumns_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetChangedColumns' -type Blueprint_GetChangedColumns_Call struct { +// Blueprint_GetCommands_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetCommands' +type Blueprint_GetCommands_Call struct { *mock.Call } -// GetChangedColumns is a helper method to define mock.On call -func (_e *Blueprint_Expecter) GetChangedColumns() *Blueprint_GetChangedColumns_Call { - return &Blueprint_GetChangedColumns_Call{Call: _e.mock.On("GetChangedColumns")} +// GetCommands is a helper method to define mock.On call +func (_e *Blueprint_Expecter) GetCommands() *Blueprint_GetCommands_Call { + return &Blueprint_GetCommands_Call{Call: _e.mock.On("GetCommands")} } -func (_c *Blueprint_GetChangedColumns_Call) Run(run func()) *Blueprint_GetChangedColumns_Call { +func (_c *Blueprint_GetCommands_Call) Run(run func()) *Blueprint_GetCommands_Call { _c.Call.Run(func(args mock.Arguments) { run() }) return _c } -func (_c *Blueprint_GetChangedColumns_Call) Return(_a0 []schema.ColumnDefinition) *Blueprint_GetChangedColumns_Call { +func (_c *Blueprint_GetCommands_Call) Return(_a0 []*schema.Command) *Blueprint_GetCommands_Call { _c.Call.Return(_a0) return _c } -func (_c *Blueprint_GetChangedColumns_Call) RunAndReturn(run func() []schema.ColumnDefinition) *Blueprint_GetChangedColumns_Call { +func (_c *Blueprint_GetCommands_Call) RunAndReturn(run func() []*schema.Command) *Blueprint_GetCommands_Call { _c.Call.Return(run) return _c } diff --git a/mocks/database/schema/ColumnDefinition.go b/mocks/database/schema/ColumnDefinition.go index ec8cc24e9..9fe90a154 100644 --- a/mocks/database/schema/ColumnDefinition.go +++ b/mocks/database/schema/ColumnDefinition.go @@ -67,38 +67,6 @@ func (_c *ColumnDefinition_AutoIncrement_Call) RunAndReturn(run func() schema.Co return _c } -// Change provides a mock function with given fields: -func (_m *ColumnDefinition) Change() { - _m.Called() -} - -// ColumnDefinition_Change_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Change' -type ColumnDefinition_Change_Call struct { - *mock.Call -} - -// Change is a helper method to define mock.On call -func (_e *ColumnDefinition_Expecter) Change() *ColumnDefinition_Change_Call { - return &ColumnDefinition_Change_Call{Call: _e.mock.On("Change")} -} - -func (_c *ColumnDefinition_Change_Call) Run(run func()) *ColumnDefinition_Change_Call { - _c.Call.Run(func(args mock.Arguments) { - run() - }) - return _c -} - -func (_c *ColumnDefinition_Change_Call) Return() *ColumnDefinition_Change_Call { - _c.Call.Return() - return _c -} - -func (_c *ColumnDefinition_Change_Call) RunAndReturn(run func()) *ColumnDefinition_Change_Call { - _c.Call.Return(run) - return _c -} - // GetAutoIncrement provides a mock function with given fields: func (_m *ColumnDefinition) GetAutoIncrement() bool { ret := _m.Called() @@ -144,51 +112,6 @@ func (_c *ColumnDefinition_GetAutoIncrement_Call) RunAndReturn(run func() bool) return _c } -// GetChange provides a mock function with given fields: -func (_m *ColumnDefinition) GetChange() bool { - ret := _m.Called() - - if len(ret) == 0 { - panic("no return value specified for GetChange") - } - - var r0 bool - if rf, ok := ret.Get(0).(func() bool); ok { - r0 = rf() - } else { - r0 = ret.Get(0).(bool) - } - - return r0 -} - -// ColumnDefinition_GetChange_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetChange' -type ColumnDefinition_GetChange_Call struct { - *mock.Call -} - -// GetChange is a helper method to define mock.On call -func (_e *ColumnDefinition_Expecter) GetChange() *ColumnDefinition_GetChange_Call { - return &ColumnDefinition_GetChange_Call{Call: _e.mock.On("GetChange")} -} - -func (_c *ColumnDefinition_GetChange_Call) Run(run func()) *ColumnDefinition_GetChange_Call { - _c.Call.Run(func(args mock.Arguments) { - run() - }) - return _c -} - -func (_c *ColumnDefinition_GetChange_Call) Return(_a0 bool) *ColumnDefinition_GetChange_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *ColumnDefinition_GetChange_Call) RunAndReturn(run func() bool) *ColumnDefinition_GetChange_Call { - _c.Call.Return(run) - return _c -} - // GetDefault provides a mock function with given fields: func (_m *ColumnDefinition) GetDefault() any { ret := _m.Called() diff --git a/mocks/database/schema/Grammar.go b/mocks/database/schema/Grammar.go index 22e5e14f4..8268e022a 100644 --- a/mocks/database/schema/Grammar.go +++ b/mocks/database/schema/Grammar.go @@ -21,17 +21,17 @@ func (_m *Grammar) EXPECT() *Grammar_Expecter { return &Grammar_Expecter{mock: &_m.Mock} } -// CompileAdd provides a mock function with given fields: blueprint -func (_m *Grammar) CompileAdd(blueprint schema.Blueprint) string { - ret := _m.Called(blueprint) +// CompileAdd provides a mock function with given fields: blueprint, command +func (_m *Grammar) CompileAdd(blueprint schema.Blueprint, command *schema.Command) string { + ret := _m.Called(blueprint, command) if len(ret) == 0 { panic("no return value specified for CompileAdd") } var r0 string - if rf, ok := ret.Get(0).(func(schema.Blueprint) string); ok { - r0 = rf(blueprint) + if rf, ok := ret.Get(0).(func(schema.Blueprint, *schema.Command) string); ok { + r0 = rf(blueprint, command) } else { r0 = ret.Get(0).(string) } @@ -46,13 +46,14 @@ type Grammar_CompileAdd_Call struct { // CompileAdd is a helper method to define mock.On call // - blueprint schema.Blueprint -func (_e *Grammar_Expecter) CompileAdd(blueprint interface{}) *Grammar_CompileAdd_Call { - return &Grammar_CompileAdd_Call{Call: _e.mock.On("CompileAdd", blueprint)} +// - command *schema.Command +func (_e *Grammar_Expecter) CompileAdd(blueprint interface{}, command interface{}) *Grammar_CompileAdd_Call { + return &Grammar_CompileAdd_Call{Call: _e.mock.On("CompileAdd", blueprint, command)} } -func (_c *Grammar_CompileAdd_Call) Run(run func(blueprint schema.Blueprint)) *Grammar_CompileAdd_Call { +func (_c *Grammar_CompileAdd_Call) Run(run func(blueprint schema.Blueprint, command *schema.Command)) *Grammar_CompileAdd_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(schema.Blueprint)) + run(args[0].(schema.Blueprint), args[1].(*schema.Command)) }) return _c } @@ -62,53 +63,7 @@ func (_c *Grammar_CompileAdd_Call) Return(_a0 string) *Grammar_CompileAdd_Call { return _c } -func (_c *Grammar_CompileAdd_Call) RunAndReturn(run func(schema.Blueprint) string) *Grammar_CompileAdd_Call { - _c.Call.Return(run) - return _c -} - -// CompileChange provides a mock function with given fields: blueprint -func (_m *Grammar) CompileChange(blueprint schema.Blueprint) string { - ret := _m.Called(blueprint) - - if len(ret) == 0 { - panic("no return value specified for CompileChange") - } - - var r0 string - if rf, ok := ret.Get(0).(func(schema.Blueprint) string); ok { - r0 = rf(blueprint) - } else { - r0 = ret.Get(0).(string) - } - - return r0 -} - -// Grammar_CompileChange_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CompileChange' -type Grammar_CompileChange_Call struct { - *mock.Call -} - -// CompileChange is a helper method to define mock.On call -// - blueprint schema.Blueprint -func (_e *Grammar_Expecter) CompileChange(blueprint interface{}) *Grammar_CompileChange_Call { - return &Grammar_CompileChange_Call{Call: _e.mock.On("CompileChange", blueprint)} -} - -func (_c *Grammar_CompileChange_Call) Run(run func(blueprint schema.Blueprint)) *Grammar_CompileChange_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(schema.Blueprint)) - }) - return _c -} - -func (_c *Grammar_CompileChange_Call) Return(_a0 string) *Grammar_CompileChange_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *Grammar_CompileChange_Call) RunAndReturn(run func(schema.Blueprint) string) *Grammar_CompileChange_Call { +func (_c *Grammar_CompileAdd_Call) RunAndReturn(run func(schema.Blueprint, *schema.Command) string) *Grammar_CompileAdd_Call { _c.Call.Return(run) return _c } @@ -390,17 +345,17 @@ func (_c *Grammar_CompileDropIfExists_Call) RunAndReturn(run func(schema.Bluepri return _c } -// CompileTables provides a mock function with given fields: database -func (_m *Grammar) CompileTables(database string) string { - ret := _m.Called(database) +// CompileTables provides a mock function with given fields: +func (_m *Grammar) CompileTables() string { + ret := _m.Called() if len(ret) == 0 { panic("no return value specified for CompileTables") } var r0 string - if rf, ok := ret.Get(0).(func(string) string); ok { - r0 = rf(database) + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() } else { r0 = ret.Get(0).(string) } @@ -414,14 +369,13 @@ type Grammar_CompileTables_Call struct { } // CompileTables is a helper method to define mock.On call -// - database string -func (_e *Grammar_Expecter) CompileTables(database interface{}) *Grammar_CompileTables_Call { - return &Grammar_CompileTables_Call{Call: _e.mock.On("CompileTables", database)} +func (_e *Grammar_Expecter) CompileTables() *Grammar_CompileTables_Call { + return &Grammar_CompileTables_Call{Call: _e.mock.On("CompileTables")} } -func (_c *Grammar_CompileTables_Call) Run(run func(database string)) *Grammar_CompileTables_Call { +func (_c *Grammar_CompileTables_Call) Run(run func()) *Grammar_CompileTables_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(string)) + run() }) return _c } @@ -431,7 +385,7 @@ func (_c *Grammar_CompileTables_Call) Return(_a0 string) *Grammar_CompileTables_ return _c } -func (_c *Grammar_CompileTables_Call) RunAndReturn(run func(string) string) *Grammar_CompileTables_Call { +func (_c *Grammar_CompileTables_Call) RunAndReturn(run func() string) *Grammar_CompileTables_Call { _c.Call.Return(run) return _c }