From a3a4bea685e7ff4b5eb63ae191bf98b4f3ed5460 Mon Sep 17 00:00:00 2001 From: Bowen Date: Sat, 2 Nov 2024 22:18:56 +0800 Subject: [PATCH 1/6] feat: [#280] Optimize --- contracts/database/schema/blueprint.go | 2 + contracts/database/schema/grammar.go | 6 +- contracts/database/schema/schema.go | 1 - database/schema/blueprint.go | 65 ++++++------ database/schema/blueprint_test.go | 120 +++------------------- database/schema/common_schema.go | 2 +- database/schema/constants/constants.go | 10 ++ database/schema/grammars/postgres.go | 30 +++--- database/schema/grammars/postgres_test.go | 94 ++++++----------- database/schema/grammars/utils.go | 21 +++- database/schema/postgres_schema.go | 18 ++-- database/schema/schema.go | 4 + mocks/database/schema/Blueprint.go | 47 +++++++++ mocks/database/schema/Grammar.go | 63 ++++++------ support/docker/docker.go | 2 +- 15 files changed, 217 insertions(+), 268 deletions(-) create mode 100644 database/schema/constants/constants.go diff --git a/contracts/database/schema/blueprint.go b/contracts/database/schema/blueprint.go index 691b24539..a61cf4f23 100644 --- a/contracts/database/schema/blueprint.go +++ b/contracts/database/schema/blueprint.go @@ -15,6 +15,8 @@ type Blueprint interface { 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/grammar.go b/contracts/database/schema/grammar.go index 3ae99fe3d..a93fa1531 100644 --- a/contracts/database/schema/grammar.go +++ b/contracts/database/schema/grammar.go @@ -6,9 +6,9 @@ import ( type Grammar interface { // CompileAdd Compile an add column command. - CompileAdd(blueprint Blueprint) string + CompileAdd(blueprint Blueprint, command *Command) string // CompileChange Compile a change column command into a series of SQL statements. - CompileChange(blueprint Blueprint) string + 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. @@ -22,7 +22,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..78b8f462f 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,13 +48,13 @@ 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, }) } @@ -88,6 +80,10 @@ func (r *Blueprint) GetChangedColumns() []schema.ColumnDefinition { return columns } +func (r *Blueprint) GetCommands() []*schema.Command { + return r.commands +} + func (r *Blueprint) GetTableName() string { // TODO Add schema for Postgres return r.prefix + r.table @@ -127,7 +123,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 +144,13 @@ 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.CommandChange: + statements = append(statements, grammar.CompileChange(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 +166,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 +178,19 @@ func (r *Blueprint) addAttributeCommands(grammar schema.Grammar) { 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 + } + r.addCommand(&schema.Command{ + Name: name, + Column: column, + }) + } } func (r *Blueprint) addCommand(command *schema.Command) { @@ -189,26 +198,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..c5a8c6e04 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) @@ -276,16 +176,16 @@ func (s *BlueprintTestSuite) TestGetTableName() { } 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 +229,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 +248,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 +261,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/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..8128c7f2c 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,22 @@ 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) 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) CompileChange(blueprint schema.Blueprint) string { - var columns []string - for _, column := range blueprint.GetChangedColumns() { - var changes []string +func (r *Postgres) CompileChange(blueprint schema.Blueprint, command *schema.Command) string { + var changes []string - for _, modifier := range r.modifiers { - if change := modifier(blueprint, column); change != "" { - changes = append(changes, change) - } + for _, modifier := range r.modifiers { + if change := modifier(blueprint, command.Column); change != "" { + changes = append(changes, change) } - - columns = append(columns, strings.Join(prefixArray("alter column "+column.GetName(), changes), ", ")) } - if len(columns) == 0 { - return "" - } + column := strings.Join(prefixArray("alter column "+command.Column.GetName(), changes), ", ") - return fmt.Sprintf("alter table %s %s", blueprint.GetTableName(), strings.Join(columns, ", ")) + return fmt.Sprintf("alter table %s %s", blueprint.GetTableName(), column) } func (r *Postgres) CompileCreate(blueprint schema.Blueprint, query orm.Query) string { @@ -78,7 +72,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') " + diff --git a/database/schema/grammars/postgres_test.go b/database/schema/grammars/postgres_test.go index f3e949c66..bd9414817 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,42 @@ func (s *PostgresSuite) SetupTest() { s.grammar = NewPostgres() } -func (s *PostgresSuite) TestCompileChange() { - var ( - mockBlueprint *mocksschema.Blueprint - mockColumn1 *mocksschema.ColumnDefinition - mockColumn2 *mocksschema.ColumnDefinition - ) +func (s *PostgresSuite) TestCompileAdd() { + mockBlueprint := mocksschema.NewBlueprint(s.T()) + mockColumn := mocksschema.NewColumnDefinition(s.T()) - 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", - }, - } + 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().GetChange().Return(false).Times(3) + mockColumn.EXPECT().GetNullable().Return(false).Once() + mockColumn.EXPECT().GetLength().Return(1).Once() + mockBlueprint.EXPECT().HasCommand("primary").Return(false).Once() - for _, test := range tests { - s.Run(test.name, func() { - mockBlueprint = mocksschema.NewBlueprint(s.T()) - mockColumn1 = mocksschema.NewColumnDefinition(s.T()) - mockColumn2 = mocksschema.NewColumnDefinition(s.T()) + sql := s.grammar.CompileAdd(mockBlueprint, &contractsschema.Command{ + Column: mockColumn, + }) - test.setup() + s.Equal("alter table users add column name varchar(1) default 'goravel' not null", sql) +} - sql := s.grammar.CompileChange(mockBlueprint) +func (s *PostgresSuite) TestCompileChange() { + mockBlueprint := mocksschema.NewBlueprint(s.T()) + mockColumn := mocksschema.NewColumnDefinition(s.T()) - s.Equal(test.expectSql, sql) - }) - } + 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() { @@ -96,7 +68,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 diff --git a/database/schema/grammars/utils.go b/database/schema/grammars/utils.go index 8733e9021..296e24dc7 100644 --- a/database/schema/grammars/utils.go +++ b/database/schema/grammars/utils.go @@ -18,17 +18,32 @@ 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 } +func getCommandsByName(commands []*schema.Command, name string) []*schema.Command { + var filteredCommands []*schema.Command + for _, command := range commands { + if command.Name == name { + filteredCommands = append(filteredCommands, command) + } + } + + return filteredCommands +} + func getDefaultValue(def any) string { switch def.(type) { case bool: 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/database/schema/schema.go b/database/schema/schema.go index 1ed447cd8..9b103b14c 100644 --- a/database/schema/schema.go +++ b/database/schema/schema.go @@ -91,6 +91,10 @@ func (r *Schema) DropIfExists(table string) error { return nil } +func (r *Schema) GetColumns() { + +} + func (r *Schema) GetConnection() string { return r.orm.Name() } diff --git a/mocks/database/schema/Blueprint.go b/mocks/database/schema/Blueprint.go index e04fb18a0..fd3bb34af 100644 --- a/mocks/database/schema/Blueprint.go +++ b/mocks/database/schema/Blueprint.go @@ -226,6 +226,53 @@ func (_c *Blueprint_GetChangedColumns_Call) RunAndReturn(run func() []schema.Col return _c } +// 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 GetCommands") + } + + 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.Command) + } + } + + return r0 +} + +// 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 +} + +// 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_GetCommands_Call) Run(run func()) *Blueprint_GetCommands_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *Blueprint_GetCommands_Call) Return(_a0 []*schema.Command) *Blueprint_GetCommands_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *Blueprint_GetCommands_Call) RunAndReturn(run func() []*schema.Command) *Blueprint_GetCommands_Call { + _c.Call.Return(run) + return _c +} + // GetTableName provides a mock function with given fields: func (_m *Blueprint) GetTableName() string { ret := _m.Called() diff --git a/mocks/database/schema/Grammar.go b/mocks/database/schema/Grammar.go index 22e5e14f4..4edcc1241 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,22 +63,22 @@ 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 { +func (_c *Grammar_CompileAdd_Call) RunAndReturn(run func(schema.Blueprint, *schema.Command) 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) +// CompileChange provides a mock function with given fields: blueprint, command +func (_m *Grammar) CompileChange(blueprint schema.Blueprint, command *schema.Command) string { + ret := _m.Called(blueprint, command) 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) + if rf, ok := ret.Get(0).(func(schema.Blueprint, *schema.Command) string); ok { + r0 = rf(blueprint, command) } else { r0 = ret.Get(0).(string) } @@ -92,13 +93,14 @@ type Grammar_CompileChange_Call struct { // 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)} +// - command *schema.Command +func (_e *Grammar_Expecter) CompileChange(blueprint interface{}, command interface{}) *Grammar_CompileChange_Call { + return &Grammar_CompileChange_Call{Call: _e.mock.On("CompileChange", blueprint, command)} } -func (_c *Grammar_CompileChange_Call) Run(run func(blueprint schema.Blueprint)) *Grammar_CompileChange_Call { +func (_c *Grammar_CompileChange_Call) Run(run func(blueprint schema.Blueprint, command *schema.Command)) *Grammar_CompileChange_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(schema.Blueprint)) + run(args[0].(schema.Blueprint), args[1].(*schema.Command)) }) return _c } @@ -108,7 +110,7 @@ func (_c *Grammar_CompileChange_Call) Return(_a0 string) *Grammar_CompileChange_ return _c } -func (_c *Grammar_CompileChange_Call) RunAndReturn(run func(schema.Blueprint) string) *Grammar_CompileChange_Call { +func (_c *Grammar_CompileChange_Call) RunAndReturn(run func(schema.Blueprint, *schema.Command) string) *Grammar_CompileChange_Call { _c.Call.Return(run) return _c } @@ -390,17 +392,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 +416,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 +432,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 } diff --git a/support/docker/docker.go b/support/docker/docker.go index 7d106b122..796015e45 100644 --- a/support/docker/docker.go +++ b/support/docker/docker.go @@ -15,7 +15,7 @@ const ( TestModelNormal // Switch this value to control the test model. - TestModel = TestModelNormal + TestModel = TestModelMinimum ) type ContainerType string From 3873c12c4d9d3b0dbbd04b05f6719a69fc305bf5 Mon Sep 17 00:00:00 2001 From: Bowen Date: Sun, 3 Nov 2024 08:34:14 +0800 Subject: [PATCH 2/6] feat: [#280] Remove Change function --- contracts/database/schema/blueprint.go | 2 +- contracts/database/schema/column.go | 4 +-- database/schema/blueprint.go | 40 ++++++++++++----------- database/schema/blueprint_test.go | 45 +++++++++++++------------- database/schema/column.go | 36 ++++++++++----------- database/schema/schema.go | 4 --- 6 files changed, 65 insertions(+), 66 deletions(-) diff --git a/contracts/database/schema/blueprint.go b/contracts/database/schema/blueprint.go index a61cf4f23..717e0c272 100644 --- a/contracts/database/schema/blueprint.go +++ b/contracts/database/schema/blueprint.go @@ -14,7 +14,7 @@ type Blueprint interface { // GetAddedColumns Get the added columns. GetAddedColumns() []ColumnDefinition // GetChangedColumns Get the changed columns. - GetChangedColumns() []ColumnDefinition + //GetChangedColumns() []ColumnDefinition // GetCommands Get the commands. GetCommands() []*Command // GetTableName Get the table name with prefix. diff --git a/contracts/database/schema/column.go b/contracts/database/schema/column.go index b5f810cde..a740e3314 100644 --- a/contracts/database/schema/column.go +++ b/contracts/database/schema/column.go @@ -4,11 +4,11 @@ type ColumnDefinition interface { // AutoIncrement set the column as auto increment AutoIncrement() ColumnDefinition // Change the column - Change() + //Change() // GetAutoIncrement returns the autoIncrement value GetAutoIncrement() bool // GetChange returns the change value - GetChange() bool + //GetChange() bool // GetDefault returns the default value GetDefault() any // GetLength returns the length value diff --git a/database/schema/blueprint.go b/database/schema/blueprint.go index 78b8f462f..cdd668981 100644 --- a/database/schema/blueprint.go +++ b/database/schema/blueprint.go @@ -61,24 +61,25 @@ func (r *Blueprint) DropIfExists() { 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) + //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) 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 @@ -180,12 +181,13 @@ 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 - } + //var name string + //if column.GetChange() { + // name = constants.CommandChange + //} else { + // name = constants.CommandAdd + //} + name := constants.CommandAdd r.addCommand(&schema.Command{ Name: name, Column: column, diff --git a/database/schema/blueprint_test.go b/database/schema/blueprint_test.go index c5a8c6e04..ced1a230e 100644 --- a/database/schema/blueprint_test.go +++ b/database/schema/blueprint_test.go @@ -138,37 +138,38 @@ 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} + //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) 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") diff --git a/database/schema/column.go b/database/schema/column.go index 6dd3a25c2..6f37a1a09 100644 --- a/database/schema/column.go +++ b/database/schema/column.go @@ -7,14 +7,14 @@ import ( type ColumnDefinition struct { autoIncrement *bool - change *bool - comment *string - def any - length *int - name *string - nullable *bool - ttype *string - unsigned *bool + //change *bool + comment *string + def any + length *int + name *string + nullable *bool + ttype *string + unsigned *bool } func (r *ColumnDefinition) AutoIncrement() schema.ColumnDefinition { @@ -23,9 +23,9 @@ func (r *ColumnDefinition) AutoIncrement() schema.ColumnDefinition { return r } -func (r *ColumnDefinition) Change() { - r.change = convert.Pointer(true) -} +//func (r *ColumnDefinition) Change() { +// r.change = convert.Pointer(true) +//} func (r *ColumnDefinition) GetAutoIncrement() (autoIncrement bool) { if r.autoIncrement != nil { @@ -35,13 +35,13 @@ func (r *ColumnDefinition) GetAutoIncrement() (autoIncrement bool) { return } -func (r *ColumnDefinition) GetChange() (change bool) { - if r.change != nil { - return *r.change - } - - 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/schema.go b/database/schema/schema.go index 9b103b14c..1ed447cd8 100644 --- a/database/schema/schema.go +++ b/database/schema/schema.go @@ -91,10 +91,6 @@ func (r *Schema) DropIfExists(table string) error { return nil } -func (r *Schema) GetColumns() { - -} - func (r *Schema) GetConnection() string { return r.orm.Name() } From 3c909dbc55f6d3196870966e18ccc6ca629ad779 Mon Sep 17 00:00:00 2001 From: hwbrzzl Date: Sun, 3 Nov 2024 00:35:00 +0000 Subject: [PATCH 3/6] chore: update mocks --- mocks/database/schema/Blueprint.go | 47 -------------- mocks/database/schema/ColumnDefinition.go | 77 ----------------------- 2 files changed, 124 deletions(-) diff --git a/mocks/database/schema/Blueprint.go b/mocks/database/schema/Blueprint.go index fd3bb34af..7ac792b3b 100644 --- a/mocks/database/schema/Blueprint.go +++ b/mocks/database/schema/Blueprint.go @@ -179,53 +179,6 @@ 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 { - ret := _m.Called() - - if len(ret) == 0 { - panic("no return value specified for GetChangedColumns") - } - - var r0 []schema.ColumnDefinition - if rf, ok := ret.Get(0).(func() []schema.ColumnDefinition); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]schema.ColumnDefinition) - } - } - - 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 { - *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")} -} - -func (_c *Blueprint_GetChangedColumns_Call) Run(run func()) *Blueprint_GetChangedColumns_Call { - _c.Call.Run(func(args mock.Arguments) { - run() - }) - return _c -} - -func (_c *Blueprint_GetChangedColumns_Call) Return(_a0 []schema.ColumnDefinition) *Blueprint_GetChangedColumns_Call { - _c.Call.Return(_a0) - return _c -} - -func (_c *Blueprint_GetChangedColumns_Call) RunAndReturn(run func() []schema.ColumnDefinition) *Blueprint_GetChangedColumns_Call { - _c.Call.Return(run) - return _c -} - // GetCommands provides a mock function with given fields: func (_m *Blueprint) GetCommands() []*schema.Command { ret := _m.Called() 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() From 4108c22fe9299dc0b3585a179182a85ffbc20dbb Mon Sep 17 00:00:00 2001 From: Bowen Date: Sun, 3 Nov 2024 09:09:38 +0800 Subject: [PATCH 4/6] Remove --- contracts/database/schema/blueprint.go | 2 - contracts/database/schema/column.go | 4 -- contracts/database/schema/grammar.go | 2 - database/schema/blueprint.go | 25 +-------- database/schema/blueprint_test.go | 23 -------- database/schema/column.go | 27 +++------ database/schema/column_test.go | 7 --- database/schema/grammars/postgres.go | 36 +----------- database/schema/grammars/postgres_test.go | 68 ----------------------- 9 files changed, 9 insertions(+), 185 deletions(-) diff --git a/contracts/database/schema/blueprint.go b/contracts/database/schema/blueprint.go index 717e0c272..fcf921ccf 100644 --- a/contracts/database/schema/blueprint.go +++ b/contracts/database/schema/blueprint.go @@ -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. diff --git a/contracts/database/schema/column.go b/contracts/database/schema/column.go index a740e3314..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 a93fa1531..5ea560eda 100644 --- a/contracts/database/schema/grammar.go +++ b/contracts/database/schema/grammar.go @@ -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. diff --git a/database/schema/blueprint.go b/database/schema/blueprint.go index cdd668981..b57f905a9 100644 --- a/database/schema/blueprint.go +++ b/database/schema/blueprint.go @@ -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 } @@ -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: @@ -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, }) } diff --git a/database/schema/blueprint_test.go b/database/schema/blueprint_test.go index ced1a230e..37b619863 100644 --- a/database/schema/blueprint_test.go +++ b/database/schema/blueprint_test.go @@ -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()) diff --git a/database/schema/column.go b/database/schema/column.go index 6f37a1a09..87fdd9f61 100644 --- a/database/schema/column.go +++ b/database/schema/column.go @@ -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 { @@ -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/grammars/postgres.go b/database/schema/grammars/postgres.go index 8128c7f2c..f9c9b96e8 100644 --- a/database/schema/grammars/postgres.go +++ b/database/schema/grammars/postgres.go @@ -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), ",")) } @@ -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())) } @@ -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 { @@ -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" } diff --git a/database/schema/grammars/postgres_test.go b/database/schema/grammars/postgres_test.go index bd9414817..64e052407 100644 --- a/database/schema/grammars/postgres_test.go +++ b/database/schema/grammars/postgres_test.go @@ -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() @@ -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()) @@ -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 @@ -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)", @@ -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'", @@ -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)) @@ -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() From 80bdf3956d5b23c1ab17fe06f5229f1fe73fa638 Mon Sep 17 00:00:00 2001 From: hwbrzzl Date: Sun, 3 Nov 2024 01:10:20 +0000 Subject: [PATCH 5/6] chore: update mocks --- mocks/database/schema/Grammar.go | 47 -------------------------------- 1 file changed, 47 deletions(-) diff --git a/mocks/database/schema/Grammar.go b/mocks/database/schema/Grammar.go index 4edcc1241..8268e022a 100644 --- a/mocks/database/schema/Grammar.go +++ b/mocks/database/schema/Grammar.go @@ -68,53 +68,6 @@ func (_c *Grammar_CompileAdd_Call) RunAndReturn(run func(schema.Blueprint, *sche return _c } -// CompileChange provides a mock function with given fields: blueprint, command -func (_m *Grammar) CompileChange(blueprint schema.Blueprint, command *schema.Command) string { - ret := _m.Called(blueprint, command) - - if len(ret) == 0 { - panic("no return value specified for CompileChange") - } - - var r0 string - if rf, ok := ret.Get(0).(func(schema.Blueprint, *schema.Command) string); ok { - r0 = rf(blueprint, command) - } 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 -// - command *schema.Command -func (_e *Grammar_Expecter) CompileChange(blueprint interface{}, command interface{}) *Grammar_CompileChange_Call { - return &Grammar_CompileChange_Call{Call: _e.mock.On("CompileChange", blueprint, command)} -} - -func (_c *Grammar_CompileChange_Call) Run(run func(blueprint schema.Blueprint, command *schema.Command)) *Grammar_CompileChange_Call { - _c.Call.Run(func(args mock.Arguments) { - run(args[0].(schema.Blueprint), args[1].(*schema.Command)) - }) - 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, *schema.Command) string) *Grammar_CompileChange_Call { - _c.Call.Return(run) - return _c -} - // CompileCreate provides a mock function with given fields: blueprint, query func (_m *Grammar) CompileCreate(blueprint schema.Blueprint, query orm.Query) string { ret := _m.Called(blueprint, query) From 2f5a99fc40cb4592e523556bd316034c4be1859e Mon Sep 17 00:00:00 2001 From: Bowen Date: Sun, 3 Nov 2024 16:12:14 +0800 Subject: [PATCH 6/6] fix lint --- database/schema/grammars/utils.go | 11 ----------- support/docker/docker.go | 2 +- 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/database/schema/grammars/utils.go b/database/schema/grammars/utils.go index 296e24dc7..420916f21 100644 --- a/database/schema/grammars/utils.go +++ b/database/schema/grammars/utils.go @@ -33,17 +33,6 @@ func getColumns(grammar schema.Grammar, blueprint schema.Blueprint) []string { return columns } -func getCommandsByName(commands []*schema.Command, name string) []*schema.Command { - var filteredCommands []*schema.Command - for _, command := range commands { - if command.Name == name { - filteredCommands = append(filteredCommands, command) - } - } - - return filteredCommands -} - func getDefaultValue(def any) string { switch def.(type) { case bool: diff --git a/support/docker/docker.go b/support/docker/docker.go index 796015e45..7d106b122 100644 --- a/support/docker/docker.go +++ b/support/docker/docker.go @@ -15,7 +15,7 @@ const ( TestModelNormal // Switch this value to control the test model. - TestModel = TestModelMinimum + TestModel = TestModelNormal ) type ContainerType string