Commits

Micha Kops committed 40f4a6b

Initial import.

  • Participants

Comments (0)

Files changed (8)

+.project
+.settings
+.classpath
+target
+# Flyway DB Migrations Tutorial
+
+Examples for using the flyway framework and maven for database migrations in an JPA 2 project.
+
+Please feel free to take a look at my blog at [www.hascode.com] for the full tutorial.
+
+----
+
+   [www.hascode.com]:http://www.hascode.com/
+
+**2013 Micha Kops / hasCode.com**
+<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.hascode.tutorial</groupId>
+	<artifactId>flyway-migration-tutorial</artifactId>
+	<version>1.0.0</version>
+	<prerequisites>
+		<maven>3.0.4</maven>
+	</prerequisites>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<h2.version>1.3.170</h2.version>
+	</properties>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>com.googlecode.flyway</groupId>
+				<artifactId>flyway-maven-plugin</artifactId>
+				<version>2.1.1</version>
+				<configuration>
+					<url>jdbc:h2:file:target/foobar</url>
+					<user>sa</user>
+				</configuration>
+				<dependencies>
+					<dependency>
+						<groupId>com.h2database</groupId>
+						<artifactId>h2</artifactId>
+						<version>${h2.version}</version>
+					</dependency>
+				</dependencies>
+			</plugin>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>3.0</version>
+				<configuration>
+					<source>1.7</source>
+					<target>1.7</target>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+	<dependencies>
+		<dependency>
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-entitymanager</artifactId>
+			<version>4.2.0.Final</version>
+		</dependency>
+		<dependency>
+			<groupId>com.h2database</groupId>
+			<artifactId>h2</artifactId>
+			<version>${h2.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.11</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit-dep</artifactId>
+			<version>4.11</version>
+			<scope>test</scope>
+		</dependency>
+	</dependencies>
+</project>

File src/main/java/com/hascode/entity/Book.java

+package com.hascode.entity;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+@Entity(name = "Book")
+public class Book {
+	@Id
+	@GeneratedValue
+	@Column(name = "id")
+	private Long id;
+
+	@Column(name = "title", length = 200, nullable = false)
+	private String title;
+
+	@Column(name = "author", length = 200)
+	private String author;
+
+	public final Long getId() {
+		return id;
+	}
+
+	public final void setId(final Long id) {
+		this.id = id;
+	}
+
+	public final String getTitle() {
+		return title;
+	}
+
+	public final void setTitle(final String title) {
+		this.title = title;
+	}
+
+	public final String getAuthor() {
+		return author;
+	}
+
+	public final void setAuthor(final String author) {
+		this.author = author;
+	}
+
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result + ((id == null) ? 0 : id.hashCode());
+		return result;
+	}
+
+	@Override
+	public boolean equals(final Object obj) {
+		if (this == obj)
+			return true;
+		if (obj == null)
+			return false;
+		if (getClass() != obj.getClass())
+			return false;
+		Book other = (Book) obj;
+		if (id == null) {
+			if (other.id != null)
+				return false;
+		} else if (!id.equals(other.id))
+			return false;
+		return true;
+	}
+}

File src/main/resources/db/migration/V1__Create_book_table.sql

+CREATE TABLE Book (
+    id INT PRIMARY KEY,
+    title VARCHAR(200) NOT NULL
+);

File src/main/resources/db/migration/V2__Insert_books.sql

+INSERT INTO Book (id, title) VALUES (0, 'The first book');

File src/main/resources/db/migration/V3__Alter_book_table_add_author.sql

+ALTER TABLE Book ADD author VARCHAR(200);

File src/test/java/it/BookIT.java

+package it;
+
+public class BookIT {
+
+}