I am looking to compress a column through Liquibase and I haven't been able to find any examples of this on the Liquidbase site.
I was wondering if anyone has an example of this?
You can add custom SQL statements to Liquibase change logs using the <sql> element and use the dbms attribute on change sets to define for which databases they are meant to be run.
<changeSet id=".." dbms="oracle">
<sql>
alter table foobar move compress;
</sql>
<rollback>
<sql>
alter table foobar nocompress;
</sql>
</rollback>
</changeSet>
You can use modifyDataType
<changeSet author="liquibase-docs" id="modifyDataType-example">
<modifyDataType catalogName="cat"
columnName="id"
newDataType="A String"
schemaName="public"
tableName="person"/>
</changeSet>
Related
What is the best way to rename index in liquibase? should I delete and recreate it?
thanks in advance.
At first, you must remove former index:
<changeSet id="20210905084713-2" author="ARMIN.NOROOZIAN">
<dropIndex tableName="tpl_order" indexName="tracking_code_idx"/>
</changeSet>
Then create a new index:
<changeSet id="20210905084713-3" author="ARMIN.NOROOZIAN">
<createIndex tableName="tpl_order" indexName="tpl_tracking_code_idx">
<column name="tpl_tracking_code"/>
</createIndex>
</changeSet>
How can I generate this SQL statement in my changelog file with liquibase?
CREATE UNIQUE INDEX RL_UK on RUN_LOG(status) WHERE status = 'R'
I have tried it like this but it doesnt work:
<changeSet author="Ferid (generated)" id="1528876614155-232">
<createIndex indexName="RL_UK" tableName="RUN_LOG" unique="true">
<where> status='R'</where>
</createIndex>
I dont use this changelog file to update the database but to create it.
The clause is not available.
As suggested in comments, you could write custom SQL statments with tag but you will have to take care of the rollback yourself :
<changeSet author="Ferid (generated)" id="1528876614155-232">
<sql>
CREATE UNIQUE INDEX RL_UK on RUN_LOG(status) WHERE status = 'R';
</sql>
<rollback>
<sql>
DROP INDEX RL_UK on RUN_LOG;
</sql>
</rollback>
</changeSet>
Although I would personnally suggest that you use the tag properly without the WHERE CLAUSE because :
I can't understand what you are trying to achieve with this where clause. Espacially since it is on the same column
Your changeset probably won't be compatible with all databases supported by Liquibase so it will work with Postgres and fail with Oracle for example. The WHERE CLAUSE in the CREATE INDEX statement is probably not even ANSI-SQL
You add bothering about the rollback for a very simple statement => not worth. Auto-rollback is available on every supported databases for this tag. Check it here : http://www.liquibase.org/documentation/changes/create_index.html
The Setup
I am using Liquibase to manage my project's migrations.
I have several tables with several sets of seed data.
Each seeded table has a changeset to create the table, followed by a changeset to load the seed data. The seeds are being loaded using loadUpdateData. This is a smart method that will load seed data from a CSV, if the CSV content is edited it will make the appropriate edits directly.
The seed ChangeSets are in a separate ChangeLog that is always run after the core ChangeLog. This way the seed files can always reflect the correct table structure.
The Problem
I need to drop a table that has seed data. The loadUpdateData command errors because the table no longer exists by the time it is run.
The Code
Create Table ChangeSet
<changeSet author="" id="create-table-help-items">
<createTable tableName="help_items">
<column name="help_item_id"
type="bigint">
<constraints primaryKey="true"/>
</column>
<column name="title"
type="text" />
<column name="description"
type="text" />
</createTable>
<rollback>
<dropTable tableName="help_items"/>
</rollback>
</changeSet>
Seed ChangeSet
<changeSet author="" id="seed-help-items" runOnChange="true">
<loadUpdateData file="db/seeds/help_items.csv"
primaryKey="help_items_id"
tableName="help_items" />
</changeSet>
Drop Table ChangeSet
<changeSet author="" id="remove-table-help-items">
<dropTable tableName="help_items"/>
<rollback changeSetId="create-table-help-items" changeSetAuthor=""/>
</changeSet>
The Questions
Given that it is bad practice to ever delete changesets from a changelog.
What is the right way to create seed migrations so that they don't break when the table is deleted?
Do I need to keep the seed files for tables that have been dropped?
I had the same problem and now i solved it, it works already by me.
The point is that you should set the Rollback in "Drop Table ChangeSet" correctly.
So by droping a table, you should already prepare for the situation of rollback.
<changeSet author="" id="remove-table-help-items">
<dropTable tableName="help_items"/>
<rollback>
<createTable tableName="help_items"/>
</rollback>
</changeSet>
I solved this issue by setting runAlways: false for loadUpdateData changeset. So, it doesn't fail when the table is dropped.
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 ?
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.
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>
I’m using Liquibase via the Gradle-Liquibase (v 1.1.1) plugin. I have the following changeset …
<changeSet id="create_my_stored_proc" author="davea" dbms="mysql" runAlways="true">
<sqlFile endDelimiter="//" path="src/main/resources/scripts/create_my_stored_proc.sql" stripComments="true"/>
</changeSet>
Is it possible to set something such that checksums are ignored for this changeset only? The underlying procedure is in a state of flux that could be repeatedly updated and rather than create a new changeset each time, I would like the existing one to run upon every Liquibase build.
You can disable the checks per changeSet using the <validCheckSum> tag with known good values.
For example, if the previous changeset had a checksum like 8:b3d6a29ce3a75940858cd093501151d1 and you wanted to tweak that changeSet (but not re-apply it where this step has already succeeded) then you could use something like this:
<changeSet author="me" id="mychangeset">
<validCheckSum>8:b3d6a29ce3a75940858cd093501151d1</validCheckSum>
<sqlFile ... />
</changeSet>
"RunAlways will still throw a checksum error by default, but you can always use runOnChange=true or any to change that."
Have a look at this ticket raised in liquibase: https://liquibase.jira.com/browse/CORE-2506
So, you could do:
<changeSet id="create_my_stored_proc" author="davea" dbms="mysql" runAlways="true">
<validCheckSum>any</validCheckSum>
<sqlFile endDelimiter="//" path="src/main/resources/scripts/create_my_stored_proc.sql" stripComments="true"/>
</changeSet>
You can add the runAlways and/or runOnChange attributes.