Skip to content
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

Add explicit renameColumn method for Table #6078

Closed
wants to merge 1 commit into from

Conversation

Tofandel
Copy link
Contributor

@Tofandel Tofandel commented Jun 29, 2023

Q A
Type feature
Fixed issues doctrine/migrations#57

Summary

Add a renameColumn in the Table class

This allow to create migration with schemas that rename columns explicitely, which is a missing feature

Right now only implicit detection is done (it looks for added columns with the same definition as a dropped column), and that means if you change an option or the type of the column, it will not consider it as a rename anymore and instead will drop and recreate, by being explicit, this is not a problem anymore

This PR allows the following migration, without losing the column data

    public function up(Schema $schema): void
    {
        $schema->getTable('test')->renameColumn('foo', 'bar')->setType(Type::getType('text'));
    }
    
    public function down(Schema $schema): void
    {
        $schema->getTable('test')->renameColumn('bar', 'foo')->setType(Type::getType('string'));
    }

Which before would have to drop the column and recreate it, as well as needing a full redefinition of the column, and losing the data in the process

    public function up(Schema $schema): void
    {
        $schema->getTable('test')->dropColumn('foo')->addColumn('bar', 'text');
    }
    
    public function down(Schema $schema): void
    {
        $schema->getTable('test')->dropColumn('bar')->addColumn('foo', 'string');
    }

PS: I'm building a feature for the doctrine/migration package to generate diffs of schema, directly using Schema instead of platform specific sql and without this renameColumn this would be impossible

@Tofandel Tofandel changed the title 4.0.x Add explicit renameColumn method for Table Jun 29, 2023
@Tofandel Tofandel changed the base branch from 3.6.x to 4.0.x June 29, 2023 01:39
@derrabus
Copy link
Member

Thank you. We don't accept features on the 4.0.x branch. Please target 3.7.x.

@Tofandel Tofandel changed the base branch from 4.0.x to 3.7.x June 29, 2023 10:59
@Tofandel Tofandel closed this Jun 29, 2023
@Tofandel Tofandel deleted the 4.0.x branch June 29, 2023 11:06
derrabus pushed a commit that referenced this pull request Jan 20, 2024
<!-- Fill in the relevant information below to help triage your pull
request. -->

|      Q       |   A
|------------- | -----------
| Type         | feature
| Fixed issues | doctrine/migrations#57

#### Summary

<!-- Provide a summary of your change. -->
Reopening #6078  on 3.7.x

Add a `renameColumn` in the `Table` class

This allow to create migration with schemas that rename columns
explicitely, which is a missing feature

Right now only implicit detection is done (it looks for added columns
with the same definition as a dropped column), and that means if you
change an option or the type of the column, it will not consider it as a
rename anymore and instead will drop and recreate, by being explicit,
this is not a problem anymore


This PR allows the following migration, without losing the column data
```php

    public function up(Schema $schema): void
    {
        $schema->getTable('test')->renameColumn('foo', 'bar')->setType(Type::getType('text'));
    }
    
    public function down(Schema $schema): void
    {
        $schema->getTable('test')->renameColumn('bar', 'foo')->setType(Type::getType('string'));
    }
 ```
 
 Which before would have to drop the column and recreate it, as well as needing a full redefinition of the column, and losing the data in the process
 
```php

    public function up(Schema $schema): void
    {
$schema->getTable('test')->dropColumn('foo')->addColumn('bar', 'text');
    }
    
    public function down(Schema $schema): void
    {
$schema->getTable('test')->dropColumn('bar')->addColumn('foo',
'string');
    }
 ```
 
PS: I'm building a feature for the `doctrine/migration` package to
generate diffs of schema, directly using `Schema` instead of platform
specific sql and without this `renameColumn` this would be impossible,
it would also allow other frameworks which use schemas (Yes I'm thinking
laravel) to rename columns [without using
internals](https://github.com/laravel/framework/blob/10.x/src/Illuminate/Database/Schema/Grammars/RenameColumn.php#L65C21-L65C35)

Co-authored-by: Grégoire Paris <postmaster@greg0ire.fr>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants