Skip to content

feat: [#572] migrator support to rename column#851

Merged
almas-x merged 1 commit intomasterfrom
almas/#572
Feb 9, 2025
Merged

feat: [#572] migrator support to rename column#851
almas-x merged 1 commit intomasterfrom
almas/#572

Conversation

@almas-x
Copy link
Contributor

@almas-x almas-x commented Feb 6, 2025

📑 Description

Go migrator support to rename column

func (r *M20250108083539UpdateTestTable) Up() error {
	return facades.Schema().Table("test", func(table schema.Blueprint) {
		table.RenameColumn("rename_column_before", "rename_column_after")
	})
}

Closes goravel/goravel#572

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced the ability to rename database columns, enhancing flexibility during schema updates and migrations.
    • Added a new field to the Column structure to accommodate additional information.
    • Introduced a new constructor function for creating column definitions.
  • Bug Fixes

    • Improved error handling in SQL statement generation and execution processes.
  • Tests

    • Added comprehensive tests to ensure the column renaming functionality works correctly across various database systems.
    • Enhanced existing tests to include error handling for SQL generation.

✅ Checks

  • Added test cases for my code

@almas-x almas-x requested a review from a team as a code owner February 6, 2025 07:26
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 6, 2025

Walkthrough

This pull request adds support for renaming columns in the database schema migrator. It introduces a new RenameColumn(from, to string) method in the Blueprint interface and its concrete implementation, along with a corresponding CompileRenameColumn(schema Schema, blueprint Blueprint, command *Command) (string, error) method in the Grammar interface. Related changes include the addition of a new command constant, updates to the SQL conversion logic, corresponding mock implementations, and new tests to verify the feature across different database drivers.

Changes

Files Change Summary
contracts/database/schema/blueprint.go, contracts/database/schema/grammar.go Added new methods: RenameColumn(from, to string) in Blueprint and CompileRenameColumn(schema Schema, blueprint Blueprint, command *Command) (string, error) in Grammar to support column renaming.
database/schema/blueprint.go Introduced CommandRenameColumn constant, implemented RenameColumn(from, to string) method, and updated ToSql to generate SQL for the rename column command.
mocks/database/schema/Blueprint.go, mocks/database/schema/Grammar.go Added mock implementations and expecter helper methods for RenameColumn and CompileRenameColumn, including custom run and return handling.
tests/schema_test.go Added TestRenameColumn to validate the renaming functionality across different database drivers.
tests/table.go, tests/query.go, database/schema/blueprint_test.go, contracts/database/schema/column.go Updated method signatures across various test files to return ([]string, error) instead of just []string, enhancing error handling capabilities.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant B as Blueprint
    participant G as Grammar
    U->>B: RenameColumn("oldColumn", "newColumn")
    B->>B: Add CommandRenameColumn with from and to values
    U->>B: ToSql(grammar)
    B->>G: CompileRenameColumn(schema, blueprint, command)
    G-->>B: Return SQL statement
    B-->>U: SQL statement for renaming column
Loading

Assessment against linked issues

Objective Addressed Explanation
Go migrator support to rename column (#572)

Possibly related PRs

Suggested reviewers

  • hwbrzzl

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 golangci-lint (1.62.2)

level=error msg="Running error: context loading failed: no go files to analyze: running go mod tidy may solve the problem"

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov
Copy link

codecov bot commented Feb 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 67.54%. Comparing base (88c374a) to head (cd95292).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #851   +/-   ##
=======================================
  Coverage   67.54%   67.54%           
=======================================
  Files         151      151           
  Lines       10343    10343           
=======================================
  Hits         6986     6986           
  Misses       2990     2990           
  Partials      367      367           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hwbrzzl
Copy link
Contributor

hwbrzzl commented Feb 6, 2025

Thanks for the PRs, FYI, please add the Review Ready tag when they are ready, I'll review them.

@almas-x
Copy link
Contributor Author

almas-x commented Feb 6, 2025

Thanks for the PRs, FYI, please add the Review Ready tag when they are ready, I'll review them.

We need to merge the PR in the driver repository first to ensure the unit tests run successfully.

@hwbrzzl
Copy link
Contributor

hwbrzzl commented Feb 6, 2025

Yes, is this PR ready?

@almas-x
Copy link
Contributor Author

almas-x commented Feb 6, 2025

Yes, is this PR ready?

It’s been ready for a while, except for one small question about MySQL that needs to be confirmed.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/schema_test.go (1)

1860-1862: Consider adding test cases for edge cases.

While the basic functionality is well tested, consider adding test cases for:

  • Renaming a column with foreign key constraints
  • Renaming a column that's part of an index
  • Renaming a column with default values or other constraints
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dc45fb9 and f40ec68.

📒 Files selected for processing (6)
  • contracts/database/schema/blueprint.go (1 hunks)
  • contracts/database/schema/grammar.go (1 hunks)
  • database/schema/blueprint.go (3 hunks)
  • mocks/database/schema/Blueprint.go (1 hunks)
  • mocks/database/schema/Grammar.go (1 hunks)
  • tests/schema_test.go (1 hunks)
🔇 Additional comments (10)
contracts/database/schema/grammar.go (1)

52-53: LGTM! Well-structured interface addition.

The new CompileRenameColumn method follows the established pattern of other Compile* methods in the interface, with clear documentation and proper parameter types.

contracts/database/schema/blueprint.go (1)

106-107: LGTM! Clean interface extension.

The new RenameColumn method has a clear, intuitive signature with well-named parameters that match the method's purpose.

database/schema/blueprint.go (3)

30-30: LGTM! Consistent constant definition.

The new command constant follows the established naming pattern and is properly placed in alphabetical order.


339-347: LGTM! Clean implementation of RenameColumn.

The implementation is straightforward and follows the established pattern of command construction in the codebase.


517-518: LGTM! Proper ToSql extension.

The new case for handling rename column commands follows the existing pattern and is correctly placed in alphabetical order.

mocks/database/schema/Grammar.go (1)

1178-1223: LGTM! Well-structured mock implementation.

The mock implementation follows the established mockery pattern and includes all necessary helper methods for testing.

tests/schema_test.go (1)

1854-1872: LGTM! The test implementation is thorough and well-structured.

The test case effectively verifies the column renaming functionality:

  • Tests across all supported database drivers
  • Validates both the existence and non-existence of columns
  • Uses clear and descriptive variable names
mocks/database/schema/Blueprint.go (3)

1-9: LGTM! Standard generated code header and imports.


2309-2312: LGTM! Clean and correct mock implementation of RenameColumn.

The implementation follows the standard mock pattern and correctly handles the method parameters.


2314-2341: LGTM! Complete implementation of mock helper types and methods.

The helper types and methods are correctly implemented following the mockery pattern, providing all necessary functionality for testing:

  • Blueprint_RenameColumn_Call struct
  • Run method for executing custom logic
  • Return method for specifying return values
  • RunAndReturn method for combining both

Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

LGTM, we can finish the mysql PR first before merging this.

@almas-x almas-x marked this pull request as draft February 7, 2025 01:50
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: b808b5f Previous: b635f38 Ratio
Benchmark_DecryptString 5275 ns/op 1592 B/op 17 allocs/op 1975 ns/op 1592 B/op 17 allocs/op 2.67
Benchmark_DecryptString - ns/op 5275 ns/op 1975 ns/op 2.67

This comment was automatically generated by workflow using github-action-benchmark.

@almas-x almas-x force-pushed the almas/#572 branch 2 times, most recently from accb5a8 to 63fe51e Compare February 8, 2025 09:57
@almas-x almas-x marked this pull request as ready for review February 8, 2025 09:59
@almas-x almas-x requested review from a team and removed request for a team February 9, 2025 01:50
Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

Great PR 👍

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
database/schema/blueprint_test.go (1)

664-665: Add test cases for error scenarios and RenameColumn functionality.

The test suite should be expanded to include:

  1. Error scenarios for ToSql
  2. Test cases for the new RenameColumn functionality

Add these test cases to the tests array:

{
    name: "Return error when CompileRenameColumn fails",
    setup: func() {
        s.blueprint.RenameColumn("old_name", "new_name")
        mockGrammar.EXPECT().GetAttributeCommands().Return([]string{}).Once()
        mockGrammar.EXPECT().CompileRenameColumn(s.blueprint.schema, s.blueprint, s.blueprint.commands[0]).
            Return("", assert.AnError).Once()
    },
    expectedSQL: nil,
    expectedErr: assert.AnError,
},
{
    name: "Successfully rename column",
    setup: func() {
        s.blueprint.RenameColumn("old_name", "new_name")
        mockGrammar.EXPECT().GetAttributeCommands().Return([]string{}).Once()
        mockGrammar.EXPECT().CompileRenameColumn(s.blueprint.schema, s.blueprint, s.blueprint.commands[0]).
            Return("ALTER TABLE users RENAME COLUMN old_name TO new_name", nil).Once()
    },
    expectedSQL: []string{"ALTER TABLE users RENAME COLUMN old_name TO new_name"},
    expectedErr: nil,
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f40ec68 and cd95292.

📒 Files selected for processing (11)
  • contracts/database/schema/blueprint.go (2 hunks)
  • contracts/database/schema/column.go (1 hunks)
  • contracts/database/schema/grammar.go (1 hunks)
  • database/schema/blueprint.go (5 hunks)
  • database/schema/blueprint_test.go (1 hunks)
  • database/schema/column.go (1 hunks)
  • mocks/database/schema/Blueprint.go (4 hunks)
  • mocks/database/schema/Grammar.go (1 hunks)
  • tests/query.go (1 hunks)
  • tests/schema_test.go (1 hunks)
  • tests/table.go (11 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • contracts/database/schema/blueprint.go
  • tests/schema_test.go
  • mocks/database/schema/Blueprint.go
🔇 Additional comments (8)
contracts/database/schema/column.go (1)

67-67: LGTM!

The addition of the Extra field to store additional column metadata is well-placed and follows Go naming conventions.

database/schema/column.go (1)

27-32: LGTM!

The NewColumnDefinition constructor is well-implemented, following Go conventions and properly handling pointer conversions.

tests/table.go (1)

35-51: LGTM!

The changes consistently improve error handling across all methods, following Go's best practices for error handling.

Also applies to: 53-207

contracts/database/schema/grammar.go (1)

52-53: LGTM!

The CompileRenameColumn method is well-defined, following the interface's patterns and Go's error handling conventions.

database/schema/blueprint.go (3)

30-30: LGTM!

The new command constant follows the existing naming convention.


344-352: LGTM!

The RenameColumn implementation follows the established pattern and correctly sets up the command structure.


522-528: LGTM!

The RenameColumn case in ToSql properly handles errors and follows the existing pattern.

mocks/database/schema/Grammar.go (1)

1178-1234: LGTM!

The mock implementation for CompileRenameColumn is correctly generated and follows the established patterns for error handling and type assertions.

@almas-x almas-x merged commit c63da33 into master Feb 9, 2025
12 of 14 checks passed
@almas-x almas-x deleted the almas/#572 branch February 9, 2025 05:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Go migrator support to rename column

2 participants