Spring Flyway Support: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 28: Line 28:
  Error creating bean with name 'flywayInitializer' defined in class path resource ...: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Found non-empty schema(s) "public" without schema history table! Use baseline() or set baselineOnMigrate to true to initialize the schema history table.
  Error creating bean with name 'flywayInitializer' defined in class path resource ...: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Found non-empty schema(s) "public" without schema history table! Use baseline() or set baselineOnMigrate to true to initialize the schema history table.


=Configuration=
Schema history table can be initiated by setting "baselineOnMigrate" to true:
spring.flyway.baselineOnMigrate=true
 
Upon initialization, a "flyway_schema_history" table is created:


{{External|https://github.com/spring-projects/spring-boot/blob/v2.1.1.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java}}
                      Table "public.flyway_schema_history"
      Column    |            Type            | Collation | Nullable | Default
----------------+-----------------------------+-----------+----------+---------
  installed_rank | integer                    |          | not null |
  version        | character varying(50)      |          |          |
  description    | character varying(200)      |          | not null |
  type          | character varying(20)      |          | not null |
  script        | character varying(1000)    |          | not null |
  checksum      | integer                    |          |          |
  installed_by  | character varying(100)      |          | not null |
  installed_on  | timestamp without time zone |          | not null | now()
  execution_time | integer                    |          | not null |
  success        | boolean                    |          | not null |
Indexes:
    "flyway_schema_history_pk" PRIMARY KEY, btree (installed_rank)
    "flyway_schema_history_s_idx" btree (success)


=Organizatorium=
A successful verification looks like this in the logs:


* Disable flyway support:
<syntaxhighlight lang='text'>
Flyway Community Edition 5.1.4 by Boxfuse
...
Database: jdbc:postgresql://localhost/fds_test (PostgreSQL 10.6)
Successfully validated 1 migration (execution time 00:00.016s)
Current version of schema "public": 1
Schema "public" is up to date. No migration necessary.
</syntaxhighlight>


=How to Enable Flyway=
=Configuration=
 
{{External|https://github.com/spring-projects/spring-boot/blob/v2.1.1.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java}}


=How to Disable Flyway=
=How to Disable Flyway=
Remove "org.flywaydb:flyway-core"  from the classpath.

Latest revision as of 00:17, 5 December 2018

Internal

Overview

TODO

  • Understand how main/resource/db/migration/V1.0_init.sql drives schema creation with Spring Data JPA.

Database Migration on Startup

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-execute-flyway-database-migrations-on-startup

Flyway database migration is automatically triggered on startup if "org.flywaydb:flyway-core" dependency is present in the application's classpath.

The migration script naming convention is V<version>_<name>.sql where:

  • <version> is an underscore separated version, such as "1" or "1_2".
  • <name> ?

The migration script default location is "classpath:db/migration[/{vendor}]", but it can be overridden with the "spring.flyway.locations" property, which should contain a comma-separated list of one or more classpath: or filesystem: locations.

spring.flyway.locations=classpath:db/migration,filesystem:/opt/migration

The {vendor} placeholder is set according to the type of database.

Flyway requires a schema history table to drive migration off of. If the schema history table is not found, the migration fails with:

Error creating bean with name 'flywayInitializer' defined in class path resource ...: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Found non-empty schema(s) "public" without schema history table! Use baseline() or set baselineOnMigrate to true to initialize the schema history table.

Schema history table can be initiated by setting "baselineOnMigrate" to true:

spring.flyway.baselineOnMigrate=true

Upon initialization, a "flyway_schema_history" table is created:

                     Table "public.flyway_schema_history"
     Column     |            Type             | Collation | Nullable | Default
----------------+-----------------------------+-----------+----------+---------
 installed_rank | integer                     |           | not null |
 version        | character varying(50)       |           |          |
 description    | character varying(200)      |           | not null |
 type           | character varying(20)       |           | not null |
 script         | character varying(1000)     |           | not null |
 checksum       | integer                     |           |          |
 installed_by   | character varying(100)      |           | not null |
 installed_on   | timestamp without time zone |           | not null | now()
 execution_time | integer                     |           | not null |
 success        | boolean                     |           | not null |
Indexes:
   "flyway_schema_history_pk" PRIMARY KEY, btree (installed_rank)
   "flyway_schema_history_s_idx" btree (success)

A successful verification looks like this in the logs:

Flyway Community Edition 5.1.4 by Boxfuse
...
Database: jdbc:postgresql://localhost/fds_test (PostgreSQL 10.6)
Successfully validated 1 migration (execution time 00:00.016s)
Current version of schema "public": 1
Schema "public" is up to date. No migration necessary.

Configuration

https://github.com/spring-projects/spring-boot/blob/v2.1.1.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java

How to Disable Flyway

Remove "org.flywaydb:flyway-core" from the classpath.