Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I rename a sequence with liquibase?

Tags:

liquibase

I have a sequence in my database, which I generated through Liquibase. During refactoring, we decided that we didn't like the name that we gave it, and we would like to rename it, preserving all data that currently exists for it.

It seems possible to alter a sequence, but I'm not seeing anything about how to rename the sequence. Is there a way to do it, or a reasonable workaround?

(If it matters, I'm using Oracle SQL)

like image 623
Thunderforge Avatar asked Dec 11 '22 17:12

Thunderforge


2 Answers

Although not documented, this refactoring is supported by Liquibase. Not sure what version this change was implemented in, but the class supporting the feature was commited on 30 Jan 2014. What's interesting though, is that the original issue is still unresolved.

Anyway, the refactoring is supposed to be working only on Oracle and Postgres. I've tested it on Oracle with Liquibase 3.4.1:

databaseChangeLog:
  - changeSet:
      id: change_set_id
      author: me
      dbms: oracle
      changes:
        - renameSequence:
            oldSequenceName: old_name_seq
            newSequenceName: new_name_seq

The above refactoring is in YAML format, but you could easily guess its XML counterpart.

On Oracle, this generates the following statement:

RENAME old_name_seq TO new_name_seq;

2 other supported parameters are catalogName and schemaName.

like image 192
jFrenetic Avatar answered Dec 28 '22 23:12

jFrenetic


There is not currently a built-in refactoring to rename a sequence. If your database engine supports it, you could execute whatever methods are supported using a <sql> or <sqlFile> change.

You said you were using Oracle SQL. The RENAME statement allows for renaming a sequence. So your Liquibase script would look like this:

<sql>RENAME old_sequence_name TO new_sequence_name</sql>
like image 25
SteveDonie Avatar answered Dec 29 '22 00:12

SteveDonie