* initial setup with spring boot/ spring data jpa/ flyway

* BAEL-1315 - added flyway test extensions for spring

* BAEL-1315 - added flyway test extensions for spring

* BAEL-1315 - created multiple migration scripts and locations

* BAEL-1315 - test insert after schema creation

* cleanup

* BAEL-1315 - test data changes by a migration

* BAEL-1315 - use a single location for migrations

* BAEL-1315 - added also a JAVA class migration
This commit is contained in:
Bogdan Stoean 2017-12-10 10:27:50 +02:00 committed by Grzegorz Piwowarek
parent f9f5a82cf4
commit 5014f5088e
17 changed files with 322 additions and 1 deletions

View File

@ -1,10 +1,10 @@
<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.baeldung</groupId>
<artifactId>flyway</artifactId>
<version>1.0</version>
<name>flyway</name>
<packaging>pom</packaging>
<description>A sample project to demonstrate Flyway migrations</description>
<parent>
@ -13,6 +13,10 @@
<version>1.0.0-SNAPSHOT</version>
</parent>
<modules>
<module>spring-flyway</module>
</modules>
<dependencies>
<dependency>
<groupId>mysql</groupId>
@ -20,6 +24,17 @@
<version>${mysql.version}</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
@ -32,5 +47,6 @@
<properties>
<mysql.version>6.0.5</mysql.version>
<flyway-maven-plugin.version>4.0.3</flyway-maven-plugin.version>
<spring.boot.version>1.5.8.RELEASE</spring.boot.version>
</properties>
</project>

24
flyway/spring-flyway/.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/

View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>spring-flyway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-flyway</name>
<description>Spring Boot Test Flyway Migrations</description>
<parent>
<artifactId>flyway</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0</version>
<relativePath>../../flyway</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,12 @@
package com.baeldung.springflyway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringFlywayApplication {
public static void main(String[] args) {
SpringApplication.run(SpringFlywayApplication.class, args);
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.springflyway.entities;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Customer {
@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String email;
}

View File

@ -0,0 +1,14 @@
package com.baeldung.springflyway.migration;
import org.flywaydb.core.api.migration.spring.SpringJdbcMigration;
import org.springframework.jdbc.core.JdbcTemplate;
public class V2__uk_lastname_customer implements SpringJdbcMigration {
final String CUSTOMER_LASTNAME_UK = "ALTER TABLE customer ADD CONSTRAINT uk_customer_lastname UNIQUE(last_name);";
@Override
public void migrate(final JdbcTemplate jdbcTemplate) throws Exception {
jdbcTemplate.execute(CUSTOMER_LASTNAME_UK);
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.springflyway.repositories;
import com.baeldung.springflyway.entities.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface CustomerRepository extends JpaRepository<Customer, Long> {
Optional<Customer> findByEmail(String email);
}

View File

@ -0,0 +1,28 @@
package com.baeldung.springflyway;
import com.baeldung.springflyway.entities.Customer;
import com.baeldung.springflyway.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepositoryInitialMigrationTest {
@Autowired CustomerRepository customerRepository;
@Test
public void givenSchemaCreationMigration_whenTryingToCreateACustomer_thenSuccess() {
Customer customer = customerRepository.save(Customer
.builder()
.email("customer@email.com")
.build());
assertNotNull(customer.getId());
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.springflyway;
import com.baeldung.springflyway.entities.Customer;
import com.baeldung.springflyway.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Optional;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepositoryInsertDataMigrationTest {
@Autowired CustomerRepository customerRepository;
@Test
public void givenASetInsertData_whenRunningMigrationsWithSuccess_thenASpecificCustomerIsFound() {
Optional<Customer> customerOptional = customerRepository.findByEmail("email@email.com");
assertTrue(customerOptional.isPresent());
}
@Test
public void givenASetInsertData_whenRunningMigrationsWithSuccess_thenASetOfCustomersIsFound() {
List<Customer> customers = customerRepository.findAll();
assertNotNull(customers);
assertEquals(customers.size(), 6);
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.springflyway;
import com.baeldung.springflyway.entities.Customer;
import com.baeldung.springflyway.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepositoryNotNullConstraintMigrationTest {
@Autowired CustomerRepository customerRepository;
@Test(expected = DataIntegrityViolationException.class)
public void givenTheNotNullConstraintMigrations_whenInsertingACustomerWithNullEmail_thenThrowException() {
customerRepository.save(Customer
.builder()
.build());
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.springflyway;
import com.baeldung.springflyway.entities.Customer;
import com.baeldung.springflyway.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(properties = {
"flyway.locations[0]=db/migration", "flyway.locations[1]=com/baeldung/springflyway/migration"
})
public class CustomerRepositoryUniqueConstraintJavaMigrationTest {
@Autowired CustomerRepository customerRepository;
@Test(expected = DataIntegrityViolationException.class)
public void givenTheUniqueConstraintMigrations_whenInsertingAnExistingLastNameCustomer_thenThrowException() {
customerRepository.save(Customer
.builder()
.lastName("LastName")
.build());
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.springflyway;
import com.baeldung.springflyway.entities.Customer;
import com.baeldung.springflyway.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepositoryUniqueConstraintMigrationTest {
@Autowired CustomerRepository customerRepository;
@Test(expected = DataIntegrityViolationException.class)
public void givenTheUniqueConstraintMigrations_whenInsertingAnExistingEmailCustomer_thenThrowException() {
customerRepository.save(Customer
.builder()
.email("email@email.com")
.build());
}
}

View File

@ -0,0 +1 @@
spring.jpa.hibernate.ddl-auto=validate

View File

@ -0,0 +1,6 @@
create table if not exists customer (
id bigint AUTO_INCREMENT not null primary key,
first_name varchar(255) ,
last_name varchar(255) ,
email varchar(255)
);

View File

@ -0,0 +1,6 @@
insert into customer (first_name, last_name, email) values ('FirstName', 'LastName', 'email@email.com');
insert into customer (first_name, last_name, email) values ('FirstName1', 'LastName1', 'email1@email.com');
insert into customer (first_name, last_name, email) values ('FirstName2', 'LastName2', 'email2@email.com');
insert into customer (first_name, last_name, email) values ('FirstName3', 'LastName3', 'email3@email.com');
insert into customer (first_name, last_name, email) values ('FirstName4', 'LastName4', 'email4@email.com');
insert into customer (first_name, last_name, email) values ('FirstName5', 'LastName5', 'email5@email.com');

View File

@ -0,0 +1 @@
ALTER TABLE customer ALTER email SET NOT NULL;

View File

@ -0,0 +1 @@
ALTER TABLE customer ADD CONSTRAINT uk_customer_email UNIQUE(email);