Refactor migration management and enhance database connectivity#35
Refactor migration management and enhance database connectivity#35iamviniciuss wants to merge 16 commits into
Conversation
…re-seeds hotfix/does-not-run-two-or-more-seeds: fix bug.
…on-database hotfix/connect-with-password-on-database: fix problem
There was a problem hiding this comment.
Pull Request Overview
This pull request refactors migration management by implementing dependency inversion principles and reorganizing the project structure for better maintainability. The changes include moving from function-based migrations to interface-based handlers, restructuring the codebase into distinct packages, and adding support for both MongoDB and MySQL databases.
Key changes:
- Introduces adapter pattern with MigrationHandler interface for better extensibility
- Reorganizes code into src/ directory with separate packages for adapters, DTOs, repositories, and core logic
- Adds comprehensive test coverage with mocks and unit tests
- Removes old global migration approach in favor of dependency injection
Reviewed Changes
Copilot reviewed 36 out of 38 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| src/adapter/ | New adapter interfaces for migration handlers and repositories |
| src/pkg/ | Core migration logic with dependency injection support |
| src/repository/ | Database-specific repository implementations for MongoDB and MySQL |
| tests/seed/ | Example seed implementations demonstrating new interface usage |
| util.go, util_test.go | Removed old utility functions for file-based version extraction |
| migration.go, migrate.go | Removed old monolithic migration implementation |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| nome VARCHAR(255), | ||
| idade INT | ||
| ); | ||
| `, tableName) |
There was a problem hiding this comment.
The variable tableName is used in the SQL query but the function parameter is name. This will cause a compilation error.
| `, tableName) | |
| `, name) |
| func (erm *MigrationRepositoryMongo) FindOne(reccord *entity.VersionRecord) (*entity.VersionRecord, error) { | ||
|
|
||
| filter := bson.M{"version": reccord.Version, "description": reccord.Description, "type": reccord.Type} |
There was a problem hiding this comment.
Parameter name has a typo: 'reccord' should be 'record' for consistency with other methods.
| func (erm *MigrationRepositoryMongo) FindOne(reccord *entity.VersionRecord) (*entity.VersionRecord, error) { | |
| filter := bson.M{"version": reccord.Version, "description": reccord.Description, "type": reccord.Type} | |
| func (erm *MigrationRepositoryMongo) FindOne(record *entity.VersionRecord) (*entity.VersionRecord, error) { | |
| filter := bson.M{"version": record.Version, "description": record.Description, "type": record.Type} |
| ) | ||
|
|
||
| const defaultMigrationsCollection = "migrations" | ||
|
|
||
| const AllAvailable = -1 | ||
|
|
There was a problem hiding this comment.
The constant AllAvailable is duplicated across multiple files (also in src/pkg/migrate.go). This should be defined in a single location to avoid inconsistency.
| ) | |
| const defaultMigrationsCollection = "migrations" | |
| const AllAvailable = -1 | |
| "github.com/iamviniciuss/golang-migrations/src/pkg" // import the package where AllAvailable is defined | |
| ) | |
| const defaultMigrationsCollection = "migrations" |
| name: "addMyIndex", | ||
| typing: "seed", | ||
| db: db, | ||
| //repositoryXXXX: XXXXX |
There was a problem hiding this comment.
Remove the commented placeholder code as it serves no purpose and clutters the codebase.
| //repositoryXXXX: XXXXX |
| id INT AUTO_INCREMENT PRIMARY KEY, | ||
| nome VARCHAR(255), | ||
| idade INT |
There was a problem hiding this comment.
The table schema in CreateCollectionIfNotExists creates a table with 'nome' and 'idade' columns which don't match the migration table structure that should have 'version', 'description', and 'type' columns.
| id INT AUTO_INCREMENT PRIMARY KEY, | |
| nome VARCHAR(255), | |
| idade INT | |
| version BIGINT PRIMARY KEY, | |
| description VARCHAR(255) NOT NULL, | |
| type VARCHAR(50) NOT NULL |
| // SetupSuite é executado uma vez antes de todos os testes da suíte. | ||
| func (suite *MigrationManagerTestSuite) SetupSuite() { | ||
| // Configuração global | ||
| suite.value = 42 | ||
| } | ||
|
|
||
| // SetupTest é executado antes de cada teste. | ||
| func (suite *MigrationManagerTestSuite) SetupTest() { | ||
| // Configuração específica para cada teste | ||
| suite.repo = new(MigrationRepositoryMock) | ||
| } | ||
|
|
||
| // TearDownTest é executado depois de cada teste. | ||
| func (suite *MigrationManagerTestSuite) TearDownTest() { | ||
| // Limpeza específica para cada teste | ||
| } | ||
|
|
||
| // TearDownSuite é executado uma vez após todos os testes da suíte. | ||
| func (suite *MigrationManagerTestSuite) TearDownSuite() { | ||
| // Limpeza global |
There was a problem hiding this comment.
Comments are in Portuguese. Consider using English for consistency with the rest of the codebase.
| // SetupSuite é executado uma vez antes de todos os testes da suíte. | |
| func (suite *MigrationManagerTestSuite) SetupSuite() { | |
| // Configuração global | |
| suite.value = 42 | |
| } | |
| // SetupTest é executado antes de cada teste. | |
| func (suite *MigrationManagerTestSuite) SetupTest() { | |
| // Configuração específica para cada teste | |
| suite.repo = new(MigrationRepositoryMock) | |
| } | |
| // TearDownTest é executado depois de cada teste. | |
| func (suite *MigrationManagerTestSuite) TearDownTest() { | |
| // Limpeza específica para cada teste | |
| } | |
| // TearDownSuite é executado uma vez após todos os testes da suíte. | |
| func (suite *MigrationManagerTestSuite) TearDownSuite() { | |
| // Limpeza global | |
| // SetupSuite is executed once before all tests in the suite. | |
| func (suite *MigrationManagerTestSuite) SetupSuite() { | |
| // Global setup | |
| suite.value = 42 | |
| } | |
| // SetupTest is executed before each test. | |
| func (suite *MigrationManagerTestSuite) SetupTest() { | |
| // Test-specific setup | |
| suite.repo = new(MigrationRepositoryMock) | |
| } | |
| // TearDownTest is executed after each test. | |
| func (suite *MigrationManagerTestSuite) TearDownTest() { | |
| // Test-specific cleanup | |
| } | |
| // TearDownSuite is executed once after all tests in the suite. | |
| func (suite *MigrationManagerTestSuite) TearDownSuite() { | |
| // Global cleanup |
| @@ -0,0 +1,24 @@ | |||
| { | |||
| "name": "contact_api", | |||
There was a problem hiding this comment.
The package name 'contact_api' doesn't match the project purpose of golang-migrations. This should be updated to reflect the actual project.
| "name": "contact_api", | |
| "name": "golang-migrations", |
Refactor MongoDB connection and repository structure https://crewhu.atlassian.net/browse/PD-1337 - Updated MongoDB driver to v2 in mongo_connection.go and migration_mongodb.go. - Changed the MongoDB connection logic to use Ping instead of Connect. - Removed the online_review_mongodb.go file and integrated its functionality into the seed package. - Modified migration_mysql.go to accept a migration table name as a parameter. - Removed unused seed.go file and created a new CreateMongoInMemory.go for in-memory MongoDB testing. - Updated seed files to reflect changes in repository structure and removed references to the deleted online_review_mongodb.go. - Adjusted test scripts to accommodate new MongoDB connection and repository changes.
Implement dependency inversion for migration handling, improve database connection options, and reorganize project structure for better maintainability. Fix bugs related to migration seeding and version checking.