how to create citext columns with liquibase? - postgresql

I've got a postgresql database that is created from liquibase. How can I configure liquibase to create CITEXT columns?
I ran across this URL but unsure where to put this configuration. I'm using version 3.3.2.
https://liquibase.jira.com/browse/CORE-1234
Thanks for any help.

You will need to edit the changelog file. You didn't specify what format your changelog is in, so this assumes XML, but the instructions are similar for other formats.
Basically, if you have a column that should be CITEXT, you can use that type in the XML declaration:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog">
<changeSet author="Pete" id="1430490076262-3216" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<createTable tableName="AA_BUILD">
<column name="STAGE" type="NUMBER">
<constraints nullable="false"/>
</column>
<column name="ITERATION" type="NUMBER">
<constraints nullable="false"/>
</column>
<column name="BUILD_NUM" type="NUMBER">
<constraints nullable="false"/>
</column>
<column name="CHANGED_IN_DEV_DT" type="date"/>
<column name="DESCRIPTION" type="CITEXT"/>
</createTable>
</changeSet>

Related

NLog not using UTF-8 encoding

Since I upgraded NLog to the latest version 5.1.0, logging special characters (e.g. Ö, ä or ß) stopped working. Outout to file is like: ö ä ü for these chars.
I checked the log file with NotePad++ and it says that the encoding is ANSI. However, I have configured NLog to use utf-8. My config:
<target name="logfile" xsi:type="File" fileName="${basedir}/Logs/nLog.csv" archiveAboveSize="50000000" archiveNumbering="Sequence" maxArchiveFiles="3" encoding="utf-8" keepFileOpen="true">
<layout xsi:type="CsvLayout">
<column name="time" layout="${longdate}" />
<column name="level" layout="${level}"/>
<column name="category" layout="${event-context:item=category}" />
<column name="message" layout="${message}" />
</layout>
</target>
What am I missing?
Based on the comments, I was able to solve the problem. However, I do not know what the real cause of the problem was.
Solution: remove encoding="utf-8" from the nLog config AND read the log file as Encoding.UTF-8 (i.e. when opening the file with a StreamReader).
I don't know why this works since nLog uses UTF-8 as default...

Error when adding new table on service.xml - Liferay 6.2

I'm using liferay 6.2 and I want to add a new table on service.xml.
The new entry on service.xml is:
<entity name="FeedError" local-service="true" remote-service="false" cache-enabled="false">
<column name="feId" type="long" primary="true" id-type="identity" />
<column name="errorCode" type="String" />
<column name="translation" type="String" />
<column name="facility" type="String" />
<column name="recordId" type="long" />
<column name="dateTime" type="Date" />
</entity>
Then, when running Build Service with Ant or Liferay Plugin, I got the following error:
BUILD FAILED
/Liferay/liferay-plugins-sdk-6.2/build-common-plugin.xml:443: The following error occurred while executing this line:
/Liferay/liferay-plugins-sdk-6.2/build-common-plugin.xml:147: Service Builder generated exceptions.
Looking at build-common-plugin.xml:443 is:
439:    <build-service
440:     service.api.dir="${basedir}/${service.base.dir}/service"
441:     service.sql.dir="${basedir}/${service.base.dir}/sql"
442:     service.test.dir=""
443:     />
The error is only when adding a new entry to service.xml; for existing ones, it builds without any issue. 
Any help is great appreciate! 
I hardly remember that there was an issue with the keyword "error" in table or column names. The service builder once greped for this keyword. You might give it a try to not use the word "error" in your column definition.

Liquibase databasechangelog xml file - how to create enum in postgresql

I use liquibase and I want to create enum in my xml file (PostgreSQL).
As an example below changelog file (only a piece of the file) that creates new table:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<changeSet id="r3.3.0_table_creation_research_document "
author="anonim">
<createTable tableName="research_document">
<column name="id" type="bigint">
<constraints primaryKey="true"
primaryKeyName="research_document_pkey" nullable="false" />
</column>
I can't find any info/examples in Internet!
I don't think liquibase supports enum for postgres natively in the xml format. However, since it is possible in postgres, you could always use liquibase's formatted sql instead of xml:
--liquibase formatted sql
--changeset ronak:1
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
CREATE TABLE person (
name text,
current_mood mood
);
--rollback drop table person;

Unique constraint is automatically created in database

