Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
594 changes: 594 additions & 0 deletions README.md

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ licenses += "New BSD License" -> url("http://opensource.org/licenses/BSD-3-Claus

version := "1.1.2-SNAPSHOT"

scalaVersion := "2.10.2"
scalaVersion := "2.11.4"

// For a single major Scala release, e.g. 2.x.y, include at most one
// Scala release candidate in crossScalaVersions, e.g. "2.x.y-RC3".
Expand All @@ -27,8 +27,8 @@ scalaVersion := "2.10.2"
crossScalaVersions := Seq("2.9.0", "2.9.0-1",
"2.9.1", "2.9.1-1",
"2.9.2", "2.9.3",
"2.10.2",
"2.11.0-M3")
"2.10.4",
"2.11.4")

// Increase warnings generated by the Scala compiler.
//
Expand All @@ -54,6 +54,7 @@ scalacOptions <++= scalaVersion map { v: String =>
}

libraryDependencies ++= Seq(
"com.google.code.findbugs" % "jsr305" % "2.0.3",
"com.novocode" % "junit-interface" % "0.10-M4" % "test",
"log4jdbc" % "log4jdbc" % "1.1" from "http://log4jdbc.googlecode.com/files/log4jdbc4-1.1.jar",
"mysql" % "mysql-connector-java" % "[5.1.0,5.2)" % "test",
Expand All @@ -62,8 +63,10 @@ libraryDependencies ++= Seq(
"org.jmock" % "jmock-junit4" % "[2.5.1,3.0)" % "test",
"org.slf4j" % "slf4j-api" % "[1.5.8,2.0)",
"org.slf4j" % "slf4j-log4j12" % "[1.5.8,2.0)" % "test",
"com.h2database" % "h2" % "1.3.176" % "test",
"postgresql" % "postgresql" % "9.1-901.jdbc4" % "test")


// Run unit tests serially otherwise races can occur between two
// threads checking if the 'schema_migrations' table exists and
// trying to create it.
Expand Down
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=0.13.7
6 changes: 3 additions & 3 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.1.0")
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.5.1")

addSbtPlugin("com.jsuereth" % "xsbt-gpg-plugin" % "0.6")
addSbtPlugin("com.typesafe.sbt" % "sbt-pgp" % "0.8.1")

addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.0.1")
addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.2.0")
3 changes: 3 additions & 0 deletions src/main/scala/com/imageworks/migration/DatabaseAdapter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ object DatabaseAdapter {
case Postgresql =>
new PostgresqlDatabaseAdapter(schemaNameOpt)

case H2 =>
new H2DatabaseAdapter(schemaNameOpt)

case null =>
throw new IllegalArgumentException("Must pass a non-null vendor to " +
"this function.")
Expand Down
132 changes: 132 additions & 0 deletions src/main/scala/com/imageworks/migration/H2DatabaseAdapter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright (c) 2015 Sony Pictures Imageworks Inc.
*
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution. Neither the name of Sony Pictures Imageworks nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.imageworks.migration

trait H2AutoIncrementingColumnDefinitionMixin
extends ColumnDefinition
with ColumnSupportsAutoIncrement {
override protected abstract def sql: String = {
if (isAutoIncrement) super.sql + " AUTO_INCREMENT"
else super.sql
}
}

class H2BigintColumnDefinition
extends DefaultBigintColumnDefinition
with H2AutoIncrementingColumnDefinitionMixin

class H2IntegerColumnDefinition
extends DefaultIntegerColumnDefinition
with H2AutoIncrementingColumnDefinitionMixin

class H2SmallintColumnDefinition
extends DefaultSmallintColumnDefinition
with H2AutoIncrementingColumnDefinitionMixin

// H2 does not support size specifiers for the TIMESTAMP data type.
class H2TimestampColumnDefinition
extends ColumnDefinition
with ColumnSupportsDefault {
override val sql = "TIMESTAMP"
}

class H2DatabaseAdapter(override val schemaNameOpt: Option[String])
extends DatabaseAdapter(schemaNameOpt) {
override val vendor = H2

override val quoteCharacter = '`'

override val unquotedNameConverter = UppercaseUnquotedNameConverter

override val userFactory = PlainUserFactory

override val alterTableDropForeignKeyConstraintPhrase = "CONSTRAINT"

override val addingForeignKeyConstraintCreatesIndex = true

override val supportsCheckConstraints = false

override def columnDefinitionFactory(columnType: SqlType,
characterSetOpt: Option[CharacterSet]): ColumnDefinition = {
columnType match {
case BigintType =>
new H2BigintColumnDefinition
case BlobType =>
new DefaultBlobColumnDefinition
case BooleanType =>
new DefaultBooleanColumnDefinition
case CharType =>
new DefaultCharColumnDefinition
case DecimalType =>
new DefaultDecimalColumnDefinition
case IntegerType =>
new H2IntegerColumnDefinition
case SmallintType =>
new H2SmallintColumnDefinition
case TimestampType =>
new H2TimestampColumnDefinition
case VarbinaryType =>
new DefaultVarbinaryColumnDefinition
case VarcharType =>
new DefaultVarcharColumnDefinition
}
}

override def lockTableSql(schemaNameOpt: Option[String],
tableName: String): String = {
"SELECT * FROM " + quoteTableName(schemaNameOpt, tableName) + " FOR UPDATE"
}

override protected def alterColumnSql(schemaNameOpt: Option[String],
columnDefinition: ColumnDefinition): String = {
new java.lang.StringBuilder(512)
.append("ALTER TABLE ")
.append(quoteTableName(schemaNameOpt, columnDefinition.getTableName))
.append(" MODIFY COLUMN ")
.append(quoteColumnName(columnDefinition.getColumnName))
.append(columnDefinition.toSql)
.toString
}

override def removeIndexSql(schemaNameOpt: Option[String],
tableName: String,
indexName: String): String = {
new java.lang.StringBuilder(128)
.append("ALTER TABLE ")
.append(quoteTableName(schemaNameOpt, tableName))
.append(" DROP INDEX ")
.append(quoteIndexName(None, indexName))
.toString
}
}
19 changes: 19 additions & 0 deletions src/main/scala/com/imageworks/migration/JavaDatabaseAdapter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,23 @@ object JavaDatabaseAdapter {
def getPostgresqlDatabaseAdapter(schemaName: String): PostgresqlDatabaseAdapter = {
new PostgresqlDatabaseAdapter(Some(schemaName))
}

/**
* Create a H2 Database Adapter.
*
* @return newly constructed H2DatabaseAdapter
*/
def getH2DatabaseAdapter: H2DatabaseAdapter = {
new H2DatabaseAdapter(None)
}

/**
* Create a H2 Database Adapter.
*
* @param schemaName the default schema name in the adapter
* @return newly constructed H2DatabaseAdapter
*/
def getH2DatabaseAdapter(schemaName: String): H2DatabaseAdapter = {
new H2DatabaseAdapter(Some(schemaName))
}
}
5 changes: 5 additions & 0 deletions src/main/scala/com/imageworks/migration/Vendor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ case object Oracle
extends Vendor
case object Postgresql
extends Vendor
case object H2
extends Vendor

object Vendor {
/**
Expand Down Expand Up @@ -77,6 +79,9 @@ object Vendor {
case "org.postgresql.Driver" =>
Postgresql

case "org.h2.Driver" =>
H2

case null =>
throw new IllegalArgumentException("Must pass a non-null JDBC " +
"driver class name to this " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,7 @@
*/
package com.imageworks.migration.tests

import com.imageworks.migration.{
DatabaseAdapter,
Derby,
DerbyDatabaseAdapter,
Mysql,
MysqlDatabaseAdapter,
Oracle,
OracleDatabaseAdapter,
Postgresql,
PostgresqlDatabaseAdapter
}

import com.imageworks.migration._
import org.junit.Assert._
import org.junit.Test

Expand All @@ -61,6 +50,9 @@ class DatabaseAdapterTests {

assertEquals(classOf[PostgresqlDatabaseAdapter],
DatabaseAdapter.forVendor(Postgresql, None).getClass)

assertEquals(classOf[H2DatabaseAdapter],
DatabaseAdapter.forVendor(H2, None).getClass)
}

@Test(expected = classOf[IllegalArgumentException])
Expand Down
20 changes: 3 additions & 17 deletions src/test/scala/com/imageworks/migration/tests/MigrationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,7 @@
*/
package com.imageworks.migration.tests

import com.imageworks.migration.{
AutoCommit,
Derby,
DuplicateMigrationDescriptionException,
DuplicateMigrationVersionException,
InstallAllMigrations,
MigrateToVersion,
Migration,
Migrator,
Mysql,
Oracle,
Postgresql,
RemoveAllMigrations,
RollbackMigration,
With
}
import com.imageworks.migration._

import org.jmock.{
Expectations,
Expand Down Expand Up @@ -371,6 +356,7 @@ class MigrationTests {
case Mysql => true
case Oracle => false
case Postgresql => false
case H2 => true
}
var autoPk = 1

Expand Down Expand Up @@ -497,7 +483,7 @@ class MigrationTests {
// With JDK 1.6 or later, a java.sql.SQLSyntaxErrorException
// could be caught here, but for 1.5 compatibility, only a
// java.sql.SQLException is caught.
case _: SQLException => // expected
case e: SQLException => // expected
}

// perform grants
Expand Down
Loading