From 7674cb57948aa7efa3f36d4bf044d2beee9f00f7 Mon Sep 17 00:00:00 2001 From: Wenbo Han Date: Fri, 8 Nov 2024 10:27:17 +0800 Subject: [PATCH] feat: [#280] Add Foreign methods (#715) --- contracts/database/schema/blueprint.go | 4 + contracts/database/schema/grammar.go | 2 + contracts/database/schema/index.go | 13 + database/schema/blueprint.go | 12 + database/schema/constants/constants.go | 1 + database/schema/grammars/postgres.go | 17 + database/schema/grammars/postgres_test.go | 48 ++ database/schema/index.go | 70 +++ database/schema/schema_test.go | 28 + mocks/database/schema/Blueprint.go | 106 ++++ mocks/database/schema/ForeignKeyDefinition.go | 521 ++++++++++++++++++ mocks/database/schema/Grammar.go | 47 ++ 12 files changed, 869 insertions(+) create mode 100644 mocks/database/schema/ForeignKeyDefinition.go diff --git a/contracts/database/schema/blueprint.go b/contracts/database/schema/blueprint.go index 0e081caec..433b4e575 100644 --- a/contracts/database/schema/blueprint.go +++ b/contracts/database/schema/blueprint.go @@ -11,10 +11,14 @@ type Blueprint interface { Create() // DropIfExists Indicate that the table should be dropped if it exists. DropIfExists() + // Foreign Specify a foreign key for the table. + Foreign(column ...string) ForeignKeyDefinition // GetAddedColumns Get the added columns. GetAddedColumns() []ColumnDefinition // GetCommands Get the commands. GetCommands() []*Command + // GetPrefix Get the table prefix. + GetPrefix() string // 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 34722fc85..087c71343 100644 --- a/contracts/database/schema/grammar.go +++ b/contracts/database/schema/grammar.go @@ -15,6 +15,8 @@ type Grammar interface { CompileDropAllViews(views []string) string // CompileDropIfExists Compile a drop table (if exists) command. CompileDropIfExists(blueprint Blueprint) string + // CompileForeign Compile a foreign key command. + CompileForeign(blueprint Blueprint, command *Command) string // CompileIndex Compile a plain index key command. CompileIndex(blueprint Blueprint, command *Command) string // CompileIndexes Compile the query to determine the indexes. diff --git a/contracts/database/schema/index.go b/contracts/database/schema/index.go index 5740e1809..bb0c12903 100644 --- a/contracts/database/schema/index.go +++ b/contracts/database/schema/index.go @@ -1,5 +1,18 @@ package schema +type ForeignKeyDefinition interface { + CascadeOnDelete() ForeignKeyDefinition + CascadeOnUpdate() ForeignKeyDefinition + On(table string) ForeignKeyDefinition + Name(name string) ForeignKeyDefinition + NoActionOnDelete() ForeignKeyDefinition + NoActionOnUpdate() ForeignKeyDefinition + NullOnDelete() ForeignKeyDefinition + References(columns ...string) ForeignKeyDefinition + RestrictOnDelete() ForeignKeyDefinition + RestrictOnUpdate() ForeignKeyDefinition +} + type IndexDefinition interface { Algorithm(algorithm string) IndexDefinition Name(name string) IndexDefinition diff --git a/database/schema/blueprint.go b/database/schema/blueprint.go index 7d9400932..fc2e8d6a4 100644 --- a/database/schema/blueprint.go +++ b/database/schema/blueprint.go @@ -60,6 +60,12 @@ func (r *Blueprint) DropIfExists() { }) } +func (r *Blueprint) Foreign(column ...string) schema.ForeignKeyDefinition { + command := r.indexCommand(constants.CommandForeign, column) + + return NewForeignKeyDefinition(command) +} + func (r *Blueprint) GetAddedColumns() []schema.ColumnDefinition { var columns []schema.ColumnDefinition for _, column := range r.columns { @@ -73,6 +79,10 @@ func (r *Blueprint) GetCommands() []*schema.Command { return r.commands } +func (r *Blueprint) GetPrefix() string { + return r.prefix +} + func (r *Blueprint) GetTableName() string { // TODO Add schema for Postgres return r.prefix + r.table @@ -149,6 +159,8 @@ func (r *Blueprint) ToSql(grammar schema.Grammar) []string { statements = append(statements, grammar.CompileCreate(r)) case constants.CommandDropIfExists: statements = append(statements, grammar.CompileDropIfExists(r)) + case constants.CommandForeign: + statements = append(statements, grammar.CompileForeign(r, command)) case constants.CommandIndex: statements = append(statements, grammar.CompileIndex(r, command)) case constants.CommandPrimary: diff --git a/database/schema/constants/constants.go b/database/schema/constants/constants.go index b3ea31bae..30b5d5ec4 100644 --- a/database/schema/constants/constants.go +++ b/database/schema/constants/constants.go @@ -5,6 +5,7 @@ const ( CommandComment = "comment" CommandCreate = "create" CommandDropIfExists = "dropIfExists" + CommandForeign = "foreign" CommandIndex = "index" CommandPrimary = "primary" DefaultStringLength = 255 diff --git a/database/schema/grammars/postgres.go b/database/schema/grammars/postgres.go index cb15b78aa..de935975a 100644 --- a/database/schema/grammars/postgres.go +++ b/database/schema/grammars/postgres.go @@ -57,6 +57,23 @@ func (r *Postgres) CompileDropIfExists(blueprint schema.Blueprint) string { return fmt.Sprintf("drop table if exists %s", blueprint.GetTableName()) } +func (r *Postgres) CompileForeign(blueprint schema.Blueprint, command *schema.Command) string { + sql := fmt.Sprintf("alter table %s add constraint %s foreign key (%s) references %s (%s)", + blueprint.GetTableName(), + command.Index, + strings.Join(command.Columns, ", "), + fmt.Sprintf("%s%s", blueprint.GetPrefix(), command.On), + strings.Join(command.References, ", ")) + if command.OnDelete != "" { + sql += " on delete " + command.OnDelete + } + if command.OnUpdate != "" { + sql += " on update " + command.OnUpdate + } + + return sql +} + func (r *Postgres) CompileIndex(blueprint schema.Blueprint, command *schema.Command) string { var algorithm string if command.Algorithm != "" { diff --git a/database/schema/grammars/postgres_test.go b/database/schema/grammars/postgres_test.go index 8325c90bd..485942b97 100644 --- a/database/schema/grammars/postgres_test.go +++ b/database/schema/grammars/postgres_test.go @@ -92,6 +92,54 @@ func (s *PostgresSuite) TestCompileDropIfExists() { s.Equal("drop table if exists users", s.grammar.CompileDropIfExists(mockBlueprint)) } +func (s *PostgresSuite) TestCompileForeign() { + var mockBlueprint *mocksschema.Blueprint + + beforeEach := func() { + mockBlueprint = mocksschema.NewBlueprint(s.T()) + mockBlueprint.EXPECT().GetPrefix().Return("goravel_").Once() + mockBlueprint.EXPECT().GetTableName().Return("users").Once() + } + + tests := []struct { + name string + command *contractsschema.Command + expectSql string + }{ + { + name: "with on delete and on update", + command: &contractsschema.Command{ + Index: "fk_users_role_id", + Columns: []string{"role_id"}, + On: "roles", + References: []string{"id"}, + OnDelete: "cascade", + OnUpdate: "restrict", + }, + expectSql: "alter table users add constraint fk_users_role_id foreign key (role_id) references goravel_roles (id) on delete cascade on update restrict", + }, + { + name: "without on delete and on update", + command: &contractsschema.Command{ + Index: "fk_users_role_id", + Columns: []string{"role_id"}, + On: "roles", + References: []string{"id"}, + }, + expectSql: "alter table users add constraint fk_users_role_id foreign key (role_id) references goravel_roles (id)", + }, + } + + for _, test := range tests { + s.Run(test.name, func() { + beforeEach() + + sql := s.grammar.CompileForeign(mockBlueprint, test.command) + s.Equal(test.expectSql, sql) + }) + } +} + func (s *PostgresSuite) TestEscapeNames() { // SingleName names := []string{"username"} diff --git a/database/schema/index.go b/database/schema/index.go index 3f7b75761..4cda02547 100644 --- a/database/schema/index.go +++ b/database/schema/index.go @@ -4,6 +4,76 @@ import ( "github.com/goravel/framework/contracts/database/schema" ) +type ForeignKeyDefinition struct { + command *schema.Command +} + +func NewForeignKeyDefinition(command *schema.Command) schema.ForeignKeyDefinition { + return &ForeignKeyDefinition{ + command: command, + } +} + +func (f *ForeignKeyDefinition) CascadeOnDelete() schema.ForeignKeyDefinition { + f.command.OnDelete = "cascade" + + return f +} + +func (f *ForeignKeyDefinition) CascadeOnUpdate() schema.ForeignKeyDefinition { + f.command.OnUpdate = "cascade" + + return f +} + +func (f *ForeignKeyDefinition) On(table string) schema.ForeignKeyDefinition { + f.command.On = table + + return f +} + +func (f *ForeignKeyDefinition) Name(name string) schema.ForeignKeyDefinition { + f.command.Index = name + + return f +} + +func (f *ForeignKeyDefinition) NoActionOnDelete() schema.ForeignKeyDefinition { + f.command.OnDelete = "no action" + + return f +} + +func (f *ForeignKeyDefinition) NoActionOnUpdate() schema.ForeignKeyDefinition { + f.command.OnUpdate = "no action" + + return f +} + +func (f *ForeignKeyDefinition) NullOnDelete() schema.ForeignKeyDefinition { + f.command.OnDelete = "set null" + + return f +} + +func (f *ForeignKeyDefinition) References(columns ...string) schema.ForeignKeyDefinition { + f.command.References = columns + + return f +} + +func (f *ForeignKeyDefinition) RestrictOnDelete() schema.ForeignKeyDefinition { + f.command.OnDelete = "restrict" + + return f +} + +func (f *ForeignKeyDefinition) RestrictOnUpdate() schema.ForeignKeyDefinition { + f.command.OnUpdate = "restrict" + + return f +} + type IndexDefinition struct { command *schema.Command } diff --git a/database/schema/schema_test.go b/database/schema/schema_test.go index 793c71fdf..54252038d 100644 --- a/database/schema/schema_test.go +++ b/database/schema/schema_test.go @@ -87,6 +87,34 @@ func (s *SchemaSuite) TestDropAllViews() { } +func (s *SchemaSuite) TestForeign() { + for driver, testQuery := range s.driverToTestQuery { + s.Run(driver.String(), func() { + schema := GetTestSchema(testQuery, s.driverToTestQuery) + table1 := "foreign1" + + err := schema.Create(table1, func(table contractsschema.Blueprint) { + table.ID() + table.String("name") + }) + + s.Require().Nil(err) + s.Require().True(schema.HasTable(table1)) + + table2 := "foreign2" + err = schema.Create(table2, func(table contractsschema.Blueprint) { + table.ID() + table.String("name") + table.Integer("foreign1_id") + table.Foreign("foreign1_id").References("id").On(table1) + }) + + s.Require().Nil(err) + s.Require().True(schema.HasTable(table2)) + }) + } +} + func (s *SchemaSuite) TestPrimary() { for driver, testQuery := range s.driverToTestQuery { s.Run(driver.String(), func() { diff --git a/mocks/database/schema/Blueprint.go b/mocks/database/schema/Blueprint.go index 6b56b0935..06ec0733c 100644 --- a/mocks/database/schema/Blueprint.go +++ b/mocks/database/schema/Blueprint.go @@ -132,6 +132,67 @@ func (_c *Blueprint_DropIfExists_Call) RunAndReturn(run func()) *Blueprint_DropI return _c } +// Foreign provides a mock function with given fields: column +func (_m *Blueprint) Foreign(column ...string) schema.ForeignKeyDefinition { + _va := make([]interface{}, len(column)) + for _i := range column { + _va[_i] = column[_i] + } + var _ca []interface{} + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + if len(ret) == 0 { + panic("no return value specified for Foreign") + } + + var r0 schema.ForeignKeyDefinition + if rf, ok := ret.Get(0).(func(...string) schema.ForeignKeyDefinition); ok { + r0 = rf(column...) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(schema.ForeignKeyDefinition) + } + } + + return r0 +} + +// Blueprint_Foreign_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Foreign' +type Blueprint_Foreign_Call struct { + *mock.Call +} + +// Foreign is a helper method to define mock.On call +// - column ...string +func (_e *Blueprint_Expecter) Foreign(column ...interface{}) *Blueprint_Foreign_Call { + return &Blueprint_Foreign_Call{Call: _e.mock.On("Foreign", + append([]interface{}{}, column...)...)} +} + +func (_c *Blueprint_Foreign_Call) Run(run func(column ...string)) *Blueprint_Foreign_Call { + _c.Call.Run(func(args mock.Arguments) { + variadicArgs := make([]string, len(args)-0) + for i, a := range args[0:] { + if a != nil { + variadicArgs[i] = a.(string) + } + } + run(variadicArgs...) + }) + return _c +} + +func (_c *Blueprint_Foreign_Call) Return(_a0 schema.ForeignKeyDefinition) *Blueprint_Foreign_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *Blueprint_Foreign_Call) RunAndReturn(run func(...string) schema.ForeignKeyDefinition) *Blueprint_Foreign_Call { + _c.Call.Return(run) + return _c +} + // GetAddedColumns provides a mock function with given fields: func (_m *Blueprint) GetAddedColumns() []schema.ColumnDefinition { ret := _m.Called() @@ -226,6 +287,51 @@ func (_c *Blueprint_GetCommands_Call) RunAndReturn(run func() []*schema.Command) return _c } +// GetPrefix provides a mock function with given fields: +func (_m *Blueprint) GetPrefix() string { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for GetPrefix") + } + + var r0 string + if rf, ok := ret.Get(0).(func() string); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(string) + } + + return r0 +} + +// Blueprint_GetPrefix_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetPrefix' +type Blueprint_GetPrefix_Call struct { + *mock.Call +} + +// GetPrefix is a helper method to define mock.On call +func (_e *Blueprint_Expecter) GetPrefix() *Blueprint_GetPrefix_Call { + return &Blueprint_GetPrefix_Call{Call: _e.mock.On("GetPrefix")} +} + +func (_c *Blueprint_GetPrefix_Call) Run(run func()) *Blueprint_GetPrefix_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *Blueprint_GetPrefix_Call) Return(_a0 string) *Blueprint_GetPrefix_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *Blueprint_GetPrefix_Call) RunAndReturn(run func() string) *Blueprint_GetPrefix_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/ForeignKeyDefinition.go b/mocks/database/schema/ForeignKeyDefinition.go new file mode 100644 index 000000000..1ddd808da --- /dev/null +++ b/mocks/database/schema/ForeignKeyDefinition.go @@ -0,0 +1,521 @@ +// Code generated by mockery. DO NOT EDIT. + +package schema + +import ( + schema "github.com/goravel/framework/contracts/database/schema" + mock "github.com/stretchr/testify/mock" +) + +// ForeignKeyDefinition is an autogenerated mock type for the ForeignKeyDefinition type +type ForeignKeyDefinition struct { + mock.Mock +} + +type ForeignKeyDefinition_Expecter struct { + mock *mock.Mock +} + +func (_m *ForeignKeyDefinition) EXPECT() *ForeignKeyDefinition_Expecter { + return &ForeignKeyDefinition_Expecter{mock: &_m.Mock} +} + +// CascadeOnDelete provides a mock function with given fields: +func (_m *ForeignKeyDefinition) CascadeOnDelete() schema.ForeignKeyDefinition { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for CascadeOnDelete") + } + + var r0 schema.ForeignKeyDefinition + if rf, ok := ret.Get(0).(func() schema.ForeignKeyDefinition); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(schema.ForeignKeyDefinition) + } + } + + return r0 +} + +// ForeignKeyDefinition_CascadeOnDelete_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CascadeOnDelete' +type ForeignKeyDefinition_CascadeOnDelete_Call struct { + *mock.Call +} + +// CascadeOnDelete is a helper method to define mock.On call +func (_e *ForeignKeyDefinition_Expecter) CascadeOnDelete() *ForeignKeyDefinition_CascadeOnDelete_Call { + return &ForeignKeyDefinition_CascadeOnDelete_Call{Call: _e.mock.On("CascadeOnDelete")} +} + +func (_c *ForeignKeyDefinition_CascadeOnDelete_Call) Run(run func()) *ForeignKeyDefinition_CascadeOnDelete_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *ForeignKeyDefinition_CascadeOnDelete_Call) Return(_a0 schema.ForeignKeyDefinition) *ForeignKeyDefinition_CascadeOnDelete_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *ForeignKeyDefinition_CascadeOnDelete_Call) RunAndReturn(run func() schema.ForeignKeyDefinition) *ForeignKeyDefinition_CascadeOnDelete_Call { + _c.Call.Return(run) + return _c +} + +// CascadeOnUpdate provides a mock function with given fields: +func (_m *ForeignKeyDefinition) CascadeOnUpdate() schema.ForeignKeyDefinition { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for CascadeOnUpdate") + } + + var r0 schema.ForeignKeyDefinition + if rf, ok := ret.Get(0).(func() schema.ForeignKeyDefinition); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(schema.ForeignKeyDefinition) + } + } + + return r0 +} + +// ForeignKeyDefinition_CascadeOnUpdate_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CascadeOnUpdate' +type ForeignKeyDefinition_CascadeOnUpdate_Call struct { + *mock.Call +} + +// CascadeOnUpdate is a helper method to define mock.On call +func (_e *ForeignKeyDefinition_Expecter) CascadeOnUpdate() *ForeignKeyDefinition_CascadeOnUpdate_Call { + return &ForeignKeyDefinition_CascadeOnUpdate_Call{Call: _e.mock.On("CascadeOnUpdate")} +} + +func (_c *ForeignKeyDefinition_CascadeOnUpdate_Call) Run(run func()) *ForeignKeyDefinition_CascadeOnUpdate_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *ForeignKeyDefinition_CascadeOnUpdate_Call) Return(_a0 schema.ForeignKeyDefinition) *ForeignKeyDefinition_CascadeOnUpdate_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *ForeignKeyDefinition_CascadeOnUpdate_Call) RunAndReturn(run func() schema.ForeignKeyDefinition) *ForeignKeyDefinition_CascadeOnUpdate_Call { + _c.Call.Return(run) + return _c +} + +// Name provides a mock function with given fields: name +func (_m *ForeignKeyDefinition) Name(name string) schema.ForeignKeyDefinition { + ret := _m.Called(name) + + if len(ret) == 0 { + panic("no return value specified for Name") + } + + var r0 schema.ForeignKeyDefinition + if rf, ok := ret.Get(0).(func(string) schema.ForeignKeyDefinition); ok { + r0 = rf(name) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(schema.ForeignKeyDefinition) + } + } + + return r0 +} + +// ForeignKeyDefinition_Name_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Name' +type ForeignKeyDefinition_Name_Call struct { + *mock.Call +} + +// Name is a helper method to define mock.On call +// - name string +func (_e *ForeignKeyDefinition_Expecter) Name(name interface{}) *ForeignKeyDefinition_Name_Call { + return &ForeignKeyDefinition_Name_Call{Call: _e.mock.On("Name", name)} +} + +func (_c *ForeignKeyDefinition_Name_Call) Run(run func(name string)) *ForeignKeyDefinition_Name_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string)) + }) + return _c +} + +func (_c *ForeignKeyDefinition_Name_Call) Return(_a0 schema.ForeignKeyDefinition) *ForeignKeyDefinition_Name_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *ForeignKeyDefinition_Name_Call) RunAndReturn(run func(string) schema.ForeignKeyDefinition) *ForeignKeyDefinition_Name_Call { + _c.Call.Return(run) + return _c +} + +// NoActionOnDelete provides a mock function with given fields: +func (_m *ForeignKeyDefinition) NoActionOnDelete() schema.ForeignKeyDefinition { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for NoActionOnDelete") + } + + var r0 schema.ForeignKeyDefinition + if rf, ok := ret.Get(0).(func() schema.ForeignKeyDefinition); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(schema.ForeignKeyDefinition) + } + } + + return r0 +} + +// ForeignKeyDefinition_NoActionOnDelete_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'NoActionOnDelete' +type ForeignKeyDefinition_NoActionOnDelete_Call struct { + *mock.Call +} + +// NoActionOnDelete is a helper method to define mock.On call +func (_e *ForeignKeyDefinition_Expecter) NoActionOnDelete() *ForeignKeyDefinition_NoActionOnDelete_Call { + return &ForeignKeyDefinition_NoActionOnDelete_Call{Call: _e.mock.On("NoActionOnDelete")} +} + +func (_c *ForeignKeyDefinition_NoActionOnDelete_Call) Run(run func()) *ForeignKeyDefinition_NoActionOnDelete_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *ForeignKeyDefinition_NoActionOnDelete_Call) Return(_a0 schema.ForeignKeyDefinition) *ForeignKeyDefinition_NoActionOnDelete_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *ForeignKeyDefinition_NoActionOnDelete_Call) RunAndReturn(run func() schema.ForeignKeyDefinition) *ForeignKeyDefinition_NoActionOnDelete_Call { + _c.Call.Return(run) + return _c +} + +// NoActionOnUpdate provides a mock function with given fields: +func (_m *ForeignKeyDefinition) NoActionOnUpdate() schema.ForeignKeyDefinition { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for NoActionOnUpdate") + } + + var r0 schema.ForeignKeyDefinition + if rf, ok := ret.Get(0).(func() schema.ForeignKeyDefinition); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(schema.ForeignKeyDefinition) + } + } + + return r0 +} + +// ForeignKeyDefinition_NoActionOnUpdate_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'NoActionOnUpdate' +type ForeignKeyDefinition_NoActionOnUpdate_Call struct { + *mock.Call +} + +// NoActionOnUpdate is a helper method to define mock.On call +func (_e *ForeignKeyDefinition_Expecter) NoActionOnUpdate() *ForeignKeyDefinition_NoActionOnUpdate_Call { + return &ForeignKeyDefinition_NoActionOnUpdate_Call{Call: _e.mock.On("NoActionOnUpdate")} +} + +func (_c *ForeignKeyDefinition_NoActionOnUpdate_Call) Run(run func()) *ForeignKeyDefinition_NoActionOnUpdate_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *ForeignKeyDefinition_NoActionOnUpdate_Call) Return(_a0 schema.ForeignKeyDefinition) *ForeignKeyDefinition_NoActionOnUpdate_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *ForeignKeyDefinition_NoActionOnUpdate_Call) RunAndReturn(run func() schema.ForeignKeyDefinition) *ForeignKeyDefinition_NoActionOnUpdate_Call { + _c.Call.Return(run) + return _c +} + +// NullOnDelete provides a mock function with given fields: +func (_m *ForeignKeyDefinition) NullOnDelete() schema.ForeignKeyDefinition { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for NullOnDelete") + } + + var r0 schema.ForeignKeyDefinition + if rf, ok := ret.Get(0).(func() schema.ForeignKeyDefinition); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(schema.ForeignKeyDefinition) + } + } + + return r0 +} + +// ForeignKeyDefinition_NullOnDelete_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'NullOnDelete' +type ForeignKeyDefinition_NullOnDelete_Call struct { + *mock.Call +} + +// NullOnDelete is a helper method to define mock.On call +func (_e *ForeignKeyDefinition_Expecter) NullOnDelete() *ForeignKeyDefinition_NullOnDelete_Call { + return &ForeignKeyDefinition_NullOnDelete_Call{Call: _e.mock.On("NullOnDelete")} +} + +func (_c *ForeignKeyDefinition_NullOnDelete_Call) Run(run func()) *ForeignKeyDefinition_NullOnDelete_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *ForeignKeyDefinition_NullOnDelete_Call) Return(_a0 schema.ForeignKeyDefinition) *ForeignKeyDefinition_NullOnDelete_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *ForeignKeyDefinition_NullOnDelete_Call) RunAndReturn(run func() schema.ForeignKeyDefinition) *ForeignKeyDefinition_NullOnDelete_Call { + _c.Call.Return(run) + return _c +} + +// On provides a mock function with given fields: table +func (_m *ForeignKeyDefinition) On(table string) schema.ForeignKeyDefinition { + ret := _m.Called(table) + + if len(ret) == 0 { + panic("no return value specified for On") + } + + var r0 schema.ForeignKeyDefinition + if rf, ok := ret.Get(0).(func(string) schema.ForeignKeyDefinition); ok { + r0 = rf(table) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(schema.ForeignKeyDefinition) + } + } + + return r0 +} + +// ForeignKeyDefinition_On_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'On' +type ForeignKeyDefinition_On_Call struct { + *mock.Call +} + +// On is a helper method to define mock.On call +// - table string +func (_e *ForeignKeyDefinition_Expecter) On(table interface{}) *ForeignKeyDefinition_On_Call { + return &ForeignKeyDefinition_On_Call{Call: _e.mock.On("On", table)} +} + +func (_c *ForeignKeyDefinition_On_Call) Run(run func(table string)) *ForeignKeyDefinition_On_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(string)) + }) + return _c +} + +func (_c *ForeignKeyDefinition_On_Call) Return(_a0 schema.ForeignKeyDefinition) *ForeignKeyDefinition_On_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *ForeignKeyDefinition_On_Call) RunAndReturn(run func(string) schema.ForeignKeyDefinition) *ForeignKeyDefinition_On_Call { + _c.Call.Return(run) + return _c +} + +// References provides a mock function with given fields: columns +func (_m *ForeignKeyDefinition) References(columns ...string) schema.ForeignKeyDefinition { + _va := make([]interface{}, len(columns)) + for _i := range columns { + _va[_i] = columns[_i] + } + var _ca []interface{} + _ca = append(_ca, _va...) + ret := _m.Called(_ca...) + + if len(ret) == 0 { + panic("no return value specified for References") + } + + var r0 schema.ForeignKeyDefinition + if rf, ok := ret.Get(0).(func(...string) schema.ForeignKeyDefinition); ok { + r0 = rf(columns...) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(schema.ForeignKeyDefinition) + } + } + + return r0 +} + +// ForeignKeyDefinition_References_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'References' +type ForeignKeyDefinition_References_Call struct { + *mock.Call +} + +// References is a helper method to define mock.On call +// - columns ...string +func (_e *ForeignKeyDefinition_Expecter) References(columns ...interface{}) *ForeignKeyDefinition_References_Call { + return &ForeignKeyDefinition_References_Call{Call: _e.mock.On("References", + append([]interface{}{}, columns...)...)} +} + +func (_c *ForeignKeyDefinition_References_Call) Run(run func(columns ...string)) *ForeignKeyDefinition_References_Call { + _c.Call.Run(func(args mock.Arguments) { + variadicArgs := make([]string, len(args)-0) + for i, a := range args[0:] { + if a != nil { + variadicArgs[i] = a.(string) + } + } + run(variadicArgs...) + }) + return _c +} + +func (_c *ForeignKeyDefinition_References_Call) Return(_a0 schema.ForeignKeyDefinition) *ForeignKeyDefinition_References_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *ForeignKeyDefinition_References_Call) RunAndReturn(run func(...string) schema.ForeignKeyDefinition) *ForeignKeyDefinition_References_Call { + _c.Call.Return(run) + return _c +} + +// RestrictOnDelete provides a mock function with given fields: +func (_m *ForeignKeyDefinition) RestrictOnDelete() schema.ForeignKeyDefinition { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for RestrictOnDelete") + } + + var r0 schema.ForeignKeyDefinition + if rf, ok := ret.Get(0).(func() schema.ForeignKeyDefinition); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(schema.ForeignKeyDefinition) + } + } + + return r0 +} + +// ForeignKeyDefinition_RestrictOnDelete_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'RestrictOnDelete' +type ForeignKeyDefinition_RestrictOnDelete_Call struct { + *mock.Call +} + +// RestrictOnDelete is a helper method to define mock.On call +func (_e *ForeignKeyDefinition_Expecter) RestrictOnDelete() *ForeignKeyDefinition_RestrictOnDelete_Call { + return &ForeignKeyDefinition_RestrictOnDelete_Call{Call: _e.mock.On("RestrictOnDelete")} +} + +func (_c *ForeignKeyDefinition_RestrictOnDelete_Call) Run(run func()) *ForeignKeyDefinition_RestrictOnDelete_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *ForeignKeyDefinition_RestrictOnDelete_Call) Return(_a0 schema.ForeignKeyDefinition) *ForeignKeyDefinition_RestrictOnDelete_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *ForeignKeyDefinition_RestrictOnDelete_Call) RunAndReturn(run func() schema.ForeignKeyDefinition) *ForeignKeyDefinition_RestrictOnDelete_Call { + _c.Call.Return(run) + return _c +} + +// RestrictOnUpdate provides a mock function with given fields: +func (_m *ForeignKeyDefinition) RestrictOnUpdate() schema.ForeignKeyDefinition { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for RestrictOnUpdate") + } + + var r0 schema.ForeignKeyDefinition + if rf, ok := ret.Get(0).(func() schema.ForeignKeyDefinition); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(schema.ForeignKeyDefinition) + } + } + + return r0 +} + +// ForeignKeyDefinition_RestrictOnUpdate_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'RestrictOnUpdate' +type ForeignKeyDefinition_RestrictOnUpdate_Call struct { + *mock.Call +} + +// RestrictOnUpdate is a helper method to define mock.On call +func (_e *ForeignKeyDefinition_Expecter) RestrictOnUpdate() *ForeignKeyDefinition_RestrictOnUpdate_Call { + return &ForeignKeyDefinition_RestrictOnUpdate_Call{Call: _e.mock.On("RestrictOnUpdate")} +} + +func (_c *ForeignKeyDefinition_RestrictOnUpdate_Call) Run(run func()) *ForeignKeyDefinition_RestrictOnUpdate_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *ForeignKeyDefinition_RestrictOnUpdate_Call) Return(_a0 schema.ForeignKeyDefinition) *ForeignKeyDefinition_RestrictOnUpdate_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *ForeignKeyDefinition_RestrictOnUpdate_Call) RunAndReturn(run func() schema.ForeignKeyDefinition) *ForeignKeyDefinition_RestrictOnUpdate_Call { + _c.Call.Return(run) + return _c +} + +// NewForeignKeyDefinition creates a new instance of ForeignKeyDefinition. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewForeignKeyDefinition(t interface { + mock.TestingT + Cleanup(func()) +}) *ForeignKeyDefinition { + mock := &ForeignKeyDefinition{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mocks/database/schema/Grammar.go b/mocks/database/schema/Grammar.go index dc5fd1373..85ef66cb5 100644 --- a/mocks/database/schema/Grammar.go +++ b/mocks/database/schema/Grammar.go @@ -343,6 +343,53 @@ func (_c *Grammar_CompileDropIfExists_Call) RunAndReturn(run func(schema.Bluepri return _c } +// CompileForeign provides a mock function with given fields: blueprint, command +func (_m *Grammar) CompileForeign(blueprint schema.Blueprint, command *schema.Command) string { + ret := _m.Called(blueprint, command) + + if len(ret) == 0 { + panic("no return value specified for CompileForeign") + } + + 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_CompileForeign_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CompileForeign' +type Grammar_CompileForeign_Call struct { + *mock.Call +} + +// CompileForeign is a helper method to define mock.On call +// - blueprint schema.Blueprint +// - command *schema.Command +func (_e *Grammar_Expecter) CompileForeign(blueprint interface{}, command interface{}) *Grammar_CompileForeign_Call { + return &Grammar_CompileForeign_Call{Call: _e.mock.On("CompileForeign", blueprint, command)} +} + +func (_c *Grammar_CompileForeign_Call) Run(run func(blueprint schema.Blueprint, command *schema.Command)) *Grammar_CompileForeign_Call { + _c.Call.Run(func(args mock.Arguments) { + run(args[0].(schema.Blueprint), args[1].(*schema.Command)) + }) + return _c +} + +func (_c *Grammar_CompileForeign_Call) Return(_a0 string) *Grammar_CompileForeign_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *Grammar_CompileForeign_Call) RunAndReturn(run func(schema.Blueprint, *schema.Command) string) *Grammar_CompileForeign_Call { + _c.Call.Return(run) + return _c +} + // CompileIndex provides a mock function with given fields: blueprint, command func (_m *Grammar) CompileIndex(blueprint schema.Blueprint, command *schema.Command) string { ret := _m.Called(blueprint, command)