Liquibase: added autoIncrement column into a table with data already present - postgresql

Hi guys I have some doubt about Liquibase
I created a table (PostgreSQL) with a classical changeset
<changeSet id="create_table">
<createTable tableName="table" schemaName="schema">
<column name="name" type="varchar">
<constraints nullable="false"/>
</column>
<column name="surname" type="varchar">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
I've already added some data to this table.
I want to add a new column 'id' and make it as a primary key as an autoincrement column.
How can I do that?
Because if I try to add a changeset like this:
<changeSet id="added_pk">
<addColumn tableName="table"
schemaName="schema">
<column name="id" type="bigint">
<constraints unique="true" nullable="false" uniqueConstraintName="PK_TABLE_ID"/>
</column>
</addColumn>
<addAutoIncrement columnDataType="bigint"
columnName="id"
tableName="table"
schemaName="schema"
incrementBy="1" startWith="1"/>
</changeSet>
I obtain error when I run the script: "column "id" of relation "table" contains null values".
It depends of the already existing values. How could I manage this situation? (I prefer to avoid to truncate all the data before add the new column)
Thanks a lot!
Regards

Rename the current table in the DB. Delete the liquibase changelogs. Rerun the liquibase file including the new id field (you can combine the two changeSets, this way you also get to decide the position of the id field). After the new table has been created you can transfer the data from the old table into the new table. This way the data from the old table will automatically receive ids. Delete the old table at the end.

Related

Liferay 7 entity not accepting date as primary key

So i am using liferay 7
and i created many columns in order to get infos from the databse,
i have a primary key which will be composed of three columns ( int , String ,Date)
but the problem is that liferay7 is not accepting DATE as a primary key and not even building service when i
add primary="true" to the column which is a date
<column db-name="A" type="Date" name="date" ></column>
<column db-name="B" type="int" name="id" primary="true"></column>
<column db-name="C" type="String" name="idU" primary="true"></column>

Spring Data Envers + Liquibase: how to create audited tables and update them automatically?

I would like to audit my models in my Spring Boot app. I use Liquibase for db migration. Say I have this entity :
#Entity
#Audited
public class User {
#Id
private Long id;
private String name;
private String firstName;
}
I create the user table using Liquibase
<changeSet id="user_table">
<createTable tableName="user">
<column name="id" type="bigint">
<constraints primaryKey="true"/>
</column>
<column name="name" type="VARCHAR(255)"/>
<column name="first_name" type="varchar(255)"/>
</createTable>
</changeSet>
How can I create the user_AUD table used for auditing? I would like to avoid creating it manually because if later I add other fields to the User entity I am sure that I will forget to add them to user_AUD, and it's too tedious to do it manually. The same question is for the REVINFO table (how to auto create it?)
Note that I disabled hibernate.ddl-auto property since I use Liquibase.
Thanks a lot for your help.

Force Liquibase to map Blob to BYTEA on PostgreSQL

How to tell Liquibase to map BLOB datatype to BYTEA on PostgreSQL?
It seems that Hibernate people has taken over and adapted the tool to their needs: https://liquibase.jira.com/browse/CORE-1863 , however, EclipseLink don't support oid's and the bug seems to be still open: https://bugs.eclipse.org/bugs/show_bug.cgi?id=337467
I need to use EclipseLink, and I need to use blobs with PostgreSQL. I'd like to use Liquibase, is it possible to make those things work together?
You have two options.
If you only need this for Postgres and don't plan to support other DBMS, simply use bytea as the column type.
Any data type that is not listed as one of the "generic" types in the description of the column tag will be passed "as-is" to the database, e.g.
<createTable tableName="foo">
<column name="id" type="integer"/>
<column name="picture" type="bytea"/>
</createTable>
If you want to support different DBMS, you can define a property depending on the DBMS:
<property name="blob_type" value="bytea" dbms="postgresql"/>
<property name="blob_type" value="blob" dbms="oracle"/>
then later
<createTable tableName="foo">
<column name="id" type="integer"/>
<column name="picture" type="${blob_type}"/>
</createTable>

Liquibase database autocomplete using Eclipse

Is there a way to get autocomplete for columns and tables from my database in a changelog file ?
<changeSet id="001" author="test">
<insert tableName="person">
<column name="id" valueComputed="seqPerson.NEXTVAL" />
<column name="myName" value="test" />
</insert>
<rollback>
DELETE FROM person WHERE myName='test';
</rollback>
</changeSet>
In this example I would like to have autocomplete for person, id, myName and seqPerson (and the Rollback tag...).
Note that I already have the LiquiBase tags autocompletion working from the xsd.
No, this is not currently possible. You might look at Datical DB - it has a changeset wizard that provides this sort of functionality.
Disclaimer - I work for Datical.

liquibase:updateSQL does not create table schema prefix

I run liquibase:updateSQL with the following parameters on postgres:
changelogSchemaName online
defaultCatalogName online
defaultSchemaName online
It generates SQL like CREATE TABLE product but what I would expect is CREATE TABLE online.product
With the generated SQL the users search_path is used so I need to modify my DB before I can use liquibase:update
any Ideas how to fix that?
Have you used the catalogName attribute as described in the documentation?
For example:
<changeSet author="liquibase-docs" id="createTable-example">
<createTable catalogName="online" tableName="product"
<column name="x" type="varchar(255)"/>
<column name="y" type="varchar(255)"/>
</createTable>
</changeSet>
Use the outputDefaultCatalog=true or outputDefaultSchema=true maven parameters. That will force the generated SQL to include the default schema even if it is not specified in the changeSet.