dramcio
dramcio

Reputation: 641

LiquiBase & Spring boot - missing sequence

In liquibase my changeset looks as below:

    <createSequence schemaName="public"
                    incrementBy="1"
                    minValue="1"
                    sequenceName="user_seq" />

    <createTable tableName="user" schemaName="public">
        <column name="id" type="bigint" defaultValueSequenceNext="user_seq">
            <constraints nullable="false" primaryKey="true"/>
        </column>
    </createTable>

My entity:

@Entity
@Table(name = "user")
public class User {
    @SequenceGenerator(name="USER_SEQ",sequenceName="USER_SEQ")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_SEQ")
    @Id
    protected long id;
}

Validation step in spring boot does not pass. Hibernate throws:

Schema-validation: missing sequence [public.user_seq]

LiquiBase execute this query:

CREATE SEQUENCE public.user_seq INCREMENT BY 1 MINVALUE 1
Sequence user_seq created

When I change ddl-auto to update, hibernate execute this query: create sequence public.user_seq start 1 increment 50 And JDBC throws exception: Sequence "user_seq" already exists; SQL statement:.

How to correctly create sequence in LiquiBase?

-- @Edit1 - I Try to use lowercase in entity: USER_SEQ -> user_seq - does not help

Upvotes: 4

Views: 4192

Answers (1)

Essex Boy
Essex Boy

Reputation: 7950

I'm using spring boot

Here's my application.properties

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=none
spring.liquibase.change-log=classpath:liquibase-changeLog.xml

Here's my entity

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_SEQ")
    @SequenceGenerator(name = "USER_SEQ", sequenceName = "USER_SEQ", allocationSize = 1)
    private long id;

    private String name;

Here's my change set

<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.4.xsd">

    <changeSet author="EssexBoy" id="101">

        <createSequence schemaName="public" startValue="1" incrementBy="1" ordered="true" sequenceName="user_seq"/>

        <createTable tableName="user" schemaName="public">
            <column name="id" type="bigint">
                <constraints nullable="false" primaryKey="true"/>
            </column>
            <column name="name" type="varchar(50)"/>
        </createTable>

    </changeSet>

</databaseChangeLog>

Here's my test

@RunWith(SpringRunner.class)
@SpringBootTest
public class LiquibaseExampleApplicationTests {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private UserRepository repository;

    @Test
    public void test1() throws Exception {
        repository.save(makeUser("EssexBoy"));
        repository.save(makeUser("EssexDad"));
        repository.save(makeUser("EssexMum"));
        repository.save(makeUser("EssexBaby"));

        repository.findAll().forEach(System.out::println);
    }

    private User makeUser(String name) {
        User user = new User();
        user.setName(name);
        return user;
    }
}

Output is

User{id=1, name='EssexBoy'}
User{id=2, name='EssexDad'}
User{id=3, name='EssexMum'}
User{id=4, name='EssexBaby'}

Upvotes: 2

Related Questions