-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: Oracle db migration (#2015) #2197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from 6 commits
555d828
37310af
f6195eb
89dea5b
871df9d
e40bf12
bd9687c
df77d06
0b4bab6
de141d8
b5435ee
823be58
2a19715
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Name,Age,Email |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
!@#$%^&* |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
some new content |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package migration | |
import ( | ||
"database/sql" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/mock/gomock" | ||
|
@@ -207,3 +208,130 @@ func initializeClickHouseRunMocks(t *testing.T) (*MockClickhouse, *container.Con | |
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure , I will mark it as nil |
||
return mockClickHouse, mockContainer | ||
} | ||
|
||
func TestOracleMigration_RunMigrationSuccess(t *testing.T) { | ||
mockOracle, mockContainer := initializeOracleRunMocks(t) | ||
|
||
ds := Datasource{Oracle: mockOracle} | ||
od := oracleDS{Oracle: mockOracle} | ||
_ = od.apply(&ds) // apply migrator wrapper. | ||
|
||
migrationMap := map[int64]Migrate{ | ||
1: {UP: func(d Datasource) error { | ||
return d.Oracle.Exec(t.Context(), "CREATE TABLE test (id INT)") | ||
}}, | ||
} | ||
|
||
mockOracle.EXPECT().Exec(gomock.Any(), CheckAndCreateOracleMigrationTable).Return(nil) | ||
mockOracle.EXPECT().Select(gomock.Any(), gomock.Any(), getLastOracleGoFrMigration).Return(nil) | ||
mockOracle.EXPECT().Exec(gomock.Any(), "CREATE TABLE test (id INT)").Return(nil) | ||
mockOracle.EXPECT().Exec(gomock.Any(), insertOracleGoFrMigrationRow, int64(1), "UP", gomock.Any(), gomock.Any()).Return(nil) | ||
|
||
Run(migrationMap, mockContainer) | ||
} | ||
|
||
func TestOracleMigration_FailCreateMigrationTable(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests for Oracle Migration should be placed inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do I need to shift only this test -> TestOracleMigration_FailCreateMigrationTable or all test related to Oracle migration in this file to oracle_test.go ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should place all test related to Oracle migration in this file to oracle_test.go |
||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockOracle := NewMockOracle(ctrl) | ||
mockContainer, _ := container.NewMockContainer(t) | ||
mockContainer.Oracle = mockOracle | ||
|
||
ds := Datasource{Oracle: mockOracle} | ||
od := oracleDS{Oracle: mockOracle} | ||
mg := od.apply(&ds) | ||
|
||
mockOracle.EXPECT().Exec(gomock.Any(), CheckAndCreateOracleMigrationTable).Return(sql.ErrConnDone) | ||
|
||
err := mg.checkAndCreateMigrationTable(mockContainer) | ||
assert.Equal(t, sql.ErrConnDone, err) | ||
} | ||
|
||
func TestOracleMigration_GetLastMigration_ReturnsZeroOnError(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockOracle := NewMockOracle(ctrl) | ||
mockContainer, _ := container.NewMockContainer(t) | ||
mockContainer.Oracle = mockOracle | ||
|
||
ds := Datasource{Oracle: mockOracle} | ||
od := oracleDS{Oracle: mockOracle} | ||
mg := od.apply(&ds) | ||
|
||
mockOracle.EXPECT().Select(gomock.Any(), gomock.Any(), getLastOracleGoFrMigration).Return(sql.ErrConnDone) | ||
|
||
lastMigration := mg.getLastMigration(mockContainer) | ||
assert.Equal(t, int64(0), lastMigration) | ||
} | ||
|
||
func TestOracleMigration_CommitMigration(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockOracle := NewMockOracle(ctrl) | ||
mockContainer, _ := container.NewMockContainer(t) | ||
mockContainer.Oracle = mockOracle | ||
|
||
ds := Datasource{Oracle: mockOracle} | ||
od := oracleDS{Oracle: mockOracle} | ||
mg := od.apply(&ds) | ||
|
||
td := transactionData{ | ||
StartTime: time.Now(), | ||
MigrationNumber: 42, | ||
} | ||
|
||
mockOracle.EXPECT(). | ||
Exec(gomock.Any(), insertOracleGoFrMigrationRow, | ||
td.MigrationNumber, "UP", td.StartTime, gomock.Any()). | ||
Return(nil) | ||
|
||
err := mg.commitMigration(mockContainer, td) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestOracleMigration_BeginTransaction_Logs(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockOracle := NewMockOracle(ctrl) | ||
mockContainer, _ := container.NewMockContainer(t) | ||
mockContainer.Logger = logging.NewLogger(logging.DEBUG) | ||
mockContainer.Oracle = mockOracle | ||
|
||
ds := Datasource{Oracle: mockOracle} | ||
od := oracleDS{Oracle: mockOracle} | ||
mg := od.apply(&ds) | ||
|
||
// Capture logs or just call method and rely on it not panicking. | ||
mg.beginTransaction(mockContainer) | ||
} | ||
|
||
func initializeOracleRunMocks(t *testing.T) (*MockOracle, *container.Container) { | ||
t.Helper() | ||
|
||
mockOracle := NewMockOracle(gomock.NewController(t)) | ||
mockContainer, _ := container.NewMockContainer(t) | ||
|
||
// Disable all other datasources by setting to nil. | ||
mockContainer.SQL = nil | ||
mockContainer.Redis = nil | ||
mockContainer.Mongo = nil | ||
mockContainer.Cassandra = nil | ||
mockContainer.PubSub = nil | ||
mockContainer.ArangoDB = nil | ||
mockContainer.SurrealDB = nil | ||
mockContainer.DGraph = nil | ||
mockContainer.Elasticsearch = nil | ||
mockContainer.OpenTSDB = nil | ||
mockContainer.ScyllaDB = nil | ||
mockContainer.Clickhouse = nil | ||
|
||
// Initialize Oracle mock and Logger. | ||
mockContainer.Oracle = mockOracle | ||
mockContainer.Logger = logging.NewMockLogger(logging.DEBUG) | ||
|
||
return mockOracle, mockContainer | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this method needed for migrations?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the
HealthCheck(ctx context.Context) (any, error)
method to theOracle
interface in the migration package to align it with theOracleDB
interface defined in the container package. Without this method, the*MockOracle
generated for testing cannot be used as acontainer.OracleDB
value, causing the compiler error:"cannot use mockOracle (variable of type *MockOracle) as container.OracleDB value in assignment: *MockOracle does not implement container.OracleDB (missing method HealthCheck)".
As I am using
in some functions, it takes the reference from
pkg/gofr/container/datasource.go
, which requires theHealthCheck
method for compatibility. To resolve this, I have added theHealthCheck
method inpkg/gofr/migration/interface.go
.By adding the
HealthCheck
method, the mock satisfies the interface requirements, ensuring type compatibility and eliminating this assignment error.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Samar1110 I agree but if you notice many of the interfaces like SQL, Redis, Mongo, ArrangoDB etc all have HealthCheck() method not implemented yet they align with their interface definition in container.
You can see how their test have been written or implemented.