Inox
Inox

Reputation: 83

Liquibase add comment to table

I'm currently trying to add comment on tables in liquibase 3.1.1

What I want to do is ALTER TABLE t1 COMMENT = 'New table comment';

I didn't find any help in the documentation of liquibase about this case, there is only help for adding comments on columns.

I am currently thinking of creating a customChange or doing the change by myself with SQL statements but as we are going to migrate from MySQL to Oracle I would like to avoid this solution (or use it in last resort).

Has anyone found another solution for this problem ?

Upvotes: 7

Views: 9982

Answers (3)

O110101
O110101

Reputation: 11

Seems like there is a solution meanwhile (2023):

<changeSet>  
  <setTableRemarks  
    remarks="A String"  
    tableName="person"
  />  
</changeSet>

See the liquibase documentation for details and other formats.

Worked on my machine against an Oracle DB.

Upvotes: 1

Roman Gorbatenko
Roman Gorbatenko

Reputation: 149

COMMENT ON TABLE is absolutely helpful in this case. See example I provided below along with some tips and pitfalls about editing tables using liqubase.

Continuing the response above, I'd like to pay attention on tag <preConditions> here. It's critical when dealing with tables to ask liqubase whether the certain table exists at the moment you try to edit one. Here's some example of this behavior:

<changeSet id="your_table_id" author="author_name" dbms="postgresql">
        <preConditions onFail="MARK_RAN">
            <tableExists tableName="table_name" schemaName="schema_name"/>
        </preConditions>
        <sql>
            COMMENT ON TABLE schema_name.table_name is 'YOUR_COMMENT_ON_TABLE';
        </sql>
 </changeSet>

In case dealing with columns (editing, adding, deleting) ALSO consider asking liqubase the similar way of the existing or absence of the specific column(s):

<preConditions onFail="MARK_RAN">
    <not>
         <columnExists tableName="table_name" schemaName="schema_name" 
             columnName="column_name"/>
    </not>
</preConditions>
<sql>
...
</sql>

The example above checks if column NOT present in table and then executes some SQL script.

Upvotes: 2

Gerardo Figueroa
Gerardo Figueroa

Reputation: 559

I solved this problem by creating a custom SQL statement with a <sql> tag (or "sql" key if you are using JSON) in the changeSet (documentation can be found here).

Since I am using PostgreSQL, the SQL syntax for adding a comment to a table is COMMENT ON TABLE table_name IS 'text'. Below is the entire changeSet in XML format:

<changeSet author="gfigueroa" id="add-comment-to-table-xyz">
    <sql dbms="postgresql"
         endDelimiter=";"
         splitStatements="true"
         stripComments="true">
        COMMENT ON TABLE xyz IS 'This is a test comment';
    </sql>
</changeSet>

Upvotes: 1

Related Questions