Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flyway integration with spring boot doesn't execute migration scripts on embedded H2 database

I am trying to make migration demo on embedded H2 data base in Spring Boot application using Flyway.

application.properties

logging.level.org.org.springframework=DEBUG
server.port=8181
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.flyway.baseline-on-migrate=true
spring.jpa.hibernate.ddl-auto=none

migration-script(V2__create_shipwreck.sql) under db/migration

CREATE TABLE SHIPWRECK(ID INT AUTO_INCREMENT,
    NAME VARCHAR(255),
    DESCRIPTION VARCHAR(2000),
    CONDITION VARCHAR(255),
    DEPTH INT,
    LATITUDE DOUBLE,
    LANGITUDE DOUBLE,
    YEARS_DISCOERED INT);

console log

INFO 7284 --- [main] o.f.c.internal.database.DatabaseFactory: Database: jdbc:h2:mem:testdb (H2 1.4)

INFO 7284 --- [main] o.f.core.internal.command.DbValidate: Successfully validated 1 migration (execution time 00:00.031s)

INFO 7284 --- [main] o.f.c.i.s.JdbcTableSchemaHistory: Creating Schema History table: "PUBLIC"."flyway_schema_history"

INFO 7284 --- [main] o.f.core.internal.command.DbMigrate: Current version of schema "PUBLIC": << Empty Schema >>

INFO 7284 --- [main] o.f.core.internal.command.DbMigrate: Migrating schema "PUBLIC" to version 2 - create shipwreck

INFO 7284 --- [main] o.f.core.internal.command.DbMigrate : Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.098s)

INFO 7284 --- [main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'

INFO 7284 --- [main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default...]

main] org.hibernate.Version : HHH000412: Hibernate Core {5.2.14.Final}

main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found

main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}

main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect

main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.boot</groupId>
    <artifactId>das-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>das-boot</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-core</artifactId>
        <version>5.0.7</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

H2 database tables UI

database tables

After starting the Spring Boot application, the table has not been created, so what's the problem here?

like image 538
Elsayed Avatar asked May 07 '18 13:05

Elsayed


1 Answers

I had the same issue, it was solved after making the change to application.properties as datasource.url to file instead of mem

able to create table with details Correct: spring.datasource.url=jdbc:h2:file:~/dasboot

application.properties

logging.level.org.org.springframework=DEBUG
server.port=8080

spring.h2.console.enabled=true
spring.h2.console.path=/h2

spring.datasource.url=jdbc:h2:file:~/dasboot
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

spring.flyway.baseline-on-migrate=true
spring.jpa.hibernate.ddl-auto=none

POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.boot</groupId>
    <artifactId>das-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <name>das-boot</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

    <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
                </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

shipwreck table was created and persisted in h2 db

like image 115
Sanish Ramankutty Avatar answered Sep 29 '22 20:09

Sanish Ramankutty