7

I'm using a H2 database to store my Data, and liquibase (with hibernate plugin) to check for differences between database and projet.

suppose I have followign Code:

@Entity
public class myEntity{

    @Column(name="val")
    private int value;

}

The database is in place and already stores some data.

Now when I rename the above Column i.e. from val to value and run liquibase:diff, the difflog says to drop the column "val" and add a column "value".

Obviously this is not what I wanted, because all the data originally stored in the "val" column would be gone.

Is there a way to tell liquibase that its not a new column, but an old renamed one?

I want to run liquibase:diff and the generated diffLog should automatically contain the rename... tag for my Column, not an add.. and a drop.. one..

Lamp3
  • 89
  • 1
  • 1
  • 3
  • 2
    What happens when you google for "liquibase rename column"? – JB Nizet Sep 14 '16 at 06:57
  • I'm well aware of the possibility to manually write a changeset that does exact the thing i want (it already is shown in an answer below), but i want liquibase to automatically create the rename-changeset when confronted with a renamed column in my code – Lamp3 Sep 14 '16 at 07:03
  • 1
    To calrify: I want to run liquibase:diff and the generated diffLog should containt the tag, what i don't want is me having to manually add the tag – Lamp3 Sep 14 '16 at 07:06
  • 4
    It can't. if the schema before is [a, b] and then it's [d, e], it can't guess that d is the new name of a, that b has disappeared and e is a brand new, totally different column. It could be the reverese. It could be two renames. It could be two deletions and 2 additions. It's a semantic difference and you are the only one to know that. So you need to take the control, and tell the dumb tool what to do. – JB Nizet Sep 14 '16 at 07:08

2 Answers2

7

Did you try using a changeset as follows (or did i get the question wrong)

   <changeSet author="liquibase-docs" id="renameColumn-example">
        <renameColumn columnDataType="int"
                newColumnName="value"
                oldColumnName="val"
                remarks="A change in names"
                schemaName="public"
                tableName="myEntity"/>
    </changeSet>
Ramachandran.A.G
  • 4,788
  • 1
  • 12
  • 24
  • The problem is i dont want to manually change the difflog, what i want is liquibase to automatically create that specific changeset when confronted with a renamen column in code – Lamp3 Sep 14 '16 at 07:01
4

There is currently no way in general for a diff to be able to detect that the column change was a rename rather than a drop and create. This is true for any system that creates changes using diffs, not just Liquibase.

Imagine yourself as liquibase - you are given two tables that are in the states that you describe. How would you determine that a column was renamed vs. one column dropped and the other column created? The only thing I can think of is that would would need to look at the contents of the columns and see that they were 'mostly the same'. In this particular case, this is impossible because the databases that are being compared include one that is populated with data and a second that is just an empty in-memory database created by Hibernate.

SteveDonie
  • 8,700
  • 3
  • 43
  • 43