aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core/src
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2015-05-20 23:30:46 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2015-05-20 23:30:46 +0200
commit0c5700c5e02397096da33b31401d9e489e7674bb (patch)
treee231c17765666c48e7cd7093e381262fcbf3db2b /sonar-core/src
parent6a3878fc89eedc318c941ee76ef961a124d2094c (diff)
downloadsonarqube-0c5700c5e02397096da33b31401d9e489e7674bb.tar.gz
sonarqube-0c5700c5e02397096da33b31401d9e489e7674bb.zip
Remove Hibernate model SchemaMigration
Diffstat (limited to 'sonar-core/src')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/SchemaMigrationMapper.java2
-rw-r--r--sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java54
-rw-r--r--sonar-core/src/main/java/org/sonar/jpa/session/AbstractDatabaseConnector.java22
-rw-r--r--sonar-core/src/main/java/org/sonar/jpa/session/DatabaseConnector.java10
-rw-r--r--sonar-core/src/main/java/org/sonar/jpa/session/MemoryDatabaseConnector.java57
-rw-r--r--sonar-core/src/main/resources/META-INF/persistence.xml1
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/persistence/SchemaMigrationMapper.xml7
-rw-r--r--sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java22
8 files changed, 28 insertions, 147 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/SchemaMigrationMapper.java b/sonar-core/src/main/java/org/sonar/core/persistence/SchemaMigrationMapper.java
index 989daebd6c4..8dc7d70a653 100644
--- a/sonar-core/src/main/java/org/sonar/core/persistence/SchemaMigrationMapper.java
+++ b/sonar-core/src/main/java/org/sonar/core/persistence/SchemaMigrationMapper.java
@@ -23,4 +23,6 @@ import java.util.List;
public interface SchemaMigrationMapper {
List<Integer> selectVersions();
+
+ void insert(String version);
}
diff --git a/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java b/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java
deleted file mode 100644
index c805ad07ddc..00000000000
--- a/sonar-core/src/main/java/org/sonar/jpa/entity/SchemaMigration.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.jpa.entity;
-
-import org.apache.commons.lang.builder.ReflectionToStringBuilder;
-import org.apache.commons.lang.builder.ToStringStyle;
-
-import javax.persistence.*;
-
-/**
- * Still used for Hibernate unit tests
- */
-@Entity
-@Table(name = "schema_migrations", uniqueConstraints = {@UniqueConstraint(columnNames = {"version"})})
-public class SchemaMigration {
-
- @Id
- @Column(name = "version", updatable = true)
- private String version;
-
- public String getVersion() {
- return version;
- }
-
- public void setVersion(String s) {
- this.version = s;
- }
-
- public void setVersion(int i) {
- this.version = String.valueOf(i);
- }
-
- @Override
- public String toString() {
- return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString();
- }
-}
diff --git a/sonar-core/src/main/java/org/sonar/jpa/session/AbstractDatabaseConnector.java b/sonar-core/src/main/java/org/sonar/jpa/session/AbstractDatabaseConnector.java
index d991e2c4485..5aacf76dd03 100644
--- a/sonar-core/src/main/java/org/sonar/jpa/session/AbstractDatabaseConnector.java
+++ b/sonar-core/src/main/java/org/sonar/jpa/session/AbstractDatabaseConnector.java
@@ -19,18 +19,16 @@
*/
package org.sonar.jpa.session;
+import java.util.Map;
+import java.util.Properties;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.core.persistence.Database;
-import org.sonar.core.persistence.DatabaseVersion;
import org.sonar.core.persistence.dialect.Dialect;
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.Persistence;
-import java.util.Map;
-import java.util.Properties;
-
public abstract class AbstractDatabaseConnector implements DatabaseConnector {
protected static final Logger LOG = LoggerFactory.getLogger(AbstractDatabaseConnector.class);
@@ -55,11 +53,6 @@ public abstract class AbstractDatabaseConnector implements DatabaseConnector {
database = null;
}
- @Override
- public EntityManagerFactory getEntityManagerFactory() {
- return factory;
- }
-
protected EntityManagerFactory createEntityManagerFactory() {
// other settings are stored into /META-INF/persistence.xml
Properties props = database.getHibernateProperties();
@@ -81,11 +74,6 @@ public abstract class AbstractDatabaseConnector implements DatabaseConnector {
}
@Override
- public final int getDatabaseVersion() {
- throw new UnsupportedOperationException("Moved to " + DatabaseVersion.class.getCanonicalName());
- }
-
- @Override
public final Dialect getDialect() {
return database.getDialect();
}
diff --git a/sonar-core/src/main/java/org/sonar/jpa/session/DatabaseConnector.java b/sonar-core/src/main/java/org/sonar/jpa/session/DatabaseConnector.java
index 44d81434218..c76f10bc55e 100644
--- a/sonar-core/src/main/java/org/sonar/jpa/session/DatabaseConnector.java
+++ b/sonar-core/src/main/java/org/sonar/jpa/session/DatabaseConnector.java
@@ -19,12 +19,10 @@
*/
package org.sonar.jpa.session;
-import org.sonar.core.persistence.dialect.Dialect;
-
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
import java.sql.Connection;
import java.sql.SQLException;
+import javax.persistence.EntityManager;
+import org.sonar.core.persistence.dialect.Dialect;
public interface DatabaseConnector {
@@ -32,10 +30,6 @@ public interface DatabaseConnector {
Connection getConnection() throws SQLException;
- EntityManagerFactory getEntityManagerFactory();
-
EntityManager createEntityManager();
- int getDatabaseVersion();
-
}
diff --git a/sonar-core/src/main/java/org/sonar/jpa/session/MemoryDatabaseConnector.java b/sonar-core/src/main/java/org/sonar/jpa/session/MemoryDatabaseConnector.java
deleted file mode 100644
index 52329635f55..00000000000
--- a/sonar-core/src/main/java/org/sonar/jpa/session/MemoryDatabaseConnector.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.jpa.session;
-
-import org.sonar.core.persistence.Database;
-import org.sonar.core.persistence.DatabaseVersion;
-import org.sonar.jpa.entity.SchemaMigration;
-
-import javax.persistence.EntityManager;
-
-public class MemoryDatabaseConnector extends DefaultDatabaseConnector {
- private int version;
-
- public MemoryDatabaseConnector(Database database) {
- super(database);
- version = DatabaseVersion.LAST_VERSION;
- }
-
- @Override
- public void start() {
- super.start();
- setupSchemaVersion(version);
- }
-
- protected void setupSchemaVersion(int version) {
- SchemaMigration migration = new SchemaMigration();
- migration.setVersion(version);
- EntityManager manager = createEntityManager();
- try {
- manager.getTransaction().begin();
- manager.persist(migration);
- manager.getTransaction().commit();
-
- } finally {
- if (manager != null) {
- manager.close();
- }
- }
- }
-}
diff --git a/sonar-core/src/main/resources/META-INF/persistence.xml b/sonar-core/src/main/resources/META-INF/persistence.xml
index 42388ef66e3..7ec76949f11 100644
--- a/sonar-core/src/main/resources/META-INF/persistence.xml
+++ b/sonar-core/src/main/resources/META-INF/persistence.xml
@@ -6,7 +6,6 @@
<persistence-unit name="sonar" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
- <class>org.sonar.jpa.entity.SchemaMigration</class>
<class>org.sonar.jpa.entity.ManualMeasure</class>
<class>org.sonar.api.database.configuration.Property</class>
<class>org.sonar.api.database.model.User</class>
diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/SchemaMigrationMapper.xml b/sonar-core/src/main/resources/org/sonar/core/persistence/SchemaMigrationMapper.xml
index 2df5fc4b400..4f8e08eafc9 100644
--- a/sonar-core/src/main/resources/org/sonar/core/persistence/SchemaMigrationMapper.xml
+++ b/sonar-core/src/main/resources/org/sonar/core/persistence/SchemaMigrationMapper.xml
@@ -3,9 +3,12 @@
<mapper namespace="org.sonar.core.persistence.SchemaMigrationMapper">
- <select id="selectVersions" resultType="int" >
- select * from schema_migrations
+ <select id="selectVersions" resultType="int">
+ select version from schema_migrations
</select>
+ <insert id="insert" parameterType="string" useGeneratedKeys="false">
+ insert into schema_migrations(version) values (#{version,jdbcType=VARCHAR})
+ </insert>
</mapper>
diff --git a/sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java b/sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java
index 7eecf83d110..7d70b9b2a97 100644
--- a/sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java
+++ b/sonar-core/src/test/java/org/sonar/jpa/test/AbstractDbUnitTestCase.java
@@ -19,7 +19,10 @@
*/
package org.sonar.jpa.test;
+import java.io.InputStream;
+import java.sql.SQLException;
import org.apache.commons.io.IOUtils;
+import org.apache.ibatis.session.SqlSession;
import org.dbunit.Assertion;
import org.dbunit.DataSourceDatabaseTester;
import org.dbunit.DatabaseUnitException;
@@ -42,14 +45,13 @@ import org.sonar.core.cluster.NullQueue;
import org.sonar.core.config.Logback;
import org.sonar.core.persistence.Database;
import org.sonar.core.persistence.DatabaseCommands;
+import org.sonar.core.persistence.DatabaseVersion;
import org.sonar.core.persistence.H2Database;
import org.sonar.core.persistence.MyBatis;
+import org.sonar.core.persistence.SchemaMigrationMapper;
import org.sonar.jpa.session.DatabaseSessionFactory;
+import org.sonar.jpa.session.DefaultDatabaseConnector;
import org.sonar.jpa.session.JpaDatabaseSession;
-import org.sonar.jpa.session.MemoryDatabaseConnector;
-
-import java.io.InputStream;
-import java.sql.SQLException;
import static org.junit.Assert.fail;
@@ -62,7 +64,7 @@ public abstract class AbstractDbUnitTestCase {
private static Database database;
private static MyBatis myBatis;
private static DatabaseCommands databaseCommands;
- private static MemoryDatabaseConnector dbConnector;
+ private static DefaultDatabaseConnector dbConnector;
private IDatabaseTester databaseTester;
private JpaDatabaseSession session;
@@ -74,11 +76,15 @@ public abstract class AbstractDbUnitTestCase {
databaseCommands = DatabaseCommands.forDialect(database.getDialect());
- dbConnector = new MemoryDatabaseConnector(database);
- dbConnector.start();
-
myBatis = new MyBatis(database, new Logback(), new NullQueue());
myBatis.start();
+ try (SqlSession session = myBatis.openSession(false)) {
+ session.getMapper(SchemaMigrationMapper.class).insert(String.valueOf(DatabaseVersion.LAST_VERSION));
+ session.commit();
+ }
+
+ dbConnector = new DefaultDatabaseConnector(database);
+ dbConnector.start();
}
}