Liquibase ERROR: Column has unsupported type "serial" when creating a Redshift auto increment int column in XML - amazon-redshift

When creating a table in XML to be deployed to Redshift database using Liquibase
<createTable schemaName= "staging" tableName="tempauto">
<column name="key" type="integer" autoIncrement="true">
<constraints nullable="false"/>
</column>
ERROR: Column "key" has unsupported type "serial"
I expected the column key to be created as an int like SQL and to auto-increment.
However Liquibase converts the auto-increment column to type 'serial' which is not supported in Redshift.

Related

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

create oid column in postgres through liquibase

The following liquibase statement when run against postgres (9.3,9.4):
<createTable tableName="document">
<column name="document_data" type="Blob">
</createTable>
creates a table document_data with the type bytea.
How do I tell liquibase that I want and OID type?
Who would expect: :
<column name="document_data" type="oid">
As of Liquibase 3.5.2, blob is mapped to oid, as one would expect: https://liquibase.jira.com/browse/CORE-1863

how to create citext columns with liquibase?

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>

How can I create triggers for a postgreSQL db using liquibase?

I'm using the dropwizard-migrations module for liquibase db refactoring. See the guide here: http://dropwizard.codahale.com/manual/migrations/
When I run
java -jar my_project.jar db migrate my_project.yml
I get the following error:
ERROR [2013-09-11 20:53:43,089] liquibase: Change Set migrations.xml::11::me failed. Error: Error executing SQL CREATE OR REPLACE TRIGGER add_current_date_to_my_table BEFORE UPDATE ON my_table FOR EACH ROW EXECUTE PROCEDURE change_update_time();: ERROR: syntax error at or near "TRIGGER"
Position: 19
Here are some relevant changesets from my migrations.xml file:
<changeSet id="1" author="me">
<createProcedure>
CREATE OR REPLACE FUNCTION change_update_time() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
NEW.updated_at := CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$;
</createProcedure>
<rollback>
DROP FUNCTION change_update_time();
</rollback>
</changeSet>
<changeSet id="2" author="me">
<preConditions>
<not>
<tableExists tableName="my_table"/>
</not>
</preConditions>
<createTable tableName="my_table">
<column name="_id" type="integer" defaultValue="0">
<constraints nullable="false"/>
</column>
<column name="updated_at" type="timestamp without time zone" defaultValue="now()">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
<changeSet id="3" author="me">
<sql splitStatements="false">
CREATE OR REPLACE TRIGGER add_current_date_to_my_table BEFORE UPDATE ON my_table FOR EACH ROW EXECUTE PROCEDURE change_update_time();
</sql>
<rollback>
DROP TRIGGER add_current_date_to_my_table ON my_table;
</rollback>
</changeSet>
Is there any way I can create the trigger add_current_date_to_my_table? Is this redundant with the "RETURNS trigger" from creating the function?
The solution is:
<changeSet id="3" author="me">
<sql>
DROP TRIGGER IF EXISTS add_current_date_to_my_table ON my_table;
CREATE TRIGGER add_current_date_to_my_table BEFORE UPDATE ON my_table FOR EACH ROW EXECUTE PROCEDURE change_update_time();
</sql>
<rollback>
DROP TRIGGER add_current_date_to_my_table ON my_table;
</rollback>
</changeSet>
H/T Jens.
Ann Kilzer has provided the right answer as there is no CREATE OR REPLACE statement in Postgres, unlike PL/SQL.
So we need to split this statement into 2 atomic operations:
DROP TRIGGER
CREATE TRIGGER