I have discovered in our Postgres Database there is a unique constraint on a table, which we haven´t created.
Error message:
SqlExceptionHelper - FEHLER: doppelter Schlüsselwert verletzt Unique-Constraint »uconstr_service_out_mapping«
Detail: Schlüssel »(service_id, mapping_id)=(7, 262144)« existiert bereits.
My first thought was, that Hibernate has to generate this constraint automatically. But we disabled the Auto-Generation of the DDL with the following configuration:
<property name="hibernate.hbm2ddl.auto" value=""/>
We are using Liquibase to create the table:
<changeSet author="foo" id="bar">
<createTable tableName="service_in_mapping">
<column name="masterdata_type" type="INT4">
<constraints nullable="false"/>
</column>
<column name="service_id" type="INT8">
<constraints nullable="false"/>
</column>
<column name="service_type" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
<column name="non_matching" type="VARCHAR(20)"/>
<column name="mapping_id" type="INT8"/>
</createTable>
</changeSet>
How can this unique constraint be constantly created? Any ideas?
Used Dependency Versions:
Hibernate 5.2.8
Postgres Jdbc 9.1-901.jdbc4

Exported Timestamp is changing value after loading back to DB

My scenario is to export data from the real DB2 and then load it back for integration tests purpose. I am using liquibase (v3.5.3) to manage it.
I have found that TIMESTAMP values are changed during this cycle. When timestamp value is exported I can see it in changelog file as "2018-06-28 22:47:38.816343". After that when I load it back into DB2 it becomes "2018-06-28 23:01:14.343".
The reason is that the "816343" part is treated not as a part of a second but rather as milliseconds amount and that amount is added to the result timestamp.
In the tests a business decision criteria is made by comparing of those timestamps. I need them to be equal.
Any thoughts and proposals will be appreciated.
There are steps to reproduce:
1. Create a file "01_test_data_to_setup.xml" with content
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<changeSet author="OK" id="1">
<createTable tableName="TT">
<column name="TS_LOADED" type="TIMESTAMP"/>
<column name="TS_GENERATED" type="TIMESTAMP" defaultValueComputed="CURRENT_TIMESTAMP">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
<changeSet author="OK" id="2">
<insert tableName="TT">
<column name="TS_LOADED"/>
</insert>
</changeSet>
</databaseChangeLog>
2. Execute above changelog file with liquibase update command
liquibase.bat --driver=com.ibm.db2.jcc.DB2Driver --logLevel=info --classpath=~jdbc driver path here~ --changeLogFile=01_test_data_to_setup.xml --url=jdbc:db2:~jdbc url here~ --defaultSchemaName=~schema name here~ --username=~user name here~ --password=~password here~ update
3. Export data from DB
liquibase.bat --driver=com.ibm.db2.jcc.DB2Driver --logLevel=info --classpath=~jdbc driver path here~ --changeLogFile=db2_exported_test_data.xml --url=jdbc:db2:~jdbc url here~ --defaultSchemaName=~schema name here~ --username=~user name here~ --password=~password here~ --diffTypes="data" generateChangeLog --includeObjects="table:TT"
As result you will have "db2_exported_test_data.xml" file
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<changeSet author="OK (generated)" id="1530226079041-1">
<insert tableName="TT">
<column name="TS_LOADED"/>
<column name="TS_GENERATED" valueDate=~your generated timestamp value here. in my case it's "2018-06-28 22:47:38.816343"/>
</insert>
</changeSet>
</databaseChangeLog>
4. A file to load exported timestamp back to DB "02_test_data_to_load.xml"
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<changeSet author="OK" id="3">
<insert tableName="TT">
<column name="TS_LOADED" valueDate=~value in TS_GENERATED from previous step, i.e."2018-06-28 22:47:38.816343"~/>
</insert>
</changeSet>
</databaseChangeLog>
Liquibase command
liquibase.bat --driver=com.ibm.db2.jcc.DB2Driver --logLevel=info --classpath=~jdbc driver path here~ --changeLogFile=02_test_data_to_load.xml --url=jdbc:db2:~jdbc url here~ --defaultSchemaName=~schema name here~ --username=~user name here~ --password=~password here~ update
5. Check exported and loaded timestamp in DB or export TT table data one more time.
TS_LOADED timestamp value in second row will be different from TS_GENERATED value in first row
I would guess this is a bug in Liquibase similar to this one
https://liquibase.jira.com/browse/CORE-1958
Or your datatype is TIMESTAMP(3) on the target table