diff --git a/.mockery.yml b/.mockery.yml index 802ff335cf..717506310b 100644 --- a/.mockery.yml +++ b/.mockery.yml @@ -50,7 +50,7 @@ packages: github.com/nucleuscloud/neosync/backend/internal/authmgmt/auth0: interfaces: Auth0MgmtClientInterface: - github.com/nucleuscloud/neosync/backend/internal/sqlconnect: + github.com/nucleuscloud/neosync/backend/pkg/sqlconnect: interfaces: SqlConnector: SqlDbContainer: diff --git a/backend/internal/cmds/mgmt/serve/connect/cmd.go b/backend/internal/cmds/mgmt/serve/connect/cmd.go index 212a7df0dc..2d35284cc8 100644 --- a/backend/internal/cmds/mgmt/serve/connect/cmd.go +++ b/backend/internal/cmds/mgmt/serve/connect/cmd.go @@ -32,8 +32,8 @@ import ( logging_interceptor "github.com/nucleuscloud/neosync/backend/internal/connect/interceptors/logging" neosynclogger "github.com/nucleuscloud/neosync/backend/internal/logger" "github.com/nucleuscloud/neosync/backend/internal/nucleusdb" - "github.com/nucleuscloud/neosync/backend/internal/sqlconnect" clientmanager "github.com/nucleuscloud/neosync/backend/internal/temporal/client-manager" + "github.com/nucleuscloud/neosync/backend/pkg/sqlconnect" v1alpha1_apikeyservice "github.com/nucleuscloud/neosync/backend/services/mgmt/v1alpha1/api-key-service" v1alpha1_authservice "github.com/nucleuscloud/neosync/backend/services/mgmt/v1alpha1/auth-service" v1alpha1_connectiondataservice "github.com/nucleuscloud/neosync/backend/services/mgmt/v1alpha1/connection-data-service" diff --git a/backend/internal/sqlconnect/mock_PgPoolContainer.go b/backend/pkg/sqlconnect/mock_PgPoolContainer.go similarity index 83% rename from backend/internal/sqlconnect/mock_PgPoolContainer.go rename to backend/pkg/sqlconnect/mock_PgPoolContainer.go index 71567df1db..ca7dc849c3 100644 --- a/backend/internal/sqlconnect/mock_PgPoolContainer.go +++ b/backend/pkg/sqlconnect/mock_PgPoolContainer.go @@ -5,7 +5,7 @@ package sqlconnect import ( context "context" - pgxpool "github.com/jackc/pgx/v5/pgxpool" + pg_queries "github.com/nucleuscloud/neosync/backend/gen/go/db/dbschemas/postgresql" mock "github.com/stretchr/testify/mock" ) @@ -55,23 +55,23 @@ func (_c *MockPgPoolContainer_Close_Call) RunAndReturn(run func()) *MockPgPoolCo } // Open provides a mock function with given fields: _a0 -func (_m *MockPgPoolContainer) Open(_a0 context.Context) (*pgxpool.Pool, error) { +func (_m *MockPgPoolContainer) Open(_a0 context.Context) (pg_queries.DBTX, error) { ret := _m.Called(_a0) if len(ret) == 0 { panic("no return value specified for Open") } - var r0 *pgxpool.Pool + var r0 pg_queries.DBTX var r1 error - if rf, ok := ret.Get(0).(func(context.Context) (*pgxpool.Pool, error)); ok { + if rf, ok := ret.Get(0).(func(context.Context) (pg_queries.DBTX, error)); ok { return rf(_a0) } - if rf, ok := ret.Get(0).(func(context.Context) *pgxpool.Pool); ok { + if rf, ok := ret.Get(0).(func(context.Context) pg_queries.DBTX); ok { r0 = rf(_a0) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(*pgxpool.Pool) + r0 = ret.Get(0).(pg_queries.DBTX) } } @@ -102,12 +102,12 @@ func (_c *MockPgPoolContainer_Open_Call) Run(run func(_a0 context.Context)) *Moc return _c } -func (_c *MockPgPoolContainer_Open_Call) Return(_a0 *pgxpool.Pool, _a1 error) *MockPgPoolContainer_Open_Call { +func (_c *MockPgPoolContainer_Open_Call) Return(_a0 pg_queries.DBTX, _a1 error) *MockPgPoolContainer_Open_Call { _c.Call.Return(_a0, _a1) return _c } -func (_c *MockPgPoolContainer_Open_Call) RunAndReturn(run func(context.Context) (*pgxpool.Pool, error)) *MockPgPoolContainer_Open_Call { +func (_c *MockPgPoolContainer_Open_Call) RunAndReturn(run func(context.Context) (pg_queries.DBTX, error)) *MockPgPoolContainer_Open_Call { _c.Call.Return(run) return _c } diff --git a/backend/internal/sqlconnect/mock_SqlConnector.go b/backend/pkg/sqlconnect/mock_SqlConnector.go similarity index 100% rename from backend/internal/sqlconnect/mock_SqlConnector.go rename to backend/pkg/sqlconnect/mock_SqlConnector.go diff --git a/backend/internal/sqlconnect/mock_SqlDbContainer.go b/backend/pkg/sqlconnect/mock_SqlDbContainer.go similarity index 88% rename from backend/internal/sqlconnect/mock_SqlDbContainer.go rename to backend/pkg/sqlconnect/mock_SqlDbContainer.go index 8795be63bc..80523d3354 100644 --- a/backend/internal/sqlconnect/mock_SqlDbContainer.go +++ b/backend/pkg/sqlconnect/mock_SqlDbContainer.go @@ -2,11 +2,7 @@ package sqlconnect -import ( - sql "database/sql" - - mock "github.com/stretchr/testify/mock" -) +import mock "github.com/stretchr/testify/mock" // MockSqlDbContainer is an autogenerated mock type for the SqlDbContainer type type MockSqlDbContainer struct { @@ -67,23 +63,23 @@ func (_c *MockSqlDbContainer_Close_Call) RunAndReturn(run func() error) *MockSql } // Open provides a mock function with given fields: -func (_m *MockSqlDbContainer) Open() (*sql.DB, error) { +func (_m *MockSqlDbContainer) Open() (SqlDBTX, error) { ret := _m.Called() if len(ret) == 0 { panic("no return value specified for Open") } - var r0 *sql.DB + var r0 SqlDBTX var r1 error - if rf, ok := ret.Get(0).(func() (*sql.DB, error)); ok { + if rf, ok := ret.Get(0).(func() (SqlDBTX, error)); ok { return rf() } - if rf, ok := ret.Get(0).(func() *sql.DB); ok { + if rf, ok := ret.Get(0).(func() SqlDBTX); ok { r0 = rf() } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(*sql.DB) + r0 = ret.Get(0).(SqlDBTX) } } @@ -113,12 +109,12 @@ func (_c *MockSqlDbContainer_Open_Call) Run(run func()) *MockSqlDbContainer_Open return _c } -func (_c *MockSqlDbContainer_Open_Call) Return(_a0 *sql.DB, _a1 error) *MockSqlDbContainer_Open_Call { +func (_c *MockSqlDbContainer_Open_Call) Return(_a0 SqlDBTX, _a1 error) *MockSqlDbContainer_Open_Call { _c.Call.Return(_a0, _a1) return _c } -func (_c *MockSqlDbContainer_Open_Call) RunAndReturn(run func() (*sql.DB, error)) *MockSqlDbContainer_Open_Call { +func (_c *MockSqlDbContainer_Open_Call) RunAndReturn(run func() (SqlDBTX, error)) *MockSqlDbContainer_Open_Call { _c.Call.Return(run) return _c } diff --git a/backend/internal/sqlconnect/sql-connector.go b/backend/pkg/sqlconnect/sql-connector.go similarity index 96% rename from backend/internal/sqlconnect/sql-connector.go rename to backend/pkg/sqlconnect/sql-connector.go index facebcb304..e56a948f1c 100644 --- a/backend/internal/sqlconnect/sql-connector.go +++ b/backend/pkg/sqlconnect/sql-connector.go @@ -11,19 +11,28 @@ import ( "strings" "github.com/jackc/pgx/v5/pgxpool" + mysql_queries "github.com/nucleuscloud/neosync/backend/gen/go/db/dbschemas/mysql" + pg_queries "github.com/nucleuscloud/neosync/backend/gen/go/db/dbschemas/postgresql" mgmtv1alpha1 "github.com/nucleuscloud/neosync/backend/gen/go/protos/mgmt/v1alpha1" nucleuserrors "github.com/nucleuscloud/neosync/backend/internal/errors" "github.com/nucleuscloud/neosync/backend/pkg/sshtunnel" "golang.org/x/crypto/ssh" ) +type SqlDBTX interface { + mysql_queries.DBTX + + PingContext(context.Context) error + BeginTx(context.Context, *sql.TxOptions) (*sql.Tx, error) +} + type SqlDbContainer interface { - Open() (*sql.DB, error) + Open() (SqlDBTX, error) Close() error } type PgPoolContainer interface { - Open(context.Context) (*pgxpool.Pool, error) + Open(context.Context) (pg_queries.DBTX, error) Close() } @@ -60,7 +69,7 @@ type PgPool struct { connectionTimeout *uint32 } -func (s *PgPool) Open(ctx context.Context) (*pgxpool.Pool, error) { +func (s *PgPool) Open(ctx context.Context) (pg_queries.DBTX, error) { details, err := getConnectionDetails(&mgmtv1alpha1.ConnectionConfig{ Config: &mgmtv1alpha1.ConnectionConfig_PgConfig{ PgConfig: s.connectionConfig, @@ -118,7 +127,7 @@ type SqlDb struct { connectionTimeout *uint32 } -func (s *SqlDb) Open() (*sql.DB, error) { +func (s *SqlDb) Open() (SqlDBTX, error) { details, err := getConnectionDetails(s.connectionConfig, s.connectionTimeout, s.logger) if err != nil { return nil, err diff --git a/backend/internal/sqlconnect/sql-connector_test.go b/backend/pkg/sqlconnect/sql-connector_test.go similarity index 100% rename from backend/internal/sqlconnect/sql-connector_test.go rename to backend/pkg/sqlconnect/sql-connector_test.go diff --git a/backend/services/mgmt/v1alpha1/connection-data-service/connection-data_test.go b/backend/services/mgmt/v1alpha1/connection-data-service/connection-data_test.go index 6f593af16f..23b96a7e25 100644 --- a/backend/services/mgmt/v1alpha1/connection-data-service/connection-data_test.go +++ b/backend/services/mgmt/v1alpha1/connection-data-service/connection-data_test.go @@ -26,7 +26,7 @@ import ( pg_queries "github.com/nucleuscloud/neosync/backend/gen/go/db/dbschemas/postgresql" awsmanager "github.com/nucleuscloud/neosync/backend/internal/aws" "github.com/nucleuscloud/neosync/backend/internal/nucleusdb" - "github.com/nucleuscloud/neosync/backend/internal/sqlconnect" + "github.com/nucleuscloud/neosync/backend/pkg/sqlconnect" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) diff --git a/backend/services/mgmt/v1alpha1/connection-data-service/service.go b/backend/services/mgmt/v1alpha1/connection-data-service/service.go index 1e11e202b9..6f2d2a2f60 100644 --- a/backend/services/mgmt/v1alpha1/connection-data-service/service.go +++ b/backend/services/mgmt/v1alpha1/connection-data-service/service.go @@ -5,7 +5,7 @@ import ( pg_queries "github.com/nucleuscloud/neosync/backend/gen/go/db/dbschemas/postgresql" "github.com/nucleuscloud/neosync/backend/gen/go/protos/mgmt/v1alpha1/mgmtv1alpha1connect" awsmanager "github.com/nucleuscloud/neosync/backend/internal/aws" - "github.com/nucleuscloud/neosync/backend/internal/sqlconnect" + "github.com/nucleuscloud/neosync/backend/pkg/sqlconnect" ) type Service struct { diff --git a/backend/services/mgmt/v1alpha1/connection-service/connection_test.go b/backend/services/mgmt/v1alpha1/connection-service/connection_test.go index 9a13ad8fdc..fac7f1bba8 100644 --- a/backend/services/mgmt/v1alpha1/connection-service/connection_test.go +++ b/backend/services/mgmt/v1alpha1/connection-service/connection_test.go @@ -17,7 +17,7 @@ import ( "github.com/nucleuscloud/neosync/backend/internal/apikey" auth_apikey "github.com/nucleuscloud/neosync/backend/internal/auth/apikey" "github.com/nucleuscloud/neosync/backend/internal/nucleusdb" - "github.com/nucleuscloud/neosync/backend/internal/sqlconnect" + "github.com/nucleuscloud/neosync/backend/pkg/sqlconnect" pg_models "github.com/nucleuscloud/neosync/backend/sql/postgresql/models" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" diff --git a/backend/services/mgmt/v1alpha1/connection-service/service.go b/backend/services/mgmt/v1alpha1/connection-service/service.go index f85152d548..2f688175c1 100644 --- a/backend/services/mgmt/v1alpha1/connection-service/service.go +++ b/backend/services/mgmt/v1alpha1/connection-service/service.go @@ -3,7 +3,7 @@ package v1alpha1_connectionservice import ( "github.com/nucleuscloud/neosync/backend/gen/go/protos/mgmt/v1alpha1/mgmtv1alpha1connect" "github.com/nucleuscloud/neosync/backend/internal/nucleusdb" - "github.com/nucleuscloud/neosync/backend/internal/sqlconnect" + "github.com/nucleuscloud/neosync/backend/pkg/sqlconnect" ) type Service struct {