Skip to content

Refactor migration management and enhance database connectivity#35

Open
iamviniciuss wants to merge 16 commits into
xakep666:masterfrom
iamviniciuss:feature/PD-1337-migracao-mongo-library-v2
Open

Refactor migration management and enhance database connectivity#35
iamviniciuss wants to merge 16 commits into
xakep666:masterfrom
iamviniciuss:feature/PD-1337-migracao-mongo-library-v2

Conversation

@iamviniciuss
Copy link
Copy Markdown

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.

Copilot AI review requested due to automatic review settings September 17, 2025 13:49
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/repository/migration_mysql.go Outdated
nome VARCHAR(255),
idade INT
);
`, tableName)
Copy link

Copilot AI Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable tableName is used in the SQL query but the function parameter is name. This will cause a compilation error.

Suggested change
`, tableName)
`, name)

Copilot uses AI. Check for mistakes.
Comment on lines +34 to +36
func (erm *MigrationRepositoryMongo) FindOne(reccord *entity.VersionRecord) (*entity.VersionRecord, error) {

filter := bson.M{"version": reccord.Version, "description": reccord.Description, "type": reccord.Type}
Copy link

Copilot AI Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter name has a typo: 'reccord' should be 'record' for consistency with other methods.

Suggested change
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}

Copilot uses AI. Check for mistakes.
Comment on lines +11 to +16
)

const defaultMigrationsCollection = "migrations"

const AllAvailable = -1

Copy link

Copilot AI Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
)
const defaultMigrationsCollection = "migrations"
const AllAvailable = -1
"github.com/iamviniciuss/golang-migrations/src/pkg" // import the package where AllAvailable is defined
)
const defaultMigrationsCollection = "migrations"

Copilot uses AI. Check for mistakes.
name: "addMyIndex",
typing: "seed",
db: db,
//repositoryXXXX: XXXXX
Copy link

Copilot AI Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the commented placeholder code as it serves no purpose and clutters the codebase.

Suggested change
//repositoryXXXX: XXXXX

Copilot uses AI. Check for mistakes.
Comment on lines +46 to +48
id INT AUTO_INCREMENT PRIMARY KEY,
nome VARCHAR(255),
idade INT
Copy link

Copilot AI Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
Comment thread src/pkg/migrate_test.go
Comment on lines +19 to +38
// 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
Copy link

Copilot AI Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments are in Portuguese. Consider using English for consistency with the rest of the codebase.

Suggested change
// 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

Copilot uses AI. Check for mistakes.
Comment thread package.json
@@ -0,0 +1,24 @@
{
"name": "contact_api",
Copy link

Copilot AI Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The package name 'contact_api' doesn't match the project purpose of golang-migrations. This should be updated to reflect the actual project.

Suggested change
"name": "contact_api",
"name": "golang-migrations",

Copilot uses AI. Check for mistakes.
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants